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

//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';

class ViewSumberBarang extends HtmlResponse {
     
   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/sumber_barang/template');
      $this->SetTemplateFile('view_sumber_barang.html');
   }
   
   function ProcessRequest() {

      $Obj = new SumberBarang();      

      $arrGet=$_GET->AsArray();
      $id=$arrGet['grp'];
      if(empty($id))
         $return['action']='add';
      $return['detail_data'] = $Obj->GetDataById($id);
	
		if(isset($_POST['data']))
         if(is_object($_POST['data']))	  
				$dt=$_POST['data']->AsArray();		 
		  else
			  $dt=$_POST['data'];
		
		$itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
		
		if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();  
         $startRec =($currPage-1) * $itemViewed;
      }
		
		$totalData = $Obj->GetCount($dt['search']);
		$dataList = $Obj->GetData($startRec,$itemViewed,$dt['search']);
		
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
               Dispatcher::Instance()->mSubModule, 
               Dispatcher::Instance()->mAction, 
               Dispatcher::Instance()->mType
               );
			   
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', 
         array($itemViewed,$totalData, $url, $currPage), 
         Messenger::CurrentRequest);
      
		$return['search'] =$dt['search'];
		$return['data'] = $dataList;
      $return['start'] = $startRec+1;	
		
		return $return;
   }
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content");
      
      for($i=0;$i<count($arrContent);$i++){
         for($j=0;$j<count($arrLabel);$j++){
            $this->mrTemplate->AddVar($arrContent[$i],$arrLabel[$j]['labelCode'],$arrLabel[$j]['labelName']);
         }
      }
   }
   
	function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
      
	  if($data['action']=='add') {
	     $label_action = 'Tambah';
		 $url_action = Dispatcher::Instance()->GetUrl('sumber_barang','addSumberBarang','do','html');
	  } else {
	     $label_action = 'Edit';
		 $url_action = Dispatcher::Instance()->GetUrl('sumber_barang','updateSumberBarang','do','html');
	  }
	  
     $urlSearch = Dispatcher::Instance()->GetUrl('sumber_barang','sumberBarang','view','html');
   	  
	  $this->mrTemplate->AddVar('content', 'URL_SEARCH', $urlSearch);
	  $this->mrTemplate->AddVar('content', 'URL_ACTION', $url_action);
      
	  
	  $this->mrTemplate->AddVar('content', 'SEARCH_NAMA', $data['search']['nama']);
	  $this->mrTemplate->AddVar('content', 'TAMBAH_ID', $data['detail_data']['id']);
	  $this->mrTemplate->AddVar('content', 'TAMBAH_NAMA', $data['detail_data']['nama']);
	  $this->mrTemplate->AddVar('content', 'TAMBAH_ACTION', $data['detail_data']['action']);
	  
	  $this->mrTemplate->AddVar('content', 'LABEL_ACTION', $label_action); 
	  
	  
	  if (isset ($data['msg'])) {	     
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['msg']['message']);
		 if($data['msg']['action']=='msg') 
		   $class='notebox-done';
		 else
		   $class = 'notebox-warning';
		 
		 $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['msg']['message']);
         $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $class);
		 
      } 	  	  
      if (empty($data['data'])) {
         $this->mrTemplate->AddVar('data_grid', 'IS_DATA_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('data_grid', 'IS_DATA_EMPTY', 'NO');
         $dataGrid = $data['data'];		 
		 $nomor= $data['start'] ;
		//data sudah siap ditampilkan... kirim ke pat-template
		for($j=0;$j < sizeof($dataGrid);$j++) {	
           $dataGrid[$j]['nomor'] = $nomor;
		   $nomor++;
		   if(($j % 2) != 0)
		     $dataGrid[$j]['class_name']='';
		   else
		     $dataGrid[$j]['class_name']='table-common-even';
			 
		   $idEnc= Dispatcher::Instance()->Encrypt($dataGrid[$j]['id']);
		   
		   //==================   start dipake componenet confirm delete =================================
	       $urlAccept = 'sumber_barang|deleteSumberBarang|do|html-cari-';
          $urlReturn = 'sumber_barang|sumberBarang|view|html-cari-';
	       $label = 'Sumber Barang';
	       $dataName = $dataGrid[$j]['nama'];				 
           $dataGrid[$j]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataName;               			   
	       //==================   end dipake componenet confirm delete =================================	   
		   
		   $dataGrid[$j]['url_edit'] = Dispatcher::Instance()->GetUrl('sumber_barang','sumberBarang','view','html').'&grp=' . $idEnc;
		   
		   $this->mrTemplate->AddVars('data_item', $dataGrid[$j], 'DATA_');
           $this->mrTemplate->parseTemplate('data_item', 'a');			   
		}
		
		
	}
	
   }

}
?>
