<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
'module/golongan_bidang/business/AppGolonganbidang.class.php';
class ProcessBidang {

	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	//css hanya dipake di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;
	var $decId;
	var $encId;

	function __construct() {
		$this->Obj = new AppGolonganbidang();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('golongan_bidang', 'golonganbidang', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('golongan_bidang', 'inputBidang', 'view', 'html');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(intval($this->_POST['kode']) == 0 OR trim($this->_POST['nama']) == "") {
				return "empty";
         }
         elseif ($this->Obj->CekKodeBidang($this->decId, $this->_POST['kode'], $this->_POST['kodegol'])){
            return "duplikasi";
			} else {
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
			$addGolonganbidang = $this->Obj->DoAddBidang($this->_POST['kodegol'], $this->_POST['kode'], $this->_POST['nama']);
			if ($addGolonganbidang === true) {
				Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
			return $this->pageView;
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('golongan_bidang', 'inputBidang', 'view', 'html', array($this->_POST,'Lengkapi isian data dengan benar!'),Messenger::NextRequest);
			return $this->pageInput;
		} elseif($cek == "duplikasi") {
			Messenger::Instance()->Send('golongan_bidang', 'inputBidang', 'view', 'html', array($this->_POST,'Kode bidang sudah digunakan'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView."&cari=".$_GET['cari']->Raw()."&page=".$_GET['page']->Integer()->Raw();
	}

	function Update() {
		$cek = $this->Check();
		if($cek === true) {
			$updateGolongan = $this->Obj->DoUpdateBidang($this->decId, $this->_POST['kodegol'], $this->_POST['kode'], $this->_POST['nama']);
			if ($updateGolongan === true) {
				Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('golongan_bidang', 'inputBidang', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		} elseif($cek == "duplikasi") {
			Messenger::Instance()->Send('golongan_bidang', 'inputBidang', 'view', 'html', array($this->_POST,'Kode bidang sudah digunakan'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView."&cari=".$_GET['cari']->Raw()."&page=".$_GET['page']->Integer()->Raw();
	}

	function Delete() {
		$arrId = $this->_POST['idDelete'];
		if (!is_array($arrId)) $arrId = array($arrId);
		$deleteArrData = $this->Obj->DoMassDeleteBidang($arrId);
		
		if($deleteArrData === true) {
			Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} else {
			//jika masuk disini, berarti PASTI ada salah satu atau lebih data yang gagal dihapus
			for($i=0;$i<sizeof($arrId);$i++) {
				$deleteData = false;
				$deleteData = $this->Obj->DoDeleteBidang($arrId[$i]);
				if($deleteData === true) $sukses += 1;
				else $gagal += 1;
			}
         if($gagal > 0) {
			   Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
				return $this->pageView."&cari=".$_GET['cari']->Raw()."&page=".$_GET['page']->Integer()->Raw();
         } else {
			   Messenger::Instance()->Send('golongan_bidang', 'golonganbidang', 'view', 'html', array($this->_POST, $sukses . ' Data Dihapus.', $this->cssDone),Messenger::NextRequest);
         }
		}
		return $this->pageView;
	}
}
?>
