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

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

class ViewPopUpKodefikasi 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/inventarisasi/template');
      $this->SetTemplateFile('pop_up_kodefikasi.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Kodefikasi;
      
      // inisialisasi messaging
		@$msg = Messenger::Instance()->Receive(__FILE__);
		@$this->Pesan = $msg[0][1];
		@$this->css = $msg[0][2];
      // End
      
      if (isset($_POST['CARI_NAMA_BARANG']))
      {
         $CARI_KODE_BARANG = $_POST['CARI_KODE_BARANG']->Raw();
         $CARI_NAMA_BARANG = $_POST['CARI_NAMA_BARANG']->Raw();
      }
      elseif (isset($_GET['cari']))
         @list($CARI_KODE_BARANG, $CARI_NAMA_BARANG) = explode('|', $_GET['cari']->Raw());
      $return['cari'] = array
      (
         'CARI_KODE_BARANG' => $CARI_KODE_BARANG,
         'CARI_NAMA_BARANG' => $CARI_NAMA_BARANG
      );
      
      $cari = implode('|',$return['cari']);
      // End of inisialisasi filter
      
      // Render Combobox....
      $GolList = $Obj->GetComboGolongan();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'cari_golongan', array('CARI_GOLONGAN', $GolList, $CARI_GOLONGAN, true, 'id="POP_UP_GOLONGAN"'), Messenger::CurrentRequest);
      
      $JnsBarangList = $Obj->GetComboJnsBarang();
      @Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'cari_jenis_barang', array('CARI_JENIS_BARANG', $JnsBarangList, $CARI_JENIS_BARANG, true, ''), Messenger::CurrentRequest);
      // ---------
      
      // Inisialisasi link URL
      $return['url']['search'] = Dispatcher::Instance()->GetUrl('inventarisasi','popUpKodefikasi','view','html').'&kibId='.Dispatcher::Instance()->Encrypt($_GET['kibId']);
      $return['url']['pop_up_bidang'] = Dispatcher::Instance()->GetUrl('inventarisasi','popupBidang','view','html');
      $return['url']['pop_up_kelompok'] = Dispatcher::Instance()->GetUrl('inventarisasi','popupKelompok','view','html');
      $return['url']['pop_up_sub_kelompok'] = Dispatcher::Instance()->GetUrl('inventarisasi','popupSubKelompok','view','html');
      // ---------$_GET['kibId']
      
      // Inisialisasi komponen paging
      $totalData = $Obj->GetDataBarangCount($return['cari'], Dispatcher::Instance()->Decrypt($_GET['kibId']));
      $itemViewed = 10;
      $currPage = 1;
      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).'&kibId='.Dispatcher::Instance()->Encrypt($_GET['kibId']);;
		$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
      $barang = $Obj->GetDataBarang($return['cari'], Dispatcher::Instance()->Decrypt($_GET['kibId']), $startRec, $itemViewed);
      $return['barang'] = $barang;
      $kelompok = $Obj->GetKelompokList();
      $return['kelompok'] = array();
      $subKelompok = $Obj->GetSubKelompokList();
      $return['subKelompok'] = array();
      
      $isFirstPage = ($currPage == 1);
      $isLastPage = ($currPage == ceil($totalData/$itemViewed));
      $isEmpty = ($totalData == 0 OR count($barang) == 0);
      $isCari = ($cari == '|' OR $cari == '|');
      
      foreach ($kelompok as $idKelompok => $value)
      {
         if (!(isset($kelCache) OR isset($barang[$idKelompok]) OR $isEmpty OR $isFirstPage)) continue;
         $kelCache[$idKelompok] = $value;
         
         if (isset($barang[$idKelompok]))
         {
            if ($isCari) $return['kelompok'] += $kelCache;
            else $return['kelompok'] += array($idKelompok => $value);
            $kelCache = array();
         }
      }
      if (is_array($kelCache) AND count($kelCache) > 0 AND $isCari AND $isLastPage)
         $return['kelompok'] += $kelCache;
      
      foreach($return['kelompok'] as $idKelompok => $value)
      {
         if (!isset($subKelompok[$idKelompok])) continue;
         $return['subKelompok'][$idKelompok]  = array();
         foreach ($subKelompok[$idKelompok] as $idSubKelompok => $value)
         {
            if (!(isset($subKelCache) OR isset($barang[$idKelompok][$idSubKelompok]) OR $isFirstPage)) continue;
            $subKelCache[$idSubKelompok] = $value;
            
            if (isset($barang[$idKelompok][$idSubKelompok]))
            {
               if ($isCari) $return['subKelompok'][$idKelompok] += $subKelCache;
               else  $return['subKelompok'][$idKelompok] += array($idSubKelompok => $value);
               $subKelCache = array();
            }
         }
         
         if (is_array($subKelCache) AND count($subKelCache) > 0 AND $isCari AND $isLastPage)
         {
            $return['subKelompok'][$idKelompok] += $subKelCache;
            $subKelCache = array();
         }
      }
      // End of generate data
      $return['kibId'] = $_GET['kibId'];
      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 filter($string){
		return addslashes($string);
	}
	
	function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // Render filter box
      $this->mrTemplate->AddVars('content', $data['cari'], '');
      // ---------
      
      // Render tabel data
		if (count($data['kelompok']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
      foreach ($data['kelompok'] as $idKelompok=>$value)
      {
			// Kelompok
         if (!isset($data['barang'][$idKelompok]) OR count($data['barang'][$idKelompok]) === 0)
            continue;
         
         $send = array();			
			$send['number'] = '';
			$send['kode'] = $value['kode'];
			$send['nama'] = $this->filter($value['nama']);
			$send['class_name'] = 'table-common-even1';
         $this->mrTemplate->AddVars('data_item', $send, 'DATA_');
         
         $this->mrTemplate->parseTemplate('data_item', 'a');
         
         foreach ($data['subKelompok'][$idKelompok] as $idSubKelompok=>$value)
			{
				// Sub Kelompok
            if (!isset($data['barang'][$idKelompok][$idSubKelompok]) OR count($data['barang'][$idKelompok][$idSubKelompok]) === 0)
               continue;
   			
            $send = array();
				$send['number'] = '';
				$send['kode'] = $value['kode'];
				$send['nama'] = $this->filter($value['nama']);
				$send['class_name'] = 'table-common-even';
            $this->mrTemplate->AddVars('data_item', $send, 'DATA_');
            
            $this->mrTemplate->parseTemplate('data_item', 'a');
            
   			foreach ($data['barang'][$idKelompok][$idSubKelompok] as $value)
   			{
   				// Barang
               $send = $value;
			   $send['nilaiResidu'] = number_format($send['nilaiResidu'],2,',','.');
			   $send['hps'] = number_format($send['hps'],2,',','.');
   				$send['number'] = $i;
   				$send['class_name'] = '';
               $this->mrTemplate->AddVars('data_item', $send, 'DATA_');
               $this->mrTemplate->AddVars('icon_pilih', $send, 'DATA_');
               
               $this->mrTemplate->SetAttribute('icon_pilih', 'visibility', 'visible');
   				$this->mrTemplate->parseTemplate('data_item', 'a');
               $this->mrTemplate->SetAttribute('icon_pilih', 'visibility', 'hidden');
               $i++;
   			}
			}
      }
      // ---------
   }
}
?>
