<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/jalur_masuk/business/AppJalurMasukToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/jalur_masuk/business/AppJalurMasukToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/jalur_masuk/business/AppJalurMasukToFinansi.class.php';
class ProcessJalurMasuk {

	var $_POST;
	var $ObjAdmisi;
	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->ObjAdmisi = new AppJalurMasukToAdmisi();
		$this->ObjSia = new AppJalurMasukToSia();
		$this->ObjFinansi = new AppJalurMasukToFinansi();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('jalur_masuk', 'jalurMasuk', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('jalur_masuk', 'inputJalurMasuk', 'view', 'html');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataJalurMasuk=$this->ObjAdmisi->GetDataJalurMasukById($this->_POST['kode_jalur']);
			if((trim($this->_POST['kode_jalur']) == "") or (trim($this->_POST['nama_jalur']) == "") ){
				return "empty";
			} elseif(is_numeric($this->_POST['kode_jalur'])) {
            return "not_string";
         }elseif((empty($this->decId)) and($dataJalurMasuk[0]['kode_jalur']===$this->_POST['kode_jalur'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataJalurMasuk[0]['kode_jalur']!=$this->decId) and ($dataJalurMasuk[0]['kode_jalur']===$this->_POST['kode_jalur'])){           
            return "already_use";           
         }else{
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjFinansi->StartTrans();
         
			$addJalurMasukToAdmisi = $this->ObjAdmisi->DoAddJalurMasuk(strtoupper($this->_POST['kode_jalur']),ucwords($this->_POST['nama_jalur']));
			$addJalurMasukToSia = $this->ObjSia->DoAddJalurMasuk(strtoupper($this->_POST['kode_jalur']),ucwords($this->_POST['nama_jalur']));
			$addJalurMasukToFinansi = $this->ObjFinansi->DoAddJalurMasuk(strtoupper($this->_POST['kode_jalur']), ucwords($this->_POST['nama_jalur']));
         
         $addData=$addJalurMasukToAdmisi && $addJalurMasukToSia && $addJalurMasukToFinansi;
         
         $this->ObjAdmisi->EndTrans($addData);
         $this->ObjSia->EndTrans($addData);
         $this->ObjFinansi->EndTrans($addData);
         
			if ($addData  === true) {
				Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);             
			}
		}elseif($cek=="already_use"){
         Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', 'view', 'html', array($this->_POST,'Kode Jalur Masuk  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "not_string") {
			Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', 'view', 'html', array($this->_POST,'Kode Jalur Masuk Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', '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->ObjFinansi->StartTrans();
         
			$updateJalurMasukToAdmisi = $this->ObjAdmisi->DoUpdateJalurMasuk(strtoupper($this->_POST['kode_jalur']),ucwords($this->_POST['nama_jalur']), $this->decId);
			$updateJalurMasukToSia = $this->ObjSia->DoUpdateJalurMasuk(strtoupper($this->_POST['kode_jalur']),ucwords($this->_POST['nama_jalur']), $this->decId);
			$updateJalurMasukToFinansi = $this->ObjFinansi->DoUpdateJalurMasuk(strtoupper($this->_POST['kode_jalur']), ucwords($this->_POST['nama_jalur']), $this->decId);
         
         $updateData=$updateJalurMasukToAdmisi && $updateJalurMasukToSia && $updateJalurMasukToFinansi;
         
         $this->ObjAdmisi->EndTrans($updateData);
         $this->ObjSia->EndTrans($updateData);
         $this->ObjFinansi->EndTrans($updateData);
         
         if ($updateData === true) {
				Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
             Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }                
		}elseif($cek=="already_use"){
         Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', 'view', 'html', array($this->_POST,'Kode Jalur Masuk  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
      }elseif($cek == "not_string") {
			Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', 'view', 'html', array($this->_POST,'Kode Jalur Masuk Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		}elseif($cek == "empty") {
			Messenger::Instance()->Send('jalur_masuk', 'inputJalurMasuk', '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->ObjFinansi->addConn)){
         
         $this->ObjAdmisi->StartTrans();
         $this->ObjSia->StartTrans();
         $this->ObjFinansi->StartTrans();
         
         $deleteDataByIdToAdmisi=$this->ObjAdmisi->DoDeleteJalurMasukById($arrId);
         $deleteDataByIdToSia=$this->ObjSia->DoDeleteJalurMasukById($arrId);
         $deleteDataByIdToFinansi=$this->ObjFinansi->DoDeleteJalurMasukById($arrId);
         
         $deleteDataById=$deleteDataByIdToAdmisi && $deleteDataByIdToSia && $deleteDataByIdToFinansi;
         
         $this->ObjAdmisi->EndTrans($deleteDataById);
         $this->ObjSia->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->ObjFinansi->StartTrans();
            
            $deleteArrDataToAbmisi = $this->ObjAdmisi-> DoDeleteJalurMasukByArrayId($arr);
            $deleteArrDataToSia = $this->ObjSia-> DoDeleteJalurMasukByArrayId($arr);
            $deleteArrDataToFinansi = $this->ObjAdmisi-> DoDeleteJalurMasukByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToAbmisi && $deleteArrDataToSia && $deleteArrDataToFinansi;
            
            $this->ObjSia->EndTrans($deleteArrData);
            $this->ObjAdmisi->EndTrans($deleteArrData);
            $this->ObjFinansi->EndTrans($deleteArrData);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', '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->ObjFinansi->StartTrans();
               
               $deleteDataToAdmisi = $this->ObjAdmisi->DoDeleteJalurMasukById($arr[$i]);
               $deleteDataToSia = $this->ObjSia->DoDeleteJalurMasukById($arr[$i]);
               $deleteDataToFinansi = $this->ObjFinansi->DoDeleteJalurMasukById($arr[$i]);
               
               $deleteData = $deleteDataToAdmisi && $deleteDataToSia && $deleteDataToFinansi;
               
               $this->ObjAdmisi->EndTrans($deleteData);
               $this->ObjSia->EndTrans($deleteData);
               $this->ObjFinansi->EndTrans($deleteData);
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif((!$this->ObjSia->addConn)and(!$this->ObjSia->addConn)){
          Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', '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->ObjSia->addConn){
          Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->ObjFinansi->addConn){
          Messenger::Instance()->Send('jalur_masuk', 'jalurMasuk', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Finansi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
