<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/kuota_waktu_kuliah/business/AppKuotaWaktuKuliahToSia.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/kuota_waktu_kuliah/business/AppKuotaWaktuKuliahToAdmisi.class.php';

class ViewInputKuotaWaktuKuliah extends HtmlResponse {
   
   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot') . 'module/kuota_waktu_kuliah/template');
      $this->SetTemplateFile('input_kuota_waktu_kuliah.html');
   }
   
   function ProcessRequest() {
      $idDec = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      $admisiObj = new AppKuotaWaktuKuliahToAdmisi();
      $siaObj = new AppKuotaWaktuKuliahToSia();

      if(!$siaObj->addConn)
         $return['siaConn']=false;
      else
         $return['siaConn']=true;
         
      $msg = Messenger::Instance()->Receive(__FILE__);
      $return['Pesan'] = $msg[0][1];
      $return['Data'] = $msg[0][0];

      $resSem = $siaObj->GetDataSemester();
      $resThnAjaran = $siaObj->GetDataTahunAjaran();
      $resProdi = $siaObj->GetDataProdi();
      $resWaktuKuliah = $siaObj->GetDataWaktuKuliah();
      $dataKuota = $admisiObj->GetDataKuotaWaktuKuliahById($idDec);
	  $semAktif = $siaObj->GetDataSemesterAktif();
	  $thnAjaran = $semAktif[0]['tahun_ajaran'];
	  $semester = $semAktif[0]['semester_id'];

      $return['decDataId'] = $idDec;
      $return['dataKuota'] = $dataKuota;
      $return['semester'] = $semester;
      $return['thnAjaran'] = $thnAjaran;
      $return['resSem'] = $resSem;
      $return['resThnAjaran'] = $resThnAjaran;
      $return['resProdi'] = $resProdi;
      $return['resWaktuKuliah'] = $resWaktuKuliah;
      return $return;
   
   }   

   function ParseTemplate($data = NULL) {
      $siaConn=$data['siaConn'];
      
      if($siaConn==false){
         $this->mrTemplate->AddVar('status_koneksi', 'BERHASIL', 'NO');
         $this->mrTemplate->SetAttribute('sia_conn', 'visibility', 'visible');
         $this->mrTemplate->AddVar('sia_conn', 'ISI_PESAN_SIA', 'Gagal Menghubungi SI Akademik');
      }else{
          $this->mrTemplate->AddVar('status_koneksi', 'BERHASIL', 'YES');
      }
      
      $url_kembali=Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html');
      $this->mrTemplate->AddVar('warning', 'URL_KEMBALI',$url_kembali );
      
      if ($data['Pesan']) {
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['Pesan']);
      }
      $dataKuota = $data['dataKuota'];
      
      if ($_REQUEST['dataId']=='') {
         $url="addKuotaWaktuKuliah";
         $tambah="Tambah";
      } else {
         $url="updateKuotaWaktuKuliah";
         $tambah="Ubah";
      }
	  
	  $resSem = $data['resSem'];
	  $resThnAjaran = $data['resThnAjaran'];
	  $resProdi = $data['resProdi'];
	  $resWaktuKuliah = $data['resWaktuKuliah'];
	  $semester = empty($dataKuota[0]['kuota_kelas_semester'])?$data['semester']:$dataKuota[0]['kuota_kelas_semester'];
	  $thnAjaran = empty($dataKuota[0]['tahun_ajaran'])?$data['thnAjaran']:$dataKuota[0]['tahun_ajaran'];
      $this->mrTemplate->AddVar('status_koneksi', 'JUDUL', $tambah);
	  $max = count($resSem);
	  for($i=0;$i<$max;$i++){
      $this->mrTemplate->AddVar('semester', 'SEMESTER', $resSem[$i]['semester_id']);
      $this->mrTemplate->AddVar('semester', 'SEMESTER_LABEL', $resSem[$i]['nama_semester']);
	  if($resSem[$i]['semester_id']==$semester)$this->mrTemplate->AddVar('semester', 'IS_SELECTED', 'selected');
	  else $this->mrTemplate->AddVar('semester', 'IS_SELECTED', '');	
	  $this->mrTemplate->parseTemplate('semester', 'a');  
	  }
	  
	  $max = count($resThnAjaran);
	  for($i=0;$i<$max;$i++){
      $this->mrTemplate->AddVar('thnAjaran', 'THN_AJARAN', $resThnAjaran[$i]['tahun_ajaran']);
      $this->mrTemplate->AddVar('thnAjaran', 'THN_AJARAN_LABEL', $resThnAjaran[$i]['tahun_ajaran']);
	  if($resThnAjaran[$i]['tahun_ajaran']==$thnAjaran)$this->mrTemplate->AddVar('thnAjaran', 'IS_SELECTED', 'selected');
	  else $this->mrTemplate->AddVar('thnAjaran', 'IS_SELECTED', '');
	  $this->mrTemplate->parseTemplate('thnAjaran', 'a');  
	  }
	  
	  $max = count($resProdi);
	  for($i=0;$i<$max;$i++){
      $this->mrTemplate->AddVar('prodi', 'PRODI_ID', $resProdi[$i]['prodiKode']);
      $this->mrTemplate->AddVar('prodi', 'PRODI_LABEL', $resProdi[$i]['prodiNamaResmi']);
	  if($resProdi[$i]['prodiKode']==$dataKuota[0]['kuota_kelas_prodi'])$this->mrTemplate->AddVar('prodi', 'IS_SELECTED', 'selected');
	  else $this->mrTemplate->AddVar('prodi', 'IS_SELECTED', '');	
	  $this->mrTemplate->parseTemplate('prodi', 'a');  
	  }
	  
	  $max = count($resWaktuKuliah);
	  for($i=0;$i<$max;$i++){
      $this->mrTemplate->AddVar('sesi_kuliah', 'SESI_KULIAH_ID', $resWaktuKuliah[$i]['wktkulId']);
      $this->mrTemplate->AddVar('sesi_kuliah', 'SESI_KULIAH_LABEL', $resWaktuKuliah[$i]['wktkulNama'].' ('.$resWaktuKuliah[$i]['wktkulKeterangan'].')');
	  if($resWaktuKuliah[$i]['wktkulId']==$dataKuota[0]['kuota_kelas_sesi_kuliah'])$this->mrTemplate->AddVar('sesi_kuliah', 'IS_SELECTED', 'selected');
	  else $this->mrTemplate->AddVar('sesi_kuliah', 'IS_SELECTED', '');	 
	  $this->mrTemplate->parseTemplate('sesi_kuliah', 'a'); 
	  }
	  	  
      $this->mrTemplate->AddVar('status_koneksi', 'KUOTA_MAX', $dataKuota[0]['kuota_kelas_jumlah']);
      $this->mrTemplate->AddVar('status_koneksi', 'URL_ACTION', Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', $url, 'do', 'html') . "&dataId=" . Dispatcher::Instance()->Encrypt($data['decDataId']));
      $this->mrTemplate->AddVar('status_koneksi', 'DATAID', Dispatcher::Instance()->Decrypt($_GET['dataId']));
      $this->mrTemplate->AddVar('status_koneksi', 'PAGE', Dispatcher::Instance()->Decrypt($_GET['page']));
   }
}
?>
