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

class ViewSumberInformasi extends HtmlResponse {

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/sumber_informasi/template');
		$this->SetTemplateFile('view_sumber_informasi.html');
	}
	
	function ProcessRequest() {
		$smbrInfoObj = new AppSumberInformasi();
      
      //filter edit atau kembalikan dari validasi
      if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['smbr_info_nama'])) {
				$smbr_info = $_POST['smbr_info_nama'];
			} elseif(isset($_GET['smbr_info_nama'])) {
				$smbr_info = Dispatcher::Instance()->Decrypt($_GET['smbr_info_nama']);
			} else {
				$smbr_info = '';
			}
         $cari = Dispatcher::Instance()->Decrypt($_GET['cari']);
		}
      
      //paging
      $totalData = $smbrInfoObj->GetCountSumberInformasi($smbr_info);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataSmbrInfo = $smbrInfoObj->getDataSumberInformasi($startRec, $itemViewed, $smbr_info);
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&smbr_info_nama=' . Dispatcher::Instance()->Encrypt($smbr_info_nama) . '&cari=' . Dispatcher::Instance()->Encrypt(1));

		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      
      //ambil data dari messenger dan kirim ke parse template
      $msg = Messenger::Instance()->Receive(__FILE__);
		$return['Pesan'] = $msg[0][0];
		$return['css'] = $msg[0][1];				

		$return['dataSmbrInfo'] = $dataSmbrInfo;
		$return['start'] = $startRec+1;

		$return['search']['smbr_info'] = $smbr_info;
      $return['search']['cari'] = $cari;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
      $search = $data['search'];
      $cari = $data['search']['cari'];
      $cari = Dispatcher::Instance()->Encrypt($cari);
      
		$this->mrTemplate->AddVar('content', 'SMBR_INFO_NAMA', $search['smbr_info']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('sumber_informasi', 'sumberInformasi', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('sumber_informasi', 'inputSumberInformasi', 'view', 'html'));
      
      //display warning box
      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 (empty($data['dataSmbrInfo'])) {
         //tidak ada data
			$this->mrTemplate->AddVar('data_sumber_informasi', 'SMBRINFO_EMPTY', 'YES');
		} else {
         //tampilkan datanya
         $decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_sumber_informasi', 'SMBRINFO_EMPTY', 'NO');
			$dataSmbrInfo = $data['dataSmbrInfo'];
         
         //mulai bikin tombol delete dan proses arahkan ke fungsi confirm delete
			$label = "Data Sumber Informasi";
			$urlDelete = Dispatcher::Instance()->GetUrl('sumber_informasi', 'deleteSumberInformasi', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('sumber_informasi', 'sumberInformasi', '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($dataSmbrInfo);$i++) {
				$no = $i+$data['start'];
				$dataSmbrInfo[$i]['number'] = $no;
				if ($no % 2 != 0) $dataSmbrInfo[$i]['class_name'] = 'table-common-even';
				else $dataSmbrInfo[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataSmbrInfo)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataSmbrInfo[$i]['smbrinfo_id']);

            $urlAccept = 'sumber_informasi|deleteSumberInformasi|do|html-cari-'.$cari;
            $urlReturn = 'sumber_informasi|sumberInformasi|view|html-cari-'.$cari;
            $dataName = $dataSmbrInfo[$i]['smbrinfo_nama'];
				$dataSmbrInfo[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('sumber_informasi', 'inputSumberInformasi', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataSmbrInfo[$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataName;

				$this->mrTemplate->AddVars('data_sumber_informasi_item', $dataSmbrInfo[$i], 'SMBRINFO_');
				$this->mrTemplate->parseTemplate('data_sumber_informasi_item', 'a');	 
			}
      }
	}
}
?>
