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

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

class ViewKondisiBarang extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/kondisi_barang/template');
      $this->SetTemplateFile('view_kondisi_barang.html');
   }
   
   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 ProcessRequest()
   {
      $Obj = new KondisiBarang;
      
      // inisialisasi messaging
		@$msg = Messenger::Instance()->Receive(__FILE__);
      @$this->Data = $msg[0][0];
		@$this->Pesan = $msg[0][1];
		@$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi data input
      if (isset($this->Data['nama'])) $return['input']['nama'] = $this->Data['nama'];
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetDataById($_GET['id']->Integer()->Raw());
         if ($result) $return['input'] = $result;
         else unset($_GET['id']);
      }
      else $return['input']['nama'] = '';
      // ---------
      
      // inisialisasi data filter
      if (isset($_POST['cari']))
         $return['cari'] = $_POST['cari']->Raw();
      elseif (isset($_GET['cari']))
         $return['cari'] = $_GET['cari']->Raw();
      else $return['cari'] = '';
      // ---------
      
      //inisialisasi paging
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page']))
      {
         $currPage = $_GET['page']->Integer()->Raw();
         if ($currPage > 0)
            $startRec =($currPage-1) * $itemViewed;
         else $currPage = 1;
      }
      
      $return['start'] = $startRec+1;
      $totalData = $Obj->GetCount($return['cari']);
      $url = Dispatcher::Instance()->GetUrl('kondisi_barang','kondisiBarang','view','html').
         '&cari='.$return['cari'];
      if (isset($_GET['id'])) $url .= '&id='.$_GET['id']->Integer()->Raw();
      
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      // ---------
      
      // Inisialisasi link URL
      $return['link']['url_action'] = Dispatcher::Instance()->GetUrl('kondisi_barang','inputKondisiBarang','do','html');
      if (isset($_GET['id'])) $return['link']['url_action'] .= '&id='.$_GET['id']->Integer()->Raw();
      
      $return['link']['url_search'] = Dispatcher::Instance()->GetUrl('kondisi_barang','kondisiBarang','view','html');
      $return['link']['url_edit'] = Dispatcher::Instance()->GetUrl('kondisi_barang','kondisiBarang','view','html');
      if ($return['cari'] != '') $return['link']['url_edit'] .= '&cari='.$return['cari'];
      if (isset($_GET['page'])) $return['link']['url_edit'] .= '&page='.$_GET['page']->Integer()->Raw();
      
      $return['link']['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
         "&urlDelete=".Dispatcher::Instance()->Encrypt('kondisi_barang|deleteKondisiBarang|do|html').
         "&urlReturn=".Dispatcher::Instance()->Encrypt('kondisi_barang|kondisiBarang|view|html').
         "&label=".Dispatcher::Instance()->Encrypt('Referensi Kondisi Barang');
      $return['link']['url_delete_js'] = Dispatcher::Instance()->GetUrl('kondisi_barang', 'deleteKondisiBarang', 'do', 'html');
      // ---------
      
      $return['dataSheet'] = $Obj->GetData($startRec,$itemViewed,$return['cari']);
      return $return;
   }
   
   function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
      
      // Message Box
      if($this->Pesan)
      {
         $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);
      }
      // ---------
      
      // Input Form
      $this->mrTemplate->AddVar('content', 'URL_ACTION', $data['link']['url_action']);
      $this->mrTemplate->AddVar('content', 'LABEL_ACTION', isset($_GET['id']) ? 'Ubah' : 'Tambah');
      $this->mrTemplate->AddVar('content', 'NAMA', $data['input']['nama']);
      // ---------
      
      // Filter Form
      $this->mrTemplate->AddVar('content', 'CARI', $data['cari']);
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', $data['link']['url_search']);
      // ---------
     
      // Data Sheet
      if (empty($data['dataSheet']))
      {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
         return NULL;
      }
      $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
      $i = $data['start'];
      $link = $data['link'];
      foreach ($data['dataSheet'] as $value)
      {
         $data = $value;
         $data['number'] = $i;
         $data['class_name'] = ($i % 2 == 0) ? '' : 'table-common-even';
         $data['url_edit'] = $link['url_edit'].'&id='.$data['id'];
         $data['url_delete'] = $link['url_delete'].
            "&id=".Dispatcher::Instance()->Encrypt($data['id']).
            "&dataName=".Dispatcher::Instance()->Encrypt($data['nama']);
         $data['url_delete_js'] = $link['url_delete_js'];
         
         $this->mrTemplate->AddVars('data_item', $data, '');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         $i++;
      }
   }
}
?>
