<?php

require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/registrasi/business/AppRegistrasi.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/registrasi/business/AppRegistrasiSia.class.php';

class ViewRegistrasi extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/registrasi/template');
		$this->SetTemplateFile('view_registrasi.html');
	}

   function ProcessRequest() {
      $msg = Messenger::Instance()->Receive(__FILE__);
		
      $regObj = new AppRegistrasi();
		$siaObj = new AppRegistrasiSia();
		
      if(!$siaObj->addConn)
		   $return['siaConn']=false;
      else
         $return['siaConn']=true;
      
		if($_POST || isset($_GET['cari'])) {

			if(isset($_POST['tahun'])){
				$tahun = $_POST['tahun'];
				$semester = $_POST['semester'];
				$prodi = $_POST['prodi'];
            $notes_nim = $_POST['notes_nim'];
            $nama = $_POST['nama'];
            $status = $_POST['status'];
            
			} elseif(isset($_GET['tahun'])) {
				$tahun = Dispatcher::Instance()->Decrypt($_GET['tahun']);
				$semester = Dispatcher::Instance()->Decrypt($_GET['semester']);
				$prodi = Dispatcher::Instance()->Decrypt($_GET['prodi']);
            $notes_nim = Dispatcher::Instance()->Decrypt($_GET['notes_nim']);
            $nama = Dispatcher::Instance()->Decrypt($_GET['nama']);
            $status = Dispatcher::Instance()->Decrypt($_GET['status']);
            
			} else {
				$tahun = '';
				$semester = '';
				$prodi = '';
            $notes_nim = '';
            $nama = '';
            $status = '';
			}
		}

		if($prodi=='all'){
         $prodi='';
      }      
      
      if($tahun=='all'){
         $tahun='';
      }  
      
      if($semester=='all'){
         $semester='';
      }
      if($status=='all'){
         $status='';
      }

		$currPage = 1;
        
      if (((isset($_GET['cari'])) && (trim($_GET['cari']) <> "1")) or(isset($_POST['cari']))) {
         $cari=empty($_GET['cari'])?$_POST['cari']:$_GET['cari'];
         $carii=explode("/", $cari);
         $tahun=$carii[0];
         $semester=$carii[1];
         $prodi=$carii[2];
         $notes_nim=$carii[3];
         $nama=$carii[4];
         $status=$carii[5];
         $currPage=$carii[6];
      }

      $totalData = $regObj->GetCountRegistrasiMahasiswa($tahun,$semester,$prodi,$nama,$notes_nim,$notes_nim,$status);
		$itemViewed = 20;
		$startRec = 0 ;
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      $dataRegistrasi = $regObj->GetDataRegistrasiMahasiswa($tahun,$semester,$prodi,$nama,$notes_nim,$notes_nim,$status,$startRec,$itemViewed);
      #print_r($dataRegistrasi);
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
            Dispatcher::Instance()->mSubModule, 
            Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . 
            '&tahun=' . Dispatcher::Instance()->Encrypt($tahun) .
            '&semester=' . Dispatcher::Instance()->Encrypt($semester) .
            '&prodi=' . Dispatcher::Instance()->Encrypt($prodi) . 
            '&notes_nim=' . Dispatcher::Instance()->Encrypt($notes_nim) . 
            '&nama=' . Dispatcher::Instance()->Encrypt($nama) . 
            '&status=' . Dispatcher::Instance()->Encrypt($status) . 
            '&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__);
      
      $dataPeriodeAktif = $regObj->GetPeriodeAktif();
      $dataNamaPeriode = $regObj->GetNamaPeriode();
      $dataPeriode=$regObj->GetDataPeriode();
		$dataProdi=$regObj->GetDataProdi();     
      $dataTahunMasuk=$regObj->GetDataTahun();
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun',
         array('tahun',$dataTahunMasuk,$tahun,'true',''), Messenger::CurrentRequest);
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'semester',
         array('semester',$dataNamaPeriode,$semester,'true',''), Messenger::CurrentRequest);

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'prodi',
         array('prodi',$dataProdi,$prodi,'true',''), Messenger::CurrentRequest);
         
      $dataStatus=$regObj->GetStatusRegistrasi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status',
            array('status',$dataStatus,$status,'true',''), Messenger::CurrentRequest);
		
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		$return['start'] = $startRec+1;
		$return['search']['tahun'] = $tahun;
		$return['search']['prodi'] = $prodi;
		$return['search']['semester'] = $semester;
      $return['search']['prodiKode'] = $dataProdiById[0]['prodi_id'];
      $return['search']['prodiNama'] = $dataProdiById[0]['prodi'];
      $return['search']['notes_nim']=$notes_nim;
      $return['search']['nama']=$nama;
      $return['search']['status']=$status;
      $return['search']['page']=$currPage;
      $return['dataRegistrasi'] = $dataRegistrasi;
      $return['dataPeriodeAktif'] = $dataPeriodeAktif;
      $return['dataNamaPeriode'] = $dataNamaPeriode;
      $return['dataPeriode'] = $dataPeriode;
      $return['connection'] = $connection;
      $return['msgError'] = $msgError;
		return $return;
   }

   function ParseTemplate($data = NULL) {
      $siaConn = $data['siaConn'];
      $search = $data['search'];
      $dataRegistrasi = $data['dataRegistrasi'];
      $dataPeriodeAktif = $data['dataPeriodeAktif'];
		$dataNamaPeriode = $data['dataNamaPeriode'];
		
		$cari=$search['tahun'].'/'.
		      $search['semester'].'/'.
            $search['prodi'].'/'.
            $search['notes_nim'].'/'.
            $search['nama'].'/'.
            $search['status'].'/'.
            $search['page'];
		
      $this->mrTemplate->AddVar('content', 'NOTES_NIM', $search['notes_nim']);
      $this->mrTemplate->AddVar('content', 'NAMA', $search['nama']);
		$this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('registrasi', 'registrasi', 'view', 'html'));
		#print_r($dataRegistrasi);
		
		if($siaConn==false){
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', 'Gagal menghubungi ke database SI Akademik');
      }elseif($siaConn==true){
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'hidden');
      }
		
      if(!empty($data['dataPeriodeAktif'])){
         $this->mrTemplate->SetAttribute('periode', 'visibility', 'visible');
         $this->mrTemplate->AddVars('periode', $data['dataPeriodeAktif'][0], '');
      }
		if(!empty($data['dataNamaPeriode'])){
         $this->mrTemplate->SetAttribute('periode', 'visibility', 'visible');
         $this->mrTemplate->AddVars('periode', $data['dataNamaPeriode'][0], '');   
      }

      if (empty($data['dataRegistrasi'])) {
			$this->mrTemplate->AddVar('data_registrasi', 'REGISTRASI_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
			$this->mrTemplate->AddVar('data_registrasi', 'REGISTRASI_EMPTY', 'NO');
			$dataRegistrasi = $data['dataRegistrasi'];

         for ($i=0; $i<sizeof($dataRegistrasi); $i++) {
				$no = $i+$data['start'];
				$dataRegistrasi[$i]['number'] = $no;
				if ($no % 2 != 0) $dataRegistrasi[$i]['class_name'] = 'table-common-even';
				else $dataRegistrasi[$i]['class_name'] = '';
				
				$idEnc = Dispatcher::Instance()->Encrypt($dataRegistrasi[$i]['id']);
				$dataRegistrasi[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('registrasi', 'updateRegistrasi', 'view', 'html') . 
               '&dataId=' . $idEnc; 
            $dataRegistrasi[$i]['url_detail'] = Dispatcher::Instance()->GetUrl('registrasi', 'detailRegistrasi', 'view', 'html') . 
               '&dataId=' . $idEnc . '&id=' . $idEnc . '&page=' . $encPage . '&cari='.$cari; 
				
				if($dataRegistrasi[$i]['status_reg'] == 'S'){
               $dataRegistrasi[$i]['STATUS_TITLE_REG'] = 'Sudah Registrasi';
            }else{
               $dataRegistrasi[$i]['STATUS_TITLE_REG'] = 'Belum Registrasi';
            }
            
            if($dataRegistrasi[$i]['status_mhs'] == '1'){
               $dataRegistrasi[$i]['status_mhs'] = 'CM'; 
               $dataRegistrasi[$i]['STATUS_TITLE_MHS'] = 'Calon Mahasiswa';
            }else{
               $dataRegistrasi[$i]['status_mhs'] = 'M';
               $dataRegistrasi[$i]['STATUS_TITLE_MHS'] = 'Mahasiswa';
            }
				
				if($dataRegistrasi[$i]['status_akd'] == 'A'){
               $dataRegistrasi[$i]['STATUS_TITLE_AKD'] = 'Aktif';
            }elseif($dataRegistrasi[$i]['status_akd'] == 'C'){
               $dataRegistrasi[$i]['STATUS_TITLE_AKD'] = 'Cuti';
            }else{
               $dataRegistrasi[$i]['STATUS_TITLE_AKD'] = 'Non-Aktif';
            }
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataRegistrasi)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$this->mrTemplate->AddVars('data_registrasi_item', $dataRegistrasi[$i], 'REGISTRASI_');
				$this->mrTemplate->parseTemplate('data_registrasi_item', 'a');	 
         }
      }
   }
}
?>