<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/smta/business/AppSmta.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/smta/business/AppSmtaToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/smta/business/AppSmtaToUm.class.php';
class ProcessSmta {

	var $_POST;
	var $ObjSmta;
	var $pageView;
	var $pageInput;
	//css hanya dipake di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;
	var $decId;
	var $encId;
	var $kodesmta;

	function __construct() {
		$this->ObjSmta = new AppSmta();
		$this->ObjSmtaToSia = new AppSmtaToSia();
		$this->umObj = new AppSmtaToUm();

		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('smta', 'smta', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('smta', 'inputSmta', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
         $dataSmta=$this->ObjSmta->GetDataSmtaById($this->_POST['smta_kode']);
			if ((empty($this->_POST['smta_kode'])) or (empty($this->_POST['smta_nama'])) or (empty($this->_POST['kota'])))  {
				return "empty";
			}elseif(!is_numeric($this->_POST['smta_kode'])) {
            return "not_number";
         }elseif((empty($this->decId)) and($dataSmta[0]['smta_kode']===$this->_POST['smta_kode'])){
            return "already_use";
         }elseif((!empty($this->decId)) and ($dataSmta[0]['smta_kode']!=$this->decId) and ($dataSmta[0]['smta_kode']===$this->_POST['smta_kode'])){           
            return "already_use";           
         }else{
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
      
         $this->ObjSmta->StartTrans();
         $this->ObjSmtaToSia->StartTrans();
         
			$addSmta = $this->ObjSmta->DoAddSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama']);
         
         $addSmtaToSia=$this->ObjSmtaToSia->DoAddSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama']);
         
         $addData=$addSmta && $addSmtaToSia;
         if($this->useUm==true){#print(aa);
            $this->umObj->StartTrans();
            $addSmtaToUm=$this->umObj->DoAddSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama']);
            $addData = $addData && $addSmtaToUm;
            #var_dump($addSmta);
            $this->umObj->EndTrans($addData);
         }
         $this->ObjSmta->EndTrans($addData);
         $this->ObjSmtaToSia->EndTrans($addData);
         
			if ($addData === true) {
				Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Penambahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);             
			}
         
		}elseif($cek=="already_use"){
      
         Messenger::Instance()->Send('smta', 'inputSmta', 'view', 'html', array($this->_POST,'Kode Smta  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
         
      }elseif($cek == "not_number") {
      
			Messenger::Instance()->Send('smta', 'inputSmta', 'view', 'html', array($this->_POST,'Kode Smta Harus Berupa Angka'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
         
		}elseif($cek == "empty") {
      
			Messenger::Instance()->Send('smta', 'inputSmta', '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->ObjSmta->StartTrans();
         $this->ObjSmtaToSia->StartTrans();
         
			$updateSmta = $this->ObjSmta->DoUpdateSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama'], $this->decId);
			$updateSmtaToSia = $this->ObjSmtaToSia->DoUpdateSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama'], $this->decId);
         
         $updateData=$updateSmta && $updateSmtaToSia;
         if($this->useUm){
            $this->umObj->StartTrans();
            $updateDataUm=$this->umObj->DoUpdateSmta($this->_POST['smta_kode'],$this->_POST['kota'],$this->_POST['smta_nama'],$this->decId);
            $updateData =$updateData && $updateDataUm;
            $this->umObj->EndTrans($updateData);
         }
         $this->ObjSmta->EndTrans($updateData);
         $this->ObjSmtaToSia->EndTrans($updateData);
         
         if ($updateData === true) {
				Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
         
		}elseif($cek=="already_use"){
      
         Messenger::Instance()->Send('smta', 'inputSmta', 'view', 'html', array($this->_POST,'Kode Smta  Sudah Digunakan'),Messenger::NextRequest);
         return $this->pageInput . "&dataId=" . $this->encId;
         
      }elseif($cek == "not_number") {
      
			Messenger::Instance()->Send('smta', 'inputSmta', 'view', 'html', array($this->_POST,'Kode Smta Harus Berupa Angka'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
         
		} elseif($cek == "empty") {
      
			Messenger::Instance()->Send('smta', 'inputSmta', '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->ObjSmtaToSia->addConn && ($this->umObj->addConn || $this->useUm==false)){
         
         $this->ObjSmta->StartTrans();
         $this->ObjSmtaToSia->StartTrans();
         
         $deleteDataByIdToSireg=$this->ObjSmta->DoDeleteSmtaById($arrId);
         
         $deleteDataByIdToSia=$this->ObjSmtaToSia->DoDeleteSmtaById($arrId);

         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         
         if($this->useUm){
            $this->umObj->StartTrans();
            $deleteDataByIdUm=$this->umObj->DoDeleteSmtaById($arrId);
            $deleteDataById =$deleteDataById && $deleteDataByIdUm;
            $this->umObj->EndTrans($deleteDataById);
         }
         $this->ObjSmta->EndTrans($deleteDataById);
         $this->ObjSmtaToSia->EndTrans($deleteDataById);
         
         if($deleteDataById==false){
            $this->ObjSmta->StartTrans();
            $this->ObjSmtaToSia->StartTrans();
            if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $deleteArrDataToSireg = $this->ObjSmta->DoDeleteSmtaByArrayId($arr);
            
            $deleteArrDataToSia= $this->ObjSmtaToSia->DoDeleteSmtaByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            if($this->useUm){
               $this->umObj->StartTrans();
               $deleteArrDataToUm=$this->umObj->DoDeleteSmtaByArrayId($arr);
               $deleteArrData =$deleteArrData && $deleteArrDataToUm;
               $this->umObj->EndTrans($deleteArrData);
            }
            $this->ObjSmtaToSia->EndTrans($deleteArrData);
            $this->ObjSmta->EndTrans($deleteArrData);
         }
         if($deleteArrData === true) {
            Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }else{

            
            for($i=0;$i<sizeof($arr);$i++) {
               $this->Obj->StartTrans();
               $this->ObjSmtaToSia->StartTrans();
               
               $deleteData = false;
               $deleteDataToSireg = $this->ObjSmta->DoDeleteSmtaById($arr[$i]);
               $deleteDataToSia = $this->ObjSmtaToSia->DoDeleteSmtaById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               if($this->useUm){
                  $this->umObj->StartTrans();
                  $deleteDataToUm=$this->umObj->DoDeleteSmtaById($arr[$i]);
                  $deleteData =$deleteData && $deleteDataToUm;
                  $this->umObj->EndTrans($deleteData);
               }
               $this->ObjSmtaToSia->EndTrans($deleteArrData);
               $this->ObjSmta->EndTrans($deleteArrData);
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;	
            }
            Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST,'Penghapusan Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjSmtaToSia->addConn){
         Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Akademik ', $this->cssFail),Messenger::NextRequest);
      }elseif(!$this->umObj->addConn){
         Messenger::Instance()->Send('smta', 'smta', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
