<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/waktu_kuliah/business/AppWaktuKuliah.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/waktu_kuliah/business/AppWaktuKuliahToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/waktu_kuliah/business/AppWaktuKuliahToUm.class.php';

class ProcessWaktuKuliah {

   var $_POST;
   var $ObjSia;
   var $ObjReg;
   var $ObjUm;
   var $pageView;
   var $pageInput;
   //css hanya dipake di view
   var $cssDone = "notebox-done";
   var $cssFail = "notebox-warning";

   var $return;
   var $decId;
   var $encId;
   var $useUm;

   function __construct() {
      $this->ObjSia = new AppWaktuKuliahToSia();
      $this->ObjReg = new AppWaktuKuliah();
      $this->ObjUm = new AppWaktuKuliahToUm();
      
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('waktu_kuliah', 'waktuKuliah', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('waktu_kuliah', 'waktuKuliah', 'view', 'html');
      
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
   }

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

   function Add() {
      $cek = $this->Check();
      if($cek === true) {

         $checkIsExist=$this->ObjSia->GetIsExist($this->_POST['wkt_nama']);
         
         if($checkIsExist==0){
         	
            $this->ObjSia->StartTrans();
            $this->ObjReg->StartTrans();
            
            $addWaktuKuliah = $this->ObjSia->DoAddWaktuKuliah(ucwords(strtolower($this->_POST['wkt_nama'])),ucwords(strtolower($this->_POST['wkt_keterangan'])));
            
            $addWaktuKuliahReg = $this->ObjReg->DoAddWaktuKuliah(ucwords(strtolower($this->_POST['wkt_nama'])),ucwords(strtolower($this->_POST['wkt_keterangan'])));
         
            $addWaktuKuliah = $addWaktuKuliah && $addWaktuKuliahReg;
            
            //cek apakah menggunakan um
            if($this->useUm == true){
            	$this->ObjUm->StartTrans();
            	
            	$addWaktuKuliahUm = $this->ObjUm->DoAddWaktuKuliah(ucwords(strtolower($this->_POST['wkt_nama'])),ucwords(strtolower($this->_POST['wkt_keterangan'])));
            	
            	$addWaktuKuliah = $addWaktuKuliah && $addWaktuKuliahUm;
            	
            	$this->ObjUm->EndTrans($addWaktuKuliah);
            }
            
            $this->ObjSia->EndTrans($addWaktuKuliah);
            $this->ObjReg->EndTrans($addWaktuKuliah);
            
            if ($addWaktuKuliah === true) {
               Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Nama Waktu Kuliah "'.$this->_POST['wkt_nama'].'" Sudah Di Gunakan'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput;
      }
      return $this->pageView;
   }

   function Update() {
      $cek = $this->Check();
      if($cek === true) {
      

         if($this->_POST['dataId']!=$this->decId)
            $checkIsExist=$this->ObjSia->GetIsExist($this->_POST['dataId']);
         else
            $checkIsExist=0;
         if($checkIsExist==0){
                     
            $this->ObjReg->StartTrans();
            $this->ObjSia->StartTrans();            
            
            $updateWaktuKuliah = $this->ObjSia->DoUpdateWaktuKuliah($this->_POST['dataId'],ucwords(strtolower($this->_POST['wkt_nama'])), ucwords(strtolower($this->_POST['wkt_keterangan'])));
            
            $updateWaktuKuliahReg = $this->ObjReg->DoUpdateWaktuKuliah($this->_POST['dataId'],ucwords(strtolower($this->_POST['wkt_nama'])), ucwords(strtolower($this->_POST['wkt_keterangan'])));
            
            $updateWaktuKuliah = $updateWaktuKuliah && $updateWaktuKuliahReg; 
            
            //cek apakah menggunakan um
            if($this->useUm == true){
            	$this->ObjUm->StartTrans();
            	
            	$updateWaktuKuliahUm = $this->ObjUm->DoUpdateWaktuKuliah($this->_POST['dataId'],ucwords(strtolower($this->_POST['wkt_nama'])), ucwords(strtolower($this->_POST['wkt_keterangan'])));
            	            	
            	$updateWaktuKuliah = $updateWaktuKuliah && $updateWaktuKuliahUm;
            	
            	$this->ObjUm->EndTrans($updateWaktuKuliah);
            }
                                    
            $this->ObjReg->EndTrans($updateWaktuKuliah);
            $this->ObjSia->EndTrans($updateWaktuKuliah);
            
            if ($updateWaktuKuliah === true) {
               Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('waktu_kuliah', 'inputWaktuKuliah', 'view', 'html', array($this->_POST,'Kode Waktu Kuliah "'.$this->_POST['gelombang_kode'].'" Sudah Di Gunakan'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('waktu_kuliah', 'inputWaktuKuliah', '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'];
      
      $this->ObjSia->StartTrans();
      $this->ObjReg->StartTrans();        
      
      $deleteDataById = $this->ObjSia->DoDeleteWaktuKuliahById($arrId);
      $deleteDataByIdReg = $this->ObjReg->DoDeleteWaktuKuliahById($arrId);
      
      $deleteDataById = $deleteDataById && $deleteDataByIdReg;
      
      	//cek apakah menggunakan um
	    if($this->useUm == true){
	    	$this->ObjUm->StartTrans();
	    	
	    	$deleteDataByIdUm = $this->ObjUm->DoDeleteWaktuKuliahById($arrId);	    	            	
	    	$deleteDataById = $deleteDataById && $deleteDataByIdUm;
	    	
	    	$this->ObjUm->EndTrans($deleteDataById);
	    }
                
      $this->ObjSia->EndTrans($deleteDataById);
      $this->ObjReg->EndTrans($deleteDataById);
      
      if($deleteDataById=== false){  
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
               
         $this->ObjSia->StartTrans();
         $this->ObjReg->StartTrans();        
         
         $deleteArrData = $this->ObjSia->DoDeleteWaktuKuliahByArrayId($arr);
         $deleteArrDataReg = $this->ObjReg->DoDeleteWaktuKuliahByArrayId($arr);
         
         $deleteArrData = $deleteArrData && $deleteArrDataReg;
         
         //cek apakah menggunakan um
	    if($this->useUm == true){
	    	$this->ObjUm->StartTrans();
	    	
	    	$deleteArrDataUm = $this->ObjUm->DoDeleteWaktuKuliahByArrayId($arr);
	    	
	    	$deleteArrData = $deleteArrData && $deleteArrDataUm;
	    	
	    	$this->ObjUm->EndTrans($deleteArrData);
	    }                  
                  
         
         $this->ObjSia->EndTrans($deleteArrData);
         $this->ObjReg->EndTrans($deleteArrData);
      }
      
      if($deleteArrData === true) {
         Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }elseif($deleteDataById===true){
            Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', '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->ObjSia->StartTrans();
            
            $deleteData = $this->ObjSia->DoDeleteGelombangById($arr[$i]);
            
            $this->ObjSia->EndTrans($deleteData);
            
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('waktu_kuliah', 'waktuKuliah', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }
}
?>