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

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

class ViewMitraKerja extends HtmlResponse {

	private $Pesan;

	function TemplateModule()
   {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/mitra_kerja/template');
		$this->SetTemplateFile('view_mitra.html');
	}
   
   function ProcessRequest()
   {
      $Obj = new MitraKerja;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		@$this->Pesan = $msg[0][1];
		@$this->css = $msg[0][2];
      
      // inisialisasi filter
      $mkNama = '';
      $mkAlamat = '';
      $mkCP = '';
      $mkBidUsaha = 'all';
      if (isset($_POST['mkNama']))
      {
         $mkNama = $_POST['mkNama']->Raw();
         $mkAlamat = $_POST['mkAlamat']->Raw();
         $mkCP = $_POST['mkCP']->Raw();
         $mkBidUsaha = $_POST['mkBidUsaha']->Raw();
      }
      elseif (isset($_GET['cari']))
         list($mkNama, $mkAlamat, $mkCP, $mkBidUsaha) = explode('|', Dispatcher::Instance()->Decrypt($_GET['cari']->Raw()));
      $return['cari'] = array
      (
         'mkNama' => $mkNama,
         'mkAlamat' => $mkAlamat,
         'mkCP' => $mkCP,
         'mkBidang' => $mkBidUsaha
      );
      $cari = Dispatcher::Instance()->Encrypt(implode('|',$return['cari']));
      
      // combo_box jenis bidang usaha
      $bidUsahaList = $Obj->GetBidangUsahaList();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'cari_bidusaha', array('mkBidUsaha', $bidUsahaList, $mkBidUsaha, true, ''), Messenger::CurrentRequest);
      
      //inisialisasi paging
		//$totalData = $Obj->GetCountMitraView($mkNama, $mkAlamat, $mkCP, $mkBidUsaha);
		$itemViewed = 10;
      $currPage = isset($_GET['page']) ? $_GET['page']->Integer()->Raw(): 1;
      if ($currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      // inisialisasi data
      $data = $Obj->GetMitraView($mkNama, $mkAlamat, $mkCP, $mkBidUsaha, $startRec, $itemViewed);
      $totalData = $Obj->GetCount();
      $return['data'] = $data;
      
      $url = Dispatcher::Instance()->GetUrl('mitra_kerja', 'mitraKerja', 'view', 'html')."&cari=$cari";
      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['startRec'] = $startRec + 1;
      
      
      
      // inisialisasi link address
      $return['link']['url_search'] = Dispatcher::Instance()->GetUrl('mitra_kerja', 'mitraKerja', 'view', 'html');
      $return['link']['url_detail'] = Dispatcher::Instance()->GetUrl('mitra_kerja', 'detailMitraKerja', 'view', 'html');
      $return['link']['url_add'] = Dispatcher::Instance()->GetUrl('mitra_kerja', 'inputMitraKerja', 'view', 'html');
      $return['link']['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
         "&urlDelete=".Dispatcher::Instance()->Encrypt('mitra_kerja|delete|do|html').
         "&urlReturn=".Dispatcher::Instance()->Encrypt('mitra_kerja|mitraKerja|view|html').
         "&label=".Dispatcher::Instance()->Encrypt('Mitra Kerja');
      
      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();
      
      // render link
      $link = $data['link'];
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', $link['url_search']);
      $this->mrTemplate->AddVar('content', 'URL_ADD', $link['url_add']);
      // ---------
      
      // 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['url_detail'] = $link['url_detail']."&mkId=".$value['mkId'];
         $data['url_edit'] = $link['url_add']."&mkId=".$value['mkId'];
         $data['url_hapus'] = $link['url_delete'].
            "&id=".Dispatcher::Instance()->Encrypt($data['mkId']).
            "&dataName=".Dispatcher::Instance()->Encrypt($data['mkNama']);
         
         $this->mrTemplate->AddVars('data_item', $data, 'DATA_');
         $this->mrTemplate->parseTemplate('data_item', 'a');
         $i++;
      }
      // ---------
   }
}
?>