<?php
/**
* @package ViewUsulanBarang
* @copyright Copyright (c) PT Gamatechno Indonesia
* @author Dyan Galih <galih@gamatechno.com>
* @version 0.1
* @startDate 2010-01-21
* @lastUpdate 2010-01-21
* @description View Usulan Barang
*/

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pb_usulan/business/Usulan.class.php';

class ViewUsulanBarang extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/pb_usulan/template');
      $this->SetTemplateFile('view_usulan_barang.html');
   }

   function ProcessRequest() {
      $Obj = new Usulan();

      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi filter
      $filter = array();
      
      if (isset($_POST['btncari'])) $filter = $_POST->AsArray();
      elseif (isset($_GET['page']) && is_array($this->Data)) $filter = $this->Data;
      
		$dataGet = $_GET->AsArray();
		if($_POST->AsArray()==''){
			$filter['status'] = 'all';
			if($dataGet['status']!=''){
				$filter['status'] = $dataGet['status'];
			}
		} 
		
		Messenger::Instance()->Send('pb_usulan', 'usulanBarang', 'view', 'html', array($filter), Messenger::UntilFetched);
      $return['filter'] = $filter;
      // ---------
		
      // render Combo Box
		$comboStatus = $Obj->getDataStatus();
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status', array('status', $comboStatus, $filter['status'], true, ''), Messenger::CurrentRequest);
		//--------
		
      // Inisialisasi komponen paging
      $totalData = $Obj->getDataCountUsulanBarang($filter);
      $itemViewed = 20;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType). '&status=' . Dispatcher::Instance()->Encrypt($filter['status']);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      // ---------
      
      // Generate data structure

      if ($totalData > 0) $return['dataGrid'] = $Obj->getDataUsulanBarang($filter, $startRec, $itemViewed);
      else $return['dataGrid'] = array();
      // ---------
      
      // Inisialisasi komponen confirmDelete
		$return['link']['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
         "&urlDelete=".Dispatcher::Instance()->Encrypt('pb_usulan|deleteUsulanBarang|do|json').
         "&urlReturn=".Dispatcher::Instance()->Encrypt('pb_usulan|usulanBarang|view|html').
         "&label=".Dispatcher::Instance()->Encrypt('Usulan Barang');
      $return['link']['url_delete_js'] = Dispatcher::Instance()->GetUrl('pb_usulan', 'deleteUsulanBarang', 'do', 'json');
      // ---------
      return $return;
   }

   function ParseTemplate($data = NULL) {
      $urlAdd = Dispatcher::Instance()->GetUrl('pb_usulan', 'inputUsulanBarang', 'view', 'html');
     
		$urlCari = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', $urlCari);
      $this->mrTemplate->AddVar('content', 'URL_TAMBAH', $urlAdd);

      $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
      $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
      $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		
		if (count($data['dataGrid']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
		$link = $data['link'];
		foreach ($data['dataGrid'] as $value)
      {
			$value['no'] = $i;
			$value['class_name'] = ($i % 2) ? 'table-common-even' : '';
         if($value['status_id']!=1 && $value['status_id']!=6)$value['display'] = 'none';
			$urlEdit = Dispatcher::Instance()->GetUrl('pb_usulan', 'inputUsulanBarang', 'view', 'html').'&id='.$value['id'];
         $this->mrTemplate->AddVar('data_item', 'URL_EDIT', $urlEdit);
			$urlDetil = Dispatcher::Instance()->GetUrl('pb_usulan', 'detilUsulanBarang', 'view', 'html').'&id='.$value['id'];
         $this->mrTemplate->AddVar('data_item', 'URL_DETIL', $urlDetil);
			
			$value['url_delete'] = $link['url_delete'].
            "&id=".Dispatcher::Instance()->Encrypt($value['id']).
            "&dataName=".Dispatcher::Instance()->Encrypt($value['kode']);
         $value['url_delete_js'] = $link['url_delete_js'];
			
			$this->mrTemplate->AddVars('data_item', $value, '');
			$this->mrTemplate->parseTemplate('data_item', 'a');   
         $i++;
      }

   }
}
?>