<?php

class rasio_Permission
{
	protected $permission = false;
	protected $menu;
	
	public function getAcl()
	{
		$acl = new Rasio_Acl;
		$res = array();
		foreach($acl as $k => $r){
			$res[] = $k;
		}
	
		return $res;
	}
	
	protected function getController()
	{
		return new RasioProject;
	}
	
	protected function getLogin($i, $sess)
	{
		$login = $_SESSION[$sess]['login'];
		return $login[$i];
	}
	
	public function getDraftMenu()
	{
		$class = new Menu;
		$class->getChildParent($class->getMenu(), '', array());
		$this->menu = $class->dataMenus;
		return $class->dataMenus;
	}
	
	protected function cekAction($action)
	{
		foreach($this->getAcl() as $row){
			if($row == $action){
				return true;
			}
		}
		return false;
	}
	
	public function cekPermission($param, $em)
	{
		if($param['action'] == 'index'){
			$action = 'View';
		}else{
			$action = $param['action'];
		}

		if($this->cekAction($action) == true){
			$this->cekAcl($param, $em, $action);
		}else{
			$this->permission = true;
		}
		
		return $this->permission;
	}
	
	protected function cekAcl($param, $em, $action)
	{
		foreach($em->getFetchObject($this->getUserAcl($param, $em, $action)) as $row){
			if($row->status == 1){
				return $this->permission = true;
			}else{
				return $this->permission = false;
			}
		}
		foreach($em->getFetchObject($this->getGroupAcl($param, $em, $action)) as $row){
			if($row->status == 1){
				return $this->permission = true;
			}
		}
	}
	
	public function db($db)
	{
		$mdb = new DB;
		return $mdb->$db;
	}
	
	protected function getGroupAcl($param, $em, $action)
	{
		$sql = $em->addSelect('*');
		$sql = $em->getFrom('acl', ' a');
		$sql = $em->addLeftJoin('group', 'b', array('(a.kodegrup = b.kodegrup)'));
		$sql = $em->addLeftJoin('grup_user', 'c', array('(b.kodegrup = c.kodegrup)'));
		$sql = $em->getWhere("a.modul = '".$param['asliGetCotroller']."/$action'");
		$sql = $em->andWhere("c.kd_user = '".$this->getLogin('kd_user')."'");
		return $em->queryEks($sql);
	}
	
	protected function getUserAcl($param, $em, $action)
	{
		$sql = $em->addSelect('status');
		$sql = $em->getFrom('acl', 'a');
		$sql = $em->getWhere("modul = '".$param['asliGetCotroller']."/$action'");
		$sql = $em->andWhere("kd_user = '".$this->getLogin('kd_user')."'");
		return $em->queryEks($sql);
	}
	
	public function getUserMenuAclTrue($em, $sess)
	{
		$sql = $em->addSelect('*');
		$sql = $em->getFrom('acl', 'a');
		$sql = $em->getWhere("kd_user = '".$this->getLogin('kd_user', $sess)."'");
		$eks = $em->queryEks($sql);
		return $em->getFetchObject($eks);
	}
	
	public function getGroupMenuAclTrue($em, $sess)
	{
		$sql = $em->addSelect('*');
		$sql = $em->getFrom('grup_user', ' b');
		$sql = $em->addLeftJoin('acl', 'a', array('(b.kodegrup = a.kodegrup)'));
		$sql = $em->getWhere("b.kd_user = '".$this->getLogin('kd_user', $sess)."'");
		$eks = $em->queryEks($sql);
		return $em->getFetchObject($eks);
	}
	
	public function trueMenus($em, $user)
	{
		$aclUser = $this->getUserMenuAclTrue($em, $user);
		$dataAclUser = array();
		foreach($aclUser as $row){
			$i = $row->modul;
			$dataAclUser[$i] = $row->status;
		}
		
		$aclGroup = $this->getGroupMenuAclTrue($em, $user);
		$dataAclGroup = array();
		foreach($aclGroup as $row){
			if($row->status === '1'){
				$i = $row->modul;
				$dataAclGroup[$i] = $row->status;
			} 
		}
		
		$result = array();
		foreach($dataAclGroup as $key => $row){
			if($dataAclUser[$key] === '99' or $dataAclUser[$key] == null){
				$dataAclUser[$key] = 1;
			}
		}

		return $dataAclUser;
	}
}