<?php
/*
 @ClassName : View Input Pengendalian Barang
 @Copyright : PT Gamatechno Indonesia
 @Analyzed By : Nanang Ruswianto <nanang@gamatechno.com>
 @Designed By : Rosyid <rosyid@gamatechno.com>
 @Author By : Dyan Galih <galih@gamatechno.com>
 @Version : 1.0
 @StartDate : Okt 15, 2008
 @LastUpdate : Okt 16, 2008
 @Description :
*/
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/pengendalian_barang/business/PegendalianBarang.class.php';

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

class ViewInputPengendalianBarang extends HtmlResponse
{
	
	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/pengendalian_barang/template');
      $this->SetTemplateFile('input_pengendalian_barang.html');
   }
   
   function ProcessRequest()
   {
   	$Obj = new PengendalianBarang();
   	
      // 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;
         $data['pengBrgTgl'] = strtotime($data['pengBrgTgl_year'].'-'.$data['pengBrgTgl_mon'].'-'.$data['pengBrgTgl_day']);
         foreach($data['barangPengendalian'] as $key=>$value){
             $data['barangPengendalian'][$key]['pengBrgKondisibrgId'] = $value['barangKondisiId'];
         }
      }
      elseif (isset($_GET['id']))
      {
         $result = $Obj->GetPengendalianById($_GET['id']->Integer()->Raw());
         if ($result === false) unset($_GET['id']);
         else $data = $result;
         $data['pengBrgTgl'] = strtotime($data['pengBrgTgl']);
      }
      else $data['pengBrgTgl'] = time();
      
      $data['pengBrgTgl_year'] = date('Y', $data['pengBrgTgl']);
      $data['pengBrgTgl_mon'] = date('m', $data['pengBrgTgl']);
      $data['pengBrgTgl_day'] = date('d', $data['pengBrgTgl']);
      
      $return['data'] = $data;
      $return['json']['barang_pengendalian'] = json_encode($data['barangPengendalian']);
      unset ($return['data']['barangPengendalian']);
      // ---------
      
   	// render Combo Box
      $statusBarang = $Obj->getStatusBarang();
      $statusKondisiBarang = $Obj->getStatusKondisiBarang();
      $statusPrioritas = $Obj->getPrioritas();
      $return['json']['combo_status'] = json_encode($statusBarang);
      $return['json']['combo_kondisi'] = json_encode($statusKondisiBarang);
      $return['json']['combo_prioritas'] = json_encode($statusPrioritas);
      
      if(isset($_GET['id'])){
      	$unitKerja = $Obj->getUnitKerja($data['pengBrgUnitId']);
      	$lokasiBarang = $Obj->getLokasiBarang($data['pengBrgUnitId'],$data['pengBrgLokasi']);
      }
      else{
      	$cbStat = 'false';
      	$unitKerja = $Obj->getUnitKerja();
      	$lokasiBarang = $Obj->getLokasiBarang($data['pengBrgUnitId']);
      }
   	  
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit_barang', array('pengBrgUnitId', $unitKerja, $data['pengBrgUnitId'], $cbStat, 'onchange="setComboLokasi(this.value);"'), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'lokasi_barang', array('pengBrgLokasi', $lokasiBarang, $data['pengBrgLokasi'], $cbStat, ''), Messenger::CurrentRequest);
		// ---------
      
      // render tanggal
      Messenger::Instance()->SendToComponent('tanggal', 'tanggal', 'view', 'html', 'pengBrgTgl', array($data['pengBrgTgl_year'].'-'.$data['pengBrgTgl_mon'].'-'.$data['pengBrgTgl_day'], 1970), Messenger::CurrentRequest);
      // ---------
   	
      // inisialisasi url
      $return['url']['action'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'inputPengendalianBarang', 'do', 'html');
      if (isset($_GET['id'])) $return['url']['action'] .= "&id=".$_GET['id'];
      $return['url']['pop_up_barang'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'inventarisasi', 'popup', 'html');
      // ---------
      
      return $return;
   }
   
	function ParseTemplate($data = NULL)
   {
      $this->TemplateLabeling();
      
      // 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 JSON data
      $this->mrTemplate->AddVars('content', $data['json'], 'JSON_');
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      // ---------
      
      // render default form value
      if (isset($_GET['id'])) {
      	$data['data']['mode'] = "Ubah";
      }
      else 
	  {$data['data']['mode'] = "Tambah";
	  $data['data']['pengBAPeng']='BAP'.date('mdY-hms');}
      
	  $this->mrTemplate->AddVars('content', $data['data'], '');
      // ---------
      
   }
}
?>
