<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_rumah/business/AppStatusRumah.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_rumah/business/AppStatusRumahToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_rumah/business/AppStatusRumahToUm.class.php';

class ProcessStatusRumah {

	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 AppStatusRumah();
		$this->ObjToSia = new AppStatusRumahToSia();
      $this->ObjToUm = new AppStatusRumahToUm();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_rumah', 'statusRumah', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_rumah', 'inputStatusRumah', 'view', 'html');
	}

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

	function Add() {
   
		$cek = $this->Check();
      
		if($cek === true) {
      
         $lastStatusRumah= $this->Obj->GetLastKodeStatusRumah();
         if(empty($lastStatusRumah[0]['statrumahId']))
            $lastStatusRumah[0]['statrumahId']=0;
            
         $newKode=($lastStatusRumah[0]['statrumahId']) + 1;
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         $this->ObjToUm->StartTrans();
         
         $addStatusRumah = $this->Obj->DoAddStatusRumah($newKode,ucwords($this->_POST['statusRumah']));
         $addStatusRumahToSia = $this->ObjToSia->DoAddStatusRumah($newKode,ucwords($this->_POST['statusRumah']));
         $addStatusRumahToUm = $this->ObjToUm->DoAddStatusRumah($newKode,ucwords($this->_POST['statusRumah']));
         
         $addData=$addStatusRumah && $addStatusRumahToSia && $addStatusRumahToUm;
         
         $this->Obj->EndTrans($addData);
         $this->ObjToSia->EndTrans($addData);
         $this->ObjToUm->EndTrans($addData);
         
			if ($addData  === true) {
				Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
         
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_rumah', 'inputStatusRumah', '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->ObjToSia->StartTrans();
         $this->ObjToUm->StartTrans();
      
			$updateStatusRumah = $this->Obj->DoUpdateStatusRumah(ucwords($this->_POST['statusRumah']), $this->decId);			
         $updateStatusRumahToSia = $this->ObjToSia->DoUpdateStatusRumah(ucwords($this->_POST['statusRumah']), $this->decId);
         $updateStatusRumahToUm = $this->ObjToUm->DoUpdateStatusRumah(ucwords($this->_POST['statusRumah']), $this->decId);

         $updateData = $updateStatusRumah && $updateStatusRumahToSia && $updateStatusRumahToUm;
         
         $this->Obj->EndTrans($updateData);        
         $this->ObjToSia->EndTrans($updateData);        
         $this->ObjToUm->EndTrans($updateData);
         
      if ($updateData === true) {
				Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_rumah', 'inputStatusRumah', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
		$arrId = $this->_POST['idDelete'];
      if($this->ObjToSia->addConn){
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         $this->ObjToUm->StartTrans();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeleteStatusRumahById($arrId);
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteStatusRumahById($arrId);         
         $deleteDataByIdToUm=$this->ObjToUm->DoDeleteStatusRumahById($arrId);         
         
         $deleteDataById=$deleteDataByIdToSireg &&  $deleteDataByIdToSia && $deleteDataByIdToUm;
         
         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);
         $this->ObjToUm->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
            if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans();
            $this->ObjToUm->StartTrans();
            
            $deleteArrDataToSireg = $this->Obj->DoDeleteStatusRumahByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeleteStatusRumahByArrayId($arr);
            $deleteArrDataToUm = $this->ObjToUm->DoDeleteStatusRumahByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia && $deleteArrDataToUm;
            
            $this->Obj->EndTrans($deleteArrData);       
            $this->ObjToSia->EndTrans($deleteArrData);       
            $this->ObjToUm->EndTrans($deleteArrData);       
         }

         if($deleteArrData === true) {
            Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('status_rumah', 'statusRumah', '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++) {
               $deleteData = false;
               $this->Obj->StartTrans();
               $this->ObjToSia->StartTrans();
               $this->ObjToUm->StartTrans();               
               
               $deleteDataToSireg = $this->Obj->DoDeleteStatusRumahById($arr[$i]);
               $deleteDataToSia = $this->Obj->DoDeleteStatusRumahById($arr[$i]);
               $deleteDataToUm = $this->Obj->DoDeleteStatusRumahById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia && $deleteDataToUm;
               
               $this->Obj->EndTrans($deleteData);  
               $this->ObjToSia->EndTrans($deleteData);  
               $this->ObjToUm->EndTrans($deleteData);  
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('status_rumah', 'statusRumah', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
