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

class ViewStatus extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/status/template');
		$this->SetTemplateFile('view_status.html');
	}
   function ProcessRequest() {
		$statusObj = new AppStatus();
      
      if(!$statusObj->addConn)
		   $return['siaConn']=false;
      else
         $return['siaConn']=true;
      
		$msg = Messenger::Instance()->Receive(__FILE__);
		$return['Pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		if($_POST || isset($_GET['cari'])) {
			if((isset($_POST['periode']))or (isset($_POST['tahun']))or(isset($_POST['prodi']))or(isset($_POST['notes_nim']))or(isset($_POST['nama']))){
				$periode = $_POST['periode'];
            $tahun = $_POST['tahun'];
				$prodi = $_POST['prodi'];
            $notes_nim = $_POST['notes_nim'];
            $nama = $_POST['nama'];
			} elseif((isset($_GET['periode']))or(isset($_GET['tahun']))or(isset($_GET['prodi']))or(isset($_GET['notes_nim']))or(isset($_GET['nama']))) {
				$periode = Dispatcher::Instance()->Decrypt($_GET['periode']);
            $tahun = Dispatcher::Instance()->Decrypt($_GET['tahun']);
				$prodi = Dispatcher::Instance()->Decrypt($_GET['prodi']);
            $notes_nim = Dispatcher::Instance()->Decrypt($_GET['notes_nim']);
            $nama = Dispatcher::Instance()->Decrypt($_GET['nama']);
			} else {
			   $periode = '';
				$tahun = '';
				$prodi = '';
            $notes_nim = '';
            $nama = '';
			}
		}
		
		if($periode=='all'){
         $periode='';
      }
		
		if($tahun=='all'){
         $tahun='';
      }
      
      if($prodi=='all'){
         $prodi='';
      }

      $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);
         $periode=$carii[0];
         $tahun=$carii[1];
         $prodi=$carii[2];
         $notes_nim=$carii[3];
         $nama=$carii[4];
         $currPage=$carii[5];
      }
		
      $totalData = $statusObj->GetCountDataMahasiswa($periode,$tahun,$prodi,$nama,$notes_nim,$notes_nim);
		$itemViewed = 20;
		$startRec = 0 ;
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      $dataStatus=$statusObj->GetDataStatusMahasiswa($periode,$tahun,$prodi,$nama,$notes_nim,$notes_nim,$startRec,$itemViewed);
      #print_r($dataStatus);
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
            Dispatcher::Instance()->mSubModule, 
            Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . 
            '&periode=' . Dispatcher::Instance()->Encrypt($periode) .
            '&tahun=' . Dispatcher::Instance()->Encrypt($tahun) .
            '&prodi=' . Dispatcher::Instance()->Encrypt($prodi) . 
            '&nama=' . Dispatcher::Instance()->Encrypt($nama) . 
            '&notes_nim=' . Dispatcher::Instance()->Encrypt($notes_nim) . 
            '&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__);
      #print_r($msg);
      $dataSemesterAktif = $statusObj->GetSemesterAktif();
      $dataPeriode=$statusObj->GetDataPeriode();
      $namaPeriode=$statusObj->GetNamaPeriode();
      
      if (isset($dataSemesterAktif['0']['sem_id']))
         $semAktif = $dataSemesterAktif['0']['sem_id'];
      else
         $semAktif = $return['Data']['0']['tahun'];
         
      $dataNamaSemester = $statusObj->GetNamaSemester($tahun);
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'periode',
         array('periode',$namaPeriode,$periode,'true',''), Messenger::CurrentRequest);
      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun',
         array('tahun',$dataPeriode,$tahun,'true',''), Messenger::CurrentRequest);
		
		$dataProdi=$statusObj->GetDataProdi();

      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'prodi',
         array('prodi',$dataProdi,$prodi,'true',''), Messenger::CurrentRequest);

		
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		$return['dataStatus'] = $dataStatus;
		$return['start'] = $startRec+1;
      $return['search']['semId'] = $dataPeriodeById[0]['tahun_id'];
      $return['search']['semTahun'] = $dataPeriodeById[0]['tahun'];
      $return['search']['prodiKode'] = $dataProdiById[0]['prodi_id'];
      $return['search']['prodiNamaResmi'] = $dataProdiById[0]['prodi'];
      $return['search']['periode']=$periode;
      $return['search']['tahun']=$tahun;
      $return['search']['prodi']=$prodi;
      $return['search']['notes_nim']=$notes_nim;
      $return['search']['nama']=$nama;
      $return['search']['page']=$currPage;
      $return['dataSemesterAktif'] = $dataSemesterAktif;
      $return['dataNamaSemester'] = $dataNamaSemester;
		return $return;
		
   }

   function ParseTemplate($data = NULL) {
      $siaConn = $data['siaConn'];
      
      $search = $data['search'];
      $dataStatus = $data['dataStatus'];
      $dataSemesterAktif = $data['dataSemesterAktif'];
		$dataNamaSemester = $data['dataNamaSemester'];
		
		$cari=$search['periode'].'/'.
            $search['tahun'].'/'.
            $search['prodi'].'/'.
            $search['notes_nim'].'/'.
            $search['nama'].'/'.
            $search['page'];
		
      $this->mrTemplate->AddVar('connection', 'NOTES_NIM', $search['notes_nim']);
      $this->mrTemplate->AddVar('connection', 'NAMA', $search['nama']);
		$this->mrTemplate->AddVar('connection', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('status', 'status', 'view', 'html'));
		
		if($siaConn==false){
         $this->mrTemplate->AddVar('connection', 'CONNECTION', 'NO');
         $this->mrTemplate->AddVar('warning', 'ISI_PESAN', 'Gagal menghubungi ke database SI Akademik');
      }elseif($siaConn==true){
         $this->mrTemplate->AddVar('connection', 'CONNECTION', 'YES');
      }
		
      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']);
		}
		
		$this->mrTemplate->AddVar('semester', 'SEMESTER', $data['dataSemesterAktif'][0]['SEMESTER']);
		
		if(!empty($data['dataNamaSemester'])){
         $this->mrTemplate->SetAttribute('semester', 'visibility', 'visible');
         $this->mrTemplate->AddVars('semester', $data['dataNamaSemester'][0], '');   
      }

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

         for ($i=0; $i<sizeof($dataStatus); $i++) {
				$no = $i+$data['start'];
				$dataStatus[$i]['number'] = $no;
				if ($no % 2 != 0) $dataStatus[$i]['class_name'] = 'table-common-even';
				else $dataStatus[$i]['class_name'] = '';
				
				if($dataStatus[$i]['status_all'] == 'A'){
               $dataStatus[$i]['STATUS_TITLE'] = 'Aktif';
            }elseif($dataStatus[$i]['status_all'] == 'C'){
               $dataStatus[$i]['STATUS_TITLE'] = 'Cuti';
            }elseif($dataStatus[$i]['status_all'] == 'L'){
               $dataStatus[$i]['STATUS_TITLE'] = 'Lulus';
            }elseif($dataStatus[$i]['status_all'] == 'D'){
               $dataStatus[$i]['STATUS_TITLE'] = 'Drop-Out';
            }elseif($dataStatus[$i]['status_all'] == 'K'){
               $dataStatus[$i]['STATUS_TITLE'] = 'Keluar';
            }else{
               $dataStatus[$i]['STATUS_TITLE'] = 'Non-Aktif';
            }
				
				$idEnc = Dispatcher::Instance()->Encrypt($dataStatus[$i]['nim']);
				$dataStatus[$i]['url_detail'] = Dispatcher::Instance()->GetUrl('status', 'detailStatus', 'view', 'html') . 
               '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari; 
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataStatus)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				$this->mrTemplate->AddVars('data_status_item', $dataStatus[$i], 'STATUS_');
				$this->mrTemplate->parseTemplate('data_status_item', 'a');	 
         }
      }
   }
}
?>