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

class ProcessBank {
   
   protected $msg;
   protected $data;
   protected $moduleName='aset_ref_bank';
   
   var $Bank;
   
    function ProcessBank(){
	  if(isset($_POST['data']))
         if(is_object($_POST['data']))	  
	        $this->data=$_POST['data']->AsArray();		 
		 else
		    $this->data=$_POST['data'];		 
			
	  $this->Bank = new Bank;
	  	 
    }
   
    function Add () {	  
	   if(isset($_POST['btnsimpan'])){	      //kalo yang diklik tombol simpan
	      
		  if($this->validation('Penambahan')){
		  
		  //upload foto
			 $fileLogo = $_FILES['bank_logo'];
				if(!empty($fileLogo['name'])){
					$file =  $this->UploadFileLogo($fileLogo);
					$this->data['bank_logo']=$file;
				}
		    
		    $add=$this->Bank->DoAdd($this->data);	
			
            if($add) {
			   $this->msg='Penambahan data berhasil dilakukan';
			   $urlRedirect=$this->generateUrl('msg');
            } else {
			   if(mysql_errno() == 1062) 
			     $this->msg='Penambahan data gagal dilakukan karena duplikasi nama bank';
			   else			   
			     $this->msg='Penambahan data gagal dilakukan';
				 
			   $urlRedirect=$this->generateUrl('err');
            }			
			
		  } else {		    
						
		    $urlRedirect = $this->generateUrl('err');
		  }
		  
	   } else {
	      //kalo yang ditekan tombol balik
		  $urlRedirect = Dispatcher::Instance()->GetUrl($this->moduleName, 'bank', 'view', 'html') ;
	   }
	   return $urlRedirect;
	}
	
	function Delete () {
	    //if(isset($_GET['grp'])) {
		if(isset($_POST['idDelete'])) {
		   $grp=Dispatcher::Instance()->Decrypt($_POST['idDelete']);		   		   
		   
		   $del=$this->Bank->DoDelete($grp);
		   if($del) {
		     $this->msg='Penghapusan data berhasil dilakukan';
		     $urlRedirect = $this->generateUrl('msg',true);
		     
		   } else {
		     $this->msg='Penghapusan data gagal dilakukan';
		     $urlRedirect = $this->generateUrl('err',true);
		   }
		} else {
		   $this->msg='Penghapusan data gagal dilakukan';
		   $urlRedirect = $this->generateUrl('err',true);
		}
		return $urlRedirect;
	}
	
	function Update() {
	   
	   if(isset($_POST['btnsimpan'])){
         if($this->validation('Perubahan')) {
		 
		  //upload foto
			 $fileLogo = $_FILES['bank_logo'];
				if(!empty($fileLogo['name'])){
					$file =  $this->UploadFileLogo($fileLogo);
					$this->data['bank_logo']=$file;
				}else{
					$this->data['bank_logo'] = $this->data['bank_hidden_logo'];}
		   
		   $update=$this->Bank->DoUpdate($this->data);
			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');
		 }		  
	   } else {
	      //kalo yang ditekan tombol balik
		  $urlRedirect = Dispatcher::Instance()->GetUrl($this->moduleName, 'bank', 'view', 'html') ;
	   }
	   return $urlRedirect;
	}
	
	function validation($action) {	   
	   
	    $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['bank_nama']) == '')	      
	      $this->msg.='Nama Bank Tidak Boleh Kosong <br />';
		
		if(trim($this->data['bank_cabang']) == '')	      
	      $this->msg.='Nama Cabang Tidak Boleh Kosong <br />';    
		 
		if($this->msg=='')
   		   return true;
		else 
		   return false; 			   
	}
	
	
	function generateUrl($type,$isHome=false){
	    //parameter isHome ditujukan bahwa url diredirect ke home module apapun bentuk pesannya
	    if(isset($_GET['grp']))
	      $grp='&grp='.Dispatcher::Instance()->Encrypt($this->data['bank']['id']);
	    else
		  $grp='';
		
		if($type=='msg' || $isHome ) $submodule='bank';
		else $submodule='inputBank';	
		
		
		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');
		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;
		}	    
	}
	
	function UploadFileLogo($files){
			if 	($files!='') {
					$ext = end(explode(".", $files['name']));
					$file = date("Y-m-d_H-i-s_") . substr(md5(microtime()), 0, 3).'.'.$ext;
					@move_uploaded_file($files['tmp_name'], GTFWConfiguration::GetValue( 'application', 
				'docroot')."document/logobank/".$file);
			}
			else {$file='';}
			
			return $file;
	}
}
?>
