<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_terdaftar/business/AppStatusTerdaftar.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_terdaftar/business/AppStatusTerdaftarToSia.class.php';
class ProcessStatusTerdaftar {

	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 AppStatusTerdaftar();
		$this->siaObj = new AppStatusTerdaftarToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_terdaftar', 'statusTerdaftar', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_terdaftar', 'inputStatusTerdaftar', 'view', 'html');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$addStatusTerdaftar = $this->Obj->DoAddStatusTerdaftar(ucwords($this->_POST['status_terdaftar_nama']));
			$dataIdStsTerdaftar = $this->Obj->GetIdStatusTerdaftar();
			for($i=0; $i<sizeof($dataIdStsTerdaftar); $i++) {
            $idEnc = $dataIdStsTerdaftar[$i]['id'];
         }
         $addStatusTerdaftarToSia = $this->siaObj->DoAddStatusTerdaftarToSia($idEnc, ucwords($this->_POST['status_terdaftar_nama']));
			$addData = $addStatusTerdaftar && $dataIdStsTerdaftar && $addStatusTerdaftarToSia;
			
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
         if ($addData === true) {
				Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_terdaftar', 'inputStatusTerdaftar', '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();
         
			$updateStatusTerdaftar = $this->Obj->DoUpdateStatusTerdaftar(ucwords($this->_POST['status_terdaftar_nama']), $this->decId);
			$updateStatusTerdaftarSia = $this->siaObj->DoUpdateStatusTerdaftarSia(ucwords($this->_POST['status_terdaftar_nama']), $this->decId);
			$updateData = $updateStatusTerdaftar && $updateStatusTerdaftarSia;
			
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
         if ($updateData === true) {
				Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_terdaftar', 'inputStatusTerdaftar', '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('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
   		$arrId = $this->_POST['idDelete'];
   		$this->Obj->StartTrans();
   		$this->siaObj->StartTrans();
   		
   		$deleteStsTerdaftarById = $this->Obj->DoDeleteStatusTerdaftarById($arrId);
   		$deleteStsTerdaftarSiaById = $this->siaObj->DoDeleteStatusTerdaftarSiaById($arrId);
   		$deleteDataById = $deleteStsTerdaftarById && $deleteStsTerdaftarSiaById;
   		
   		$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();
            
            $deleteArrStsTerdaftar = $this->Obj->DoDeleteStatusTerdaftarByArrayId($arr);
            $deleteArrStsTerdaftarSia = $this->siaObj->DoDeleteStatusTerdaftarSiaByArrayId($arr);
            $deleteArrData = $deleteArrStsTerdaftar && $deleteArrStsTerdaftarSia;
            
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
   		}
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', '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;
   				$deleteStsTerdaftar = $this->Obj->DoDeleteStatusTerdaftarById($arr[$i]);
   				$deleteStsTerdaftarSia = $this->siaObj->DoDeleteStatusTerdaftarSiaById($arr[$i]);
   				$deleteData = $deleteStsTerdaftar && $deleteStsTerdaftarSia;
   				
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('status_terdaftar', 'statusTerdaftar', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>