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

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

require_once GTFWConfiguration::GetValue( 'application', 'docroot').   'module/'.Dispatcher::Instance()->mModule.'/response/ViewEncDec.html.class.php';

class ViewCetakKibQrcode extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/inventarisasi_detail/template');
		$this->SetTemplateFile('view_cetak_kib_qrcode.html');
	}
   
    function TemplateBase()
    {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application', 'docroot') . 'main/template/');      
   }
   
   function ProcessRequest()
   {
      $Obj = new AppInventarisasi();
      
		$result = explode('|', $_GET['id']->Raw());
		for($i=0;$i<sizeof($result);$i++){
			$return['data2'][] = $Obj->GetInventarisasiById($result[$i]);
		}
		if (!isset($_GET['id']) or $_GET['id'] == ''){
         $return['data2'] = array();
		}
      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 GetQrcode ($getData, $label, $lokasi, $unit, $id)
   {
		$PNG_TEMP_DIR = 'file/tmp_qrcode/'; 
		
		$Obj2 = new ViewEncDec;
		$url = $_SERVER['HTTP_HOST'].Dispatcher::Instance()->GetUrl('inventarisasi_detail', 'detailInventarisasi2', 'view', 'html').'&id='.$id;
		
		$getSize		= $_GET['size']->raw();
      $data 	= (isset($getData)) ? $getData : 'GTFW Qrcode Generator';

      $size 	= (isset($getSize)) ? $getSize : 5;
		/*
			Level L 	7% of codewords can be restored.
			Level M 	15% of codewords can be restored.
			Level Q 	25% of codewords can be restored.
			Level H 	30% of codewords can be restored.
		*/
		$level 	= 'H';
		if(GTFWConfiguration::GetValue( 'application', 'qr_code_encrypt')===true){
			$Enc = $Obj2->Encrypt($getData.'|'.$label.'|'.$id);//$Obj2->Encrypt($url);
		} else {
			$Enc = $getData.'|'.$label.'|'.$id;
		}
		$data2	= $Enc;
		/*.';'.$lokasi.';'.$unit*/;
		/* $Dec = $Obj2->Decrypt($data2);*/
		$filename = $PNG_TEMP_DIR.$data;
		$result = QRcode::png($data2, $filename, $level, $size, 2); 
		
      return $filename;
   }
	
   function ParseTemplate($data = NULL)
   {
      //$keys = implode('|', array_keys($data));
		
      //set Label
      $this->TemplateLabeling();

      if (empty($data['data2']))
      {
			$this->mrTemplate->SetAttribute('empty', 'visibility', 'visible');
         return;
      }
      else $this->mrTemplate->SetAttribute('kib', 'visibility', 'visible');
      
      foreach ($data['data2'] as $value)
      {
			
         //list($tmp['kode_a'], $tmp['kode_b'])  = explode('|', $value);
			$tmp['invKodeAset'] = $value['invKodeAset'];
			$tmp['invDetId'] = $value['invDetId'];
			$tmp['qrcode'] = $this->GetQrcode($tmp['invKodeAset'], $value['invMstLabel'],$value['lokasiAset'],$value['unitkerjaNama'], $value['invDetId']);
			
			$this->mrTemplate->AddVars('kib', $tmp, '');
         $this->mrTemplate->parseTemplate('kib', 'a');
      }
   }
   
}
?>