<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/sumber_informasi/business/AppSumberInformasi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'main/lib/gtfw/validasi/validation.class.php';

class ProcessSumberInformasi {
   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 AppSumberInformasi();		
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('sumber_informasi', 'sumberInformasi', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('sumber_informasi', 'inputSumberInformasi', 'view', 'html');      
	}
   
   function validasi(){
      $valObj = new ValidationData();
      $valObj->required($this->_POST['smbr_info_nama'],'Sumber Informasi');      
      $pesanErr = $valObj->validate();
      
      return $pesanErr;
   }
   
   function Add(){
      //cek apakah tombol batal yang di pencet
      if(isset($this->_POST['btnbalik'])){         
         return $this->pageView;
      }
      
      //validasi data
      $validasi = $this->validasi();      
      if($validasi != ""){         
         Messenger::Instance()->Send('sumber_informasi', 'inputSumberInformasi', 'view', 'html', array($validasi,$this->cssFail),Messenger::NextRequest);
			return $this->pageInput;
      }
      
      $this->Obj->StartTrans();            
      $addSumberInformasi = $this->Obj->AddSumberInformasi($this->_POST['smbr_info_nama']);
      $this->Obj->EndTrans($addSumberInformasi);
      
      if($addSumberInformasi === true){
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
      }
      
      return $this->pageView;
   }
   
   function Update(){
      //cek apakah tombol batal yang di pencet
      if(isset($this->_POST['btnbalik'])){         
         return $this->pageView;
      }
      
      //validasi data
      $validasi = $this->validasi();      
      if($validasi != ""){         
         Messenger::Instance()->Send('sumber_informasi', 'inputSumberInformasi', 'view', 'html', array($validasi,$this->cssFail),Messenger::NextRequest);
			return $this->pageInput."&dataId=".$this->encId;
      }
      
      $this->Obj->StartTrans();            
      $updateSumberInformasi = $this->Obj->updateSumberInformasi($this->_POST['smbr_info_nama'],$this->decId);
      $this->Obj->EndTrans($updateSumberInformasi);
      
      if($updateSumberInformasi === true){
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Edit Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Gagal Mengedit Data', $this->cssFail),Messenger::NextRequest);
      }
      
      return $this->pageView;
   }
   
   function Delete(){
      //delete 1 id
      $arrId = $this->_POST['idDelete'];      
      $this->Obj->StartTrans();
      $deleteSumberInformasiById = $this->Obj->deleteSumberInformasiById($arrId);
      $this->Obj->EndTrans($deleteSumberInformasiById);
      
      if($deleteSumberInformasiById == false){
         //delete banyak id dalam array
         
         if(is_array($arrId)==false)
            $arr[0]=$arrId;
         else
            $arr=$arrId;
         
         $this->Obj->StartTrans();
         $deleteArrSumberInformasi = $this->Obj->deleteArrSumberInformasi($arr);
         $this->Obj->EndTrans($deleteArrSumberInformasi);
      }
      
      if($deleteArrSumberInformasi === true) {
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      } elseif($deleteSumberInformasiById === true) {
         Messenger::Instance()->Send('sumber_informasi', 'sumberInformasi', 'view', 'html', array('Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
      }else{
         //proses delete gagal
         for($i=0;$i<sizeof($arr);$i++) {
            $this->Obj->StartTrans();
            
            $deleteData = false;
            $deleteSumberInformasiById = $this->Obj->deleteSumberInformasiById($arr[$i]);            
            $deleteData = $deleteSumberInformasiById;
            
            $this->Obj->EndTrans($deleteData);
            if($deleteData === true) $sukses += 1;
            else $gagal += 1;
         }
         Messenger::Instance()->Send('sumber_informasi','sumberInformasi', 'view', 'html', array($gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
      }      
      return $this->pageView;
   }      
}
?>
