<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/agama/business/AppAgama.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/agama/business/AppAgamaToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/agama/business/AppAgamaToUm.class.php';
class ProcessAgama {

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

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
			$this->siaObj->StartTrans();
			
         $addAgama = $this->Obj->DoAddAgama(strtoupper($this->_POST['agama_nama']));
         $dataIdAgama = $this->Obj->GetIdAgama();
         $idEnc = $dataIdAgama[0]['id'];
			$addAgamaToSia = $this->siaObj->DoAddAgamaToSia($idEnc, strtoupper($this->_POST['agama_nama']));
			$addData = $addAgama && $dataIdAgama && $addAgamaToSia;
         
         #tambahan untuk um
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $addAgamaToUm = $this->umObj->DoAddAgamaToUm($idEnc, strtoupper($this->_POST['agama_nama']));
            $addData = $addData && $addAgamaToUm;
            $this->umObj->EndTrans($addData);
         }
         #end
         
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('agama', 'inputAgama', '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();
         
			$updateAgama = $this->Obj->DoUpdateAgama(strtoupper($this->_POST['agama_nama']), $this->decId);
			$updateAgamaToSia = $this->siaObj->DoUpdateAgamaToSia(strtoupper($this->_POST['agama_nama']), $this->decId);
			$updateData = $updateAgama && $updateAgamaToSia;
			
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $updateAgamaToUm = $this->umObj->DoUpdateAgamaToUm(strtoupper($this->_POST['agama_nama']), $this->decId);
            $updateData = $updateData && $updateAgamaToUm;
            $this->umObj->EndTrans($updateData);
         }
         
			$this->Obj->EndTrans($updateData);
         $this->siaObj->EndTrans($updateData);
			if ($updateData === true) {
				Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('agama', 'inputAgama', '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('agama', 'agama', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif($this->useUm==true && !$this->umObj->addConn){
         Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database GtAdmisi', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
   		$arrId = $this->_POST['idDelete'];
         
   		$this->Obj->StartTrans();
   		$this->siaObj->StartTrans();

         $deleteAgamaById = $this->Obj->DoDeleteAgamaById($arrId);
   		$deleteAgamaSiaById = $this->siaObj->DoDeleteAgamaSiaById($arrId);
   		$deleteById = $deleteAgamaById && $deleteAgamaSiaById;
         
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteAgamaUmById = $this->umObj->DoDeleteAgamaUmById($arrId);
            $deleteById = $deleteById &&  $deleteAgamaUmById;
            $this->umObj->EndTrans($deleteById);
         }
         
         $this->Obj->EndTrans($deleteById);
   		$this->siaObj->EndTrans($deleteById);
         if($deleteById==false){
            $this->Obj->StartTrans();
            $this->siaObj->StartTrans();
            
   		   if(is_array($arrId)==false)$arr[0]=$arrId;else$arr=$arrId;
                
            $deleteAgamaByArrayId = $this->Obj->DoDeleteAgamaByArrayId($arr);
            $deleteAgamaSiaByArrayId = $this->siaObj->DoDeleteAgamaSiaByArrayId($arr);
            $deleteArrData = $deleteAgamaByArrayId && $deleteAgamaSiaByArrayId;

            if($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteAgamaUmByArrayId = $this->umObj->DoDeleteAgamaUmByArrayId($arr);
               $deleteArrData = $deleteArrData &&  $deleteAgamaUmByArrayId;
               $this->umObj->EndTrans($deleteArrData);
            }
            
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
         }
         
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteById === true) {
   			Messenger::Instance()->Send('agama', 'agama', '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;
   				$deleteAgama = $this->Obj->DoDeleteAgamaById($arr[$i]);
   				$deleteAgamaSia = $this->siaObj->DoDeleteAgamaSiaById($arr[$i]);
   				$deleteData = $deleteAgama && $deleteAgamaSia;

               if($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteAgamaUm=$this->umObj->DoDeleteAgamaUmById($arr[$i]);
                  $deleteData = $deleteData && $deleteAgamaUm;
                  $this->umObj->EndTrans($deleteData);
               }
               
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('agama', 'agama', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
      return $this->pageView;
      }
   }
}
?>
