<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_nikah/business/AppStatusNikah.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_nikah/business/AppStatusNikahToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_nikah/business/AppStatusNikahToUm.class.php';
class ProcessStatusNikah {

	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 AppStatusNikah();
		$this->siaObj = new AppStatusNikahToSia();
		$this->umObj = new AppStatusNikahToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_nikah', 'statusNikah', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_nikah', 'inputStatusNikah', 'view', 'html');
		$this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$addStatusNikah = $this->Obj->DoAddStatusNikah(ucwords($this->_POST['status_nikah_nama']));
			$dataIdStatus = $this->Obj->GetIdStatusNikah();

			$idEnc = $dataIdStatus[0]['id'];
			
			$addStatusNikahToSia = $this->siaObj->DoAddStatusNikahToSia($idEnc, ucwords($this->_POST['status_nikah_nama']));
		
      	$addData = $addStatusNikah && $dataIdStatus && $addStatusNikahToSia;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $addStatusNikahToUm=$this->umObj->DoAddStatusNikahToUm($idEnc, ucwords($this->_POST['status_nikah_nama']));
            $addData = $addData && $addStatusNikahToUm;
            $this->umObj->EndTrans($addData);
         }
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_nikah', 'inputStatusNikah', '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();
         
			$updateStatusNikah = $this->Obj->DoUpdateStatusNikah(ucwords($this->_POST['status_nikah_nama']), $this->decId);
			$updateStatusNikahSia = $this->siaObj->DoUpdateStatusNikahSia(ucwords($this->_POST['status_nikah_nama']), $this->decId);
			$updateData = $updateStatusNikah && $updateStatusNikahSia;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $updateStatusNikahUm=$this->umObj->DoUpdateStatusNikahUm(ucwords($this->_POST['status_nikah_nama']),$this->decId);
            $updateData = $updateData && $updateStatusNikahUm;
            $this->umObj->EndTrans($updateData);
         }
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
			if ($updateData === true) {
				Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_nikah', 'inputStatusNikah', '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_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }elseif(!$this->umObj->addConn){
         Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('status_nikah', 'statusNikah', '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();
   		
   		$deleteStatusNikahById = $this->Obj->DoDeleteStatusNikahById($arrId);
   		$deleteStatusNikahSiaById = $this->siaObj->DoDeleteStatusNikahSiaById($arrId);
   		$deleteDataById = $deleteStatusNikahById && $deleteStatusNikahSiaById;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteStatusNikahUmById=$this->umObj->DoDeleteStatusNikahUmById($arrId);
            $deleteDataById = $deleteDataById && $deleteStatusNikahUmById;
            $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();
            
            $deleteArrStatusNikah = $this->Obj->DoDeleteStatusNikahByArrayId($arr);
            $deleteArrStatusNikahSia = $this->siaObj->DoDeleteStatusNikahSiaByArrayId($arr);
            $deleteArrData = $deleteArrStatusNikah && $deleteArrStatusNikahSia;
            if($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrStatusNikahUm=$this->umObj->DoDeleteStatusNikahUmByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrStatusNikahUm;
               $this->umObj->EndTrans($deleteArrData);
            }
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
   		}
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('status_nikah', 'statusNikah', '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;
   				$deleteStatusNikah = $this->Obj->DoDeleteStatusNikahById($arr[$i]);
   				$deleteStatusNikahSia = $this->siaObj->DoDeleteStatusNikahSiaById($arr[$i]);
   				$deleteData = $deleteStatusNikah && $deleteStatusNikahSia;
               if($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteStatusNikahUm=$this->umObj->DoDeleteStatusNikahUmById($arr[$i]);
                  $deleteData = $deleteData && $deleteStatusNikahUm;
                  $this->umObj->EndTrans($deleteData);
               }
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('status_nikah', 'statusNikah', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
