<?php

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/group/business/AppGroup.class.php';

class ViewPopupTemplateGroup extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/group/template');
      $this->SetTemplateFile('view_popup_template_group.html');
   }
   
   function ProcessRequest() {
		$obj = new AppGroup();
		
		# filter handler
		if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['group'])){	$group = $_POST['group'];
			}elseif(isset($_GET['group'])) { $group = Dispatcher::Instance()->Decrypt($_GET['group']);
			}else{ $group = '';}
		  
			if(isset($_POST['unit'])){	$unitkerja = $_POST['unit'];
			}elseif(isset($_GET['unit'])){ $unitkerja = Dispatcher::Instance()->Decrypt($_GET['unit']);
			}else{ $unitkerja = '';}
		}

		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		
		$dataTemplate = $obj->GetDataGroup($group,$unitkerja,false,$startRec, $itemViewed);
		$totalData = $obj->GetCount();
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&group=' . Dispatcher::Instance()->Encrypt($group) . '&unit=' . Dispatcher::Instance()->Encrypt($unitkerja) . '&cari=' . Dispatcher::Instance()->Encrypt(1));
		$dest = "popup-subcontent";
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage, $dest), Messenger::CurrentRequest);

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

		# search array
		$return['search']['group'] = $group;
		$return['search']['unitkerja'] = $unitkerja;

		return $return;
	}
	
	function filter($string){
		return addslashes($string);
	}

   function ParseTemplate($data = NULL) {
   	
   	$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType ));
   	
   	if(!empty($data['search'])){
   		$search = $data['search'];
   		$this->mrTemplate->AddVar('content', 'GROUP', $search['group']);
   		$this->mrTemplate->AddVar('content', 'UNIT', $search['unitkerja']);
   	}
   	
   	if (empty($data['dataTemplate'])) {
   		$this->mrTemplate->AddVar('data_template', 'TEMPLATE_EMPTY', 'YES');
   	} else {
   		$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
   		$encPage = Dispatcher::Instance()->Encrypt($decPage);
   		$this->mrTemplate->AddVar('data_template', 'TEMPLATE_EMPTY', 'NO');
   		$dataTemplate = $data['dataTemplate'];
   	
   		$x=0; $no=1;
   		$unitkerjaId='';
   		$nomor_satuankerja=1;
   		for ($i=0; $i<sizeof($dataTemplate); $i++) {
   			$no = $i+$data['start'];
   			$dataTemplate[$i]['number'] = $no;
   			if((int)$dataTemplate[$i]['isParent'] > 0)$dataTemplate[$i]['class_name'] = 'table-common-even';
   			else $dataTemplate[$i]['class_name'] = '';
   			$dataTemplate[$i]['unit'] = $this->filter($dataTemplate[$i]['unit']);
   	
   			$this->mrTemplate->AddVars('data_template_item', $dataTemplate[$i], 'TEMPLATE_');
   			$this->mrTemplate->parseTemplate('data_template_item', 'a');
   		}
   	} 
   }
}
?>
