<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/gelombang/business/AppGelombangToAdmisi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/gelombang/business/AppGelombangToFinansi.class.php';
class ProcessGelombang {

   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 AppGelombangToAdmisi();
      $this->ObjFinansi = new AppGelombangToFinansi();
      
      $this->_POST = $_POST->AsArray();
      $this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $this->encId = Dispatcher::Instance()->Encrypt($this->decId);
      $this->pageView = Dispatcher::Instance()->GetUrl('gelombang', 'gelombang', 'view', 'html');
      $this->pageInput = Dispatcher::Instance()->GetUrl('gelombang', 'inputGelombang', 'view', 'html');
   }

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

   function Add() {
      $cek = $this->Check();
      if($cek === true) {

         
         #$maxId=$this->ObjAdmisi->GetMaxIdGelombang();
         $checkIsExist=$this->ObjAdmisi->GetIsExist($this->_POST['gelombang_id']);
         
         if($checkIsExist==0){
            $this->ObjAdmisi->StartTrans();
            $this->ObjFinansi->StartTrans();
            
            $addGelombangAdmisi = $this->ObjAdmisi->DoAddGelombang($this->_POST['gelombang_id'],ucwords(strtolower($this->_POST['gelombang_nama'])));
            $addGelombangFinansi = $this->ObjFinansi->DoAddGelombang($this->_POST['gelombang_id'],ucwords(strtolower($this->_POST['gelombang_nama'])));
            
            $addGelombang=$addGelombangFinansi && $addGelombangAdmisi;
            
            $this->ObjAdmisi->EndTrans($addGelombang);
            $this->ObjFinansi->EndTrans($addGelombang);
            
            if ($addGelombang === true) {
               Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('gelombang', 'inputGelombang', 'view', 'html', array($this->_POST,'Kode Gelombang "'.$this->_POST['gelombang_kode'].'" Sudah Di Gunakan'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('gelombang', 'inputGelombang', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
         return $this->pageInput;
      }
      return $this->pageView;
   }

   function Update() {
      $cek = $this->Check();
      if($cek === true) {
      

         if($this->_POST['gelombang_id']!=$this->decId)
            $checkIsExist=$this->ObjAdmisi->GetIsExist($this->_POST['gelombang_id']);
         else
            $checkIsExist=0;
         if($checkIsExist==0){
         
            $this->ObjAdmisi->StartTrans();
            $this->ObjFinansi->StartTrans();
            $updateGelombangAdmisi = $this->ObjAdmisi->DoUpdateGelombang($this->_POST['gelombang_id'],ucwords(strtolower($this->_POST['gelombang_nama'])), $this->decId);
            $updateGelombangFinansi = $this->ObjFinansi->DoUpdateGelombang($this->_POST['gelombang_id'],ucwords(strtolower($this->_POST['gelombang_nama'])), $this->decId);
            
            $updateGelombang=$updateGelombangAdmisi && $updateGelombangFinansi;
            
            $this->ObjAdmisi->EndTrans($updateGelombang);
            $this->ObjFinansi->EndTrans($updateGelombang);
            
            if ($updateGelombang === true) {
               Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST,'Perubahan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
            } else {
               Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST,'Perubahan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
            }
         }else{
            Messenger::Instance()->Send('gelombang', 'inputGelombang', 'view', 'html', array($this->_POST,'Kode Gelombang "'.$this->_POST['gelombang_kode'].'" Sudah Di Gunakan'),Messenger::NextRequest);
            return $this->pageInput;
         }
      } elseif($cek == "empty") {
         Messenger::Instance()->Send('gelombang', 'inputGelombang', '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'];
      
      $this->ObjAdmisi->StartTrans();
      $this->ObjFinansi->StartTrans();
      
      $deleteDataByIdAdmisi = $this->ObjAdmisi->DoDeleteGelombangById($arrId);
      $deleteDataByIdFinansi = $this->ObjFinansi->DoDeleteGelombangById($arrId);
      
      $deleteDataById=$deleteDataByIdAdmisi && $deleteDataByIdFinansi;
      
      $this->ObjAdmisi->EndTrans($deleteDataById);
      $this->ObjFinansi->EndTrans($deleteDataById);
      
      if($deleteDataById=== false){  
   		   if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
         $this->ObjAdmisi->StartTrans();
         $this->ObjFinansi->StartTrans();
         
         $deleteArrDataAdmisi = $this->ObjAdmisi->DoDeleteGelombangByArrayId($arr);
         $deleteArrDataFinansi = $this->ObjFinansi->DoDeleteGelombangByArrayId($arr);
         
         $deleteArrData =$deleteArrDataAdmisi && $deleteArrDataFinansi;
         
         $this->ObjAdmisi->EndTrans($deleteArrData);
         $this->ObjFinansi->EndTrans($deleteArrData);
         
      }
      
      if($deleteArrData === true) {
         Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }elseif($deleteDataById===true){
            Messenger::Instance()->Send('gelombang', 'gelombang', '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->ObjFinansi->StartTrans();
            
            $deleteDataAdmisi = $this->ObjAdmisi->DoDeleteGelombangById($arr[$i]);
            $deleteDataFinansi = $this->ObjFinansi->DoDeleteGelombangById($arr[$i]);
            
            $deleteData=$deleteDataAdmisi && $deleteDataFinansi;
            
            $this->ObjAdmisi->EndTrans($deleteData);
            $this->ObjFinansi->EndTrans($deleteData);
            
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('gelombang', 'gelombang', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }
}
?>