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


class ViewStatusRumah extends HtmlResponse {

	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/status_rumah/template');
		$this->SetTemplateFile('view_status_rumah.html');
	}
	
	function ProcessRequest() {
		$statusObj = new AppStatusRumah();
      if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['status_rumah'])) {
				$statusRumah = $_POST['status_rumah'];

			} elseif(isset($_GET['status_rumah'])) {
				$statusRumah = Dispatcher::Instance()->Decrypt($_GET['status_rumah']);
			} else {
				$statusRumah = '';
			}
		}
	//view
		$totalData = $statusObj->GetCountDataStatusRumah($statusRumah);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataStatusRumah = $statusObj->GetDataStatusRumah($startRec, $itemViewed,$statusRumah);
//	print_r($dataStatusRumah);
		$url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&status_rumah=' . Dispatcher::Instance()->Encrypt($statusRumah). '&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['dataStatusRumah'] = $dataStatusRumah;
		$return['search']['statusRumah']=$statusRumah;
		$return['start'] = $startRec+1;
   
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
	   $search=$data['search'];
      $this->mrTemplate->AddVar('content', 'STATUS_RUMAH', $search['statusRumah']);
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('status_rumah', 'inputStatusRumah', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('status_rumah', 'statusRumah', '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['dataStatusRumah'])) {
			$this->mrTemplate->AddVar('data_status_rumah', 'STATUSRUMAH_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_status_rumah', 'STATUSRUMAH_EMPTY', 'NO');
			$dataStatusRumah = $data['dataStatusRumah'];
		
//mulai bikin tombol delete
			$label = "Data Status Rumah";
			$urlDelete = Dispatcher::Instance()->GetUrl('status_rumah', 'deleteStatusRumah', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('status_rumah', 'statusRumah', '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($dataStatusRumah); $i++) {
				$no = $i+$data['start'];
				$dataStatusRumah[$i]['number'] = $no;
				//print_r($dataStatusRumah);
				if ($no % 2 != 0) $dataStatusRumah[$i]['class_name'] = 'table-common-even';
				else $dataStatusRumah[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataStatusRumah)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataStatusRumah[$i]['status_id']);
            $urlAccept = 'status_rumah|deleteStatusRumah|do|html-cari-'.$cari;
            $urlReturn = 'status_rumah|statusRumah|view|html-cari-'.$cari;
				$dataStatusRumah[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('status_rumah', 'inputStatusRumah', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage ;
            $dataStatusRumah[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataStatusRumah[$i]['status_id'].'&label='.$label.'&dataName='.$dataStatusRumah[$i]['status_nama'];
				$this->mrTemplate->AddVars('data_status_rumah_item', $dataStatusRumah[$i], 'STATUSRUMAH_');
				$this->mrTemplate->parseTemplate('data_status_rumah_item', 'a');	 
			}
		}
	}
}
?>
