<?php
class Access{
	var $id;
	var $groups;
	var $aid;
	var $depth;
	
	function setId($id){
		global $db;
		$this->id = $db->clean($id);
	}
	
	function setGroups($groups){
		global $db;
		$this->groups = $db->clean($groups);
	}
	
	function setAccessId($aid){
		global $db;
		$this->aid = $db->clean($aid);
	}
	
	function getAccessGroup(){
		global $db;
		
		$agarray = array();
		$dbag = $db->fetch_all("SELECT * FROM accessed ORDER BY menuorder");
		if (sizeof($dbag) > 0){
			foreach ($dbag as $dbg){
				if (!in_array($dbg['accessgroup'],$agarray)){
					array_push($agarray,$dbg['accessgroup']);
				}
			}
		}
		return $agarray;
	}
	
	function getListAccess($mode){
		global $db;
		
		$sqls = array();
		if ($mode == 'partial'){
			array_push($sqls,'STATUS = 1');
		}
		if (!empty($this->groups)){
			array_push($sqls,'accessgroup=\''.$this->groups.'\'');
		}
		
		$sql = '';
		if (sizeof($sqls) > 0){
			$sql = ' WHERE '.implode(' AND ',$sqls);
		}
		$dbaccess = $db->fetch_all("SELECT * FROM accessed".$sql." ORDER BY accessid");
		
		return $dbaccess;
	}
	
	function getAccessDetail(){
		global $db;
		
		if (!empty($this->aid)){
			$dbaccess = $db->fetch_all("SELECT * FROM accessed WHERE accessid IN (".$this->aid.") AND STATUS = 1 ORDER BY MENUORDER");
			return $dbaccess;
		}
	}
	
	function generateParentMenu(){
		global $db, $arrallaccessid, $useraccess;
		
		$allparent = $db->fetch_all("SELECT accessid, accessname, menulabel, jsaction, ismenu FROM accessed WHERE parentid = -1 AND status = 1 ORDER BY menuorder");
		$allmenu = '';
		if (sizeof($allparent) > 0){
			foreach ($allparent as $parent){
				if (in_array($parent['accessid'],$arrallaccessid)){
					$texttoprint = '';
					$useraccess[$parent['accessname']] = 1;
					if ($parent['ismenu'] == 1){
						/* if (!empty($parent['jsaction'])){
							$texttoprint = '<li class="treeview">
										  <a href="#">
											<i class="fa fa-folder"></i> <span>'.htmlspecialchars($parent['menulabel']).'</a>';
						}
						else{ */
							$texttoprint = '<li class="treeview">
											  <a href="#">
												<i class="fa fa-folder"></i> <span>'.htmlspecialchars($parent['menulabel']).'</span>
												<i class="fa fa-angle-left pull-right"></i>
											  </a>';
						$child_menu = $this->generateChildMenu($parent['accessid']);
						if(!empty($child_menu))
						{
							$allmenu .= $texttoprint.'<ul class="treeview-menu">
														'.$child_menu.'
													</ul>';
						}
						else{
							$allmenu .= $texttoprint.'</li>';
						}
						
						/* } 
						$allmenu .= '<li style="padding: 5px 0">'.$texttoprint.$this->generateChildMenu($parent['accessid']).'</li>';
						*/
					}
				}
			}
		}
		return $allmenu;
	}
	
	function generateChildMenu($parentid){
		global $db, $arrallaccessid, $useraccess;
		
		$allchild = $db->fetch_all("SELECT accessid, accessname, menulabel, jsaction, ismenu FROM accessed WHERE parentid = '".$db->clean($parentid)."' AND status = 1 ORDER BY menuorder");
		$allchildmenu = '';
		$returnfromchildall = '';
		if (sizeof($allchild) > 0){
			foreach ($allchild as $child){
				if (in_array($child['accessid'],$arrallaccessid)){
					$texttoprint = '';
					$useraccess[$child['accessname']] = 1;
					if ($child['ismenu'] == 1){
						$returnfromchild = $this->generateChildMenu($child['accessid']);
						if (empty($returnfromchild)){
							/* if (!empty($child['jsaction'])){
								$texttoprint = '<li style="padding: 5px 0"><a href="'.$child['jsaction'].'" target="mcontent">'.htmlspecialchars($child['menulabel']).'</a></li>';
							}
							else{ */
								$texttoprint = '<li><a href="'.$child['jsaction'].'"><i class="fa fa-circle-o"></i>'.htmlspecialchars($child['menulabel']).'</a></li>';
							/* } */
							$allchildmenu .= $texttoprint;
						}
						else{
							/* if (!empty($child['jsaction'])){
								$texttoprint = '<li style="padding: 5px 0"><a href="'.$child['jsaction'].'" target="mcontent">'.htmlspecialchars($child['menulabel']).'</a></li>';
							}
							else{ */
								$texttoprint = '<li><a href="'.$child['jsaction'].'"><i class="fa fa-circle-o"></i>'.htmlspecialchars($child['menulabel']).'</a></li>';
							/* } */
							$allchildmenu .= $texttoprint;
						}
						$returnfromchildall .= $returnfromchild;
					}
				}
			}
			if (!empty($allchildmenu)){
				/* $allchildmenu = '<div id="sub_menu_'.$parentid.'" style="display: none"><ul style="padding: 0; margin-left: 14px">'.$allchildmenu.'</ul></div>'; */
				$allchildmenu .= $returnfromchildall;
			}
		}
		return $allchildmenu;
	}
	
	function generateParentMenuOnUserGroup(){
		global $db, $thisugaccess;
		
		$allparent = $db->fetch_all("SELECT accessid, accesslabel, menulabel, jsaction FROM accessed WHERE parentid = -1 AND status = 1 ORDER BY menuorder");
		$allmenu = '';
		$this->depth = 1;
		$perRow = 4;
		$width = 100 / $perRow;
		$counter = 1;
		if (sizeof($allparent) > 0){
			$allmenu = '<div class="col-md-12 margin-bottom-20 padding-0">';
			foreach ($allparent as $parent){
				$allmenu .= '
					<fieldset style="float: left; /* width: 250px */width:'.$width.'%;">
						<legend><b>'.htmlspecialchars($parent['accesslabel']).'</b></legend>
						<div align="left"><input type="checkbox" value="'.$parent['accessid'].'" name="accessp[]"'.((in_array($parent['accessid'],$thisugaccess))?' checked':'').' id="checkall_'.$parent['accessid'].'"> <label for="checkall_'.$parent['accessid'].'">'.htmlspecialchars($parent['accesslabel']).'</label></div>
						'.$this->generateChildMenuOnUserGroup($parent['accessid']).'
					</fieldset>
				';
				if($counter % $perRow == 0)$allmenu .= '<div class="clearfix"></div></div><div class="col-md-12 margin-bottom-20 padding-0">';
				$counter++;
			}
			
			$allmenu .= '</div>';
		}
		return $allmenu;
	}
	
	function generateChildMenuOnUserGroup($parentid){
		global $db, $thisugaccess;
		
		$allchild = $db->fetch_all("SELECT accessid, accesslabel, menulabel, jsaction, ismenu FROM accessed WHERE parentid = '".$db->clean($parentid)."' AND status = 1 ORDER BY menuorder");
		$allchildmenu = '';
		if (sizeof($allchild) > 0){
			foreach ($allchild as $child){
				$prefix = str_repeat('&nbsp;',$this->depth * 5);
				/* if ($child['ISMENU'] == 1 && $child['MENULABEL'] != $child['ACCESSLABEL']){
					$allchildmenu .= '<div align="left">'.$prefix.'&bull; '.htmlspecialchars($child['MENULABEL']).'</div>';
				} */
				$this->depth++;
				$allchildmenu .= '
					<div align="left">'.$prefix.'<input type="checkbox" value="'.$child['accessid'].'" name="accessp[]"'.((in_array($child['accessid'],$thisugaccess))?' checked':'').' id="checkall_'.$child['accessid'].'"> <label for="checkall_'.$child['accessid'].'">'.htmlspecialchars($child['accesslabel']).'</label></div>
					'.$this->generateChildMenuOnUserGroup($child['accessid']).'
				';
				$this->depth--;
			}
		}
		return $allchildmenu;
	}
}
?>