<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/tahap_penerimaan/business/AppTahapPenerimaan.class.php';

class ProcessJurusanSmta {

	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;

	function __construct() {
		$this->Obj = new AppJurusanSmta();
		$this->ObjToSia = new AppJurusanSmtaToSia();
		$this->ObjUm = new AppJurusanSmtaToUm();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('tahap_penerimaan', 'InputTahapPenerimaan', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {   
         $dataJurusan=$this->Obj->GetDataJurusanSmtaById($this->_POST['jurusan_kode']);
			if((trim($this->_POST['jurusan_nama']) == "") or (trim($this->_POST['jurusan_kode']) == ""))  {
				return "empty";
			} elseif(!is_numeric($this->_POST['jurusan_kode'])) {
            return "not_number";
         }elseif((empty($this->decId)) and($dataJurusan[0]['jurusan_kode']===$this->_POST['jurusan_kode'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataJurusan[0]['jurusan_kode']!=$this->decId) and ($dataJurusan[0]['jurusan_kode']===$this->_POST['jurusan_kode'])){           
            return "already_use";           
         }else{
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
			$addJurusan = $this->Obj->DoAddJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']));
			$addJurusanToSia = $this->ObjToSia->DoAddJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']));
         
         $addData=$addJurusan && $addJurusanToSia;
         If($this->useUm==true){
            $this->ObjUm->StartTrans();
            $addJurusanToUm = $this->ObjUm->DoAddJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']));
            $addData = $addData && $addJurusanToUm; 
            $this->ObjUm->EndTrans($addData);
         }
         $this->Obj->EndTrans($addData);
         $this->ObjToSia->EndTrans($addData);
         
         if ($addData === true) {
				Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
         
		}elseif($cek=="already_use"){
      
         Messenger::Instance()->Send('tahap_penerimaan', 'InputTahapPenerimaan', 'view', 'html', array($this->_POST,'Kode Jurusan Smta  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
         
      }elseif($cek == "not_number") {
      
			Messenger::Instance()->Send('tahap_penerimaan', 'InputTahapPenerimaan', 'view', 'html', array($this->_POST,'Kode Jurusan Harus Berupa Angka'),Messenger::NextRequest);
			return $this->pageInput;
         
		}elseif($cek == "empty") {
      
			Messenger::Instance()->Send('tahap_penerimaan', 'InputTahapPenerimaan', '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->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
			$updateJurusan = $this->Obj->DoUpdateJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']), $this->decId);
			
         $updateJurusanToSia = $this->ObjToSia->DoUpdateJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']), $this->decId);
         
         $updateData=$updateJurusan && $updateJurusanToSia;
         If($this->useUm==true){
            $this->ObjUm->StartTrans();
            $updateJurusanToUm = $this->ObjUm->DoUpdateJurusanSmta($this->_POST['jurusan_kode'],ucwords($this->_POST['jurusan_nama']), $this->decId);
            $updateData = $updateData && $updateJurusanToUm; 
            $this->ObjUm->EndTrans($updateData);
         }
         $this->Obj->EndTrans($updateData);
         $this->ObjToSia->EndTrans($updateData);
         
         if ($updateData === true) {
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest); 
			}           
         
      }elseif($cek=="already_use"){
      
         Messenger::Instance()->Send('tahap_penerimaan', 'InputTahapPenerimaan', 'view', 'html', array($this->_POST,'Kode Jurusan Smta  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
         
      } elseif($cek == "not_number") {
      
			Messenger::Instance()->Send('tahap_penerimaan', 'InputTahapPenerimaan', 'view', 'html', array($this->_POST,'Kode Jurusan Harus Berupa Angka'),Messenger::NextRequest);
			return $this->pageInput;
         
		} elseif($cek == "empty") {
      
			Messenger::Instance()->Send('jurusan_smta', 'InputTahapPenerimaan', '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->ObjToSia->addConn && ($this->ObjUm->addConn  || $this->useUm==false)){
         
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeleteJurusanSmtaById($arrId);
         
         $deleteDataByIdToSia=$this->ObjToSia->DoDeleteJurusanSmtaById($arrId);
         
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         If($this->useUm==true){
            $this->ObjUm->StartTrans();
            $deleteDataByIdToUm = $this->ObjUm->DoDeleteJurusanSmtaById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdToUm; 
            $this->ObjUm->EndTrans($deleteDataById);
         }
         $this->Obj->EndTrans($deleteDataById);
         $this->ObjToSia->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
            $this->Obj->StartTrans();
            $this->ObjToSia->StartTrans();
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $deleteArrDataToSireg = $this->Obj-> DoDeleteJurusanSmtaByArrayId($arr);
            
            $deleteArrDataToSia = $this->Obj-> DoDeleteJurusanSmtaByArrayId($arr);
            
            $deleteArrData= $deleteArrDataToSireg && $deleteArrDataToSia;
            If($this->useUm==true){
               $this->ObjUm->StartTrans();
               $deleteArrDataToUm = $this->ObjUm->DoDeleteJurusanSmtaByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataToUm; 
               $this->ObjUm->EndTrans($deleteArrData);
            }
            $this->ObjToSia->EndTrans($deleteArrData);
            $this->Obj->EndTrans($deleteArrData);
         }

         if($deleteArrData === true) {
         
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         
         }elseif($deleteDataById===true){
         
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', '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->Obj->StartTrans(); 
               $this->ObjToSia->StartTrans(); 
               
               $deleteDataToSireg = $this->Obj->DoDeleteJurusanSmtaById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeleteJurusanSmtaById($arr[$i]);
               
               $deleteData = $deleteDataToSireg && $deleteDataToSia;
               If($this->useUm==true){
                  $this->ObjUm->StartTrans();
                  $deleteDataToAdmin = $this->ObjUm->DoDeleteJurusanSmtaById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataToAdmin; 
                  $this->ObjUm->EndTrans($deleteData);
               }
               $this->Obj->EndTrans($deleteData);
               $this->ObjToSia->EndTrans($deleteData);
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;				
               

            }
            Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->ObjUm->addConn){
         Messenger::Instance()->Send('tahap_penerimaan', 'TahapPenerimaan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
