<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/'.Dispatcher::Instance()->mModule.'/business/PopupKelompok.class.php';

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

class ViewPopupKelompok extends HtmlResponse
{
   function TemplateBase()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');
      $this->SetTemplateFile('document-common-popup.html');
      $this->SetTemplateFile('layout-common-popup.html');
   }
   
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/'.Dispatcher::Instance()->mModule.'/template');
      $this->SetTemplateFile('popup_kelompok.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Popup;
      
      // Inisialisasi filter
      $CARI_BIDANG = '';
      $CARI_KELOMPOK = '';
      
      if (isset($_POST['CARI_BIDANG']))
      {
         $CARI_BIDANG = $_POST['CARI_BIDANG']->Raw();
         $CARI_KELOMPOK = $_POST['CARI_KELOMPOK']->Raw();
      }
      elseif (isset($_GET['cari']))
         list($CARI_BIDANG, $CARI_KELOMPOK) = explode('|', Dispatcher::Instance()->Decrypt($_GET['cari']->Raw()));
      elseif (isset($_GET['bidang']))
         $CARI_BIDANG = $_GET['bidang']->Raw();
      $return['cari'] = array
      (
         'CARI_BIDANG' => $CARI_BIDANG,
         'CARI_KELOMPOK' => $CARI_KELOMPOK
      );
      $cari = Dispatcher::Instance()->Encrypt(implode('|',$return['cari']));
      // End of inisialisasi filter
      
      // inisialisasi komponen paging
		$totalData = $Obj->GetCountData($CARI_BIDANG, $CARI_KELOMPOK);
		$itemViewed = 10;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = $_GET['page']->Integer()->Raw();
			if ($currPage < 1) $currPage = 1;
			$startRec = ($currPage-1) * $itemViewed;
		}
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&cari=' . Dispatcher::Instance()->Encrypt($cari));
		$dest = "popup-subcontent";
		Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage, $dest), Messenger::CurrentRequest);
		$return['start'] = $startRec+1;
      // ---------
		
		
		// generate data structure
		$data = $Obj->GetData($startRec, $itemViewed, $CARI_BIDANG, $CARI_KELOMPOK);
		if (empty($data))
         $return['kelompok'] = $return['bidang'] = array();
      else
      {
         foreach ($data as $value)
         {
   			$kelompok[$value['kodeBidang']][] = $value;
            $bidang[$value['kodeBidang']] = $value['namaBidang'];
   		}
         $return['kelompok'] = $kelompok;
         $return['bidang'] = $bidang;
      }
		// ---------
      
      // Inisialisasi link URL
      $return['url']['search'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule,'popupKelompok','view','html');
      $return['url']['pop_up_bidang'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule,'popupBidang','view','html');
      // ---------
      
      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 filter box
      $this->mrTemplate->AddVars('content', $data['cari'], '');
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      
		// Render data list
		if (count($data['bidang']) === 0)
			return $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
		$bidang = $data['bidang'];
		$kelompok = $data['kelompok'];
		
		foreach ($bidang as $key=>$value)
		{
			// parent
			$send = array();			
			$send['number'] = '';
			$send['kode'] = $key;
			$send['nama'] = $value;
			$send['class_name'] = 'table-common-even';
			
         $this->mrTemplate->SetAttribute('icon_pilih', 'visibility', 'hidden');
			$this->mrTemplate->AddVars('icon_pilih', $send, 'DATA_');
			$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
			$this->mrTemplate->parseTemplate('data_item', 'a');
			
			if (!is_array($kelompok[$key]) OR count($kelompok) === 0) continue;
         $this->mrTemplate->SetAttribute('icon_pilih', 'visibility', 'visible');
			foreach ($kelompok[$key] as $value)
			{
				// child
				$send = array();
				$send['number'] = $i;
				$send['kode'] = $value['kode'];
            $send['last_code'] = $value['last_code'];
				$send['id'] = $value['id'];
				$send['nama'] = $value['nama'];
				$send['class_name'] = '';
				
				$this->mrTemplate->AddVars('icon_pilih', $send, 'DATA_');
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
				$this->mrTemplate->parseTemplate('data_item', 'a');
				$i++;
			}
		}
   }
}
?>