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

class ViewNegara extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/negara/template');
		$this->SetTemplateFile('view_negara.html');
	}
   function ProcessRequest() {
		$negaraObj = new AppNegara();
		if($_POST || isset($_GET['cari'])) {
			if((isset($_POST['negara_kode']))or(isset($_POST['negara_nama']))or(isset($_POST['negara_nama_singkat']))){
				$negaraKode = $_POST['negara_kode'];
            $negaraNama = $_POST['negara_nama'];
            $negaraNamaSingkat = $_POST['negara_nama_singkat'];
			} elseif((isset($_GET['negara_kode']))or(isset($_GET['negara_nama']))or(isset($_GET['negara_nama_singkat']))) {
				$negaraKode = Dispatcher::Instance()->Decrypt($_GET['negara_kode']);
            $negaraNama=Dispatcher::Instance()->Decrypt($_GET['negara_nama']);
            $negaraNamaSingkat=Dispatcher::Instance()->Decrypt($_GET['negara_nama_singkat']);
			} else {
				$negaraKode = '';
            $negaraNama='';
            $negaraNamaSingkat='';
			}
		}
      $totalData = $negaraObj->GetCountDataNegara($negaraKode,$negaraNama,$negaraNamaSingkat);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      $dataNegara=$negaraObj->GetDataNegara($negaraKode,$negaraNama,$negaraNamaSingkat,$startRec,$itemViewed);
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&negara_kode=' . Dispatcher::Instance()->Encrypt($negaraKode) . '&negara_nama=' . Dispatcher::Instance()->Encrypt($negaraNama) . '&negara_nama_singkat=' . Dispatcher::Instance()->Encrypt($negaraNamaSingkat) . '&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['dataNegara'] = $dataNegara;
		$return['start'] = $startRec+1;
		$return['search']['negara_kode'] = $negaraKode;
      $return['search']['negara_nama']=$negaraNama;
      $return['search']['negara_nama_singkat']=$negaraNamaSingkat;
		return $return;
		
   }

   function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'NEGARA_KODE', $search['negara_kode']);
      $this->mrTemplate->AddVar('content', 'NEGARA_NAMA', $search['negara_nama']);
      $this->mrTemplate->AddVar('content', 'NEGARA_NAMA_SINGKAT', $search['negara_nama_singkat']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('negara', 'negara', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('negara', 'inputNegara', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_IMPORT', Dispatcher::Instance()->GetUrl('negara', 'importNegara', '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['dataNegara'])) {
			$this->mrTemplate->AddVar('data_negara', 'NEGARA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_negara', 'NEGARA_EMPTY', 'NO');
			$dataNegara = $data['dataNegara'];
        
         $label = "Data Negara";
			$urlDelete = Dispatcher::Instance()->GetUrl('negara', 'deleteNegara', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('negara', 'negara', '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($dataNegara); $i++) {
				$no = $i+$data['start'];
				$dataNegara[$i]['number'] = $no;
				if ($no % 2 != 0) $dataNegara[$i]['class_name'] = 'table-common-even';
				else $dataNegara[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataNegara)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataNegara[$i]['negara_kode']);
            $urlAccept = 'negara|deleteNegara|do|html-cari-'.$cari;
            $urlReturn = 'negara|negara|view|html-cari-'.$cari;
				$dataNegara[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('negara', 'inputNegara', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataNegara[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataNegara[$i]['negara_kode'].'&label='.$label.'&dataName='.$dataNegara[$i]['negara_nama'];     
				$this->mrTemplate->AddVars('data_negara_item', $dataNegara[$i], 'NEGARA_');
				$this->mrTemplate->parseTemplate('data_negara_item', 'a');	 
			}
		}
   }
}
?>
