<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/periode/business/AppPeriode.class.php';
class ViewPeriode extends HtmlResponse {
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/periode/template');
		$this->SetTemplateFile('view_periode.html');
	}
	
	function ProcessRequest() {
		$prdObj = new AppPeriode();
		$msg = Messenger::Instance()->Receive(__FILE__);
		$return['Pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		#print_r($msg);
		
		unset($_SESSION['tahun']);
		unset($_SESSION['semester']);
		
	//view
		$totalData = $prdObj->GetCountDataPeriode($perId);
		
      $itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
		if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
		$dataPeriodeAktif = $prdObj->GetPeriodeAktif();
		$dataPeriode = $prdObj->GetDataPeriode($startRec, $itemViewed, $perTahun,$perNmperId,$perIsAktif);
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
		
		$dataNamaPeriode = $prdObj->GetComboNamaPeriode();
		
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'semester', 
         array('semester',$dataNamaPeriode,$unit_selected,'false'), Messenger::CurrentRequest);
		
		$dataTahun=$prdObj->GetTahun(1999);    
      //combo tahun Akademik    
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun',
         array('tahun',$dataTahun,$tahunAkademik,'false'), Messenger::CurrentRequest);
		
		$return['dataPeriode'] = $dataPeriode;
		$return['dataPeriodeAktif'] = $dataPeriodeAktif;
		$return['start'] = $startRec+1;
		
		return $return;
	}
	
	function ParseTemplate($data = NULL) {
      #print_r($data);
      $dataPeriodeAktif = $data['dataPeriodeAktif'];
		$this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('periode', 'inputPeriode', '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['dataPeriodeAktif'])){
         $this->mrTemplate->SetAttribute('periode', 'visibility', 'visible');
         $this->mrTemplate->AddVars('periode', $data['dataPeriodeAktif'][0], '');
		}else{
         $this->mrTemplate->SetAttribute('warning_periode', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_periode', 'ISI_PESAN_PERIODE', 'Belum Ada Periode Aktif');
      }
		
		if (empty($data['dataPeriode'])) {
			$this->mrTemplate->AddVar('data_periode', 'PERIODE_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_periode', 'PERIODE_EMPTY', 'NO');
			$dataPeriode = $data['dataPeriode'];
		
//mulai bikin tombol delete
			$label = "Periode";
			$urlDelete = Dispatcher::Instance()->GetUrl('periode', 'deletePeriode', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('periode', 'periode', '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($dataPeriode); $i++) {
				$no = $i+$data['start'];
				$dataPeriode[$i]['number'] = $no;
				if ($no % 2 != 0) $dataPeriode[$i]['class_name'] = 'table-common-even';
				else $dataPeriode[$i]['class_name'] = '';
            $idEnc = Dispatcher::Instance()->Encrypt($dataPeriode[$i]['id']);
            $dataPeriode[$i]['url_aktif'] = Dispatcher::Instance()->GetUrl('periode', 'aktifPeriode', 'do', 'html') . '&dataId=' . $idEnc;
				
            if ($dataPeriode[$i]['is_aktif']=='0'){
               $dataPeriode[$i]['is_aktif'] = 'Tidak Aktif';
               $this->mrTemplate->AddVar('aktif','IS_SHOW_AKTIFKAN','YES');
               $this->mrTemplate->AddVar('aktif','PERIODE_URL_AKTIF',$dataPeriode[$i]['url_aktif']);
            } else {
               $dataPeriode[$i]['is_aktif'] = 'Aktif';
               $dataPeriode[$i]['class_name'] = 'active';
               $this->mrTemplate->AddVar('aktif','IS_SHOW_AKTIFKAN','NO');
            }
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataPeriode)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
                             
            $urlAccept = 'periode|deletePeriode|do|html-cari-'.$cari;            
            $urlReturn = 'periode|periode|view|html-cari-'.$cari;            
            $dataTahun = $dataPeriode[$i]['tahun'];
            $dataName = $dataPeriode[$i]['semester'];
            $idEnc = Dispatcher::Instance()->Encrypt($dataPeriode[$i]['id']);
            $dataPeriode[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('periode', 'inputPeriode', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataPeriode[$i]['url_delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$idEnc.'&label='.$label.'&dataName='.$dataTahun.' '.$dataName;
				$this->mrTemplate->AddVars('data_periode_item', $dataPeriode[$i], 'PERIODE_');
				$this->mrTemplate->parseTemplate('data_periode_item', 'a');	 
			}
		}
	}
}
?>