<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pendidikan/business/AppPendidikan.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pendidikan/business/AppPendidikanToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pendidikan/business/AppPendidikanToUm.class.php';
class ProcessPendidikan{
   
   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 AppPendidikan();
		$this->ObjToSia = new AppPendidikanToSia();
		$this->umObj = new AppPendidikanToUm();
      
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('pendidikan', 'pendidikan', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('pendidikan', 'inputPendidikan', 'view', 'html');
      $this->useUm=GTFWConfiguration::GetValue( 'application', 'use_gtum');
	}
	
	function Check() {
		if (isset($_POST['btnsimpan'])) {
			if(trim($this->_POST['nama_pendidikan']) == "")   {
				return "empty";
			} else{
				return true;
			}
		}
		return false;
	}
	
	function Add() {
		$cek = $this->Check();
		if($cek === true) {
         
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $maxId=$this->Obj->GetMaxIdPendidikan();
         if(empty($maxId))
            $maxId=0;
         $newId=$maxId+1;
         
			$addPendidikanToSireg = $this->Obj->DoAddPendidikan($newId,strtoupper($this->_POST['nama_pendidikan']));
			$addPendidikanToSia = $this->ObjToSia->DoAddPendidikan($newId,strtoupper($this->_POST['nama_pendidikan']));
         
         $addData=$addPendidikanToSireg && $addPendidikanToSia;
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $addPendidikanToUm = $this->umObj->DoAddPendidikan($newId,strtoupper($this->_POST['nama_pendidikan']));
            $addData = $addData && $addPendidikanToUm; 
            $this->umObj->EndTrans($addData);
         }
         $this->Obj->EndTrans($addData);
         $this->ObjToSia->EndTrans($addData);
         
			if ($addData === true) {
				Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('pendidikan', 'inputPendidikan', '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();
         
			$updatePendidikanToSireg = $this->Obj->DoUpdatePendidikan(strtoupper($this->_POST['nama_pendidikan']), $this->decId);
			$updatePendidikanToSia = $this->ObjToSia->DoUpdatePendidikan(strtoupper($this->_POST['nama_pendidikan']), $this->decId);
         
         $updateData=$updatePendidikanToSireg && $updatePendidikanToSia;
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $updatePendidikanToUm = $this->umObj->DoUpdatePendidikan(strtoupper($this->_POST['nama_pendidikan']), $this->decId);
            $updateData = $updateData && $updatePendidikanToUm; 
            $this->umObj->EndTrans($addData);
         }
         $this->Obj->EndTrans($updateData);
         $this->ObjToSia->EndTrans($updateData);
         
      if ($updateData === true) {
				Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('pendidikan', 'inputPendidikan', '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->umObj->addConn||$this->useUm==true)){
         
         $this->Obj->StartTrans();
         $this->ObjToSia->StartTrans();
         
         $deleteDataByIdToSireg=$this->Obj->DoDeletePendidikanById($arrId);
         $deleteDataByIdToSia=$this->ObjToSia->DoDeletePendidikanById($arrId);
         
         $deleteDataById=$deleteDataByIdToSireg && $deleteDataByIdToSia;
         If($this->useUm==true){
            $this->umObj->StartTrans();
            $deleteDataByIdToUm = $this->umObj->DoDeletePendidikanById($arrId);
            $deleteDataById = $deleteDataById && $deleteDataByIdToUm; 
            $this->umObj->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->DoDeletePendidikanByArrayId($arr);
            $deleteArrDataToSia = $this->ObjToSia->DoDeletePendidikanByArrayId($arr);
            
            $deleteArrData=$deleteArrDataToSireg && $deleteArrDataToSia;
            If($this->useUm==true){
               $this->umObj->StartTrans();
               $deleteArrDataToUm = $this->umObj->DoDeletePendidikanByArrayId($arr);
               $deleteArrData = $deleteArrData && $deleteArrDataToUm; 
               $this->umObj->EndTrans($deleteArrData);
            }
            
            $this->Obj->EndTrans($deleteArrData);
            $this->ObjToSia->EndTrans($deleteArrData);         
         }

        if($deleteArrData === true) {
            Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         }elseif($deleteDataById===true){
            Messenger::Instance()->Send('pendidikan', 'pendidikan', '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->ObjToSia->StartTrans();   
               
               $deleteData = false;
               $deleteDataToSireg = $this->Obj->DoDeletePendidikanById($arr[$i]);
               $deleteDataToSia = $this->ObjToSia->DoDeletePendidikanById($arr[$i]);
               
               $deleteData=$deleteDataToSireg && $deleteDataToSia;
               If($this->useUm==true){
                  $this->umObj->StartTrans();
                  $deleteDataToUm = $this->umObj->DoDeletePendidikanById($arr[$i]);
                  $deleteData = $deleteData && $deleteDataToUm; 
                  $this->umObj->EndTrans($deleteData);
               } 
               $this->Obj->EndTrans($deleteData);
               $this->ObjToSia->EndTrans($deleteData);  
               
               
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
      }elseif(!$this->ObjToSia->addConn){
         Messenger::Instance()->Send('pendidikan', 'pendidikan', '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('pendidikan', 'pendidikan', 'view', 'html', array($this->_POST, '<h4>Gagal Menghapus Data</h4>Gagal Menghubungi SI Admisi ', $this->cssFail),Messenger::NextRequest);
      }
		return $this->pageView;
	}
}
?>
