<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jurusan/business/AppJurusanToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jurusan/business/AppJurusanToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jurusan/business/AppJurusanToPortal.class.php';

class ProcessJurusan{
   var $_POST;
   var $Obj;
   var $pageView;
   var $pageInput;
   var $cssDone = "notebox-done";
   var $cssFail = "notebox-warning";
   var $return;
   var $decId;
   var $encId;

   function __construct() {
   
      $this->ObjAdmisi = new AppJurusanToAdmisi();
      $this->ObjSia = new AppJurusanToSia();
      $this->ObjPortal = new AppJurusanToPortal();
      
      $this->_POST = $_POST->AsArray();
      
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      
      $this->pageView = Dispatcher::Instance()->GetUrl('jurusan', 'jurusan', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('jurusan', 'inputJurusan', 'view', 'html');
   }
   function Check() {
      if (isset($_POST['btnsimpan'])) {
         if( (trim($this->_POST['jur_fak_kode']) == "") or (trim($this->_POST['jur_nama_resmi']) == "")or (trim($this->_POST['jur_nama_singkat']) == ""))  {
            return "empty"; 
         }else{
            return true;
         }
      }
      return false;
   }

   function Add(){
      $cek=$this->Check();
      if($cek===true){
         
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         
         $maxId=$this->ObjAdmisi->GetMaxIdJurusan();
         
         if(empty($maxId[0]['max_id']))
            $maxId[0]['max_id']=0;
            
         $addJurusanToAdmisi=$this->ObjAdmisi->DoAddJurusan($maxId[0]['max_id']+1,$this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']));
         
         $addJurusanToSia=$this->ObjSia->DoAddJurusan($maxId[0]['max_id']+1,$this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']));
         
         $addJurusanToPortal=$this->ObjPortal->DoAddJurusan($maxId[0]['max_id']+1,$this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']));
         
         $addData=$addJurusanToAdmisi && $addJurusanToSia && $addJurusanToPortal;
         
         /**
         var_dump($addJurusanToAdmisi);
         var_dump($addJurusanToSia);
         var_dump($addJurusanToPortal);
         exit;
         /**/
   
         $this->ObjAdmisi->EndTrans($addData);
         $this->ObjSia->EndTrans($addData);
         $this->ObjPortal->EndTrans($addData);
         
         if($addData===true){
            Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('jurusan', 'inputJurusan', '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->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         
         $updateJurusanAdmisi=$this->ObjAdmisi->DoUpdateJurusan($this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']),$this->decId);
         
         $updateJurusanSia=$this->ObjSia->DoUpdateJurusan($this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']),$this->decId);
         
         $updateJurusanPortal=$this->ObjPortal->DoUpdateJurusan($this->_POST['jur_fak_kode'],strtoupper($this->_POST['jur_nama_resmi']),strtoupper($this->_POST['jur_nama_singkat']),$this->decId);
                  
         $updateData=$updateJurusanAdmisi && $updateJurusanSia && $updateJurusanPortal;
         
         $this->ObjAdmisi->EndTrans($updateData);
         $this->ObjSia->EndTrans($updateData);
         $this->ObjPortal->EndTrans($updateData);
         
         if($updateData===true){
            Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('jurusan', 'inputJurusan', '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->ObjSia->addConn) and ($this->ObjPortal->addConn))
      {      
      $this->ObjAdmisi->StartTrans();
      $this->ObjSia->StartTrans();
      $this->ObjPortal->StartTrans();
      
      $deleteDataByIdToAdmisi=$this->ObjAdmisi->DoDeleteJurusanById($arrId); 
      $deleteDataByIdToSia=$this->ObjSia->DoDeleteJurusanById($arrId); 
      $deleteDataByIdToPortal=$this->ObjPortal->DoDeleteJurusanById($arrId); 
     // print_r($deleteDataByIdToAdmisi);
    //  exit;
      $deleteDataById=$deleteDataByIdToAdmisi && $deleteDataByIdToSia && $deleteDataByIdToPortal;
      
      $this->ObjAdmisi->EndTrans($deleteDataById);
      $this->ObjSia->EndTrans($deleteDataById);
      $this->ObjPortal->EndTrans($deleteDataById);
      
      if($deleteDataById==false){
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
          //print_r($arrId);  
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();

         $deleteArrDataToAdmisi= $this->ObjAdmisi->DoDeleteJurusanByArrayId($arr);
         $deleteArrDataToSia = $this->ObjSia->DoDeleteJurusanByArrayId($arr);
         $deleteArrDataToPortal = $this->ObjPortal->DoDeleteJurusanByArrayId($arr);
         
         $deleteArrData=$deleteArrDataToAdmisi && $deleteArrDataToSia && $deleteArrDataToPortal;
         
         $this->ObjAdmisi->EndTrans($deleteArrData);
         $this->ObjSia->EndTrans($deleteArrData);
         $this->ObjPortal->EndTrans($deleteArrData);
      }
      if($deleteArrData === true) {
         Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }elseif($deleteDataById===true){
         Messenger::Instance()->Send('jurusan', 'jurusan', '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->ObjAdmisi->StartTrans();
            $this->ObjSia->StartTrans();
            $this->ObjPortal->StartTrans();  
            
            $deleteDataToAdmisi = $this->ObjAdmisi->DoDeleteJurusanById($arr[$i]);
            $deleteDataToSia = $this->ObjSia->DoDeleteJurusanById($arr[$i]);
            $deleteDataToPortal = $this->ObjPortal->DoDeleteJurusanById($arr[$i]);
            
            $deleteData=$deleteDataToAdmisi && $deleteDataToSia && $deleteDataToPortal;
            
            $this->ObjAdmisi->EndTrans($deleteData);
            $this->ObjSia->EndTrans($deleteData);
            $this->ObjPortal->EndTrans($deleteData);
            
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }
      }elseif((!$this->fakObjSia->addConn)and(!$this->fakObjPortal->addConn)){
         Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik <br>Gagal Menghubungi SI Portal', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->fakObjSia->addConn){
         Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik <br>Gagal Menghubungi SI Portal', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->fakObjPortal->addConn){
         Messenger::Instance()->Send('jurusan', 'jurusan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik <br>Gagal Menghubungi SI Portal', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }
}
?>
