<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jenjang_akademik/business/AppJenjangAkademikToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jenjang_akademik/business/AppJenjangAkademikToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jenjang_akademik/business/AppJenjangAkademikToPortal.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jenjang_akademik/business/AppJenjangAkademikToFinansi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'module/jenjang_akademik/business/AppJenjangAkademikToUm.class.php';

class ProcessJenjangAkademik{
	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 AppJenjangAkademikToAdmisi();
		$this->ObjSia = new AppJenjangAkademikToSia();
		$this->ObjPortal = new AppJenjangAkademikToPortal();
		$this->ObjFinansi = new AppJenjangAkademikToFinansi();
		$this->ObjUm = new AppJenjangAkademikToUm();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('jenjang_akademik', 'jenjangAkademik', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('jenjang_akademik', 'inputJenjangAkademik', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}
   function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataJenjang=$this->ObjAdmisi->GetDataJenjangAkademikById($this->_POST['jenjang_kode']);
			if((trim($this->_POST['jenjang_kode']) == "") or (trim($this->_POST['jenjang_kode_dikti']) == "") or (trim($this->_POST['jenjang_nama']) == "")or (trim($this->_POST['label_nim']) == ""))  {
				return "empty";
			}elseif((empty($this->decId)) and($dataJenjang[0]['jenjang_kode']===$this->_POST['jenjang_kode'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataJenjang[0]['jenjang_kode']!=$this->decId) and ($dataJenjang[0]['jenjang_kode']===$this->_POST['jenjang_kode'])){           
            return "already_use";           
         }else{
				return true;
			}
		}
		return false;
	}

   function Add(){
      $cek=$this->Check();
      if($cek===true){
         
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         $this->ObjFinansi->StartTrans();
         
         $addJenjangAkademikToAdmisi=$this->ObjAdmisi->DoAddJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama'],$this->_POST['label_nim']);
         $addJenjangAkademikToSia=$this->ObjSia->DoAddJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama']);
         $addJenjangAkademikToPortal=$this->ObjPortal->DoAddJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama']);
         $addJenjangAkademikToFinansi=$this->ObjFinansi->DoAddJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_nama']);
         
         $addData=$addJenjangAkademikToAdmisi && $addJenjangAkademikToSia && $addJenjangAkademikToPortal && $addJenjangAkademikToFinansi;
         If($this->useUm==true){
            $this->ObjUm->StartTrans();
            $addJenjangAkademikToUm = $this->ObjUm->DoAddJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_nama']);
            $addData = $addData && $addJenjangAkademikToUm; 
            $this->ObjUm->EndTrans($addData);
         }
         $this->ObjAdmisi->EndTrans($addData);
         $this->ObjSia->EndTrans($addData);
         $this->ObjPortal->EndTrans($addData);
         $this->ObjFinansi->EndTrans($addData);
         
         if($addData===true){
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="already_use"){
         Messenger::Instance()->Send('jenjang_akademik', 'inputJenjangAkademik', 'view', 'html', array($this->_POST,'Kode Jenjang Akademik  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('jenjang_akademik', 'inputJenjangAkademik', '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();
         $this->ObjFinansi->StartTrans();
         
         $updateJenjangAkademikToAdmisi=$this->ObjAdmisi->DoUpdateJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama'],$this->_POST['label_nim'],$this->decId);
         $updateJenjangAkademikToSia=$this->ObjSia->DoUpdateJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama'],$this->decId);
         $updateJenjangAkademikToPortal=$this->ObjPortal->DoUpdateJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_kode_dikti'],$this->_POST['jenjang_nama'],$this->decId);
         $updateJenjangAkademikToFinansi=$this->ObjFinansi->DoUpdateJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_nama'],$this->decId);
                  
         $updateData=$updateJenjangAkademikToAdmisi && $updateJenjangAkademikToSia &&  $updateJenjangAkademikToPortal && $updateJenjangAkademikToPortal && $updateJenjangAkademikToFinansi;
         if($this->useUm==true){
            $this->ObjUm->StartTrans();
            $updateJenjangAkademikToUm = $this->ObjUm->DoUpdateJenjangAkademik($this->_POST['jenjang_kode'],$this->_POST['jenjang_nama'],$this->decId);
            $updateData = $updateData && $updateJenjangAkademikToUm; 
            $this->ObjUm->EndTrans($updateData);
         }
         $this->ObjAdmisi->EndTrans($updateData);
         $this->ObjSia->EndTrans($updateData);
         $this->ObjPortal->EndTrans($updateData);
         $this->ObjFinansi->EndTrans($updateData);
         
         if($updateData===true){
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Perubahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="already_use"){
         Messenger::Instance()->Send('jenjang_akademik', 'inputJenjangAkademik', 'view', 'html', array($this->_POST,'Kode Jenjang Akademik  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek=="empty"){
         Messenger::Instance()->Send('jenjang_akademik', 'inputJenjangAkademik', '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)and ($this->ObjFinansi->addConn) and($this->ObjUm->addConn || $this->useUm==false)){
      
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         $this->ObjFinansi->StartTrans();

         $deleteDataByIdToAdmisi=$this->ObjAdmisi->DoDeleteJenjangAkademikById($arrId); 
         $deleteDataByIdToSia=$this->ObjSia->DoDeleteJenjangAkademikById($arrId); 
         $deleteDataByIdToPortal=$this->ObjPortal->DoDeleteJenjangAkademikById($arrId); 
         $deleteDataByIdToFinansi=$this->ObjFinansi->DoDeleteJenjangAkademikById($arrId); 
         
         $deleteDataById=$deleteDataByIdToAdmisi && $deleteDataByIdToSia && $deleteDataByIdToPortal && $deleteDataByIdToFinansi;
         
         if($this->useUm==true){
            $this->ObjUm->StartTrans();
            $deleteDataByIdToUm = $this->ObjUm->DoDeleteJenjangAkademikById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdToUm; 
            $this->ObjUm->EndTrans($deleteDataById);
         }
         
         $this->ObjAdmisi->EndTrans($deleteDataById);
         $this->ObjSia->EndTrans($deleteDataById);
         $this->ObjPortal->EndTrans($deleteDataById);
         $this->ObjFinansi->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->ObjAdmisi->StartTrans();
            $this->ObjSia->StartTrans();
            $this->ObjPortal->StartTrans();
            $this->ObjFinansi->StartTrans();
            
            $deleteArrDataToAdmisi = $this->ObjAdmisi->DoDeleteJenjangAkademikByArrayId($arr);
            $deleteArrDataToSia = $this->ObjSia->DoDeleteJenjangAkademikByArrayId($arr);
            $deleteArrDataToPortal = $this->ObjPortal->DoDeleteJenjangAkademikByArrayId($arr);
            $deleteArrDataToFinansi = $this->ObjFinansi->DoDeleteJenjangAkademikByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToAdmisi && $deleteArrDataToSia && $deleteArrDataToPortal && $deleteArrDataToFinansi;
        
            if($this->useUm==true){
               $this->ObjUm->StartTrans();
               $deleteArrDataToUm = $this->ObjUm->DoDeleteJenjangAkademikByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataToUm; 
               $this->ObjUm->EndTrans($deleteArrData);
            }
            
            $this->ObjAdmisi->EndTrans($deleteArrData);
            $this->ObjSia->EndTrans($deleteArrData);
            $this->ObjPortal->EndTrans($deleteArrData);
            $this->ObjFinansi->EndTrans($deleteArrData);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif(($deleteDataById===false) and ($deleteArrData === false)){
               Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssFail),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();   
               $this->ObjFinansi->StartTrans();   
               
               $deleteDataToAdmisi = $this->ObjAdmisi->DoDeleteJenjangAkademikById($arr[$i]);
               $deleteDataToSia = $this->ObjSia->DoDeleteJenjangAkademikById($arr[$i]);
               $deleteDataToPortal = $this->ObjPortal->DoDeleteJenjangAkademikById($arr[$i]);
               $deleteDataToFinansi = $this->ObjFinansi->DoDeleteJenjangAkademikById($arr[$i]);
               
               $deleteData=$deleteDataToAdmisi && $deleteDataToSia && $deleteDataToPortal && $deleteDataToFinansi;
               
               if($this->useUm==true){
                  $this->ObjUm->StartTrans();
                  $deleteDataToUm = $this->ObjUm->DoDeleteJenjangAkademikById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataToUm; 
                  $this->ObjUm->EndTrans($deleteData);
               }
               
               $this->ObjAdmisi->EndTrans($deleteData);
               $this->ObjSia->EndTrans($deleteData); 
               $this->ObjPortal->EndTrans($deleteData); 
               $this->ObjFinansi->EndTrans($deleteData); 
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif((!$this->ObjSia->addConn)and(!$this->ObjPortal->addConn)and(!$this->ObjFinansi->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', '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->ObjSia->addConn)and(!$this->ObjPortal->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', '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->ObjSia->addConn)and(!$this->ObjFinansi->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', '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->ObjPortal->addConn)and(!$this->ObjFinansi->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', '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->ObjSia->addConn)){       
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->ObjPortal->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Portal ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->ObjFinansi->addConn)){
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->ObjUm->addConn)&& ($this->useUm==true)){
      //print(aa);
         Messenger::Instance()->Send('jenjang_akademik', 'jenjangAkademik', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);   
      }
		return $this->pageView;
   }
}
?>
