<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_smta/business/AppStatusSmta.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_smta/business/AppStatusSmtaToSia.class.php';
class ProcessStatusSmta {

	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 AppStatusSmta();
		$this->ObjToSia = new AppStatusSmtaToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_smta', 'statusSmta', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_smta', 'inputStatusSmta', 'view', 'html');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $maxId= $this->Obj->GetMaxIdStatusSmta();
         if(empty($maxId))
            $maxId=0;
         $newId=$maxId+ 1;
         
			$addStatusSmtaToSireg = $this->Obj->DoAddStatusSmta($newId,ucwords($this->_POST['status_smta']));
			$addStatusSmtaToSia = $this->ObjToSia->DoAddStatusSmta($newId,ucwords($this->_POST['status_smta']));
         
         $addStatusSmta=$addStatusSmtaToSireg && $addStatusSmtaToSia;
         
         $this->Obj->EndTrans($addStatusSmta);
         $this->ObjToSia->EndTrans($addStatusSmta);
         
			if ($addStatusSmta  === true) {
				Messenger::Instance()->Send('status_smta', 'statusSmta', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_smta', 'statusSmta', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_smta', 'inputStatusSmta', '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();
         
			$updateStatusSmtaToSireg = $this->Obj->DoUpdateStatusSmta(ucwords($this->_POST['status_smta']), $this->decId);
			$updateStatusSmtaToSia = $this->ObjToSia->DoUpdateStatusSmta(ucwords($this->_POST['status_smta']), $this->decId);
         
         $updateStatusSmta=$updateStatusSmtaToSireg && $updateStatusSmtaToSia;
         
         $this->Obj->EndTrans($updateStatusSmta);
         $this->ObjToSia->EndTrans($updateStatusSmta);
         
      if ($updateStatusSmta === true) {
				Messenger::Instance()->Send('status_smta', 'statusSmta', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('status_smta', 'statusSmta', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_smta', 'inputStatusSmta', '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();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeleteStatusSmtaById($arrId);
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteStatusSmtaById($arrId);
         
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         
         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
            if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans();
            
            $deleteArrDataToSireg = $this->Obj->DoDeleteStatusSmtaByArrayId($arr);
            $deleteArrDataToSia = $this->Obj->DoDeleteStatusSmtaByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            
            $this->Obj->EndTrans($deleteArrData);
            $this->ObjToSia->EndTrans($deleteArrData);
         }

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