<?php

CLASS BasicGroup extends RasioProject
{
	protected $set;
	protected $enableCk = true;

	public function __construct($config)
	{
		$this->getConfig($config);
		$this->validation();
		$this->set();
	}

	protected function set()
	{
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'Group';
		$this->set->dBaseH 		= 'group';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kodegrup');
		$this->set->dSort 		= 'asc';
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array();
		$this->set->notNull 	= array("kodegrup", "namagrup");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT", "HakAkses");
		$this->set->addClear 	= true;
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "Group", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kodegrup', "size" => 15)));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Nama", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'namagrup', "size" => 30)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Remark", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', 'size' => 50)));

		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 125, 'col' => 500, "label" => array("name" => "Last", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chtime', 'size' => 20, 'readonly' => true)));
		return $form;
	}
	
	protected function setGrid()
	{
		$kode = new Kode;
		$res->page 	= 50;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kodegrup', "sort" => 'true', "width" => 100, 'align' => 'left', 'focus' => 'true');
		$res->attr[]	= array("label" => 'Group', 'attr' => 'a.namagrup', "sort" => 'true', "width" => 300, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', "width" => 400, 'align' => 'left');
		
		return $res;
	}
	
	protected function setPrint()
	{
		$kode = new Kode;
		$res->sortA = $kode->getPk($this->set->dBasePk);
		$res->sortT = 'asc';
		$res->width = '600px';
		
		$res->attr[]	= array("label" => 'Kode', 'attr' => 'a.kodegrup', "sort" => 'true', "width" => 100, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Group', 'attr' => 'a.namagrup', "sort" => 'true', 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Remark', 'attr' => 'a.ket', "sort" => 'true', "width" => 300, 'align' => 'left', 'filter' => true);
		
		return $res;
	}
	
	public function Index()
	{
		if($_GET['loadAct'] == 'akses'){
			$this->Akses();
			return;
		}
		if($_GET['loadAct'] == 'AksesAct'){
			$this->AksesAct();
			return;
		}
	
		$this->data = $this->set;
		$this->view = "default/index";
	}
	
	public function data()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayData";
		$this->layout = "view";
	}
	
	public function Find()
	{
		$basic = new Basic;
		$this->data = $basic->find($_GET, $this->set);
		$this->view = "default/displayForm";
		$this->layout = "view";
	}
	
	protected function validasi()
	{
		$basic = new Basic;
		$error[] = $basic->validasiBasic($_POST, $this->set);
		return $basic->getError($error);
	}
	
	public function proses()
	{
		$basic = new Basic;
		$validasi = $this->validasi();
		if($validasi == true){
			$attrTambah['kodegrup'] = strtoupper($_POST['kodegrup']);
			$attrTambah['namagrup'] = strtoupper($_POST['namagrup']);
			$basic->proses($_POST, $this->set, $attrTambah);
		}
		$this->layout = "null";
	}
	
	public function Delete()
	{
		$basic = new Basic;
		$this->data = $basic->delete($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function cetak()
	{
		$basic = new Basic;
		$this->data->data = $basic->printer($_GET, $this->set);
		$this->data->width = $this->set->cetak->width;
		$this->view = "default/cetak";
		$this->layout = "print";
	}
	
	public function load()
	{
		$basic = new Basic;
		$this->data = $basic->load($_GET, $this->set);
		$this->layout = "null";
	}
	
	public function popUp()
	{
		$basic = new Basic;
		$this->data = $basic->data($_GET, $this->set);
		$this->view = "default/displayDataPopUp";
		$this->layout = "view";
	}
	
	protected function delHA()
	{
		$sql = $this->em->addDelete("acl");
		$sql = $this->em->getWhere("upper(kodegrup) = upper('".$_GET['grup']."')");
		$sql = $this->em->andWhere("upper(app) = upper('".$_GET['app']."')");
		return $sql;
	}
	
	protected function getSimpan($param, $kode, $modul, $post, $no, $row)
	{
		$sql = $this->em->addInsert('acl');
		$sql = $this->em->getAttrib("id_acl, modul, app, $param, status, chtime, chusr, ket");
		$sql = $this->em->addValues( 
										"'".date('YmdHis').$no."', "
										."'$modul', "
										."'".strtoupper($_GET['app'])."', "
										."'$kode', "
										."'$post', "
										."'".date("Y-m-d H:i:s")."', "
										."'".$this->login['kd_user']."', "
										."'".$row['menu']."'"
									);
		return $sql;
	}
	
	public function Akses()
	{
		$folder = BASEPATH.BASENAME;
		$dir = opendir($folder);
		$menu = array();
		while($file = readdir($dir)){
			$ex = explode(".", $file);
			if(count($ex) == 1){
				$menu[$file] = $file;
			}
		}
		$this->data->menu = $menu;
		$this->view = $this->preView.'grupMenu';
	}
	
	public function AksesAct()
	{
		$param = $this->getParam;
		
		$grup = $_GET['grup'];
		$data = $this->em->getFind('group', '*', array("kodegrup = '".$grup."'"));
		
		$menu = new Menu;
		// $dataMenu = $menu->listDetMenu($_GET['app']);
		// $draftMenu= $menu->detailMenuApp($dataMenu);

		$dataMenu = $menu->listDetMenu($_GET['app']);
		$draftMenu= $menu->listDetMenuAkses($_GET['app']);
		
		$aclModel  = new Acl;
		
		if($_POST){
			
			$sql[] = $this->delHA();
			
			$no = 1;
			$acl = new Rasio_Acl;
			foreach($draftMenu as $menu => $row ){ 
				$key = $row['url'];
				foreach($acl as $key2 => $akses){
					$modul = $key.'/'.$key2;
					
					if(!empty($_POST['centangAll'])){
						$sql[] = $this->getSimpan('kodegrup', $grup, $modul, 1, $no++, $row);
					}else{
						if(!empty($_POST[$modul])){
							$sql[] = $this->getSimpan('kodegrup', $grup, $modul, $_POST[$modul], $no++, $row);
						}
					}
				}
			}	
			$this->em->getFlush($sql);
		}
		
		$dataAcl = $this->em->getFindBy('acl', 'modul, status', array("kodegrup = '".$grup."'"), "a.modul asc");
		
		$acl = array();
		foreach($dataAcl as $row){
			$modul = $row->modul;
			$acl[$modul] = true;
		}
		
		$this->data->dataMenu = $dataMenu;
		$this->data->data = $data;
		$this->data->menu = $draftMenu;
		$this->data->acl  = new Rasio_Acl;
		$this->data->dataAcl = $acl;
		$this->view = $this->preView.'hakAkses';
	}
}