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

class ViewPekerjaan extends HtmlResponse {

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/pekerjaan/template');
		$this->SetTemplateFile('view_pekerjaan.html');
	}
	
	function ProcessRequest() {
		$pekerjaanObj = new AppPekerjaan();
		if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['pekerjaan'])) {
				$pekerjaan = $_POST['pekerjaan'];
			} elseif(isset($_GET['pekerjaan'])) {
				$pekerjaan = Dispatcher::Instance()->Decrypt($_GET['pekerjaan']);
			} else {
				$pekerjaan = '';
			}
		}
		
	//view
		$totalData = $pekerjaanObj->GetCountDataPekerjaan($pekerjaan);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataPekerjaan = $pekerjaanObj->getDataPekerjaan($startRec, $itemViewed, $pekerjaan);
//		print_r($dataPekerjaan);
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&pekerjaan=' . Dispatcher::Instance()->Encrypt($pekerjaan) . '&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['dataPekerjaan'] = $dataPekerjaan;
		$return['start'] = $startRec+1;

		$return['search']['pekerjaan'] = $pekerjaan;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'PEKERJAAN', $search['pekerjaan']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('pekerjaan', 'pekerjaan', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('pekerjaan', 'inputPekerjaan', '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['dataPekerjaan'])) {
			$this->mrTemplate->AddVar('data_pekerjaan', 'PEKERJAAN_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_pekerjaan', 'PEKERJAAN_EMPTY', 'NO');
			$dataPekerjaan = $data['dataPekerjaan'];
		
//mulai bikin tombol delete
			$label = "Data Pekerjaan";
			$urlDelete = Dispatcher::Instance()->GetUrl('pekerjaan', 'deletePekerjaan', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('pekerjaan', 'pekerjaan', '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($dataPekerjaan); $i++) {
				$no = $i+$data['start'];
				$dataPekerjaan[$i]['number'] = $no;
				if ($no % 2 != 0) $dataPekerjaan[$i]['class_name'] = 'table-common-even';
				else $dataPekerjaan[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataPekerjaan)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataPekerjaan[$i]['pekerjaan_id']);

            $urlAccept = 'pekerjaan|deletePekerjaan|do|html-cari-'.$cari;
            $urlReturn = 'pekerjaan|pekerjaan|view|html-cari-'.$cari;
            $dataName = $dataPekerjaan[$i]['pekerjaan_nama'];
				$dataPekerjaan[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('pekerjaan', 'inputPekerjaan', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataPekerjaan[$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataName;

				$this->mrTemplate->AddVars('data_pekerjaan_item', $dataPekerjaan[$i], 'PEKERJAAN_');
				$this->mrTemplate->parseTemplate('data_pekerjaan_item', 'a');	 
			}
		}
	}
}
?>
