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

class PopupMitra extends HtmlResponse {

	private $Pesan;

	function TemplateModule()
   {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/sewa_aset/template');
		$this->SetTemplateFile('popup_mitra.html');
	}
	
	function TemplateBase() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');
      $this->SetTemplateFile('document-common.html');
      $this->SetTemplateFile('layout-common-popup.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new SewaAset;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      
      // inisialisasi filter
      $mkNama = '';
      $mkAlamat = '';

      if (isset($_POST['mkNama']))
      {
         $mkNama = $_POST['mkNama']->Raw();
         $mkAlamat = $_POST['mkAlamat']->Raw();
      }
      elseif (isset($_GET['cari']))
         list($mkNama, $mkAlamat) = explode('|', Dispatcher::Instance()->Decrypt($_GET['cari']->Raw()));
      $return['cari'] = array
      (
         'mkNama' => $mkNama,
         'mkAlamat' => $mkAlamat
      );
      $cari = Dispatcher::Instance()->Encrypt(implode('|',$return['cari']));
      
      //inisialisasi paging
		#$totalData = $Obj->GetDataMitraCount($mkNama, $mkAlamat);
		$itemViewed = 10;
      $currPage = isset($_GET['page']) ? $_GET['page']->Integer()->Raw(): 1;
      if ($currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      // inisialisasi data
      $data = $Obj->GetDataMitra($mkNama, $mkAlamat, $startRec, $itemViewed);
      $return['data'] = $data;
      $totalData = $Obj->GetCount();
	  $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&cari=' . Dispatcher::Instance()->Encrypt($cari));
	  Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, array($currPage, $filter['keyword']), 'popup-subcontent'), Messenger::CurrentRequest);
	  
	  $return['startRec'] = $startRec + 1;
      
      // inisialisasi link address
      $return['link']['url_search'] = Dispatcher::Instance()->GetUrl('sewa_aset', 'mitra', 'popup', 'html');
      return $return;
   }
   
	function ParseTemplate($data = NULL)
   {      
      // render link
      $link = $data['link'];
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', $link['url_search']);
      // ---------
      
      // Render filter box
      $this->mrTemplate->AddVars('content', $data['cari'], '');
      // ---------
      
      // render 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);
		}
      // ---------
      
      if (!is_array($data['data']) OR count($data['data']) == 0)
         return $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
      // render data sheet
      $i = $data['startRec'];
      foreach ($data['data'] as $value)
      {
         $data = $value;
         $data['number'] = $i;
         $data['class_name'] = ($i % 2 == 0) ? '' : 'table-common-even';
			$data['mkNama'] = addslashes($data['mkNama']);
         $this->mrTemplate->AddVars('data_item', $data, 'DATA_');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         $i++;
      }
      // ---------
   }
}
?>