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

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

class ViewUsulanInput extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/usulan_barang/template');
      $this->SetTemplateFile('view_usulan_input.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Usulan;
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
        $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi default value
      $data = array();
      if (is_array($this->Data))
         $data = $this->Data;
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetUsulanById($_GET['id']->Integer()->Raw());
         if ($result === false) unset($_GET['id']);
         else
         {
         	$result['usulanBrgMstUnitIdNama'] = $result['usulanBrgMstUnitNama'];
         	$urlDownload = Dispatcher::Instance()->GetUrl('download', 'ReadFile', 'view', 'html');
            $result['usulanBrgMstFilePath'] = $urlDownload.'&file='.$result['usulanBrgMstFile'];
            $data = $result;
         }
      }
      
		if (!isset($data['usulanBrgMstPIC']))
      {
         $data['usulanBrgMstPIC'] = $_SESSION['username'];
      }
		
		if (!isset($data['usulanBrgMstNoUsulan']))
      {
         $date = date('Ymd.His');
         $data['usulanBrgMstNoUsulan'] = 'usul.'.$date;
      }
		
		if (!isset($data['tglUsulan1Display']) || !isset($data['usulanBrgMstTglUsulan']))
      {
			$datenow = date('Y-m-d+1');
			$data['usulanBrgMstTglUsulan'] = strtotime($datenow);
		}
		
      $data['json_detail_barang'] = json_encode((!empty($data['detail_barang'])) ? $data['detail_barang'] : new stdClass);
      unset($data['detail_barang']);
      $return['data'] = $data;
      // ---------
      
      // render Combo Box
      $comboPeriode = $Obj->GetDataPeriode();
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'periode', array('periode', $comboPeriode, $data['periode'], 'false', ''), Messenger::CurrentRequest);
      // ---------
      
      // inisialisasi link address
      $return['url']['action'] = Dispatcher::Instance()->GetUrl('usulan_barang', 'usulanInput', 'do', 'html');
      if (isset($_GET['id'])) $return['url']['action'] .= '&id='.$_GET['id']->Integer()->Raw();
      $return['url']['pop_up_kodefikasi'] = Dispatcher::Instance()->GetUrl('usulan_barang', 'popUpKodefikasi', '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)
   {
      if ($data == null) return null;
      
      // set Label
      $this->TemplateLabeling();
      $this->mrTemplate->AddVar('content', 'JUDUL', (isset($_GET['id']))?'Ubah':'Tambah');
      $this->mrTemplate->AddVar('content', 'URL_POP_UP_MAK', Dispatcher::Instance()->GetUrl('usulan_barang', 'rencanaPengeluaran', 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'URL_POPUP_UNIT', Dispatcher::Instance()->GetUrl('usulan_barang', 'popUpUnitPj', 'view', 'html'));
      // ---------
      
      // render message box
      if($this->Pesan)
      {
         $msg = '';
         if (count($this->Pesan) > 1) foreach ($this->Pesan as $value)
            $msg .= "\t$value<br/>\n";
         else $msg .= $this->Pesan[0];
         
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $msg);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
      // render link
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // render form default value
      $this->mrTemplate->AddVars('content', $data['data'], '');
      if (!empty($data['data']['usulanBrgMstFile']))
      {
         $this->mrTemplate->SetAttribute('file_lama', 'visibility', 'visible');
         $this->mrTemplate->AddVars('file_lama', $data['data'], '');
      }
      // ---------
      
   }
}
?>
