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

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

class ViewGolonganbidang extends HtmlResponse {

	var $Pesan;

   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 TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/golongan_bidang/template');
		$this->SetTemplateFile('view_golongan_bidang.html');
	}
	
	function ProcessRequest() {
		$Obj = new AppGolonganbidang();
      
      // inisialisasi data filter
      if (count($_POST)>0)
      {
         $nama = $_POST['CARI_KODE']->Raw();
         $gol = $_POST['CARI_GOL']->Raw();
      }
      elseif (isset($_GET['cari']))
      {
         list($nama,$gol) = explode('|', Dispatcher::Instance()->Decrypt($_GET['cari']));
      }
      else
      {
         $nama = '';
         $gol = '';
      }
      
      $cari = Dispatcher::Instance()->Encrypt("$nama|$gol");
      
      // combo_box golongan
		$GolList = $Obj->GetComboGolongan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'cari_golongan', array('CARI_GOL', $GolList, $gol, true, ''), Messenger::CurrentRequest);
      
      // inisialisasi komponen paging
		$totalData = $Obj->GetCountData($gol, $nama);
		$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));

		Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
		
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
		
		$return['start'] = $startRec+1;
		$return['cari'] = $cari;
		
		// generate data structure
		$data = $Obj->getData($startRec, $itemViewed, $gol, $nama);
		foreach ($data as $value)
			$bidang[$value['gol_id']][] = $value;
		$return['bidang'] = $bidang;
		
      $GolList = $Obj->GetGolonganList();
      $golongan = array();
      foreach ($GolList as $value)
      {
         if (!(isset($GolCache) OR $currPage == 1 OR isset($bidang[$value['id']]))) continue;
         
         $GolCache[$value['id']] = $value;
         if (isset($bidang[$value['id']]))
         {
            $golongan += $GolCache;
            $GolCache = array();
         }
      }
      
      if (is_array($GolCache) AND count($GolCache) > 0 AND ($currPage == ceil($totalData/$itemViewed) OR count($bidang) == 0))
         $golongan += $GolCache;
      
      if (is_numeric($gol)) $golongan = isset($golongan[$gol]) ? array($gol=>$golongan[$gol]) : array();
		$return['golongan'] = $golongan;
		
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
      
      //set Label
      $this->TemplateLabeling();

		$cari = $data['cari'];
      list($nama,$gol) = explode('|', Dispatcher::Instance()->Decrypt($cari));
      
		$this->mrTemplate->AddVar('content', 'nama', $nama);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('golongan_bidang', 'golonganbidang', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD_GOLONGAN', Dispatcher::Instance()->GetUrl('golongan_bidang', 'inputGolongan', 'view', 'html')."&cari=$cari");
		$this->mrTemplate->AddVar('content', 'URL_ADD_BIDANG', Dispatcher::Instance()->GetUrl('golongan_bidang', 'inputBidang', 'view', 'html')."&cari=$cari");
		
      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);
		}
		
		$label = "Referensi Golongan Bidang";
		$urlDelete = Dispatcher::Instance()->GetUrl('golongan_bidang', 'delete', 'do', 'html')."&cari=$cari&page=".$_GET['page']->Raw();
		$urlReturn = Dispatcher::Instance()->GetUrl('golongan_bidang', 'golonganbidang', 'view', 'html')."&cari=$cari&page=".$_GET['page']->Raw();
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
		$this->mrTemplate->AddVar('content', 'URL_DELETE', Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html'));
		
		if (count($data['golongan']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
		
		// parse data
		$i = $data['start'];
      $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $i);
		$golongan = $data['golongan'];
		$bidang = $data['bidang'];
		
		foreach ($golongan as $key=>$value)
		{
			// golongan
			$send = array();
			$idEnc = Dispatcher::Instance()->Encrypt($key);
			
			$send['number'] = '';
			$send['jsid'] = "gol_id_$key";
			$send['id'] = "gol$key";
			$send['kode'] = sprintf('%04d', $value['kode']);
			$send['nama'] = $value['name'];
			$send['class_name'] = 'table-common-even';
			$send['url_edit'] = Dispatcher::Instance()->GetUrl('golongan_bidang', 'inputGolongan', 'view', 'html') . '&dataId=' . $idEnc . "&cari=$cari&page=".$_GET['page']->Integer()->Raw();
			if ($value['bidang'] === NULL)
				$this->mrTemplate->SetAttribute('check_box', 'visibility', 'visible');
			else $this->mrTemplate->SetAttribute('check_box', 'visibility', 'hidden');
			
			$this->mrTemplate->AddVars('check_box', $send, 'DATA_');
			$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
			$this->mrTemplate->parseTemplate('data_item', 'a');
			
			if (!is_array($bidang[$key]) OR count($bidang) === 0) continue;
			foreach ($bidang[$key] as $value)
			{
				// bidang
				$send = array();
				$idEnc = Dispatcher::Instance()->Encrypt($value['id']);
				
				$send['number'] = $i;
				$send['jsid'] = "bidang_id_$i";
				$send['id'] = $idEnc;
				$send['kode'] = sprintf('%04d', $value['kode']);
				$send['nama'] = $value['nama'];
				$send['class_name'] = '';
				$send['url_edit'] = Dispatcher::Instance()->GetUrl('golongan_bidang', 'inputBidang', 'view', 'html') . '&dataId=' . $idEnc . "&cari=$cari&page=".$_GET['page']->Integer()->Raw();
				$this->mrTemplate->SetAttribute('check_box', 'visibility', 'visible');
			
				$this->mrTemplate->AddVars('check_box', $send, 'DATA_');
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
				$this->mrTemplate->parseTemplate('data_item', 'a');
				$i++;
			}
		}
      $this->mrTemplate->AddVar('content', 'LAST_NUMBER', --$i);
	}
}
?>
