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

class ViewJurusanSmta extends HtmlResponse {

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/jurusan_smta/template');
		$this->SetTemplateFile('view_jurusan_smta.html');
	}
	
	function ProcessRequest() {
		$jurObj = new AppJurusanSmta();
		if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['jurusan'])) {
				$jurusan = $_POST['jurusan'];
			} elseif(isset($_GET['jurusan'])) {
				$jurusan = Dispatcher::Instance()->Decrypt($_GET['jurusan']);
			} else {
				$jurusan = '';
			}
		}
		
	//view
		$totalData = $jurObj->GetCountDataJurusanSmta($jurusan);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		
		$dataJurusan = $jurObj->getDataJurusanSmta($startRec, $itemViewed, $jurusan);

		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&jurusan=' . Dispatcher::Instance()->Encrypt($jurusan) . '&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['dataJurusan'] = $dataJurusan;
		$return['start'] = $startRec+1;
		$return['search']['jurusan'] = $jurusan;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'JURUSAN', $search['jurusan']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('jurusan_smta', 'jurusanSmta', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('jurusan_smta', 'inputJurusanSmta', '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 (empty($data['dataJurusan'])) {
			$this->mrTemplate->AddVar('data_jurusan', 'JURUSAN_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_jurusan', 'JURUSAN_EMPTY', 'NO');
			$dataJurusan = $data['dataJurusan'];
		
//mulai bikin tombol delete
			$label = "Data Jurusan SMTA";
			$urlDelete = Dispatcher::Instance()->GetUrl('jurusan_smta', 'deleteJurusanSmta', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('jurusan_smta', 'jurusanSmta', '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($dataJurusan); $i++) {
				$no = $i+$data['start'];
				$dataJurusan[$i]['number'] = $no;
				if ($no % 2 != 0) $dataJurusan[$i]['class_name'] = 'table-common-even';
				else $dataJurusan[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataJurusan)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataJurusan[$i]['jurusan_kode']);
            $urlAccept = 'jurusan_smta|deleteJurusanSmta|do|html-cari-'.$cari;
            $urlReturn = 'jurusan_smta|jurusanSmta|view|html-cari-'.$cari;
				$dataJurusan[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('jurusan_smta', 'inputJurusanSmta', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataJurusan[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataJurusan[$i]['jurusan_kode'].'&label='.$label.'&dataName='.$dataJurusan[$i]['jurusan_nama'];     
				$this->mrTemplate->AddVars('data_jurusan_item', $dataJurusan[$i], 'JURUSAN_');
				$this->mrTemplate->parseTemplate('data_jurusan_item', 'a');	 
			}
		}
	}
}
?>
