<?php 
function actMenu($acl, $menu)
{
	if(count($menu['subUrl']) > 0){
		foreach($menu['subUrl'] as $r2){
			$res = actMenu($acl, $r2);
			if($res == true){
				return true;
			}
		}
	}else{
		$id = strtolower($menu['url']);
		if(!empty($acl[$id])){
			return true;
		}
	}
	return false;
}

function displayMenu($menu)
{
	$ex = explode(" ", $menu);
	
	$res = '';
	foreach($ex as $row){
		if(!empty($res)){
			$res.= ' ';
		}
		$res.= ucfirst($row);
	}
	
	return $res;
}
function setMenu($menu, $res = '', $url, $acl, $modul, $kd2 = '', $no = 1)
{
    foreach($menu as $k => $row){
        $row['menu'] = str_ireplace("-", " ", $row['menu']);
        $idAct = actMenu($acl, $row);
        if($idAct == true){
            $kd = $kd2.'_'.$k;
            if(count($row['subUrl']) > 0){
                $res.= '<li class="treeview" id="_treeLiFolder'.$kd.'">';
                $res.= '<a href="#">
                            <i class="fa fa-th"></i> <span>'.ucfirst($row['menu']).'</span>
                            <i class="fa fa-angle-left pull-right"></i>
                          </a>';
                $res.= '<ul class="treeview-menu" id="_treeUlFolder'.$kd.'">';
                $res = setMenu($row['subUrl'], $res, $url, $acl, $modul, $kd, $no++);
                $res.= '</ul>';
                $res.= '</li>';
            }else{
                $res.= '<li id="_treeUlList'.$kd.'"><a class="_ajakMenuLoading" id="url_'.$row['url'].'" href="'.$url.$row['url'].'/index/'.$modul.'" link="'.$row['url'].'">'.displayMenu($row['menu']).'</a></li>';
            }
        }
    }
    return $res;
}
$menu = new Menu;			
$ctrl = strtolower($this->getParameter['asliGetCotroller']);
$path = $menu->setPathMenu($this->getParameter['modul'], $ctrl); 
$menu = setMenu($this->menu, '', $this->baseUrl, $this->acl, $this->getParameter['modul']);
?>

<ul class="sidebar-menu">
    <li class="header">MAIN MENU</li>
    <?php echo $menu ?>
    <li><a href="<?php echo $this->baseUrl.'index' ?>">Dashboard</a></li>
    <li><a href="<?php echo $this->baseUrl.'login/logout' ?>">Log Out</a></li>
</ul>

<script type="text/javascript">
$(document).ready(function(){
    <?php 
    $id = '';
    $jum = count($path) - 1;
    foreach($path as $k => $r){ 
        $id .= '_'.$r;
        $id = str_ireplace("__", "_", $id);
    ?>
        // Expand parent folder and mark it active
        $('#_treeLiFolder<?php echo $id ?>').addClass('active menu-open');
        $('#_treeUlFolder<?php echo $id ?>').css('display', 'block');

        <?php if($jum == $k){ ?>
        // Highlight the active menu item (last in path)
        $('#_treeUlList<?php echo $id ?>').addClass('active');
        $('#url_<?php echo $ctrl ?>').closest('li').addClass('active');
        <?php } ?>
    <?php } ?>
});
</script>