<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/program_studi/business/AppProgramStudiToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/program_studi/business/AppProgramStudiToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/program_studi/business/AppProgramStudiToPortal.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/program_studi/business/AppProgramStudiToFinansi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/program_studi/business/AppProgramStudiToUm.class.php';

class ProcessProgramStudi{
   var $_POST;
   var $Obj;
   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->ObjAdmisi = new AppProgramStudiToAdmisi();
      $this->ObjSia = new AppProgramStudiToSia();
      $this->ObjPortal = new AppProgramStudiToPortal();
      $this->ObjFinansi = new AppProgramStudiToFinansi();
      $this->umObj = new AppProgramStudiToUm();
      
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('program_studi', 'programStudi', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('program_studi', 'inputProgramStudi', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
                 # print_r($this->_POST);exit;
   }

   function Check() {
      if (isset($_POST['btnsimpan'])) {
         $dataProgramStudi=$this->ObjAdmisi->GetDataProgramStudiById($this->_POST['kode_prodi']);
         if ((empty($this->_POST['kode_prodi'])) or (empty($this->_POST['kode_univ'])) or (empty($this->_POST['label_nim'])) or (empty($this->_POST['nama_prodi']))or (empty($this->_POST['jenjang']))or ($this->_POST['ekstakta']=="")or (empty($this->_POST['model']))or (empty($this->_POST['jurusan'])))  {
            return "empty";
         }elseif(!is_numeric($this->_POST['kode_prodi'])) {
            return "prodi_not_number";
         /*}elseif(!is_numeric($this->_POST['kode_univ'])) {
            return "univ_not_number";*/
         }elseif((empty($this->decId)) and($dataProgramStudi[0]['kode_prodi']===$this->_POST['kode_prodi'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataProgramStudi[0]['kode_prodi']!=$this->decId) and ($dataProgramStudi[0]['kode_prodi']===$this->_POST['kode_prodi'])){           
            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();
         
         $userId=$this->ObjAdmisi->GetUserId($_SESSION['username']);        
         $addProgramStudiAdmisi = $this->ObjAdmisi->DoAddProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$userId[0]['UserId'],$this->_POST['ekstakta'],$this->_POST['model'],$this->_POST['jurusan']);
         $kodeFak=$this->ObjAdmisi->GetKodeFakultas($this->_POST['kode_prodi']);
         $tunit = $this->ObjSia->GetTUnit();
         
         $addProgramStudiPortal = $this->ObjPortal->DoAddProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$this->_POST['model'],$this->_POST['jurusan'],$kodeFak[0]['fak_kode']);
         $addProgramStudiSia = $this->ObjSia->DoAddProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$this->_POST['ekstakta'],$this->_POST['model'],$this->_POST['jurusan'],$kodeFak[0]['fak_kode'],$tunit[0]['untId']);
       
                      
         $idJenjang= $this->ObjFinansi->GetIdJenjangAkademik($this->_POST['jenjang']);
         
         //ambil kode fakultas khusus finansi         
         $idFakFinansi = $this->ObjFinansi->GetFakultasId($kodeFak[0]['fak_kode']);         
         $addProgramStudiFinansi = $this->ObjFinansi->DoAddProgramStudi($this->_POST['kode_prodi'],$this->_POST['nama_prodi'],$idFakFinansi[0]['fak_id'],$idJenjang[0]['id_jenjang'],$this->_POST['model']);
         
         $addProgramStudi=$addProgramStudiAdmisi && $addProgramStudiSia && $addProgramStudiPortal && $addProgramStudiFinansi;
      //   var_dump($addProgramStudiAdmisi);exit;
         if($this->useUm==true){
            
//print($this->_POST['jenjang']);
            $this->umObj->StartTrans();
            $addProgramStudiUm=$this->umObj->DoAddProgramStudi($this->_POST['kode_prodi'],$this->_POST['kelompok'],$kodeFak[0]['fak_kode'],$this->_POST['nama_prodi'],$this->_POST['spma1'],$this->_POST['spma2'],$this->_POST['spma3'],$this->_POST['spma4'],$this->_POST['spmapbs'],$this->_POST['spmapbupd'],$this->_POST['jenjang']);
            $addProgramStudi = $addProgramStudi && $addProgramStudiUm;
            $this->umObj->EndTrans($addProgramStudi);
         }

         $this->ObjAdmisi->EndTrans($addProgramStudi);
         $this->ObjSia->EndTrans($addProgramStudi);
         $this->ObjPortal->EndTrans($addProgramStudi);
         $this->ObjFinansi->EndTrans($addProgramStudi);

         if ($addProgramStudi === true) {
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="already_use"){
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Program Studi Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "prodi_not_number") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Program Studi Harus Berupa Angka'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      /*}elseif($cek == "univ_not_number") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Universitas Harus Berupa Angka'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;*/
      }elseif($cek == "empty") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }
      return $this->pageView;
   }

   function Update() {
      $cek = $this->Check();
      if($cek === true) {
      
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         $this->ObjFinansi->StartTrans();
         
         $userId=$this->ObjAdmisi->GetUserId($_SESSION['username']);
         
         $kodeFak=$this->ObjAdmisi->GetKodeFakultas($this->_POST['kode_prodi']);
         
         $updateProgramStudiAdmisi = $this->ObjAdmisi->DoUpdateProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$userId[0]['UserId'],$this->_POST['ekstakta'],$this->_POST['model'],$this->_POST['jurusan'], $this->decId);
         
         $updateProgramStudiSia = $this->ObjSia->DoUpdateProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$this->_POST['ekstakta'],$this->_POST['model'],$this->_POST['jurusan'],$kodeFak[0]['fak_kode'], $this->decId);
         
         $updateProgramStudiPortal = $this->ObjPortal->DoUpdateProgramStudi($this->_POST['kode_prodi'],$this->_POST['kode_univ'],$this->_POST['label_nim'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$this->_POST['model'],$this->_POST['jurusan'],$kodeFak[0]['fak_kode'], $this->decId);
         
            
         $idJenjang= $this->ObjFinansi->GetIdJenjangAkademik($this->_POST['jenjang']);
         
         $updateProgramStudiFinansi = $this->ObjFinansi->DoUpdateProgramStudi($this->_POST['kode_prodi'],$this->_POST['nama_prodi'],$kodeFak[0]['fak_kode'],$idJenjang[0]['id_jenjang'],$this->_POST['model'],$this->decId);
      
         $updateProgramStudi=$updateProgramStudiAdmisi && $updateProgramStudiSia && $updateProgramStudiPortal && $updateProgramStudiFinansi;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $updateProgramStudiUm=$this->umObj->DoUpdateProgramStudi($this->_POST['kode_prodi'],$kodeFak[0]['fak_kode'],$this->_POST['nama_prodi'],$this->_POST['jenjang'],$this->decId);
            $updateProgramStudi = $updateProgramStudi && $updateProgramStudiUm;
            $this->umObj->EndTrans($updateProgramStudi);
         }
         /*var_dump($updateProgramStudiAdmisi);
         var_dump($updateProgramStudiSia);
         var_dump($updateProgramStudiPortal);
         var_dump($updateProgramStudiFinansi);
         exit;*/
         
         $this->ObjAdmisi->EndTrans($updateProgramStudi);
         $this->ObjSia->EndTrans($updateProgramStudi);
         $this->ObjPortal->EndTrans($updateProgramStudi);
         $this->ObjFinansi->EndTrans($updateProgramStudi);
         
         if ($updateProgramStudi === true) {
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif($cek=="already_use"){
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Program Studi Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "prodi_not_number") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Program Harus Berupa Angka'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      /*}elseif($cek == "univ_not_number") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', 'view', 'html', array($this->_POST,'Kode Program Studi Harus Berupa Angka'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;*/
      }elseif($cek == "empty") {
         Messenger::Instance()->Send('program_studi', 'inputProgramStudi', '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->umObj->addConn || $this->useUm==false))
      {
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjPortal->StartTrans();
         $this->ObjFinansi->StartTrans();
  
         $deleteDataByIdAdmisi=$this->ObjAdmisi->DoDeleteProgramStudiById($arrId);
         $deleteDataByIdSia=$this->ObjSia->DoDeleteProgramStudiById($arrId);
         $deleteDataByIdPortal=$this->ObjPortal->DoDeleteProgramStudiById($arrId);
         $deleteDataByIdFinansi=$this->ObjFinansi->DoDeleteProgramStudiById($arrId);
         
         $deleteDataById=$deleteDataByIdAdmisi && $deleteDataByIdSia && $deleteDataByIdPortal && $deleteDataByIdFinansi;
         if($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteDataByIdUm=$this->umObj->DoDeleteProgramStudiById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdUm;
            $this->umObj->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();
         
            $deleteArrDataAdmisi = $this->ObjAdmisi->DoDeleteProgramStudiByArrayId($arr);
            $deleteArrDataSia = $this->ObjSia->DoDeleteProgramStudiByArrayId($arr);
            $deleteArrDataPortal = $this->ObjPortal->DoDeleteProgramStudiByArrayId($arr);
            $deleteArrDataFinansi = $this->ObjFinansi->DoDeleteProgramStudiByArrayId($arr);
            
            $deleteArrData=$deleteArrDataAdmisi && $deleteArrDataSia && $deleteArrDataPortal && $deleteArrDataFinansi;
            if($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrDataUm=$this->umObj->DoDeleteProgramStudiByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataUm;
               $this->umObj->EndTrans($deleteArrData);
            }
            $this->ObjAdmisi->EndTrans($deleteArrData);
            $this->ObjSia->EndTrans($deleteArrData);
            $this->ObjPortal->EndTrans($deleteArrData);  
            $this->ObjFinansi->EndTrans($deleteArrData);  
            
         }      
         if($deleteArrData === true) {
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{
            for($i=0;$i<sizeof($arr);$i++) {
               $deleteData = false;
               
               $this->ObjAdmisi->StartTrans();
               $this->ObjSia->StartTrans();
               $this->ObjPortal->StartTrans();
               $this->ObjFinansi->StartTrans();
               
               $deleteDataAdmisi = $this->ObjAdmisi->DoDeleteProgramStudiById($arr[$i]);
               $deleteDataSia = $this->ObjSia->DoDeleteProgramStudiById($arr[$i]);
               $deleteDataPortal = $this->ObjPortal->DoDeleteProgramStudiById($arr[$i]);
               $deleteDataFinansi = $this->ObjFinansi->DoDeleteProgramStudiById($arr[$i]);

               $deleteData=$deleteDataAdmisi && $deleteDataSia && $deleteDataPortal && $deleteDataFinansi;
               if($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteDataUm=$this->umObj->DoDeleteProgramStudiById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataUm;
                  $this->umObj->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('program_studi', 'programStudi', 'view', 'html', array($this->_POST,'Penghapusan Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif((!$this->ObjSia->addConn)and(!$this->ObjPortal->addConn)and(!$this->ObjFinansi->addConn)){
         Messenger::Instance()->Send('program_studi', 'programStudi', '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('program_studi', 'programStudi', '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('program_studi', 'programStudi', '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('program_studi', 'programStudi', '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('program_studi', 'programStudi', '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('program_studi', 'programStudi', '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('program_studi', 'programStudi', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }elseif((!$this->umObj->addConn)){
         Messenger::Instance()->Send('program_studi', 'programStudi', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }
}
?>
