<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/kuota_waktu_kuliah/business/AppKuotaWaktuKuliahToAdmisi.class.php';
class ProcessKuotaWaktuKuliah {
/*
semua proses tambah, edit, hapus hanya merubah data referensi kuota di db gtregistrasi
karena di akademik,hanya mengambil jumlah mahasiswa untuk sesi tsb
*/
   var $_POST;
   var $admisiObj;
   var $pageView;
   var $pageInput;
   var $cssDone = "notebox-done";
   var $cssFail = "notebox-warning";

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

   function __construct() {
      $this->admisiObj = new AppKuotaWaktuKuliahToAdmisi();
      
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html') . '&dataId=' . $this->encId;
   }

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

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

         
         #$maxId=$this->ObjAdmisi->GetMaxIdGelombang();
         $checkIsExist=$this->admisiObj->GetIsExist($this->_POST['thnAjaran'],$this->_POST['semester'],$this->_POST['prodi'],$this->_POST['sesi_kuliah']);
         
         if($checkIsExist==0){
            $this->admisiObj->StartTrans();
            
            $addKuota = $this->admisiObj->DoAddKuotaWaktuKuliah($this->_POST['thnAjaran'],$this->_POST['semester'],$this->_POST['prodi'],$this->_POST['sesi_kuliah'],$this->_POST['kuota_max']);
            
            
            $this->admisiObj->EndTrans($addKuota);
            
            if ($addKuota === true) {
               Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Data Sudah Terdapat Di Database'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput;
      } elseif($cek == "invalid"){
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Jumlah Kuota Maksimum Tidak Valid'),Messenger::NextRequest);
         return $this->pageInput;
	  }
      return $this->pageView;
   }

   function Update() {
      $cek = $this->Check();
      if($cek === true) {
      
		 $valid = true;
         $checkIsExist=$this->admisiObj->GetIsExist($this->_POST['thnAjaran'],$this->_POST['semester'],$this->_POST['prodi'],$this->_POST['sesi_kuliah']);
		 //cek jika perubahan data bertabrakan dengan data yg sudah ada
		 if(is_array($checkIsExist)){
		 foreach($checkIsExist as $data){
		 if($data['id']!=$this->decId)$valid = false;
		 }}
         if($valid){
         
            $this->admisiObj->StartTrans();
            $updateKuota = $this->admisiObj->DoUpdateKuotaWaktuKuliah($this->_POST['thnAjaran'],$this->_POST['semester'],$this->_POST['prodi'],$this->_POST['sesi_kuliah'],$this->_POST['kuota_max'],$this->decId);
                        
            $this->admisiObj->EndTrans($updateKuota);
            
            if ($updateKuota === true) {
               Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Data Sudah Terdapat Di Database'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      } elseif($cek == "invalid"){
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Jumlah Kuota Maksimum Tidak Valid'),Messenger::NextRequest);
         return $this->pageInput;
	  }
      return $this->pageView;
   }

   function Delete() {
      $arrId = $this->_POST['idDelete'];
      
      $this->admisiObj->StartTrans();
      
      $deleteDataById = $this->admisiObj->DoDeleteKuotaWaktuKuliahById($arrId);
            
      $this->admisiObj->EndTrans($deleteDataById);
      
      if($deleteDataById=== false){  
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
         $this->admisiObj->StartTrans();
         
         $deleteArrData = $this->admisiObj->DoDeleteKuotaWaktuKuliahByArrayId($arr);
         
         $this->admisiObj->EndTrans($deleteArrData);
         
      }
      
      if($deleteArrData === true) {
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }elseif($deleteDataById===true){
            Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', '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->admisiObj->StartTrans();
            
            $deleteData = $this->admisiObj->DoDeleteKuotaWaktuKuliahById($arr[$i]);
            
            $this->ObjAdmisi->EndTrans($deleteData);
            
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }
}
?>