<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/gedung/business/AppGedung.class.php';
//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
'module/label/response/Label.proc.class.php';
class ViewGedung extends HtmlResponse {

   #var $Pesan;

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/gedung/template');
      $this->SetTemplateFile('view_gedung.html');
   }
   
   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 ProcessRequest() {
      $Obj = new AppGedung();
      $post = $_POST->AsArray();
		$User = $Obj->GetDataUserByUsername($_SESSION['username']);
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
		$notebox['msg'] = $msg[0][1];
		$notebox['css'] = $msg[0][2];
      $return['notebox'] = $notebox;
      // ---------
		
      if(!empty($post['key']))
         $key = $post['key'];
      else
         $key = '';
      if(!empty($post['tahun']))
         $tahun = $post['tahun'];
      else
         $tahun = date("Y");
         
      if(isset($_GET['cari'])) {
         $get_data = $_GET->AsArray();
         $tahun = $get_data['tahun'];
         $key = $get_data['key'];
      }
		//print_r($User);
      /*if ($User['role_id'] != 1)
      {
         $unit = $Obj->GetDataUnitByUnitname($post['unitkerjaId']);
         if ($User['unitkerjaId'] != $unit['unitkerjaId'] AND $User['unitkerjaId'] != $unit['unitkerjaParentId'])
            $post['unitkerjaId'] = $User['unitkerjaNama'];
      }*/
		
      //combo
      $cbxsumberdana = $Obj->GetCbxSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sbr_dana', array('sbr_dana', $cbxsumberdana, $post['sbr_dana'], true, ''), Messenger::CurrentRequest);
      
      $cbxkepemilikan = $Obj->GetCbxKepemilikan();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kepemilikan', array('kepemilikan', $cbxkepemilikan, $post['kepemilikan'], true, ''), Messenger::CurrentRequest);
      
      $cbxlokasi = $Obj->GetCbxLokasiKampus();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'lokasi', array('lokasi', $cbxlokasi, $post['lokasi'], true, ''), Messenger::CurrentRequest);
      
      $cbxkondisi = $Obj->GetCbxKondisi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'kondisi', array('kondisi', $cbxkondisi, $post['kondisi'], true, ''), Messenger::CurrentRequest);
      
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();  
         $startRec =($currPage-1) * $itemViewed;
      }
      
      #$totalData = $Obj->GetCount($post['tahun'], $post['sbr_dana'], $post['kepemilikan'], $post['lokasi'], $post['kondisi'], $post['unitkerjaId'], $key);
      $data_gedung = $Obj->GetDataGedung($post['tahun'], $post['sbr_dana'], $post['kepemilikan'], $post['lokasi'], $post['kondisi'], $post['unitkerjaId'], $key, $startRec,$itemViewed);
      $totalData = $Obj->GetCount();
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . 
      '&key=' . Dispatcher::Instance()->Encrypt($key) . 
      '&cari=' . Dispatcher::Instance()->Encrypt(1));
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      
      
      $return['gedung'] = $data_gedung;
      $return['start'] = $startRec+1;
      $return['key'] = $key;
      $return['cbx_thn'] = $post['tahun'];
		
      // filter saat ini
      $cbxsumberdana = $this->ComboBoxToArray($cbxsumberdana);
      $cbxkepemilikan = $this->ComboBoxToArray($cbxkepemilikan);
      $cbxlokasi = $this->ComboBoxToArray($cbxlokasi);
      $cbxkondisi = $this->ComboBoxToArray($cbxkondisi);
		
      $return['filter']['thn'] = (!isset($post['tahun']) OR $post['tahun'] == 'all') ? 'Semua' : $post['tahun'];
      $return['filter']['sbr_dana'] = (!isset($post['sbr_dana']) OR $post['sbr_dana'] == 'all') ? 'Semua' : $cbxsumberdana[$post['sbr_dana']];
      $return['filter']['kepemilikan'] = (!isset($post['kepemilikan']) OR $post['kepemilikan'] == 'all') ? 'Semua' : $cbxkepemilikan[$post['kepemilikan']];
      $return['filter']['lokasi'] = (!isset($post['lokasi']) OR $post['lokasi'] == 'all') ? 'Semua' : $cbxlokasi[$post['lokasi']];
      $return['filter']['kondisi'] = (!isset($post['kondisi']) OR $post['kondisi'] == 'all') ? 'Semua' : $cbxkondisi[$post['kondisi']];
		$return['filter']['unitkerjaId'] = (!isset($post['unitkerjaId']) OR $post['unitkerjaId'] == '') ? 'Semua' : $post['unitkerjaNama'];
      $return['filter']['gedung'] = (!isset($post['key']) OR $post['key'] == 'all') ? 'Semua' : $post['key'];
      // ---------
		$return['filter_print']['thn']=$post['tahun'];
		$return['filter_print']['sbr_dana']=$post['sbr_dana'];
		$return['filter_print']['kepemilikan']=$post['kepemilikan'];
		$return['filter_print']['lokasi']=$post['lokasi'];
      $return['filter_print']['kondisi']=$post['kondisi'];
		$return['filter_print']['unitkerjaId']=$post['unitkerjaNama'];
		$return['filter_print']['gedung']=$post['key'];
      return $return;
   }
   
   function ParseTemplate($data = NULL) {
      //set Label
      $this->TemplateLabeling();
      
      // render message box
      if($data['notebox']['msg'])
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['notebox']['msg']);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $data['notebox']['css']);
		}
      // ---------
      
      //combo
      if(!empty($data['cbx_thn']))
         $cbx_tahun = $data['cbx_thn'];
      $this->GenerateTahun($cbx_tahun);
      
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('gedung', 'Gedung', 'view', 'html') );
      $this->mrTemplate->AddVar('content', 'URL_ADD_GEDUNG', Dispatcher::Instance()->GetUrl('gedung', 'InputGedung', 'view', 'html') );
		$this->mrTemplate->AddVar('content', 'URL_POP_UP_UNIT_PJ', Dispatcher::Instance()->GetUrl('gedung', 'PopUpUnitPj' , 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'KEY', $data['key']);
	
	  $filter=$data['filter_print'];
	  $filter='&thn='.$filter['thn'].'&sbr_dana='.$filter['sbr_dana'].'&kepemilikan='.$filter['kepemilikan'].'&lokasi='.$filter['lokasi'].'&kondisi='.$filter['kondisi'].'&gedung='.$filter['gedung'];
	  
	  $this->mrTemplate->AddVar('content', 'URL_CETAK_GEDUNG', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'cetakGedung', 'view', 'html').$filter);
      
      // filter saat ini
      $this->mrTemplate->AddVars('content', $data['filter'], 'FILTER_');
      // ---------
      
      if (empty($data['gedung'])) {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
      } else {
         $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
         
      $label = "Manajemen Gedung";
      $urlDelete = Dispatcher::Instance()->GetUrl('gedung', 'DeleteGedung', 'do', 'html');
      $urlReturn = Dispatcher::Instance()->GetUrl('gedung', 'Gedung', 'view', 'html');
      Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn, ''),Messenger::NextRequest);
      $this->mrTemplate->AddVar('content', 'URL_DELETE', Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html'));
      
      $nomor = 1;
      for($i=0; $i<count($data['gedung']); $i++) {
         $value = $data['gedung'][$i];
         $nomor = $i+$data['start'];
         if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $nomor);
         if($i == sizeof($data['gedung'])-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $nomor);
         if($nomor % 2 != 0)
            $this->mrTemplate->AddVar("data_item", "CLASS_NAME", 'table-common-even');
         else
            $this->mrTemplate->AddVar("data_item", "CLASS_NAME", '');
         $this->mrTemplate->AddVar("data_item", "NOMOR", $nomor);
         $this->mrTemplate->AddVar("data_item", "GEDUNG_KODE", $data['gedung'][$i]['gedung_kode']);
         $this->mrTemplate->AddVar("data_item", "GEDUNG_ID", $data['gedung'][$i]['gedung_id']);
         $this->mrTemplate->AddVar("data_item", "GEDUNG_NAMA", $data['gedung'][$i]['gedung_nama']);
         $this->mrTemplate->AddVar("data_item", "NILAI_RESIDU", number_format($data['gedung'][$i]['nilai_residu'], 2, ',', '.'));
         $this->mrTemplate->AddVar("data_item", "SBR_DANA", $data['gedung'][$i]['sumber_dana']);
         $this->mrTemplate->AddVar('data_item', "URL_LANTAI", Dispatcher::Instance()->GetUrl('gedung', 'listLantai', 'view', 'html')."&gId=".$data['gedung'][$i]['gedung_id']);
         if($data['gedung'][$i]['gedung_id'] == '1'){
         	$this->mrTemplate->AddVar("data_item", "DISABLED", 'disabled="disabled"');
         	$this->mrTemplate->AddVar("data_item", "DISPLAY_LINK", 'none');
         }
         if($data['gedung'][$i]['gedung_id'] <> '1') $this->mrTemplate->AddVar('data_item', 'URL_EDIT', Dispatcher::Instance()->GetUrl('gedung', 'InputGedung', 'view', 'html').
         '&key=' . Dispatcher::Instance()->Encrypt($data['key']) . 
         '&id_gedung=' . Dispatcher::Instance()->Encrypt($data['gedung'][$i]['gedung_id']));
         $this->mrTemplate->AddVar("data_item", "BESAR_DANA", number_format($data['gedung'][$i]['nilai_gedung'], 2, ',', '.'));
         $this->mrTemplate->AddVar("data_item", "THN_DIBANGUN", $data['gedung'][$i]['thn_perolehan']);
         $this->mrTemplate->AddVar("data_item", "JML_RUANG", $data['gedung'][$i]['jml_ruang']);
         
         if ($value['foto_denah'] != '' && is_readable(GTFW_APP_DIR.'/images/gedung/'.$value['foto_denah']))
            $this->mrTemplate->AddVar('data_item', 'URL_DENAH', Dispatcher::Instance()->GetUrl('gedung', 'fotoDenah', 'popup', 'html')."&gId=".$data['gedung'][$i]['gedung_id']);
			else $this->mrTemplate->AddVar('data_item', 'URL_DENAH', 'javascript: alert(\'Foto denah ['.$data['gedung'][$i]['gedung_nama'].'] belum ada!\');');
			
         if ($value['foto_gedung'] != '' && is_readable(GTFW_APP_DIR.'/images/gedung/'.$value['foto_gedung']))
            $this->mrTemplate->AddVar('data_item', 'URL_FOTO', Dispatcher::Instance()->GetUrl('gedung', 'fotoGedung', 'popup', 'html')."&gId=".$data['gedung'][$i]['gedung_id']);
         else $this->mrTemplate->AddVar('data_item', 'URL_FOTO', 'javascript: alert(\'Foto gedung ['.$data['gedung'][$i]['gedung_nama'].'] belum ada!\');');
         
         if($data['gedung'][$i]['gedung_id'] <> '1')$this->mrTemplate->AddVar('data_item', 'URL_PENYESUAIAN', Dispatcher::Instance()->GetUrl('gedung', 'PenyesuaianGedung', 'view', 'html').
         '&key=' . Dispatcher::Instance()->Encrypt($data['key']) . 
         '&id_gedung=' . Dispatcher::Instance()->Encrypt($data['gedung'][$i]['gedung_id']));
        
         $this->mrTemplate->parseTemplate("data_item", "a");
      }
      }
      
   }
   function GenerateTahun($default = NULL) {
      $now = (int) date('Y');
      $now++;

      for ($i=0; $i < 110; $i++) {
         $this->mrTemplate->addVar("cbxtahun", "ID_TAHUN", $now);
         $this->mrTemplate->addVar("cbxtahun", "TAHUN", $now);
         if ($default == $now) {
            $this->mrTemplate->addVar("cbxtahun", "TAHUN_SELECTED", "selected");
         }else{
            $this->mrTemplate->addVar("cbxtahun", "TAHUN_SELECTED", "");
         }
         $now--;
         $this->mrTemplate->parseTemplate("cbxtahun","a");
      }
   }
   
   function ComboBoxToArray($array)
   {
      foreach ($array as $value) $return[$value['id']] = $value['name'];
      return $return;
   }
}
?>