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

class ViewUnitkerja extends HtmlResponse {

	var $Pesan;

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/unitkerja/template');
		$this->SetTemplateFile('view_unitkerja.html');
	}
	
	function ProcessRequest() {
		$unitkerjaObj = new AppUnitkerja();
		if($_POST || isset($_GET['cari'])) {
					
			if(isset($_POST['kode'])) {
				$kode = $_POST['kode'];
			} elseif(isset($_GET['kode'])) {
				$kode = Dispatcher::Instance()->Decrypt($_GET['kode']);
			} else {
				$kode = '';
			}
		  
			if(isset($_POST['nama'])) {
				$unitkerja = $_POST['nama'];
			} elseif(isset($_GET['nama'])) {
				$unitkerja = Dispatcher::Instance()->Decrypt($_GET['nama']);
			} else {
				$unitkerja = '';
			}

			if($_POST['tipeunit'] != "all") {
				$tipeunit = $_POST['tipeunit'];
			} elseif(isset($_GET['tipeunit'])) {
				$tipeunit = Dispatcher::Instance()->Decrypt($_GET['tipeunit']);
			} else {
				$tipeunit = '';
			}
		}
		
		
	//view
		$totalData = $unitkerjaObj->GetCountDataUnitkerja($satker, $kode, $unitkerja, $tipeunit);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}

		//$arrUnitKerja = $unitkerjaObj->GetUnitKerja($startRec, $itemViewed, $kode,$unitkerja,$tipeunit);
		
		$dataUnitkerja = $unitkerjaObj->getDataUnitkerja($startRec, $itemViewed, $unitkerja, $kode, $tipeunit);
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&kode=' . Dispatcher::Instance()->Encrypt($kode) . '&nama=' . Dispatcher::Instance()->Encrypt($unitkerja) . '&tipeunit=' . Dispatcher::Instance()->Encrypt($tipeunit) . '&cari=' . Dispatcher::Instance()->Encrypt(1));

		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);

		
		$arr_tipeunit = $unitkerjaObj->GetDataTipeunit();

		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tipeunit', array('tipeunit', $arr_tipeunit, $tipeunit, 'true', ' style="width:200px;" '), Messenger::CurrentRequest);

		$msg = Messenger::Instance()->Receive(__FILE__);

		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
				
		$return['dataUnitkerja'] = $dataUnitkerja;
		$return['start'] = $startRec+1;

		$return['search']['satker'] = $satker;
		$return['search']['kode'] = $kode;
		$return['search']['unitkerja'] = $unitkerja;
		$return['search']['tipeunit'] = $tipeunit;
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
		$search = $data['search'];
		$this->mrTemplate->AddVar('content', 'SATKER', $search['satker']);
		$this->mrTemplate->AddVar('content', 'UNITKERJA_KODE', $search['kode']);
		$this->mrTemplate->AddVar('content', 'UNITKERJA', $search['unitkerja']);
		$this->mrTemplate->AddVar('content', 'TIPEUNIT', $search['tipeunit']);

		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('unitkerja', 'unitkerja', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD_UNIT', Dispatcher::Instance()->GetUrl('unitkerja', 'inputUnitkerja', 'view', 'html') . '&jenis=' . Dispatcher::Instance()->Encrypt('unit'));
		$this->mrTemplate->AddVar('content', 'URL_ADD_SUBUNIT', Dispatcher::Instance()->GetUrl('unitkerja', 'inputUnitkerja', 'view', 'html') . '&jenis=' . Dispatcher::Instance()->Encrypt('subunit'));

		if($this->Pesan) {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}

		if (empty($data['dataUnitkerja'])) {
			$this->mrTemplate->AddVar('data_unitkerja', 'UNITKERJA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_unitkerja', 'UNITKERJA_EMPTY', 'NO');
			$dataUnitkerja = $data['dataUnitkerja'];
         //print_r($dataUnitkerja);
//mulai bikin tombol delete
			$label = "Manajemen Unit/ Sub Unit Kerja";
			$urlDelete = Dispatcher::Instance()->GetUrl('unitkerja', 'deleteUnitkerja', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('unitkerja', 'unitkerja', 'view', 'html');
			Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn, 'Penghapusan data Unit akan menghapus Sub unit dibawahnya'),Messenger::NextRequest);
			$this->mrTemplate->AddVar('content', 'URL_DELETE', Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html'));
//selesai bikin tombol delete

         //$this->mrTemplate->AddVar('content', 'MESSAGE', 'Penghapusan Data ini akan menghapus sub unit dibawahnya');
         $x=0; $no=1;
         $unitkerjaId='';
         $nomor_satuankerja=1;
         //echo "<pre>";
         //print_r($dataUnitkerja);
         //echo "</pre>";
			for ($i=0; $i<sizeof($dataUnitkerja); $i++) {
				$no = $i+$data['start'];
				$dataUnitkerja[$i]['number'] = $no;
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);
				if($i == sizeof($dataUnitkerja)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				if($dataUnitkerja[$i]['parentId'] == 0) {
               //print_r($dataUnitkerja[$i]);
               //unit/SATKER
               $this->mrTemplate->SetAttribute('is_satker', 'visibility', 'visible');
               $this->mrTemplate->AddVar('is_satker', 'UNITKERJA_ID', $dataUnitkerja[$i]['id']);
               //$this->mrTemplate->AddVar('is_satker', 'IS_SATKER', $dataUnitkerja[$i]['id']);
               $dataUnitkerja[$i]['class_name'] = 'table-common-even1';
   			   $dataUnitkerja[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('unitkerja', 'inputUnitkerja', 'view', 'html') . '&jenis=' . Dispatcher::Instance()->Encrypt('unit') . '&dataId=' . $dataUnitkerja[$i]['id'];
            } else {
               //subunit
               $this->mrTemplate->SetAttribute('is_satker', 'visibility', 'hidden');
               $dataUnitkerja[$i]['class_name'] = '';
   			   $dataUnitkerja[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('unitkerja', 'inputUnitkerja', 'view', 'html') . '&jenis=' . Dispatcher::Instance()->Encrypt('subunit') . '&dataId=' . $dataUnitkerja[$i]['id'];
            }

				$this->mrTemplate->AddVars('data_unitkerja_item', $dataUnitkerja[$i], 'UNITKERJA_');
				$this->mrTemplate->parseTemplate('data_unitkerja_item', 'a');	 
			}
		}
	}
}
?>
