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

class ViewKota extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/kota/template');
		$this->SetTemplateFile('view_kota.html');
	}
   function ProcessRequest() {
		$kotaObj = new AppKota();
		if($_POST || isset($_GET['cari'])) {
			if((isset($_POST['kota_kode']))or(isset($_POST['kota_nama']))or(isset($_POST['propinsi']))){
				$kotaKode = $_POST['kota_kode'];
            $kotaNama = $_POST['kota_nama'];
            $propinsi = $_POST['propinsi'];
			} elseif((isset($_GET['kota_kode']))or(isset($_GET['kota_nama']))or(isset($_GET['propinsi']))) {
				$kotaKode = Dispatcher::Instance()->Decrypt($_GET['kota_kode']);
            $kotaNama = Dispatcher::Instance()->Decrypt($_GET['kota_nama']);
            $propinsi = Dispatcher::Instance()->Decrypt($_GET['propinsi']);
			} else {
				$kotaKode = '';
            $kotaNama = '';
            $propinsi = '';
			}
		}
      $totalData = $kotaObj->GetCountDataKota($kotaKode,$kotaNama,$propinsi);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      $dataKota=$kotaObj->GetDataKota($kotaKode,$kotaNama,$propinsi,$startRec,$itemViewed);
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&kota_kode=' . Dispatcher::Instance()->Encrypt($kotaKode) . '&kota_nama=' . Dispatcher::Instance()->Encrypt($kotaNama) . '&propinsi=' . Dispatcher::Instance()->Encrypt($propinsi) . '&cari=' . Dispatcher::Instance()->Encrypt(1));
      
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      $msg = Messenger::Instance()->Receive(__FILE__);
		
		$dataPropinsi=$kotaObj->GetDataPropinsi();
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'propinsi',
         array('propinsi',$dataPropinsi,$propinsi,'false','onChange="searchByPropinsi()";'), Messenger::CurrentRequest);
		
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		
		$return['siaPesan'] = $msg[1][1];
		$return['siaCss'] = $msg[1][2];
		
		$return['dataKota'] = $dataKota;
		$return['start'] = $startRec+1;
		$return['search']['kota_kode'] = $kotaKode;
      $return['search']['kota_nama'] = $kotaNama;
      $return['search']['propKode'] = $dataPropinsiById[0]['propinsi_kode'];
      $return['search']['propNama'] = $dataPropinsiById[0]['propinsi_nama'];

      $return['dataPropinsi'] = $dataPropinsi;
		return $return;
   }

   function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$dataPropinsi = $data['dataPropinsi'];
		$this->mrTemplate->AddVar('content', 'KOTA_KODE', $search['kota_kode']);
      $this->mrTemplate->AddVar('content', 'KOTA_NAMA', $search['kota_nama']);

		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('kota', 'kota', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('kota', 'inputKota', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_IMPORT', Dispatcher::Instance()->GetUrl('kota', 'importKota', 'view', 'html'));
		
      if($data['pesan']) {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['pesan']);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $data['css']);
		}
		
		if($data['siaPesan']) {
			$this->mrTemplate->SetAttribute('warning_connection', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_connection', 'ISI_PESAN', $data['siaPesan']);
			$this->mrTemplate->AddVar('warning_connection', 'CLASS_PESAN', $data['siaCss']);
		}
      
      if (empty($data['dataKota'])) {
			$this->mrTemplate->AddVar('data_kota', 'KOTA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_kota', 'KOTA_EMPTY', 'NO');
			$dataKota = $data['dataKota'];
        
         $label = "Data Kota";
			$urlDelete = Dispatcher::Instance()->GetUrl('kota', 'deleteKota', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('kota', 'kota', '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'));
	      
         for ($i=0; $i<sizeof($dataKota); $i++) {
				$no = $i+$data['start'];
				$dataKota[$i]['number'] = $no;
				if ($no % 2 != 0) $dataKota[$i]['class_name'] = 'table-common-even';
				else $dataKota[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataKota)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataKota[$i]['kota_kode']);
            $urlAccept = 'kota|deleteKota|do|html-cari-'.$cari;
            $urlReturn = 'kota|kota|view|html-cari-'.$cari;
				$dataKota[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('kota', 'inputKota', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataKota[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataKota[$i]['kota_kode'].'&label='.$label.'&dataName='.$dataKota[$i]['kota_nama'];     
				$this->mrTemplate->AddVars('data_kota_item', $dataKota[$i], 'KOTA_');
				$this->mrTemplate->parseTemplate('data_kota_item', 'a');	 
			}
		}
   }
}
?>
