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

class ViewStatusKeluarga extends HtmlResponse {
   function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/status_keluarga/template');
		$this->SetTemplateFile('view_status_keluarga.html');
	}
   
   function ProcessRequest() {
      $staKlrgaObj = new AppStatusKeluarga();
      
      //filter edit atau kembalikan dari validasi
      if($_POST || isset($_GET['cari'])) {
			if(isset($_POST['sts_klrga_nama'])) {
				$sts_klrga = $_POST['sts_klrga_nama'];
			} elseif(isset($_GET['sts_klrga_nama'])) {
				$sts_klrga = Dispatcher::Instance()->Decrypt($_GET['sts_klrga_nama']);
			} else {
				$sts_klrga = '';
			}
         $cari = Dispatcher::Instance()->Decrypt($_GET['cari']);
		}
      
      //paging
      $totalData = $staKlrgaObj->GetCountStatusKeluarga($sts_klrga);
		$itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataStsKlrga = $staKlrgaObj->getDataStatusKeluarga($startRec, $itemViewed, $sts_klrga);
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&sts_klrga_nama=' . Dispatcher::Instance()->Encrypt($sts_klrga) . '&cari=' . Dispatcher::Instance()->Encrypt(1));

		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      
      //ambil data dari messenger dan kirim ke parse template
      $msg = Messenger::Instance()->Receive(__FILE__);
		$return['Pesan'] = $msg[0][0];
		$return['css'] = $msg[0][1];				

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

		$return['search']['sts_klrga'] = $sts_klrga;
      $return['search']['cari'] = $cari;
		return $return;
   }
   
   function ParseTemplate($data=NULL) {
      $search = $data['search'];
      $cari = $data['search']['cari'];
      $cari = Dispatcher::Instance()->Encrypt($cari);
      
		$this->mrTemplate->AddVar('content', 'STS_KLRGA_NAMA', $search['sts_klrga']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('status_keluarga', 'statusKeluarga', 'view', 'html'));
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('status_keluarga', 'inputStatusKeluarga', 'view', 'html'));
      
      //display warning box
      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['dataStsKlrga'])) {
         //tidak ada data
			$this->mrTemplate->AddVar('data_status_keluarga', 'STSKLRGA_EMPTY', 'YES');
		} else {
         //ada datanya
         $decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_status_keluarga', 'STSKLRGA_EMPTY', 'NO');
			$dataStsKlrga = $data['dataStsKlrga'];
         
         //mulai bikin tombol delete dan proses arahkan ke fungsi confirm delete
			$label = "Data ".GTFWConfiguration::GetValue('language','status_keluarga');
			$urlDelete = Dispatcher::Instance()->GetUrl('status_keluarga', 'deleteStatusKeluarga', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('status_keluarga', 'statusKeluarga', '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($dataStsKlrga);$i++) {
				$no = $i+$data['start'];
				$dataStsKlrga[$i]['number'] = $no;
				if ($no % 2 != 0) $dataStsKlrga[$i]['class_name'] = 'table-common-even';
				else $dataStsKlrga[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataStsKlrga)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$idEnc = Dispatcher::Instance()->Encrypt($dataStsKlrga[$i]['stsklrga_id']);

            $urlAccept = 'status_keluarga|deleteStatusKeluarga|do|html-cari-'.$cari;
            $urlReturn = 'status_keluarga|statusKeluarga|view|html-cari-'.$cari;
            $dataName = $dataStsKlrga[$i]['stsklrga_nama'];
				$dataStsKlrga[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('status_keluarga', 'inputStatusKeluarga', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataStsKlrga[$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataName;

				$this->mrTemplate->AddVars('data_status_keluarga_item', $dataStsKlrga[$i], 'STSKLRGA_');
				$this->mrTemplate->parseTemplate('data_status_keluarga_item', 'a');	 
			}         
      }
   }
}
?>
