<?php

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

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

	protected function set()
	{
		// ini_set('display_errors', 1);
		$this->set->em 			= $this->em;
		$this->set->modul 		= 'User';
		$this->set->dBaseH 		= 'sub';
		$this->set->dBaseD 		= false;
		$this->set->dBasePk 	= array('kd_sub');
		$this->set->dSort 		= 'asc';
		$this->set->where 		= array("jenis = '1'", "kd_sub != 'ADMIN'");
		$this->set->form 		= $this->setForm();
		$this->set->clearForm 	= array("reset");
		$this->set->notNull 	= array("kd_sub", "nm_sub", "kd_cabang", "jabatan", "aktif");
		$this->set->grid 		= $this->setGrid();
		$this->set->cetak 		= $this->setPrint();
		$this->set->button 		= array("Save", "First", "Prev", "Next", "Last", "Browse", "Add", "Del", "Edit", "prinT", "Group");
		$this->set->addClear 	= true;
		$this->set->funct 		= '_addUser';
		
		if(!empty($_GET['lpbm'])){
			$this->set->where 	= array("jenis = '1'", "kd_sub != 'ADMIN'", "jabatan = 'PETUGAS TL'");
		}
		if(!empty($_GET['kasir'])){
			$this->set->where 	= array("jenis = '1'", "kd_sub != 'ADMIN'", "jabatan = 'KASIR'");
		}
		
		return $this->set;
	}
	
	protected function setForm()
	{
		$form[] = array('row' => 0, 'col' => 25, "label" => array("name" => "Kode", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_sub', "size" => 15)));
		$form[] = array('row' => 25, 'col' => 25, "label" => array("name" => "Nama", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'nm_sub', "size" => 30)));
		$form[] = array('row' => 50, 'col' => 25, "label" => array("name" => "Cabang", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'kd_cabang', "size" => 4, 'maxlength' => 3, 'after' => 'load("cabang", "false", "")')));
		$form[] = array('row' => 75, 'col' => 25, "label" => array("name" => "Jabatan", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'jabatan', "size" => 15, 'after' => 'select("")', 'select' => 'jabatan')));
		$form[] = array('row' => 100, 'col' => 25, "label" => array("name" => "Aktif", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'aktif', "size" => 4, 'after' => 'select("")', 'select' => 'aktif')));
		$form[] = array('row' => 125, 'col' => 25, "label" => array("name" => "Reset Password", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'reset', "size" => 4, 'after' => 'select("")', 'select' => 'tf')));
		$form[] = array('row' => 150, 'col' => 25, "label" => array("name" => "Remark", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'ket', 'size' => 50)));
		
		$form[] = array('row' => 175, 'col' => 25, "label" => array("name" => "By", "width" => 100), "input" => array("type" => 'text',	'attr' => array("name" => 'chusr', 'size' => 15, 'readonly' => true)));
		$form[] = array('row' => 200, 'col' => 25, "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.kd_sub', "sort" => 'true', "width" => 100, 'align' => 'left', 'focus' => 'true');
		$res->attr[]	= array("label" => 'Nama User', 'attr' => 'a.nm_sub', "sort" => 'true', "width" => 200, 'align' => 'left');
		$res->attr[]	= array("label" => 'User Name', 'attr' => 'a.username', "sort" => 'true', "width" => 100, 'align' => 'left');
		$res->attr[]	= array("label" => 'Jabatan', 'attr' => 'a.jabatan', "sort" => 'true', "width" => 100, 'align' => 'left');
		$res->attr[]	= array("label" => 'Keterangan', 'attr' => 'a.ket', "sort" => 'true', "width" => 200, '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.kd_user', "sort" => 'true', "width" => 100, 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'User Name', 'attr' => 'a.username', "sort" => 'true', 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'User', 'attr' => 'a.nm_user', "sort" => 'true', 'align' => 'left', 'filter' => true);
		$res->attr[]	= array("label" => 'Jabatan', 'attr' => 'a.jabatan', "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'] == 'Group'){
			if(!empty($_GET['kode'])){
				$sql = $this->em->addDelete('grup_user');
				$sql = $this->em->getWhere("id_grup_pengguna = '".$_GET['kode']."'");
				$qb[] = $sql;
				$this->em->getFlush($qb);
			}
			$this->Group();
			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();
		
		$attrTambah['kd_sub'] = strtoupper($_POST['kd_sub']);
		$attrTambah['kd_cabang'] = strtoupper($_POST['kd_cabang']);
		$attrTambah['jenis'] = 1;
		$attrTambah['status'] = 'AKTIF';
		$attrTambah['hapus'] = 0;
		$attrTambah['username'] = strtoupper($_POST['kd_sub']);
		$attrTambah['nm_sub'] = strtoupper($_POST['nm_sub']);
		
		if($_POST['reset'] == 'T'){
			$attrTambah['pass'] = md5("12345");
		}
		
		if($validasi == true){
			$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']."')");
		return $sql;
	}
	
	protected 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 deleteGrup()
	{
		$sql = $this->em->addDelete('grup_user');
		$sql = $this->em->getWhere("id_grup_pengguna = '$kode'");
		return $sql;
	}
	
	public function getSimpanGrup()
	{
	
		$user = $this->em->getFind("sub", "*", array("username = '".strtoupper($_GET['user'])."'"));
		
		$sql = $this->em->addInsert('grup_user', 'a');
		$sql = $this->em->getAttrib('id_grup_pengguna, kd_user , kodegrup, chtime');
		$sql = $this->em->addValues( 
										"'".date('YmdHis')."', "
										."'".strtoupper($user->kd_sub)."', "
										."'".strtoupper($_POST['grup'])."', "
										."'".date("Y-m-d H:i:s")."' "
									);
		$qb[] = $sql;
		$qb[] = $this->deleteGrup();
		return $this->em->getFlush($qb);
	}
	
	public function Group()
	{
		if($_GET['act'] == 'del'){
			$sql = $this->em->addDelete('grup_user');
			$sql = $this->em->getWhere("upper(id_grup_pengguna) = '".strtoupper($_GET['user'])."'");
			$sql = $this->em->andWhere("upper(kodegrup) = '".strtoupper($_GET['id'])."'");
			$qb[] = $sql;
			
			$qb = array();
		}
	
		$user = $this->em->getFind("sub", "*", array("username = '".strtoupper($_GET['user'])."'"));

		if($_POST){
			$data = $this->em->getFind("grup_user", "id_grup_pengguna", array("upper(kodegrup) = '".strtoupper($_POST['grup'])."'", "kd_user = '".$user->kd_sub."'"));
			if(!empty($data->id_grup_pengguna)){
				echo "<script>alert('Kode Sudah ada');</script>";
			}else{
				$simpan = $this->getSimpanGrup();
				if($simpan == false){
					echo "<script>alert('Gagal');</script>";
				}
			}
		}
	
		$sql = $this->em->addSelect('a.id_grup_pengguna, b.kodegrup, b.namagrup, a.kd_user');
		$sql = $this->em->getFrom('grup_user', 'a');
		$sql = $this->em->addLeftJoin('group', 'b', array('upper(a.kodegrup) = upper(b.kodegrup)'));
		$sql = $this->em->getWhere("upper(a.kd_user) = '".$user->kd_sub."'");
		$eks = $this->em->queryEks($sql);
	
		$this->data->grup = $this->em->getFindBy('group', '*', array(), 'kodegrup asc');
		$this->data->user = $this->em->getFind('sub', '*', array("upper(kd_sub) = '".$user->kd_sub."'"));
		$this->data->data = $this->em->getFetchObject($eks);

		$this->view = $this->preView.'grup';
	}
	
	public function Disdef()
	{
		$data = $this->em->getFind($this->set->dBaseH, 'nm_sub', array("kd_sub = '".$_GET['idRef']."'"));
		
		$res->target = $_GET['target'];
		$res->nama 	= $data->nm_sub;
		echo json_encode($res);
		$this->layout = "null";
	}
	public function loadDisplay()
	{
		$pk = $this->set->dBasePk[0];
		$data = $this->em->getFind($this->set->dBaseH, '*', array("$pk = '".$_GET['idRef']."'"));
		
		$basic = new Basic;
		$res = $basic->loadDisplay($data, $_GET);
		echo json_encode($res);
		$this->layout = "null";
	}
}