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

class ProcSumberBarang {
   
   protected $msg;
   protected $data;  
   
   public $moduleName = 'sumber_barang';
   public $moduleHome = 'sumberBarang';
   public $moduleInput = 'sumberBarang';
   public $moduleAdd = 'addSumberBarang';
   public $moduleUpdate = 'updateSumberBarang';
   public $moduleDelete = 'deleteSumberBarang';
   
   
   public $db;  //yang berhubungan ke database
   
    function ProcSumberBarang(){ //constructor
	  if(isset($_POST['data']))
         if(is_object($_POST['data']))	  
	        $this->data=$_POST['data']->AsArray();		 
		 else
		    $this->data=$_POST['data'];
								 
	  $this->db = new SumberBarang;	  	  
    }    
   
    function Add() {
	      //kalo yang diklik tombol simpan
		  if($this->validation('Penambahan')){
		    $add=$this->db->DoAdd($this->data['tambah']);	
            if($add) {
			   $this->msg='Penambahan data berhasil dilakukan';
			   $urlRedirect=$this->generateUrl('msg');			   
            } else {
			   $this->msg='Penambahan data gagal dilakukan <br />';
			   $this->msg.= $pesan;
			   $urlRedirect=$this->generateUrl('err');
			   //$ret = false;
            }			
		  } else {				
		    $urlRedirect = $this->generateUrl('err');			
		  }	
		  
	   
	   return $urlRedirect;
	}
	
	
	
	function Delete () {	    
		if(isset($_POST['idDelete'])) {
		   $grp=Dispatcher::Instance()->Decrypt($_POST['idDelete']);		   
		   		   
		   $del=$this->db->DoDelete($grp);
		   if($del) {
		     $this->msg='Penghapusan data berhasil dilakukan';
		     $urlRedirect = $this->generateUrl('msg');
		     
		   } else {
		     $this->msg='Penghapusan data gagal dilakukan';
		     $urlRedirect = $this->generateUrl('err');
		   }
		} else {
		   $this->msg='Penghapusan data gagal dilakukan';
		   $urlRedirect = $this->generateUrl('err');
		}
		return $urlRedirect;
	}
	
	
	
	
	function Update() {
		
		if($this->validation('Perubahan')) {	
		  $this->data['tambah']['id']= Dispatcher::Instance()->Decrypt($this->data['tambah']['id']);
			$update=$this->db->DoUpdate($this->data['tambah']);
			if($update) {
			  $this->msg='Perubahan data berhasil dilakukan'; 		   
				$urlRedirect = $this->generateUrl('msg');		  
			} else {
			  $this->msg='Perubahan data gagal dilakukan silahkan ulangi lagi'; 		   
				$urlRedirect = $this->generateUrl('err');		  
			}	   
		} else {		    		   
			$urlRedirect = $this->generateUrl('err');
		}		
	  
	   return $urlRedirect;
	}
	
	
	
	function validation($action) {
		
		if (isset($_POST['btnbatal']))
      {
         $this->msg = '';
         return false;
      }
	   
	    $this->msg='';
	    if(!isset($_POST['data'])) { //kalo gak ada data yang di POST apa yang mau di  validasi
		   $this->msg=$action.' data gagal dilakukan ';
		   return false;
		}		      
		  
	      if(trim($this->data['tambah']['nama'])=='')
		     $this->msg .= 'Nama Tidak Boleh Kosong <br />';		   
		
		if($this->msg=='')
   		   return true;
		else 
		   return false; 
	}
	
	
	
	
	function generateUrl($type,$isHome=false){		 
		 
	  if($type=='msg' || $isHome ) $submodule=$this->moduleHome;
	  else $submodule= $this->moduleInput;				
		
	  Messenger::Instance()->Send($this->moduleName, $submodule, 'view', 'html', array($this->data,$type,$this->msg),Messenger::NextRequest);				
	  $urlRedirect = Dispatcher::Instance()->GetUrl($this->moduleName, $submodule, 'view', 'html').$grp;
	  return $urlRedirect;
	}	
	
	function parsingUrl($file) {
	    $msg = Messenger::Instance()->Receive($file);	
		
		if(!empty($msg)) {		
		   $tmp['data']=$msg[0][0];
		   $tmp['msg']['action']=$msg[0][1];
		   $tmp['msg']['message']=$msg[0][2];
		   return $tmp;
		} else {
		  return false;
		}	    
	}
}
?>
