<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pekerjaan/business/AppPekerjaan.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pekerjaan/business/AppPekerjaanToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pekerjaan/business/AppPekerjaanToUm.class.php';
class ProcessPekerjaan {

	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 AppPekerjaan();
		$this->siaObj = new AppPekerjaanToSia();
		$this->umObj = new AppPekerjaanToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('pekerjaan', 'pekerjaan', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('pekerjaan', 'inputPekerjaan', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['pekerjaan_nama']) == "") {
				return "empty";
			} else {
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$addPekerjaan = $this->Obj->DoAddPekerjaan($this->_POST['pekerjaan_nama']);
			$dataIdPekerjaan = $this->Obj->GetIdPekerjaan();
         $idDec = $dataIdPekerjaan[0]['id'];

			$addPekerjaanToSia = $this->siaObj->DoAddPekerjaanToSia($idDec, $this->_POST['pekerjaan_nama']);
			$addData = $addPekerjaan && $dataIdPekerjaan && $addPekerjaanToSia;
			
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $addPekerjaanToUm = $this->umObj->DoAddPekerjaanToUm($idDec, $this->_POST['pekerjaan_nama']);
            $addData = $addData && $addPekerjaanToUm; 
            $this->umObj->EndTrans($addData);
         }
         
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('pekerjaan', 'inputPekerjaan', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}
		return $this->pageView;
	}

	function Update() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$updatePekerjaan = $this->Obj->DoUpdatePekerjaan($this->_POST['pekerjaan_nama'], $this->decId);
			$updatePekerjaanSia = $this->siaObj->DoUpdatePekerjaanSia($this->_POST['pekerjaan_nama'], $this->decId);
			$updateData = $updatePekerjaan && $updatePekerjaanSia;
         
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $updatePekerjaanUm = $this->umObj->DoUpdatePekerjaanUm($this->_POST['pekerjaan_nama'], $this->decId);
            $updateData = $updateData && $updatePekerjaanUm; 
            $this->umObj->EndTrans($updatePekerjaanUm);
         }
         
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
			
			if ($updateData === true) {
				Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('pekerjaan', 'inputPekerjaan', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
      if(!$this->siaObj->addConn){
         Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif(!$this->umObj->addConn && $this->useUm==true ){
         Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Admisi', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
   		$arrId = $this->_POST['idDelete'];
   		$this->Obj->StartTrans();
   		$this->siaObj->StartTrans();
   		
   		$deletePekerjaanById = $this->Obj->DoDeletePekerjaanById($arrId);
   		$deletePekerjaanSiaById = $this->siaObj->DoDeletePekerjaanSiaById($arrId);
   		$deleteDataById = $deletePekerjaanById && $deletePekerjaanSiaById;
   		
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $deletePekerjaanUmById = $this->umObj->DoDeletePekerjaanUmById($arrId);
            $deleteDataById = $deleteDataById && $deletePekerjaanUmById; 
            $this->umObj->EndTrans($deleteDataById);
         }
         
   		$this->Obj->EndTrans($deleteDataById);
   		$this->siaObj->EndTrans($deleteDataById);
   		if($deleteDataById==false) {
   		    if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->siaObj->StartTrans();
            
            $deleteArrPekerjaan = $this->Obj->DoDeletePekerjaanByArrayId($arr);
            $deleteArrPekerjaanSia = $this->siaObj->DoDeletePekerjaanSiaByArrayId($arr);
            $deleteArrData = $deleteArrPekerjaan && $deleteArrPekerjaanSia;
            
            If($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrPekerjaanUm = $this->umObj->DoDeletePekerjaanUmByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrPekerjaanUm; 
               $this->umObj->EndTrans($deleteArrData);
            }
            
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
   		}
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('pekerjaan', 'pekerjaan', '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($arr);$i++) {
   			   $this->Obj->StartTrans();
   			   $this->siaObj->StartTrans();
   			   
   				$deleteData = false;
   				$deletePekerjaan = $this->Obj->DoDeletePekerjaanById($arr[$i]);
   				$deletePekerjaanSia = $this->siaObj->DoDeletePekerjaanSiaById($arr[$i]);
   				$deleteData = $deletePekerjaan && $deletePekerjaanSia;
   				
               If($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deletePekerjaanUm = $this->umObj->DoDeletePekerjaanUmById($arr[$i]);
                  $deleteData = $deleteData && $deletePekerjaanUm; 
                  $this->umObj->EndTrans($deleteData);
               }
               
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('pekerjaan', 'pekerjaan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
