Creating CMS with CodeIgniter and for authentication I am using Ion Auth.
Here I am trying to create recursive admin menu based on access level (user group) defined in Ion AUth db group table.
I have 3 groups syste_admin, Managers and Human Resource I want to set access to the menu so only user who has access can see such menu item.
Array defining menu items
$menu = array(
'dashboard' => array(
'display' => 'Dashboard',
'url' => 'admin/dashboard',
'iconclass' => 'fa fa-home',
'attributes' => 'class="hellow-atts"',
'access' => array('system_admin','managers', 'hr')
),
'employee' => array(
'display' => 'Employee',
'url' => 'admin/hr',
'iconclass' => false,
'access' => array('system_admin','hr'),
'sub' => array(
'manage' => array('display' => 'Manage All'),
'add_new' => array('display' => 'Add New'),
'access' => array('system_admin','hr'),
),
),
'links' => array(
'display' => 'Recommended Links',
'iconclass' => 'fa fa-th',
'access' => array('system_admin'),
),
'contact' => array(
'display' => 'Contact Us',
'access' => array('system_admin'),
),
'part' => array('display' => '', 'divider' => true, 'url' => '#'),
);
echo '<nav id="page-leftbar" role="navigation">';
echo admin_side_menu($menu);
echo '</nav>';
Menu Helper function
if ( ! function_exists('admin_side_menu'))
{
function admin_side_menu($menu_array, $is_sub=FALSE, $attributes=FALSE, $iconclass=FALSE, $divider=FALSE)
{
$CI =& get_instance();
$attr = (!$is_sub) ? ' id="sidebar" class="acc-menu"' : ' class="acc-menu"';
$menu = "<ul$attr>"; // Open the menu container
/*
* I want to check here if user is in defined
* access group than s/he will be elligable to
* access the menu else it won't display
*/
// this is to test if getting $access
// however finally I want to wrap this
// condition to the list item to make
// it works with the access levele
if($CI->ion_auth->in_group($access)) {
echo 'You have access';
} else {
echo 'You don\'t have access';
} //end if/else
foreach($menu_array as $id => $properties) {
echo '<pre>', print_r($properties), '</pre>';
foreach($properties as $key => $val) {
if(is_array($val))
{
$sub = admin_side_menu($val, TRUE);
}
else
{
$sub = NULL;
$$key = $val;
}
}
if(!isset($url)) {
$url = $id;
}
$lclass = ($divider) ? ' class="divider"' : '';
$icon = (isset($iconclass)) ? '<i class="'.$iconclass.'"></i>' : '';
$item = (!$divider) ? anchor($url, $icon.'<span>'.$display.'</span>', $attributes).$sub : '';
$menu .= '<li'.$lclass.'>'.$item.'</li>';
unset($url, $display, $sub);
} // end foreach
return $menu . "</ul>";
}
}
With about code I am getting errors like Invalid argument supplied for foreach , Undefined variable: display, Undefined variable: sub etc..
This is may be because I am not calling access properly or don't know what is wrong. I am not so much expert and first time creating recursive menu with group permission.
Can anyone tell me how can I use and set array for access so I can check it throughout menu including sub-levels and display as per the access level defined.