<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/fakultas/business/AppFakultasToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/fakultas/business/AppFakultasToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/fakultas/business/AppFakultasToPortal.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/fakultas/business/AppFakultasToFinansi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/fakultas/business/AppFakultasToUm.class.php';

class ProcessFakultas {

   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->fakObjAdmisi = new AppFakultasToAdmisi();
      $this->fakObjSia = new AppFakultasToSia();
      $this->fakObjPortal = new AppFakultasToPortal();
      $this->fakObjFinansi = new AppFakultasToFinansi();
      $this->fakObjUm = new AppFakultasToUm();
      
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('fakultas', 'fakultas', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('fakultas', 'inputFakultas', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
   }
   
   function Check() {
      if (isset($_POST['btnsimpan'])) {
         if((trim($this->_POST['fak_nama_resmi']) == "") or(trim($this->_POST['fak_nama_singkat']) == "")) {
            return "empty";
         } else{
            return true;
         }
      }
      return false;
   }
   
   function Add(){
      $cek = $this->Check();
      if($cek === true) {
         
         $this->fakObjAdmisi->StartTrans();
         $this->fakObjSia->StartTrans();
         $this->fakObjPortal->StartTrans();
         $this->fakObjFinansi->StartTrans();
         
         $maxId=$this->fakObjAdmisi->GetMaxIdFakultas();
         if(empty($maxId[0]['max_id']))
            $maxId[0]['max_id']=0;
         
         
         $addFakultasPortal = $this->fakObjPortal->DoAddFakultas($maxId[0]['max_id']+1,strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']));
         $addFakultasAdmisi = $this->fakObjAdmisi->DoAddFakultas($maxId[0]['max_id']+1,strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']));
         $addFakultasSia = $this->fakObjSia->DoAddFakultas($maxId[0]['max_id']+1,strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']));
         $addFakultasFinansi = $this->fakObjFinansi->DoAddFakultas($maxId[0]['max_id']+1,$maxId[0]['max_id']+1,strtoupper($this->_POST['fak_nama_resmi']));
         
         $addFakultas=$addFakultasAdmisi && $addFakultasSia && $addFakultasPortal && $addFakultasFinansi;
         
         if($this->useUm==true){
            $this->fakObjUm->StartTrans();
            $addFakultasUm=$this->fakObjUm->DoAddFakultas($maxId[0]['max_id']+1,strtoupper($this->_POST['fak_nama_resmi']));
            $addFakultas = $addFakultas && $addFakultasUm;
            $this->fakObjUm->EndTrans($addFakultas);
         }
        /*var_dump($addFakultasAdmisi);
        var_dump($addFakultasSia);
        var_dump($addFakultasPortal);
        var_dump($addFakultasFinansi);exit;*/
         
         $this->fakObjAdmisi->EndTrans($addFakultas);
         $this->fakObjSia->EndTrans($addFakultas);
         $this->fakObjPortal->EndTrans($addFakultas);
         $this->fakObjFinansi->EndTrans($addFakultas);
         
         if ($addFakultas  === true) {
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('fakultas', 'inputFakultas', '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->fakObjAdmisi->StartTrans();
         $this->fakObjSia->StartTrans();
         $this->fakObjPortal->StartTrans();
         $this->fakObjFinansi->StartTrans();
         $maxId=$this->fakObjAdmisi->GetMaxIdFakultas();
         
         $updateFakultasAdmisi = $this->fakObjAdmisi->DoUpdateFakultas(strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']),$this->decId);
         $updateFakultasSia = $this->fakObjSia->DoUpdateFakultas(strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']),$this->decId);
         $updateFakultasPortal = $this->fakObjPortal->DoUpdateFakultas(strtoupper($this->_POST['fak_nama_resmi']),strtoupper($this->_POST['fak_nama_singkat']),$this->decId);
         $updateFakultasFinansi = $this->fakObjFinansi->DoUpdateFakultas($this->decId,strtoupper($this->_POST['fak_nama_resmi']),$this->decId);
         
         $updateFakultas=$updateFakultasAdmisi && $updateFakultasSia && $updateFakultasPortal && $updateFakultasFinansi;
         
         if($this->useUm==true){
            $this->fakObjUm->StartTrans();
            $updateFakultasUm=$this->fakObjUm->DoUpdateFakultas(strtoupper($this->_POST['fak_nama_resmi']),$this->decId);
            $updateFakultas = $updateFakultas && $updateFakultasUm;
            $this->fakObjUm->EndTrans($updateFakultas);
         }
         
         $this->fakObjAdmisi->EndTrans($updateFakultas);
         $this->fakObjSia->EndTrans($updateFakultas);
         $this->fakObjPortal->EndTrans($updateFakultas);
         $this->fakObjFinansi->EndTrans($updateFakultas);
         
         if ($updateFakultas  === true) {
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('fakultas', 'inputFakultas', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput;
      }
      return $this->pageView; 
   }
   
   function Delete(){
      $arrId = $this->_POST['idDelete'];
      if(($this->fakObjSia->addConn) and ($this->fakObjPortal->addConn)and ($this->fakObjFinansi->addConn) and ($this->fakObjUm->addConn || $this->useUm==false))
      {
         $this->fakObjAdmisi->StartTrans();
         $this->fakObjSia->StartTrans();
         $this->fakObjPortal->StartTrans();
         $this->fakObjFinansi->StartTrans();
         
         $deleteDataByIdAdmisi=$this->fakObjAdmisi->DoDeleteFakultasById($arrId);
         $deleteDataByIdSia=$this->fakObjSia->DoDeleteFakultasById($arrId);
         $deleteDataByIdPortal=$this->fakObjPortal->DoDeleteFakultasById($arrId);
         $deleteDataByIdFinansi=$this->fakObjFinansi->DoDeleteFakultasById($arrId);
     
         $deleteDataById=$deleteDataByIdAdmisi && $deleteDataByIdSia && $deleteDataByIdPortal && $deleteDataByIdFinansi;
         
         if($this->useUm==true){
            $this->fakObjUm->StartTrans();
            $deleteDataByIdUm=$this->fakObjUm->DoDeleteFakultasById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdUm;
            $this->fakObjUm->EndTrans($deleteDataById);
         }
         
         $this->fakObjAdmisi->EndTrans($deleteDataById);
         $this->fakObjSia->EndTrans($deleteDataById);
         $this->fakObjPortal->EndTrans($deleteDataById);
         $this->fakObjFinansi->EndTrans($deleteDataById);
         

         if($deleteDataById=== false){   
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->fakObjAdmisi->StartTrans();
            $this->fakObjSia->StartTrans();
            $this->fakObjPortal->StartTrans();
            $this->fakObjFinansi->StartTrans();
         
            $deleteArrDataAdmisi = $this->fakObjAdmisi->DoDeleteFakultasByArrayId($arr);
            $deleteArrDataSia = $this->fakObjSia->DoDeleteFakultasByArrayId($arr);
            $deleteArrDataPortal = $this->fakObjPortal->DoDeleteFakultasByArrayId($arr);
            $deleteArrDataFinansi = $this->fakObjFinansi->DoDeleteFakultasByArrayId($arr);
            /**
            var_dump($deleteArrDataAdmisi);
               var_dump($deleteArrDataSia);
               var_dump($deleteArrDataPortal);
               var_dump($deleteArrDataFinansi);exit;
               /**/
            
            $deleteArrData=$deleteArrDataAdmisi && $deleteArrDataSia && $deleteArrDataPortal && $deleteArrDataFinansi;
            
            if($this->useUm==true){
               $this->fakObjUm->StartTrans();
               $deleteArrDataUm=$this->fakObjUm->DoDeleteFakultasByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataUm;
               $this->fakObjUm->EndTrans($deleteArrData);
            }
            
            $this->fakObjAdmisi->EndTrans($deleteArrData);
            $this->fakObjSia->EndTrans($deleteArrData);
            $this->fakObjPortal->EndTrans($deleteArrData);
            $this->fakObjFinansi->EndTrans($deleteArrData);
         }

         if($deleteArrData === true) {
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('fakultas', 'fakultas', '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->fakObjAdmisi->StartTrans();
               $this->fakObjSia->StartTrans();
               $this->fakObjPortal->StartTrans();
               $this->fakObjFinansi->StartTrans();
               
               $deleteDataAdmisi = $this->fakObjAdmisi->DoDeleteFakultasById($arr[$i]);
               $deleteDataSia = $this->fakObjSia->DoDeleteFakultasById($arr[$i]);
               $deleteDataPortal = $this->fakObjPortal->DoDeleteFakultasById($arr[$i]);
               $deleteDataFinansi = $this->fakObjFinansi->DoDeleteFakultasById($arr[$i]);
               
               /**
               var_dump($deleteDataAdmisi);
               var_dump($deleteDataSia);
               var_dump($deleteDataPortal);
               var_dump($deleteDataFinansi);
exit;
/**/
               $deleteData=$deleteDataAdmisi && $deleteDataSia && $deleteDataPortal && $deleteDataFinansi;
               
               if($this->useUm==true){
                  $this->fakObjUm->StartTrans();
                  $deleteDataUm=$this->fakObjUm->DoDeleteFakultasById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataUm;
                  $this->fakObjUm->EndTrans($deleteData);
               }
               
               $this->fakObjAdmisi->EndTrans($deleteData);
               $this->fakObjSia->EndTrans($deleteData);
               $this->fakObjPortal->EndTrans($deleteData);
               $this->fakObjFinansi->EndTrans($deleteData);
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif((!$this->fakObjSia->addConn)and(!$this->fakObjPortal->addConn)and(!$this->fakObjFinansi->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik <br>Gagal Menghubungi SI Portal<br>Gagal Menghubungi SI Finansi', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->fakObjSia->addConn)and(!$this->fakObjPortal->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', '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)and(!$this->fakObjFinansi->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', '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->fakObjPortal->addConn)and(!$this->fakObjFinansi->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Portal <br>Gagal Menghubungi SI Finansi', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->fakObjSia->addConn)){       
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->fakObjPortal->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Portal ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->fakObjFinansi->addConn)){
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->fakObjUm->addConn)&& ($this->useUm==true)){
         Messenger::Instance()->Send('fakultas', 'fakultas', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);   
      }
      return $this->pageView;
   }
}
?>