<?php

class Menu
{
	public $res;
	public $listMenu = array();
	public $listGrp = array();
	public $em;
	
	public function menu()
	{
		return array();
	}

	public function fileName($name)
	{
		$name = explode("-", $name);
		$res = '';
		foreach($name as $row){
			$res.= ucfirst($row);
		}
		return $res;
	}
	
	protected function detailMenu($menu, $grup, $logo)
	{
		foreach($menu as $row){
			if(count($row['subUrl']) > 0){
				$this->detailMenu($row['subUrl'], $grup, $logo);
			}else{
				$id = $row['url'];
				$this->listMenu[$id]['url'] 	= $row['url'];
				$this->listMenu[$id]['file'] 	= $this->fileName($row['url']);
				$this->listMenu[$id]['nama'] 	= $row['menu'];
				$this->listMenu[$id]['logo'] 	= $row['logo'];
				$this->listMenu[$id]['grup'] 	= $grup;
				$this->listMenu[$id]['grupIc'] 	= $logo;
			}
		}
	}

	public function listMenu()
	{
		$folder = BASEPATH.BASENAME;
		$dir = opendir($folder);
		while($file = readdir($dir)){
			$ex = explode(".", $file);
			if(count($ex) == 1){
				$phpMenu = $folder.'/'.$file.'/menu.php';
				require_once($phpMenu);
				$menu = 'menu_'.$file;
				$menu = $menu();

				$logo = 'logo_'.$file;
				$logo = $logo();
				$this->detailMenu($menu, $file, $logo);
			}
		}
		return $this->listMenu;
	}

	public function cekAcl($em, $menu = false)
	{
		$this->em = $em;
		$sess = SESSION;
		$login = $_SESSION[$sess]['login'];
		
		$filter = array();
		$filter[1] = "a.kd_user = '".$login['kd_user']."'";
		$filter[2] = "a.modul like '%/View'";
		if($menu != false){
			$menu = trim(str_ireplace(" ", "", strtoupper($menu)));
			if(empty($menu)){
				$menu = 'apankkOkeTakAdaMenu';
			}
			$filter[3] = "replace(upper(a.ket), '-', '') like '%".$menu."%'";
		}
		
		$data = $this->em->getFindBy("acl", "*", $filter, "modul asc");
		$acl = array();
		foreach($data as $row){
			$id = strtolower(str_ireplace("/View", "", $row->modul));
			$acl[$id] = $row;
		}

		$filter[1] = "a.kd_user = '".$login['kd_user']."'";
		$filter[2] = "c.modul like '%/View'";
		if($menu != false){
			$filter[3] = "replace(upper(c.ket), '-', '') like '%".$menu."%'";
		}
		
		$sql = $this->em->addSelect("c.*");
		$sql = $this->em->getFrom('grup_user', 'a');
		$sql = $this->em->addInnerJoin('group', 'b', array("a.kodegrup = b.kodegrup"));
		$sql = $this->em->addInnerJoin('acl', 'c', array("b.kodegrup = c.kodegrup"));
		$sql = $this->em->getWhere('c.app is not null');
		foreach($filter as $row){
			$sql = $this->em->andWhere($row);
		}
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);

		foreach($res as $row){
			$id = strtolower(str_ireplace("/View", "", $row->modul));
			$acl[$id] = $row;
		}
		
		return $acl;
	}

	public function modulActive($em)
	{
		$acl  = $this->cekAcl($em);
		$grup = array();
		foreach($this->listMenu() as $key => $row){
			$key = strtolower($key);
			if(!empty($acl[$key])){
				$id = $row['grup'];
				$grup[$id]->nama = $id;
				$grup[$id]->logo = $row['grupIc'];
			}
		}
		return $grup;
	}
	
	public function listDetMenuAkses($modul)
	{
		$folder = BASEPATH.BASENAME;
		$dir = opendir($folder);
		
		$tmp = array();
		while($file = readdir($dir)){
			$ex = explode(".", $file);
			if(count($ex) == 1){
				$ex = $ex[0];
				$tmp[$ex] = $ex;
			}
		}
		
		array_multisort($tmp, SORT_ASC, $tmp);
		
		$tmpM = array();
		$cek = array();
		foreach($tmp as $r){
			$folder = BASEPATH.BASENAME.'/'.$r;
			require_once($folder.'/menu.php');
			$menu = 'menu_'.$r;
			$menu = $menu();
			$menu = $this->detailMenuApp($menu);
			
			foreach($menu as $k2 => $r2){
				$idU = $r2['url'];
				if(empty($cek[$idU])){
					$tmpM[$r][$k2] = $r2;
					$cek[$idU] = $k2;
				}
			}
		}
		
			
		return $tmpM[$modul];
	}
	
	public function listDetMenu($modul)
	{
		$folder = BASEPATH.BASENAME;
		$dir = opendir($folder);
		while($file = readdir($dir)){
			$ex = explode(".", $file);
			if(count($ex) == 1){
				if(!empty($modul)){
					$folder = BASEPATH.BASENAME.'/'.$modul;
					require_once($folder.'/menu.php');
					$menu = 'menu_'.$modul;
					$menu = $menu();
					return $menu;
				}
			}
		}
		return array();
	}
	
	public function detailMenuApp($data, $res = array(), $awal = '')
	{
		if(!empty($awal)){
			$awal.= ' &raquo; ';
		}
				
		foreach($data as $row){
			if(count($row['subUrl']) > 0){
				$nama = $awal.$row['menu'];
				$res = $this->detailMenuApp($row['subUrl'], $res, $nama);
			}else{
				$nama = $awal.$row['menu'];
				$res[$nama] = $row;
			}
		}
		return  $res;
	}
	
	public function getHeadChild($data, $res, $id = '', $pat = '', $no = 0, $key = '')
	{
		foreach($data as $k => $row){
			$url = strtolower($row['url']);
			$key2 = $key.'___'.strtolower($k);
			$pat2 = $pat.'___'.strtolower($row['menu']);
			$res[$url]->menu = $id;
			$res[$url]->path = $pat2;
			$res[$url]->key  = $key2;
			
			if(count($row['subUrl']) > 0){
				$no = '';
				$res = $this->getHeadChild($row['subUrl'], $res, $url, $pat2, $no, $key2);
				$this->listGrp[$key2]->child 	= $row['subUrl'];
				$this->listGrp[$key2]->path 	= $pat2;
			}
		}
		return  $res;
	}
	
	public function getDetMenu($data, $res)
	{
		foreach($data as $row){
			if(count($row['subUrl']) > 0){
				$res = $this->getDetMenu($row['subUrl'], $res);
			}else{
				if(!empty($row['url'])){
					$id = strtolower($row['url']);
					$res[$id] = $row;
				}
			}
		}
		return $res;
	}
	
	public function getTitle($menu, $data, $res = '')
	{
		$nm = trim(strtolower($data[$menu]));
		$symbol = '';
		if(!empty($nm)){
			$symbol = ' &raquo; ';
		}
		$res = $symbol.str_ireplace("-", " ", ucfirst($menu)).$res;
		if(!empty($nm)){
			$res = $this->getTitle($nm, $data, $res);
		}
		return $res;
	}
	
	public function setUcfirst($row)
	{
		$res.= str_ireplace("-", " ", ucfirst($row));
		$exp = explode(" ", $res);
		$res = '';
		foreach($exp as $row){
			$res.= " ".ucfirst($row);
		}
		return trim($res);
	}
	
	public function setTitile($modul, $ctrl, $link)
	{
		$ctrl 	= strtolower($ctrl);
		$menu 	= $this->listDetMenu($modul);
		$heci 	= $this->getHeadChild($menu, array(), '', '', 0);
		
		$exp = explode("___", $heci[$ctrl]->path);
		$key = explode("___", $heci[$ctrl]->key);
		
		$ctr = explode('/', $ctrl);
		
		$res = '<a href="'.$link.'application/index/'.$modul.'">'.ucfirst($modul).'</a>';
		foreach($exp as $k => $row){
			
			$url = '';
			for($i = 0; $i <= $k; $i++){
				$url.= '___'.$key[$i]; 
			}
			$url = str_ireplace("______", "___", $url);
			$url = str_ireplace("___", ".", $url);
			$url = $link.'listMenu/index/'.$modul.'/&menu='.$url;
			
			if($k == (count($exp) - 1)){//&raquo;
				$url = $link.$ctr[0].'/index/'.$modul;
			}
			
			if(!empty($row)){
				$res.= ' &raquo <a href="'.$url.'">'.$this->setUcfirst($row).'</a>';
			}
		}
		
		if(empty($res)){
			$res = ucfirst($modul);
		}
		return $res;
	}
	
	public function setTitileMenu($modul, $ctrl)
	{
		$menu 	= $this->listDetMenu($modul);
		$heci 	= $this->getHeadChild($menu, array(), '', '', 0);
		
		$exp = explode("___", $heci[$ctrl]->path);
		$res = '';
		foreach($exp as $row){
			$res = str_ireplace("-", " ", ucfirst($row));
		}
		
		if(empty($res)){
			$res = ucfirst($modul);
		}
		return $res;
	}
	
	public function setTitleGroup($modul, $ctrl, $link, $id)
	{
		$menu 	= $this->listDetMenu($modul);
		$heci 	= $this->getHeadChild($menu, array(), '', '', 0);
		
		$id = str_ireplace(".", "___", $id);
		
		$exp = explode("___", $this->listGrp[$id]->path);
		$key = explode("___", $id);
		
		$ctr = explode('/', $ctrl);
		
		$res = '<a href="'.$link.'application/index/'.$modul.'">'.ucfirst($modul).'</a>';
		foreach($exp as $k => $row){
			
			$url = '';
			for($i = 0; $i <= $k; $i++){
				$url.= '___'.$key[$i]; 
			}
			$url = str_ireplace("______", "___", $url);
			$url = str_ireplace("___", ".", $url);
			$url = $link.'listMenu/index/'.$modul.'/&menu='.$url;
			
			if(!empty($row)){
				$res.= ' &raquo; <a href="'.$url.'">'.$this->setUcfirst($row).'</a>';
			}
		}
		
		if(empty($res)){
			$res = ucfirst($modul);
		}
		return $res;
	}
	
	public function actMenu($acl, $menu, $dis = false)
	{
		if($dis == true){
			var_dump($menu);
			echo "<hr>";
		}
	
		if(count($menu['subUrl']) > 0){
			foreach($menu['subUrl'] as $r2){
				$res = $this->actMenu($acl, $r2, $dis);
				if($res == true){
					return true;
				}
			}
		}else{
			$id = strtolower($menu['url']);
			if(!empty($acl[$id])){
				return true;
			}
		}
		return false;
	}

	public function setMenuIcon($modul, $ctrl, $link, $id, $dashboard = false, $acl)
	{
		$menu 	= $this->listDetMenu($modul);
		$heci 	= $this->getHeadChild($menu, array(), '', '', 0);
		
		$id = str_ireplace(".", "___", $id);
		$child = $this->listGrp[$id]->child;
		
		if($dashboard == true){
			$child = $menu;
		}
		
		$res = '';
		foreach($child as $k => $row){
			
			$akses = $this->actMenu($acl, $row, false);
			if($akses == true){
				$url = '';
				if(count($row['subUrl']) > 0){
					for($i = 0; $i <= $k; $i++){
						$url.= '___'.$key[$i]; 
					}
					$url = str_ireplace("___", ".", $id.'.'.$k);
					$url = $link.'listMenu/index/'.$modul.'/&menu='.$url;
				}else{
					$url = $link.$row['url'].'/index/'.$modul;
				}
			
				$res.= '<a href="'.$url.'">';
				$res.= '<div class="_menuIcon">';
				
				$res.= '<img src="'.$link.'assets/menu/'.$row['logo'].'" style="width: 40px">';
				$res.= '<br>'.$this->setUcfirst($row['menu']);
				
				$res.= '</div>';
				$res.= '</a>';
			}
			
		}
		
		return $res;
	}
	
	public function setPathMenu($modul, $ctrl)
	{
		$menu 	= $this->listDetMenu($modul);
		$heci 	= $this->getHeadChild($menu, array(), '', '', 0);
		
		$key = explode("___", $heci[$ctrl]->key);

		return $key;
	}
}