<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/predikat_lulus/business/AppPredikatLulus.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/predikat_lulus/business/AppPredikatLulusToSia.class.php';
class ProcessPredikatLulus {

	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 AppPredikatLulus();
		$this->ObjToSia = new AppPredikatLulusToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('predikat_lulus', 'predikatLulus', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('predikat_lulus', 'inputPredikatLulus', 'view', 'html');
	}

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

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
      
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $lastId= $this->Obj->GetLastIdPredikatLulus();
         if(empty($lastId))
            $lastId=0;
            
         $newKode=($lastId[0]['lastId']) + 1;
        
			$addPredikat = $this->Obj->DoAddPredikatLulus($newKode,ucwords($this->_POST['predikat_nama_lokal']),ucwords($this->_POST['predikat_nama_asing']));
         
			$addPredikatToSia = $this->ObjToSia->DoAddPredikatLulus($newKode,ucwords($this->_POST['predikat_nama_lokal']),ucwords($this->_POST['predikat_nama_asing']));
         
         $addData=$addPredikat && $addPredikatToSia;
         
         $this->Obj->EndTrans($addData);
         $this->ObjToSia->EndTrans($addData);
         
			if ($addData === true) {
				Messenger::Instance()->Send('predikat_lulus', 'predikatLulus', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('predikat_lulus', 'predikatLulus', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
         
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('predikat_lulus', 'inputPredikatLulus', '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();
         
			$updatePredikat = $this->Obj->DoUpdatePredikatLulus($this->decId,ucwords($this->_POST['predikat_nama_lokal']), ucwords($this->_POST['predikat_nama_asing']));
			
         $updatePredikatToSia = $this->ObjToSia->DoUpdatePredikatLulus($this->decId,ucwords($this->_POST['predikat_nama_lokal']), ucwords($this->_POST['predikat_nama_asing']));

         $updateData=$updatePredikat && $updatePredikatToSia;
         
         $this->Obj->EndTrans($updateData);
         $this->ObjToSia->EndTrans($updateData);
         
      if ($updateData === true) {
				Messenger::Instance()->Send('predikat_lulus', 'predikatLulus', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('predikat_lulus', 'predikatLulus', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
         
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('predikat_lulus', 'inputPredikatLulus', '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->DoDeletePredikatLulusById($arrId);
         
         $deleteDataByIdToSia=$this->ObjToSia->DoDeletePredikatLulusById($arrId);

         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         
         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);
          
         if($deleteDataById==false){
   		    if(is_array($arrId)==false){
               $arr[0]=$arrId;
            //print_r($arr);  
            }
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans();
         
            $deleteArrDataToSireg = $this->Obj->DoDeletePredikatLulusByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeletePredikatLulusByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            
            $this->Obj->EndTrans($deleteArrData);  
            $this->ObjToSia->EndTrans($deleteArrData);  
         }

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