Dropdown menus in MVC approach - php

I have been trying to understand this existing php code that displays drop down menus with their subsequent menus. while the code works in one level, i am having a hard time how to or what to add another level of the dropdown... i have been able to do that but in this case where php is used to display these fields, it's kind of mind-boggling for me... here's the existing code in the view side -->
<ul class="nav navbar-nav">
<?php
foreach ($nav as $item) {
if (empty($item['children'])) {
?>
<li<?php if (!empty($item['is_active'])) { ?> class="active"<?php } ?>>
<a href="<?php echo $item['link']; ?>"><?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>" data-toggle="tooltip" data-placement="bottom" data-original-title="A large tooltip."></i> <?php } echo $item['label']; ?></a></li>
<?php
}
else
{
?>
<li class="dropdown <?php if (!empty($item['is_active'])) { ?> active<?php } ?>">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">
<?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>"></i>
<?php echo $item['label']; ?>
<b class="caret"></b><?php } ?>
</a>
<ul class="dropdown-menu">
<?php
_main_menu_widget_display_children($item['children']);
?>
</ul>
</li>
<?php
}
}
?>
and the controller looks like this -->
$nav['settings'] = array('label' => 'Settings', 'icon' => 'fa fa-wrench fa-lg', );
$nav['settings/access_rights'] = array('label' => 'Approver', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('admin/access_rights'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_1'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/billing_param'] = array('label' => 'Technician', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('billing_param'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_2'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/support_settings'] = array('label' => 'Support', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('support_settings'), 'parent_id' => 'settings');
//sub-menu sub of Support
$nav['example1'] = array('label' => 'Request Supportt', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example2'] = array('label' => 'Need Features', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example3'] = array('label' => 'Telephone Number', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example4'] = array('label' => 'Testimonials', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example6'] = array('label' => 'Known Issues', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example5'] = array('label' => 'Road Map', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
here's what it looks like :
i just wanted those list under 'support' not shown when i clicked the 'settings' menu but rather after i hover or clicked (it doesnt matter really) the 'support' sub-menu...
i know there's something missing in the 'view' side but i dont know what is... another if-else maybe but i cant see any connection so far... any help is very much appreciated thanks... :)

Related

Get multiple values of an Array inside PHP function

This is my code:
function menu_MORE (Array $ITEMS){
foreach ($ITEMS as $ITEM) {
echo "<li><i class='$ITEM[icon]'></i>$ITEM[link]</li>";
}
$ITEMS = array('link' => array('Edit','Remove'),
'icon' => array('fa fa-pencil','fa fa-trash'), );
menu_MORE($ITEMS);
I want the output to be:
<li><i class="fa fa-pencil"></i>Edit</li>
<li><i class="fa fa-trash"></i>Remove</li>
I think i need a second rule/parameter but I can't figured it out how.
Thank you in advance!
Your array is er... "inverted" -- you're doing this, where each key has multiple values:
$ITEMS = array(
'link' => array('Edit','Remove'),
'icon' => array('fa fa-pencil','fa fa-trash')
);
You want to do this, where you have multiple entries of items, each with a single value:
$ITEMS = array(
array(
'link' => 'Edit',
'icon' => 'fa fa-pencil',
),
array(
'link' => 'Remove',
'icon' => 'fa fa-trash',
),
);

Retrieve Data From Php Function

How to get data from php function? I want to call icon with:
<i class="<?php $this->Html->_($value['icon']);?>"></i>
Not work.
If it works would be like this
<i class="fa fa-cloud"></i>
But if call another function like:
<?php $this->Html->_($value['name']); ?>
Work well.
Main php function:
/**
* Retrieves the primary navigation for the client interface
*
* #param string $base_uri The base_uri for the currently logged in user
* #return array An array of main navigation elements in key/value pairs
* where each key is the URI and each value is an array representing that element including:
* - name The name of the link
* - active True if the element is active
* - sub An array of subnav elements (optional) following the same indexes as above
*/
public function getPrimaryClient($base_uri)
{
$nav = [
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false
],
$base_uri . 'accounts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts'),
'active' => false,
'secondary' => [
$base_uri . 'accounts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts'),
'active' => false,
'icon' => 'fa fa-list'
],
$base_uri . 'accounts/add/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_paymentaccounts_add'),
'active' => false,
'icon' => 'fa fa-plus-square'
],
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_return'),
'active' => false,
'icon' => 'fa fa-arrow-left'
]
]
],
$base_uri . 'contacts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts'),
'active' => false,
'secondary' => [
$base_uri . 'contacts/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts'),
'active' => false,
'icon' => 'fa fa-list'
],
$base_uri . 'contacts/add/' => [
'name' => $this->_('Navigation.getprimaryclient.nav_contacts_add'),
'active' => false,
'icon' => 'fa fa-plus-square'
],
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_return'),
'active' => false,
'icon' => 'fa fa-arrow-left'
]
]
]
];
// Include the primary client's plugin navigation
return $this->getPluginNavPrimaryClient($nav, $base_uri);
}
I want to call icon here, as I mark // Get Icon
<?php
$active_nav = null;
?>
<ul class="nav navbar-nav">
<?php
foreach ($this->Html->ifSet($nav, array()) as $link => $value) {
$attributes = array();
$link_attributes = array();
$dropdown = !empty($value['sub']);
$active = false;
if ($value['active']) {
$active = true;
$attributes['class'][] = "active";
$active_nav = $value;
}
if ($dropdown) {
$attributes['class'][] = "dropdown";
$link_attributes['class'][] = "dropdown-toggle";
$link_attributes['data-toggle'][] = "dropdown";
// Set parent to active if child is
if (!$active) {
foreach ($this->Html->ifSet($value['sub'], array()) as $sub_link => $sub_value) {
if ($sub_value['active']) {
$attributes['class'][] = "active";
break;
}
}
}
}
?>
<li<?php echo $this->Html->buildAttributes($attributes);?>>
<a href="<?php $this->Html->_($link);?>"<?php echo $this->Html->buildAttributes($link_attributes);?>>
// Get Icon
<i class="<?php $this->Html->_($value['icon']);?>"></i>
// Get Icon
<?php
$this->Html->_($value['name']);
if ($dropdown) {
?>
<b class="caret"></b>
<?php
}
?>
</a>
<?php
if (!empty($value['sub'])) {
?>
<ul class="dropdown-menu">
<?php
foreach ($this->Html->ifSet($value['sub'], array()) as $sub_link => $sub_value) {
?>
<li>
<i class="<?php $this->Html->_($sub_value['icon']);?>"></i> <?php $this->Html->_($sub_value['name']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
</li>
<?php
}
?>
</ul>
Did anyone have any ideas for this?
I really appreciate all the help.
Well, this is working b
<i class="<?php $this->Html->_($value['name']);?>"></i>
because there is name attribute is very first base URI
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false
],
if you add icon as well, it will work
$base_uri => [
'name' => $this->_('Navigation.getprimaryclient.nav_dashboard'),
'active' => false,
'icon' => 'fa fa-cloud'
],
Hope this helps.

Separate php Array from main site

So, I have a page to display products which I named apple.php :
<?php
foreach($apple as $item) {
?>
<div class="col-lg-4 col-sm-4 col-xs-12 text-xs-center">
<div class="view third-effect">
<a href="<?php echo $item['link'] ?>">
<div class="overlay-thing"><i class="fa fa-share" aria-hidden="true"></i></div>
<img src="<?php echo $item['image'] ?>" />
</a>
</div>
<p><?php echo $item['name'] ?> </p>
</div>
<?php
}
?>
I use an hardcoded array which looks like this:
$apple = array(
array(
'id' => '1',
'name' => 'iPhone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'images/iphone4.png'
),
array(
'id' => '2',
'name' => 'iPhone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'images/iphone4s.png'
)
);
etc etc...
It has around 10 products.
This array is on top on the file, and I wonder how can I separate this array into an separate file, calling it for example products.php.
How can i then access that array in the file apple.php for the foreach loop.
Also, I will have arrays for samsung, sony products and should I put them all into the same file?
You could use include.
i.e. :
product.php
$apple = array(
array(
'id' => '1',
'name' => 'iPhone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'images/iphone4.png'
),
array(
'id' => '2',
'name' => 'iPhone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'images/iphone4s.png'
)
);
apple.php
<?php
include 'product.php';
foreach($apple as $item) {
?>
<div class="col-lg-4 col-sm-4 col-xs-12 text-xs-center">
<div class="view third-effect">
<a href="<?php echo $item['link'] ?>">
<div class="overlay-thing"><i class="fa fa-share" aria-hidden="true"></i></div>
<img src="<?php echo $item['image'] ?>" />
</a>
</div>
<p><?php echo $item['name'] ?> </p>
</div>
<?php
}
?>
Hope it helps.
Create a file apple-products.php or any name and add following lines:
<?php // apple-list.php
return array(
array(
'id' => '1',
'name' => 'iphone 4',
'link' => 'reparatur/apple/iphone-4.php',
'category' => 'iPhone',
'image' => 'image/iphone4.png',
),
array(
'id' => '2',
'name' => 'iphone 4s',
'link' => 'reparatur/apple/iphone-4s.php',
'category' => 'iPhone',
'image' => 'image/iphone4s.png',
),
);
Now in the top of apple.php file replace your array with this:
$apple = require('apple-products.php'); // assuming apple.php and apple-products.php are in same directory
That's it. Hope that helped!

ZF2 - Active <li> in Zend Navigation Menu

I've managed to generate menu using Zend Navigation. However, the active page is never set (active class is not set for any <li> element).
My partial:
foreach ($pages as $page): ?>
<?php if (!$page->isVisible() || !$this->navigation()->menu()->accept($page)) continue; ?>
<li role="presentation" <?php if ($page->isActive()) echo 'class="active"' ?>>
<a href="<?php echo $page->getHref() ?>">
<?php if ($icon = $page->get('icon')) {
echo '<span class="' . $icon . '"></span>';
} ?>
<span> <?php echo $this->translate($page->getLabel()) ?> </span>
</a>
</li>
<?php endforeach ?>
Extract of module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Page 1',
'route' => 'application/default',
'namespace' => 'Application\Controller',
'controller' => 'Index',
'action' => 'page1',
'icon' => 'fa fa-2x fa-file-text',
'order' => 10,
),
array(
'label' => 'Page 2',
'route' => 'application/default',
'namespace' => 'Application\Controller',
'controller' => 'Index',
'action' => 'page2',
'icon' => 'fa fa-2x fa-file-text',
'order' => 20,
),
),
),
The menu is rendered properly on the page, but without any active class:
$partial = array('partial/menu.phtml', 'default');
echo $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setPartial($partial);
After some research into ZF code, I've found something I don't understand (in Zend\View\Helper\Navigation\Menu.php):
// in renderNormalMenu function, line 288
$isActive = $page->isActive(true);
Any idea or suggestion regarding my problem?
Thanks a lot,
Problem was in module.config.php ; the isActive method (from Zend\Navigation\Mvc) was expected the "full" controller name (including namespace).
My config was splitting namespace and controller name, wich causes the issue.
Solution:
array(
'label' => 'Page 1',
'route' => 'application/default',
'controller' => 'Application\Controller\Index',
'action' => 'page1',
'icon' => 'fa fa-2x fa-file-text',
'order' => 10,
),

CakePHP Making a form to be global with validation

My application has contacts controller with add action which able to post contacts message to the database table comments. It works fine with its validation.
Now I want to use the add view (The contacts form) as a global form to be rendered in all pages of the application.
I know, to do that, I have to make an element contains the form (or the add view) as follows:
elements/contact.ctp
<div class="panel">
<h4><?php echo __('contact'); ?></h4>
<?php echo $form->create('Contact',array('action'=>'index', 'class' => ''));?>
<?php echo $form->input('name', array('size' => 45, 'class' => 'input-text', 'label' => array( 'text' => __('name',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('email', array('size' => 45, 'class' => 'input-text', 'label' => array('text' => __('email',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php echo $form->input('subject', array('type' => 'select', 'options' => array(null => __('choose subject',true), 'g' => __('general', true), 'r' => __('report', true)), 'class' => 'input-text', 'label' => array('text' => __('subject', true).'<sup>*</sup>'),'error' => array('class' => 'error', 'wrap' => 'small'))); ?>
<?php echo $form->input('content', array('class' => 'input-text', 'style' => 'height: 140px', 'title' => __('message', true), 'label' => array('text' => __('message',true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
<?php //echo $fck->load('Contact.message','Mini'); ?>
<span>
<?php
App::import('Vendor','FoxCaptcha', array('file' => 'Fox_captcha.php'));
$cap = new Fox_captcha(120,30,5);
$cap->image_dir = $html->url('/').'img/';
$cap->lines_amount = 13;
$cap->en_reload = $html->image('reload.png', array('alt' => __('reload', true), 'title' => __('reload the captcha', true), 'id' => 'frel', 'style' => 'vertical-align:middle'));
?>
</span>
<div>
<span class="display:inline"><?php echo $cap->make_it('HTML');?></span>
<?php echo $form->input('vcode', array('size' => 45, 'class' => 'small input-text', 'label' => array('text' => __('captcha', true).'<sup>*</sup>'), 'error' => array('class' => 'error', 'wrap' => 'small')));?>
</div>
<?php echo $form->submit(__('send',true), array('class' => 'nice medium radius white button'));?>
<?php echo $form->end();?>
<div class="alert-box warning"><?php echo __('fields label with * are required');?></div>
</div>
The problem is: When I use this form from any page (for example posts/view/22) it submits to contacts/add. I want after submit posts/view/22 is rendered with any validation message triggered.
There is no solution for this requirement without Ajax and JSON. It includes submitting the form with Ajax to the contacts controller index action with event handler component activated and then check isAjax then render JSON output retrieved by the post's view and then populates the results.

Categories