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

class ViewPropinsi extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/propinsi/template');
		$this->SetTemplateFile('view_propinsi.html');
	}
   function ProcessRequest() {
		$propObj = new AppPropinsi();
		if($_POST || isset($_GET['cari'])) {
			if((isset($_POST['propinsi_kode']))or(isset($_POST['propinsi_nama']))or(isset($_POST['propinsi_nama_singkat']))){
				$propKode = $_POST['propinsi_kode'];
            $propNama = $_POST['propinsi_nama'];
            $propNamaSingkat = $_POST['propinsi_nama_singkat'];
			} elseif((isset($_GET['propinsi_kode']))or(isset($_GET['propinsi_nama']))or(isset($_GET['propinsi_nama_singkat']))) {
				$propKode = Dispatcher::Instance()->Decrypt($_GET['propinsi_kode']);
            $propNama=Dispatcher::Instance()->Decrypt($_GET['propinsi_nama']);
            $propNamaSingkat=Dispatcher::Instance()->Decrypt($_GET['propinsi_nama_singkat']);
			} else {
				$propKode = '';
            $propNama='';
            $propNamaSingkat='';
			}
		}
      $totalData = $propObj->GetCountDataPropinsi($propKode,$propNama,$propNamaSingkat);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      $dataPropinsi=$propObj->GetDataPropinsi($propKode,$propNama,$propNamaSingkat,$startRec,$itemViewed);
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&propinsi_kode=' . Dispatcher::Instance()->Encrypt($propKode) . '&propinsi_nama=' . Dispatcher::Instance()->Encrypt($propNama) . '&propinsi_nama_singkat=' . Dispatcher::Instance()->Encrypt($propNamaSingkat) . '&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__);
		
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		
		$return['siaPesan'] = $msg[1][1];
		$return['siaCss'] = $msg[1][2];
		
		$return['dataPropinsi'] = $dataPropinsi;
		$return['start'] = $startRec+1;
		$return['search']['propinsi_kode'] = $propKode;
      $return['search']['propinsi_nama']=$propNama;
      $return['search']['propinsi_nama_singkat']=$propNamaSingkat;
		return $return;
   }

   function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'PROPINSI_KODE', $search['propinsi_kode']);
      $this->mrTemplate->AddVar('content', 'PROPINSI_NAMA', $search['propinsi_nama']);
      $this->mrTemplate->AddVar('content', 'PROPINSI_NAMA_SINGKAT', $search['propinsi_nama_singkat']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('propinsi', 'propinsi', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('propinsi', 'inputPropinsi', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_IMPORT', Dispatcher::Instance()->GetUrl('propinsi', 'importPropinsi', '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['dataPropinsi'])) {
			$this->mrTemplate->AddVar('data_propinsi', 'PROPINSI_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_propinsi', 'PROPINSI_EMPTY', 'NO');
			$dataPropinsi = $data['dataPropinsi'];
        
         $label = "Data Propinsi";
			$urlDelete = Dispatcher::Instance()->GetUrl('propinsi', 'deletePropinsi', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('propinsi', 'propinsi', '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($dataPropinsi); $i++) {
				$no = $i+$data['start'];
				$dataPropinsi[$i]['number'] = $no;
				if ($no % 2 != 0) $dataPropinsi[$i]['class_name'] = 'table-common-even';
				else $dataPropinsi[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataPropinsi)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataPropinsi[$i]['propinsi_kode']);
            $urlAccept = 'propinsi|deletePropinsi|do|html-cari-'.$cari;
            $urlReturn = 'propinsi|propinsi|view|html-cari-'.$cari;
				$dataPropinsi[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('propinsi', 'inputPropinsi', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataPropinsi[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataPropinsi[$i]['propinsi_kode'].'&label='.$label.'&dataName='.$dataPropinsi[$i]['propinsi_nama'];     
				$this->mrTemplate->AddVars('data_propinsi_item', $dataPropinsi[$i], 'PROPINSI_');
				$this->mrTemplate->parseTemplate('data_propinsi_item', 'a');	 
			}
		}
   }
}
?>
