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

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

class ViewListRuang extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/gedung/template');
      $this->SetTemplateFile('view_ruang.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Ruang;
      $gId = $Obj->gId;
      if ($gId === FALSE)
      {
         $urlRedirect = Dispatcher::Instance()->GetUrl('gedung','gedung','view','html');
         if (isset($_GET['ascomponent']) AND $_GET['ascomponent']->Raw())
            $urlRedirect .= '&ascomponent=1';
         $this->RedirectTo($urlRedirect);
         return null;
      }
     
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		$this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi filter
      $filter = array
      (
         'jnsRuangId' => 'all',
         'tipeRuangId' => 'all',
         'key' => '',
			'lantaiId' => $_GET['lantaiId']->integer()->Raw()
      );
		
      if (isset($_POST['btncari'])) $filter = $_POST->AsArray();
      elseif (isset($_GET['page']) && is_array($this->Data)) $filter = $this->Data;
      
		if(isset($_GET['lantaiId'])){
			$filter['lantaiId'] = $_GET['lantaiId']->integer()->Raw();
		}
		
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType, array($filter), Messenger::UntilFetched);
		$return['filter'] = $filter;
      // ---------
      
      // render combo box
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'jenis_ruang', array('jnsRuangId', $Obj->ComboJnsRuang(), $filter['jnsRuangId'], 'true', ''), Messenger::CurrentRequest);
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tipe_ruang', array('tipeRuangId', $Obj->ComboTipeRuang(), $filter['tipeRuangId'], 'true', ''), Messenger::CurrentRequest);
      // ---------

      // Inisialisasi link URL
      $return['link']['search'] = Dispatcher::Instance()->GetUrl('gedung', 'listRuang', 'view', 'html').'&lantaiId='.$filter['lantaiId'];
      
      if($gId <> '1'){
      	$return['link']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html');
      	$return['link']['add_ruang'] = Dispatcher::Instance()->GetUrl('gedung','inputRuang','view','html').'&lantaiId='.$filter['lantaiId'];
      }
      
      $return['link']['back'] = Dispatcher::Instance()->GetUrl('gedung','listLantai','view','html').'&gId='.$gId;
      $return['link']['konstruksi'] = Dispatcher::Instance()->GetUrl('gedung','konstruksiRuang','view','html').'&lantaiId='.$filter['lantaiId'];
      $return['link']['inventaris'] = Dispatcher::Instance()->GetUrl('gedung','InventarisRuang','view','html').'&lantaiId='.$filter['lantaiId'];
		$return['link']['add_inventaris'] = Dispatcher::Instance()->GetUrl('gedung','inputInventarisasi','view','html').'&lantaiId='.$filter['lantaiId'];
		$return['link']['penempatan_aset'] = Dispatcher::Instance()->GetUrl('gedung','popupPenempatanAset','view','html').'&lantaiId='.$filter['lantaiId'];
		$return['link']['pindah_ruang'] = Dispatcher::Instance()->GetUrl('gedung','inputPindahRuang','view','html').'&lantaiId='.$filter['lantaiId'];
      // ---------
      
      // Inisialisasi komponen confirmDelete
		$label = "Ruang";
		$urlDelete = Dispatcher::Instance()->GetUrl('gedung', 'deleteRuang', 'do', 'html').'&lantaiId='.$filter['lantaiId'];
		$urlReturn = Dispatcher::Instance()->GetUrl('gedung', 'listRuang', 'view', 'html').'&lantaiId='.$filter['lantaiId'];
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
      // ---------
      
      // Inisialisasi komponen paging
		#$totalData = $Obj->GetListCount($filter);
      $itemViewed = 10;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      $return['dataGrid'] = $Obj->GetList($filter, $startRec, $itemViewed);
      $totalData = $Obj->GetCount();
      $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;
      // ---------
      
      $return['dataGrd'] = $Obj->GetGedungLantai($filter['lantaiId']);
    
      /*if ($totalData > 0)
      {
         $return['dataGrid'] = $Obj->GetList($filter, $startRec, $itemViewed);
      }
      else $return['dataGrd'] = $Obj->GetGedungLantai($filter);*/
      // ---------
      
	  $return['idGedung']=$gId;
      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)
   {  
      $this->TemplateLabeling();
      // Render URL, filter, and other data
      $this->mrTemplate->AddVars('content', $data['link'], 'URL_');
      $this->mrTemplate->AddVars('content', $data['filter'], '');
      
      # hide action link for gedung default
      if($data['idGedung'] == '1') $this->mrTemplate->AddVar('content', 'DISPLAY_LINK', 'none');
      
      // ---------
	  $filter=$data['filter'];
	  $filter='&idGedung='.$data['idGedung'].'&jnsRuangId='.$filter['jnsRuangId'].'&tipeRuangId='.$filter['tipeRuangId'].'&key='.$filter['key'].'&lantaiId='.$filter['lantaiId'];
	  
	  $this->mrTemplate->AddVar('content', 'URL_CETAK_RUANG', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'cetakRuang', 'view', 'html').$filter);
      
	  //info gedung
	  empty($data['dataGrd']) ? $data['dataGrd']=$data['dataGrid'] : $data['dataGrd'];
	  
	  $this->mrTemplate->AddVar('content', 'KODE_GEDUNG',$data['dataGrd'][0]['gedungKode']);
	  $this->mrTemplate->AddVar('content', 'NAMA_GEDUNG',$data['dataGrd'][0]['gedungNama']);
	  $this->mrTemplate->AddVar('content', 'LANTAIKODE',$data['dataGrd'][0]['lantaiKode']);
	  $this->mrTemplate->AddVar('content', 'LANTAINAMA',$data['dataGrd'][0]['lantaiNama']);
	  
	  
      // 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['dataGrid']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
      foreach ($data['dataGrid'] as $value)
      {
		 $value['nomor'] = $i;
		 $value['class_name'] = ($i % 2) ? 'table-common-even' : '';
		 
		 # hide action link for gedung default
		 if($value['gedung_id'] <> '1'){
         	$value['url_edit'] = $data['link']['add_ruang']."&id=".$value['id'];
         	$value['url_konstruksi'] = $data['link']['konstruksi']."&id=".$value['id'];
         	$value['url_delete'] = $data['link']['delete'].
         	"&urlDelete=".Dispatcher::Instance()->Encrypt('gedung|deleteKelompok|do|html').
         	"&id=".Dispatcher::Instance()->Encrypt($idKelompok).
         	"&dataName=".Dispatcher::Instance()->Encrypt($value['nama']);
         	$value['url_pindah_ruang'] = $data['link']['pindah_ruang']."&id=".$value['id']."&gedungId=".$value['gedung_id'];
         	if ($value['foto'] != '' && is_readable(GTFW_APP_DIR.'/images/ruang/'.$value['foto']))
         		$value ['url_foto'] = Dispatcher::Instance()->GetUrl('gedung', 'fotoGedung', 'popup', 'html').'&id='.$value['id'];
         	else $value['url_foto'] = "javascript: alert('Foto ruang [".$value['nama']."] belum ada!');";
		 }else{
		 	$value['display_link'] = 'none';
		 	$value['disabled'] = 'disabled="disabled"';
		 }
		 
         $value['url_inventaris'] = $data['link']['inventaris']."&id=".$value['id'];
         $value['url_popup_aset'] = $data['link']['penempatan_aset']."&id=".$value['id']."&gedungId=".$value['gedung_id']."&kampusId=".$value['gedungKampusId'];
         $value['url_add_inventaris'] = $data['link']['add_inventaris']."&id=".$value['id']."&gedungId=".$value['gedung_id'];
		
		 if(empty($value['kapasitas']) or empty($value['luas'])){
		 $value['ratio']='';}else
		 $value['ratio']=$value['kapasitas']/$value['luas'];
		 
		 $value['ratio']= round($value['ratio'],2);
		 $value['kapasitas'] = number_format($value['kapasitas'],0,',','.');
		 $value['luas'] = number_format($value['luas'],2,',','.');
		 $value['dayaTampung'] = number_format($value['dayaTampung'],0,',','.');

         $this->mrTemplate->AddVars('data_item', $value, '');
			$this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>
