<?php

class acl
{
	protected $em;
	protected $param;
	protected $sess;
	
	public function getDelete($param, $kode)
	{
		$sql = $this->em->addDelete('acl');
		$sql = $this->em->getWhere("$param = '$kode'");
		return $sql;
	}
	
	public function getSimpan($param, $kode, $modul, $post, $no)
	{
		$sql = $this->em->addInsert('acl');
		$sql = $this->em->getAttrib("id_acl, modul , $param, status");
		$sql = $this->em->addValues( 
										"'".date('YmdHis').$no."', "
										."'$modul', "
										."'$kode', "
										."'$post'"
									);
		return $sql;
	}
	
	public function getFindBy($param, $kode)
	{
		$sql = $this->em->addSelect('modul, status');
		$sql = $this->em->getFrom('acl', 'a');
		$sql = $this->em->getWhere("$param = '$kode'");
		return $this->em->queryEks($sql);
	}
	
	public function getAclUserLock($param)
	{
		$sql = $this->em->addSelect('modul, status');
		$sql = $this->em->getFrom('acl', 'a');
		$sql = $this->em->getWhere("modul = '$param/Toleransi'");
		$sql = $this->em->andWhere("kd_user = '".$_SESSION['loginHspm']['kode']."'");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		return $res[0];
	}
	
	public function getLock($class, $tgl)
	{
		$lockGlobal = $this->getLockGlobal($class, $tgl);

		if($lockGlobal == false){
			return true;
		}else{
			$lockTime = $this->getLockTimeUser($class, $tgl);
			if(empty($lockTime)){
				$lockTime = $this->getLockTimeGlobal();
			}

			return $this->getLockByDay($lockTime, $tgl);
		}
	}
	
	protected function getLockByDay($lockTime, $tgl)
	{
		$prevN = mktime(0, 0, 0, date("m"), date("d") - $lockTime, date("Y"));
		$date  = date("Y-m-d", $prevN); 
		$tgl   = date("Y-m-d", strtotime($tgl)); 
		
		if($date > $tgl){
			return true;
		}else{
			return false;
		}
	}
	
	protected function getLockTimeGlobal()
	{
		$set = $this->getModel('setting')->getFind(3);
		return $set->value;
	}
	
	protected function getLockTimeUser($class, $tgl)
	{
		$data = $this->getAclUserLock($class);
		return $data->status;
	}
	
	protected function getLockGlobal($class, $tgl)
	{
		$set = $this->getModel('setting')->getFind(2);
		if(!empty($set)){
			if($set->value == 'Y'){

				$set  = $this->getModel('setting')->getFind(1);
				$lock = date('Y-m-d', strtotime($set->value));
				$date = date('Y-m-d', strtotime($tgl));
				
				if($date > $lock){
					$result = true;
				}else{
					$result = false;
				}
				
				
			}else{
				$result = true;
			}
		}else{
			$result = true;
		}
		
		return $result;
	}
	
	public function cekAcl($param)
	{
		$sql = $this->em->addSelect('*');
		$sql = $this->em->getFrom('grup_user', 'a');
		$sql = $this->em->addLeftJoin('acl', 'b', array('a.kodegrup = b.kodegrup'));
		$sql = $this->em->getWhere("b.status = '1'");

		foreach($param as $row){
			$sql = $this->em->andWhere($row);
		}

		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		return $res[0]->status;
	}
	
	public function cekAclUser($param)
	{
		$sql = $this->em->addSelect('*');
		$sql = $this->em->getFrom('acl',' a');
		$sql = $this->em->getWhere("a.status = '1'");

		foreach($param as $row){
			$sql = $this->em->andWhere($row);
		}

		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		return $res[0]->status;
	}
	
	public function getUserAclTrue($param)
	{
		$sql = $this->em->addSelect('*');
		$sql = $this->em->getFrom('acl', 'a');
		$sql = $this->em->getWhere("kd_user = '".$this->sess['kode']."'");
		$sql = $this->em->andWhere($param);
		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function getGroupAclTrue($param)
	{
		$sql = $this->em->addSelect('*');
		$sql = $this->em->getFrom('grup_user', ' b');
		$sql = $this->em->addLeftJoin('acl', 'a', array('b.kodegrup = a.kodegrup'));
		$sql = $this->em->getWhere("b.kd_user = '".$this->sess['kode']."'");
		$sql = $this->em->andWhere($param);
		$eks = $this->em->queryEks($sql);
		return $this->em->getFetchObject($eks);
	}
	
	public function getAclGroup($menu, $kode_user)
	{
		$sql = $this->em->addSelect('a.status');
		$sql = $this->em->getFrom('grup_user', ' b');
		$sql = $this->em->addLeftJoin('acl', 'a', array('b.kodegrup = a.kodegrup'));
		$sql = $this->em->getWhere("b.kd_user = '".$kode_user."'");
		$sql = $this->em->andWhere("a.modul = '".$menu."'");
		$eks = $this->em->queryEks($sql);
		$res = $this->em->getFetchObject($eks);
		if($res[0]->status == '1'){
			return true;
		}else{
			return false;
		}
	}
	
	public function getActive($menu, $tool, $kode_user)
	{
		$menu = $menu.'/'.$tool;
		
		$user = $this->em->getFind('acl', 'status', array("a.kd_user = '".$user."'", "a.modul = '$menu'"));
		if($user->status != null){
			if($user->status == '1'){
				$st = true;
			}elseif($user->status == '2'){
				$st = false;
			}else{
				$st = $this->getAclGroup($menu, $kode_user);
			}
		}else{
			$st = $this->getAclGroup($menu, $kode_user);
		}
		return $st;
	}
	
}