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

class ProcessJenisTes {
   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 AppJenisTes();
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('jenis_tes', 'jenisTes', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('jenis_tes', 'inputJenisTes', 'view', 'html');
   }

   function Check() {
      if (isset($_POST['btnsimpan'])) {
         if((trim($this->_POST['jenis_smta_nama']) == "")||(trim($this->_POST['jenis_smta_status']) == "")) {
            return "empty";
         } else {
            return true;
         }
      }
      return false;
   }

   function Add() {
      $cek = $this->Check();
      if($cek === true) {
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
         $addJenisSmta = $this->Obj->DoAddJenisSmta($this->_POST['jenis_smta_nama'],$this->_POST['jenis_smta_status']);
         $dataIdJenisSmta = $this->Obj->GetIdJenisSmta();

         $idJenis = $dataIdJenisSmta[0]['id'];

         $addJenisSmtaToSia = $this->siaObj->DoAddJenisSmtaToSia($idJenis, $this->_POST['jenis_smta_nama'],$this->_POST['jenis_smta_status']);
         
         $addData = $addJenisSmta && $dataIdJenisSmta && $addJenisSmtaToSia;
         
         $this->Obj->EndTrans($addData);
         $this->siaObj->EndTrans($addData);
         if ($addData === true) {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('jenis_tes', 'inputJenisTes', '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();
         
         $updateJenisSmta = $this->Obj->DoUpdateJenisSmta($this->_POST['jenis_smta_nama'],$this->_POST['jenis_smta_status'], $this->decId);
         $updateJenisSmtaSia = $this->siaObj->DoUpdateJenisSmtaSia($this->_POST['jenis_smta_nama'],$this->_POST['jenis_smta_status'], $this->decId);
         $updateData = $updateJenisSmta && $updateJenisSmtaSia;
         
         $this->Obj->EndTrans($updateData);
         $this->siaObj->EndTrans($updateData);
         if ($updateData === true) {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } else {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('jenis_tes', 'inputJenisTes', '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('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
         Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Gagal Menghubungi ke database SI Akademik', $this->cssFail),Messenger::NextRequest);
         return $this->pageView;
      }else{
         $arrId = $this->_POST['idDelete'];
         $this->Obj->StartTrans();
         $this->siaObj->StartTrans();
         
         $deleteJenisSmtaById = $this->Obj->DoDeleteJenisSmtaById($arrId);
         $deleteJenisSmtaSiaById = $this->siaObj->DoDeleteJenisSmtaSiaById($arrId);
         $deleteDataById = $deleteJenisSmtaById && $deleteJenisSmtaSiaById;
         
         $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();
            
            $deleteArrJenisSmta = $this->Obj->DoDeleteJenisSmtaByArrayId($arr);
            $deleteArrJenisSmtaSia = $this->siaObj->DoDeleteJenisSmtaSiaByArrayId($arr);
            $deleteArrData = $deleteArrJenisSmta && $deleteArrJenisSmtaSia;
            
            $this->Obj->EndTrans($deleteArrData);
            $this->siaObj->EndTrans($deleteArrData);
         }
         
         if($deleteArrData === true) {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
         } elseif($deleteDataById === true) {
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', '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;
               $deleteJenisSmta = $this->Obj->DoDeleteJenisSmtaById($arr[$i]);
               $deleteJenisSmtaSia = $this->siaObj->DoDeleteJenisSmtaSiaById($arr[$i]);
               $deleteData = $deleteJenisSmta && $deleteJenisSmtaSia;
               
               $this->Obj->EndTrans($deleteData);
               $this->siaObj->EndTrans($deleteData);
               if($deleteData === true) $sukses += 1;
               else $gagal += 1;
            }
            Messenger::Instance()->Send('jenis_tes', 'jenisTes', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
         }
         return $this->pageView;
      }
   }
}
?>