<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_mahasiswa/business/AppStatusMahasiswa.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/status_mahasiswa/business/AppStatusMahasiswaToSia.class.php';
class ProcessStatusMahasiswa {

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

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

	function __construct() {
		$this->Obj = new AppStatusMahasiswa();
		$this->siaObj = new AppStatusMahasiswaToSia();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('status_mahasiswa', 'statusMahasiswa', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html');
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if((trim($this->_POST['status_mahasiswa_kode']) == "") or (trim($this->_POST['status_mahasiswa_nama']) == "")) {
				return "empty";
			} elseif(is_numeric($this->_POST['status_mahasiswa_kode'])) {
            return "not_string";
         }else {
				return true;
			}
		}
		return false;
	}

	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
			$addStatusMahasiswa = $this->Obj->DoAddStatusMahasiswa($this->_POST['status_mahasiswa_kode'],ucwords($this->_POST['status_mahasiswa_nama']));
			$addStatusMahasiswaToSia = $this->siaObj->DoAddStatusMahasiswaToSia($this->_POST['status_mahasiswa_kode'],ucwords($this->_POST['status_mahasiswa_nama']));
			$addData = $addStatusMahasiswa && $addStatusMahasiswaToSia;
			
			$this->Obj->EndTrans($addData);
			$this->siaObj->EndTrans($addData);
			if ($addData === true) {
				Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
            $dataStatusMahasiswa=$this->Obj->GetDataStatusMahasiswaById($this->_POST['status_mahasiswa_kode']);;
            if($dataStatusMahasiswa[0]['status_mahasiswa_kode']===$this->_POST['status_mahasiswa_kode']){
               Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html', array($this->_POST,'Kode Status Mahasiswa Sudah Digunakan'),Messenger::NextRequest);
               return $this->pageInput . "&dataId=" . $this->encId;
         }else{
				Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		 }
      } elseif($cek == "not_string") {
			Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html', array($this->_POST,'Status Mahasiswa Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', '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->siaObj->StartTrans();
         
			$updateStatusMahasiswa = $this->Obj->DoUpdateStatusMahasiswa($this->_POST['status_mahasiswa_kode'],ucwords($this->_POST['status_mahasiswa_nama']), $this->decId);
			$updateStatusMahasiswaSia = $this->siaObj->DoUpdateStatusMahasiswaSia($this->_POST['status_mahasiswa_kode'],ucwords($this->_POST['status_mahasiswa_nama']), $this->decId);
			$updateData = $updateStatusMahasiswa && $updateStatusMahasiswaSia;
			
			$this->Obj->EndTrans($updateData);
			$this->siaObj->EndTrans($updateData);
			if ($updateData === true) {
				Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
			   $dataStatusMahasiswa=$this->Obj->GetDataStatusMahasiswaById($this->_POST['status_mahasiswa_kode']);;
            if($dataStatusMahasiswa[0]['status_mahasiswa_kode']===$this->_POST['status_mahasiswa_kode']){
               Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html', array($this->_POST,'Kode Status Mahasiswa Sudah Digunakan'),Messenger::NextRequest);
               return $this->pageInput . "&dataId=" . $this->encId;
         }else{
				Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		  } 
      } elseif($cek == "not_string") {
			Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html', array($this->_POST,'Status Mahasiswa Harus Berupa Huruf'),Messenger::NextRequest);
			return $this->pageInput;
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('status_mahasiswa', 'inputStatusMahasiswa', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}
		return $this->pageView;
	}

	function Delete() {
      if(!$this->siaObj->addConn){
         Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssAlert),Messenger::NextRequest);
         return $this->pageView;
      }else{
   		$arrId = $this->_POST['idDelete'];
   		$this->Obj->StartTrans();
   		$this->siaObj->StartTrans();
   		
   		$deleteStatusMhsById = $this->Obj->DoDeleteStatusMahasiswaById($arrId);
   		$deleteStatusMhsSiaById = $this->siaObj->DoDeleteStatusMahasiswaSiaById($arrId);
   		$deleteDataById = $deleteStatusMhsById && $deleteStatusMhsSiaById;
   		
   		$this->Obj->EndTrans($deleteDataById);
   		$this->siaObj->EndTrans($deleteDataById);
   		if($deleteDataById==false) {
            if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
            $this->Obj->StartTrans();
            $this->siaObj->StartTrans();
            
            $deleteStatusMhsByArrayId = $this->Obj->DoDeleteStatusMahasiswaByArrayId($arr);
            $deleteStatusMhsSiaByArrayId = $this->siaObj->DoDeleteStatusMahasiswaSiaByArrayId($arr);
            $deleteArrData = $deleteStatusMhsByArrayId && $deleteStatusMhsSiaByArrayId;
            
            $this->Obj->EndTrans($deleteArrData);
   			$this->siaObj->EndTrans($deleteArrData);
   		}
   		if($deleteArrData === true) {
   			Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
   		} elseif($deleteDataById === true) {
   			Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', '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++) {
   			   $this->Obj->StartTrans();
   			   $this->siaObj->StartTrans();
   			   
   				$deleteData = false;
   				$deleteStatusMhs = $this->Obj->DoDeleteStatusMahasiswaById($arr[$i]);
   				$deleteStatusMhsSia = $this->siaObj->DoDeleteStatusMahasiswaSiaById($arr[$i]);
   				$deleteData = $deleteStatusMhs && $deleteStatusMhsSia;
   				
   				$this->Obj->EndTrans($deleteData);
   				$this->siaObj->EndTrans($deleteData);
   				if($deleteData === true) $sukses += 1;
   				else $gagal += 1;
   			}
   			Messenger::Instance()->Send('status_mahasiswa', 'statusMahasiswa', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
   		}
   		return $this->pageView;
   	}
   }
}
?>
