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

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

class ViewKodefikasi extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/kodefikasi_barang/template');
      $this->SetTemplateFile('main_page.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Kodefikasi;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // End
      
      // Inisialisasi filter
      $CARI_GOLONGAN = 'all';
      $CARI_BIDANG_ID = '';
      $CARI_BIDANG_NAME = '';
      $CARI_KELOMPOK_ID = '';
      $CARI_KELOMPOK_NAME = '';
      $CARI_SUB_KELOMPOK_ID = '';
      $CARI_SUB_KELOMPOK_NAME = '';
      $CARI_NAMA_BARANG = '';
      $CARI_JENIS_BARANG = 'all';
      
      if (isset($_POST['CARI_GOLONGAN']))
      {
         $tmp = $_POST->AsArray();
         $CARI_GOLONGAN = $tmp['CARI_GOLONGAN'];
         $CARI_BIDANG_ID = $tmp['CARI_BIDANG_ID'];
         $CARI_BIDANG_NAME = $tmp['CARI_BIDANG_NAME'];
         $CARI_KELOMPOK_ID = $tmp['CARI_KELOMPOK_ID'];
         $CARI_KELOMPOK_NAME = $tmp['CARI_KELOMPOK_NAME'];
         $CARI_SUB_KELOMPOK_ID = $tmp['CARI_SUB_KELOMPOK_ID'];
         $CARI_SUB_KELOMPOK_NAME = $tmp['CARI_SUB_KELOMPOK_NAME'];
         $CARI_NAMA_BARANG = $tmp['CARI_NAMA_BARANG'];
         $CARI_JENIS_BARANG = $tmp['CARI_JENIS_BARANG'];
      }
      elseif (isset($_GET['cari']))
         list($CARI_GOLONGAN, $CARI_BIDANG_ID, $CARI_BIDANG_NAME, $CARI_KELOMPOK_ID, $CARI_KELOMPOK_NAME, $CARI_SUB_KELOMPOK_ID, $CARI_SUB_KELOMPOK_NAME, $CARI_NAMA_BARANG, $CARI_JENIS_BARANG) = explode('|', Dispatcher::Instance()->Decrypt($_GET['cari']->Raw()));
      $return['cari'] = array
      (
         'CARI_GOLONGAN' => $CARI_GOLONGAN,
         'CARI_BIDANG_ID' => $CARI_BIDANG_ID,
         'CARI_BIDANG_NAME' => $CARI_BIDANG_NAME,
         'CARI_KELOMPOK_ID' => $CARI_KELOMPOK_ID,
         'CARI_KELOMPOK_NAME' => $CARI_KELOMPOK_NAME,
         'CARI_SUB_KELOMPOK_ID' => $CARI_SUB_KELOMPOK_ID,
         'CARI_SUB_KELOMPOK_NAME' => $CARI_SUB_KELOMPOK_NAME,
         'CARI_NAMA_BARANG' => $CARI_NAMA_BARANG,
         'CARI_JENIS_BARANG' => $CARI_JENIS_BARANG
      );
      $cari = Dispatcher::Instance()->Encrypt(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('kodefikasi_barang','kodefikasi','view','html');
      $return['url']['pop_up_bidang'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','popupBidang','view','html');
      $return['url']['pop_up_kelompok'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','popupKelompok','view','html');
      $return['url']['pop_up_sub_kelompok'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','popupSubKelompok','view','html');
      $return['url']['add_kelompok'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','inputKelompok','view','html');
      $return['url']['add_sub_kelompok'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','inputSubKelompok','view','html');
      $return['url']['add_barang'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','inputBarang','view','html');
      $return['url']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
         "&urlReturn=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|kodefikasi|view|html').
         "&label=".Dispatcher::Instance()->Encrypt('Kodefikasi Barang');
      $return['url']['detail_barang'] = Dispatcher::Instance()->GetUrl('kodefikasi_barang','detailBarang','view','html');
      // ---------
      
      // Inisialisasi komponen paging
      $totalData = $Obj->GetDataBarangCount($return['cari']);
      $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);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      // ---------
      
      // Generate data structure
      $barang = $Obj->GetDataBarang($return['cari'], $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 == 'all||||||||all' 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 $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 URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // 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);
		}
      // ---------
      
      // Render tabel data
		if (count($data['barang']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
		$barang = $data['barang'];
		$idKelompok="";
		$idSubKelompok="";
		
		for($j=0;$j<count($barang);$j++){
			if($idKelompok != $barang[$j]['idKelompok']){
				$idKelompok = $barang[$j]['idKelompok'];
				$send = array();			
				$send['number'] = '';
				$send['kode'] = $barang[$j]['kode'];
				$send['nama'] = $barang[$j]['kelNama'];
				
				$send['class_name'] = 'table-common-even1';
				$send['url_edit'] = $data['url']['add_kelompok']."&id=$idKelompok";
				$send['url_delete'] = $data['url']['delete'].
					"&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteKelompok|do|html').
					"&id=".Dispatcher::Instance()->Encrypt($idKelompok).
					"&dataName=".Dispatcher::Instance()->Encrypt($barang['kelNama']);
				if(empty($barang[$j]['idSubKelompok']))
					$this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
				else
					$this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'hidden');
				$this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'hidden');
				$this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
				$this->mrTemplate->parseTemplate('data_item', 'a');
			}
			
			
			if($idSubKelompok != $barang[$j]['idSubKelompok']){
				$idSubKelompok = $barang[$j]['idSubKelompok'];
				$send = array();			
				$send['number'] = '';
				$send['kode'] = $barang[$j]['subkode'];
				$send['nama'] = $barang[$j]['subKel'];
				
				$send['class_name'] = 'table-common-even';
				$send['url_edit'] = $data['url']['add_sub_kelompok']."&id=$idSubKelompok";
				$send['url_delete'] = $data['url']['delete'].
					"&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteSubKelompok|do|html').
					"&id=".Dispatcher::Instance()->Encrypt($idSubKelompok).
					"&dataName=".Dispatcher::Instance()->Encrypt($barang[$j]['subKel']);
				if(empty($barang[$j]['id']))
					$this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
				else
					$this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'hidden');

				$this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'hidden');
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
				$this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
				$this->mrTemplate->parseTemplate('data_item', 'a');
			}
			
			if(!empty($barang[$j]['id'])){
				$send = array();			
				$send['number'] = '';
				$send['kode'] = $barang[$j]['kd'];
				$send['nama'] = $barang[$j]['nama'];
				$send['hps'] = number_format($barang[$j]['hps'], 2, ',', '.');
				$send['url_edit'] = $data['url']['add_barang']."&id=".$barang[$j]['id'];
				$send['url_detail'] = $data['url']['detail_barang']."&id=".$barang[$j]['id'];
				$send['url_delete'] = $data['url']['delete'].
					"&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteBarang|do|html').
					"&id=".Dispatcher::Instance()->Encrypt($barang[$j]['id']).
					"&dataName=".Dispatcher::Instance()->Encrypt($barang[$j]['nama']);
					
				if(!empty($barang[$j]['foto_barang'])){
				$send['link_foto'] = "document/barang/".$barang[$j]['foto_barang'];
				$send['status_foto'] = "silakan klik icon untuk melihat foto";
				}
				else{
				$send['link_foto'] = "kosong";
				$send['status_foto'] = "tidak ada foto tersimpan";
				}
				
				
				$this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'visible');
				$this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
            $this->mrTemplate->AddVars('icon_detail', $send, 'DATA_');
            $this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
			
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
				$this->mrTemplate->parseTemplate('data_item', 'a');
			}
         $i++;
		}
		
      /*foreach ($data['kelompok'] as $idKelompok=>$value)
      {
			// Kelompok
         $send = array();			
			$send['number'] = '';
			$send['kode'] = $value['kode'];
			$send['nama'] = $value['nama'];
			$send['class_name'] = 'table-common-even1';
         $send['url_edit'] = $data['url']['add_kelompok']."&id=$idKelompok";
         $send['url_delete'] = $data['url']['delete'].
            "&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteKelompok|do|html').
            "&id=".Dispatcher::Instance()->Encrypt($idKelompok).
            "&dataName=".Dispatcher::Instance()->Encrypt($value['nama']);
         
         $this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'hidden');
			$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
         
			if (!isset($data['subKelompok'][$idKelompok]) OR count($data['subKelompok'][$idKelompok]) === 0)
         {
            $this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
            $this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
   			$this->mrTemplate->parseTemplate('data_item', 'a');
            continue;
			}
         
         $this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'hidden');
			$this->mrTemplate->parseTemplate('data_item', 'a');
         
         /*foreach ($data['subKelompok'][$idKelompok] as $idSubKelompok=>$value)
			{
				// Sub Kelompok
            $send = array();
				$send['number'] = '';
				$send['kode'] = $value['kode'];
				$send['nama'] = $value['nama'];
				$send['class_name'] = 'table-common-even';
            $send['url_edit'] = $data['url']['add_sub_kelompok']."&id=$idSubKelompok";
            $send['url_delete'] = $data['url']['delete'].
               "&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteSubKelompok|do|html').
               "&id=".Dispatcher::Instance()->Encrypt($idSubKelompok).
               "&dataName=".Dispatcher::Instance()->Encrypt($value['nama']);
				
            $this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'hidden');
				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
            
   			if (!isset($data['barang'][$idKelompok][$idSubKelompok]) OR count($data['barang'][$idKelompok][$idSubKelompok]) === 0)
            {
               $this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
               $this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
      			$this->mrTemplate->parseTemplate('data_item', 'a');
               continue;
   			}
            
            $this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'hidden');
   			$this->mrTemplate->parseTemplate('data_item', 'a');
            
   			foreach ($data['barang'][$idKelompok][$idSubKelompok] as $value)
   			{
   				// Barang
               $send = array();
   				$send['number'] = $i;
   				$send['kode'] = $value['kode'];
   				$send['nama'] = $value['nama'];
   				$send['class_name'] = '';
               $send['url_edit'] = $data['url']['add_barang']."&id=".$value['id'];
               $send['url_delete'] = $data['url']['delete'].
                  "&urlDelete=".Dispatcher::Instance()->Encrypt('kodefikasi_barang|deleteBarang|do|html').
                  "&id=".Dispatcher::Instance()->Encrypt($value['id']).
                  "&dataName=".Dispatcher::Instance()->Encrypt($value['nama']);
               $send['url_detail'] = $data['url']['detail_barang']."&id=".$value['id'];
   				
               $this->mrTemplate->SetAttribute('icon_detail', 'visibility', 'visible');
   				$this->mrTemplate->AddVars('icon_detail', $send, 'DATA_');
               $this->mrTemplate->SetAttribute('icon_delete', 'visibility', 'visible');
               $this->mrTemplate->AddVars('icon_delete', $send, 'DATA_');
   				$this->mrTemplate->AddVars('data_item', $send, 'DATA_');
   				$this->mrTemplate->parseTemplate('data_item', 'a');
               $i++;
   			}
			}
      }*/
      // ---------
   }
}
?>
