<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/jenis_semester/business/AppJenisSemesterToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/jenis_semester/business/AppJenisSemesterToFinansi.class.php';
class ProcessJenisSemester {

	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->ObjToSia = new AppJenisSemesterToSia();
		$this->ObjToFinansi = new AppJenisSemesterToFinansi();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('jenis_semester', 'jenisSemester', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('jenis_semester', 'inputJenisSemester', 'view', 'html');
	}

	function Check() {

		if (isset($_POST['btnsimpan'])) {
			if((trim($this->_POST['label']) == "") or (trim($this->_POST['label_singkat']) == "")  or (trim($this->_POST['kategori']) == "") or (trim($this->_POST['periode']) == "") ){
            
				return "empty";
			}else{
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->ObjToSia->StartTrans();
         $this->ObjToFinansi->StartTrans();
         
         $idJenisSems=$this->ObjToSia->GetMaxIdJenisSemeter();
         if(empty($idJenisSems[0]['MAX']))
            $idJenisSems[0]['MAX']=0;
            
			$addJenisSemsToSia = $this->ObjToSia->DoAddJenisSemester($idJenisSems[0]['MAX']+1,$this->_POST['kategori'],$this->_POST['periode'],$this->_POST['label'],$this->_POST['label_singkat']);
			$addJenisSemsToFinansi = $this->ObjToFinansi->DoAddJenisSemester($idJenisSems[0]['MAX']+1,$this->_POST['label'],$this->_POST['periode']);
         
         $addData=$addJenisSemsToSia && $addJenisSemsToFinansi;
         
         $this->ObjToSia->EndTrans($addData);
         $this->ObjToFinansi->EndTrans($addData);
         
			if ($addData  === true) {
				Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);             
			}
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jenis_semester', 'inputJenisSemester', '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->ObjToSia->StartTrans();
         $this->ObjToFinansi->StartTrans();
         
			$updateJenisSemesterToSia = $this->ObjToSia->DoUpdateJenisSemester($this->_POST['kategori'],$this->_POST['periode'],$this->_POST['label'],$this->_POST['label_singkat'], $this->decId);
			$updateJenisSemesterToFinansi = $this->ObjToFinansi->DoUpdateJenisSemester($this->_POST['label'],$this->_POST['periode'],$this->decId);
         
         $updateData=$updateJenisSemesterToSia && $updateJenisSemesterToFinansi;
         
         $this->ObjToSia->EndTrans($updateData);
         $this->ObjToFinansi->EndTrans($updateData);
         
         if ($updateData === true) {
				Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
             Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }                
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jenis_semester', 'inputJenisSemester', '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) and($this->ObjToFinansi->addConn)){
         
         $this->ObjToFinansi->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $deleteDataByIdToFinansi=$this->ObjToFinansi->DoDeleteJenisSemesterById($arrId);
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteJenisSemesterById($arrId);
         
         $deleteDataById=$deleteDataByIdToFinansi && $deleteDataByIdToSia;
         
         $this->ObjToFinansi->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->ObjToFinansi->StartTrans();
            $this->ObjToSia->StartTrans();
            
            $deleteArrDataToFiansi = $this->ObjToFinansi-> DoDeleteJenisSemesterByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia-> DoDeleteJenisSemesterByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToFiansi && $deleteArrDataToSia;
            
            $this->ObjToSia->EndTrans($deleteArrData);
            $this->ObjToFinansi->EndTrans($deleteArrData);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('jenis_semester', 'jenisSemester', '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->ObjToFinansi->StartTrans();
               $this->ObjToSia->StartTrans();
               
               $deleteDataToFinansi = $this->ObjToFinansi->DoDeleteJalurMasukById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeleteJalurMasukById($arr[$i]);
               
               $deleteData = $deleteDataToSia && $deleteDataToFinansi;
               
               $this->ObjToFinansi->EndTrans($deleteData);
               $this->ObjToSia->EndTrans($deleteData);
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif((!$this->ObjToSia->addConn)and(!$this->ObjToSia->addConn)){
          Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik <br> Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->ObjToSia->addConn){
          Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->ObjToFinansi->addConn){
          Messenger::Instance()->Send('jenis_semester', 'jenisSemester', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
