<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/warga_negara/business/AppWargaNegara.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/warga_negara/business/AppWargaNegaraToSia.class.php';
class ProcessWargaNegara {

	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 AppWargaNegara();
		$this->siaObj = new AppWargaNegaraToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('warga_negara', 'wargaNegara', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('warga_negara', 'inputWargaNegara', 'view', 'html');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$addWargaNegara = $this->Obj->DoAddWargaNegara(strtoupper($this->_POST['warga_negara_nama']));
			$dataIdWargaNegara = $this->Obj->GetIdWargaNegara();
			for ($i=0; $i<sizeof($dataIdWargaNegara); $i++) {
            $idEnc = $dataIdWargaNegara[$i]['id'];
         }
         $addWargaNegaraToSia = $this->siaObj->DoAddWargaNegaraToSia($idEnc, strtoupper($this->_POST['warga_negara_nama']));
			$addData = $addWargaNegara && $dataIdWargaNegara && $addWargaNegaraToSia;
			
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('warga_negara', 'inputWargaNegara', '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();
         
			$updateWargaNegara = $this->Obj->DoUpdateWargaNegara(strtoupper($this->_POST['warga_negara_nama']), $this->decId);
			$updateWargaNegaraSia = $this->siaObj->DoUpdateWargaNegaraSia(strtoupper($this->_POST['warga_negara_nama']), $this->decId);
			$updateData = $updateWargaNegara && $updateWargaNegaraSia;
			
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
         if ($updateData === true) {
				Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('warga_negara', 'inputWargaNegara', '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('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('warga_negara', 'wargaNegara', '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();
   		
   		$deleteWnById = $this->Obj->DoDeleteWargaNegaraById($arrId);
   		$deleteWnSiaById = $this->siaObj->DoDeleteWargaNegaraSiaById($arrId);
   		$deleteDataById = $deleteWnById && $deleteWnSiaById;
   		
   		$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();
            
            $deleteArrWn = $this->Obj->DoDeleteWargaNegaraByArrayId($arr);
            $deleteArrWnSia = $this->siaObj->DoDeleteWargaNegaraSiaByArrayId($arr);
            $deleteArrData = $deleteArrWn && $deleteArrWnSia;
            
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
   		}
         if($deleteArrData === true) {
   			Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('warga_negara', 'wargaNegara', '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->siaObj->StartTrans();
   			   $this->siaObj->StartTrans();
   			   
   				$deleteData = false;
   				$deleteWn = $this->Obj->DoDeleteWargaNegaraById($arr[$i]);
   				$deleteWnSia = $this->siaObj->DoDeleteWargaNegaraSiaById($arr[$i]);
   				$deleteData = $deleteWn && $deleteWnSia;
   				
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('warga_negara', 'wargaNegara', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
