<?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 ViewKuotaWaktuKuliah extends HtmlResponse {

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').'module/kuota_waktu_kuliah/template');
      $this->SetTemplateFile('view_kuota_waktu_kuliah.html');
   }
   
   function ProcessRequest() {
      $admisiObj = new AppKuotaWaktuKuliahToAdmisi();
      $siaObj = new AppKuotaWaktuKuliahToSia();
      if($_POST || isset($_GET['cari'])) {
         if(isset($_POST['thnAjaran'])) {
            $thnAjaran = $_POST['thnAjaran'];
            $semester = $_POST['semester'];
            $prodi = $_POST['prodi'];
         } elseif(isset($_GET['thnAjaran'])) {
            $thnAjaran = Dispatcher::Instance()->Decrypt($_GET['thnAjaran']);
			$semester = Dispatcher::Instance()->Decrypt($_GET['semester']);
			$prodi = Dispatcher::Instance()->Decrypt($_GET['prodi']);
         } else {
			$semAktif = $siaObj->GetDataSemesterAktif();
            $thnAjaran = $semAktif[0]['tahun_ajaran'];
			$semester = $semAktif[0]['semester_id'];
			$prodi = '%';
         }
      }
	  //default pengambilan=semua prodi
      if(empty($prodi) or $prodi=='')$prodi = '%';
      $resSem = $siaObj->GetDataSemester();
      $resThnAjaran = $siaObj->GetDataTahunAjaran();
      $resProdi = $siaObj->GetDataProdi();
      $resWaktuKuliah = $siaObj->GetDataWaktuKuliah();
	  
   //view
      $totalData = $admisiObj->GetCountDataKuotaWaktuKuliah($thnAjaran,$semester,$prodi);
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
         $startRec =($currPage-1) * $itemViewed;
      }
      $dataKuotaAdmisi = $admisiObj->GetDataKuotaWaktuKuliah($thnAjaran,$semester,$prodi,$startRec,$itemViewed);
      //echo '<pre>'; print_r($dataKuotaAdmisi); exit;
      
	  $max = count($resProdi);
	  $tempProdi = array();
	  for($i=0;$i<$max;$i++)$tempProdi[$resProdi[$i]['prodiKode']] = $resProdi[$i]['prodiNamaResmi'];
	  $max = count($resWaktuKuliah);
	  $tempWaktuKuliah = array();
	  for($i=0;$i<$max;$i++)$tempWaktuKuliah[$resWaktuKuliah[$i]['wktkulId']] = $resWaktuKuliah[$i]['wktkulNama'].' ('.$resWaktuKuliah[$i]['wktkulKeterangan'].')';
	  
	  // get prodi kode from data admisi
	  $max = count($dataKuotaAdmisi);
	  $tempProdiKode = array();
	  for($i=0;$i<$max;$i++)$tempProdiKode[] = $dataKuotaAdmisi[$i]['kuota_kelas_prodi'];
	  $prodiKode4Sia = implode('\',\'',$tempProdiKode);
	  
      $dataKuotaSia = $siaObj->GetDataKuotaWaktuKuliah($prodiKode4Sia,$thnAjaran);      
      
	  // merge data from sia n admisi	  
	  for($i=0;$i<$max;$i++){
	  	
	  $dataKuotaAdmisi[$i]['kuota_kelas_prodi_nama'] = $tempProdi[$dataKuotaAdmisi[$i]['kuota_kelas_prodi']];	  
	  $dataKuotaAdmisi[$i]['kuota_waktu_kuliah_nama'] = $tempWaktuKuliah[$dataKuotaAdmisi[$i]['kuota_kelas_sesi_kuliah']];
	  
	  foreach($dataKuotaSia as $key => $data){
		  if($dataKuotaAdmisi[$i]['kuota_kelas_sesi_kuliah'] == $data['WKTID'] && $dataKuotaAdmisi[$i]['kuota_kelas_prodi'] == $data['PRODI_KODE']){
			  $dataKuotaAdmisi[$i]['kuota_kelas_jumlah_mahasiswa'] = $data['JML_MHS'];
			  unset($dataKuotaSia[$key]);
		  break;
	  	  }	  
      }
      	  
  	  }
  	    	  
  	  
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType . '&thnAjaran=' . Dispatcher::Instance()->Encrypt($thnAjaran) . '&semester=' . Dispatcher::Instance()->Encrypt($semester) . '&prodi=' . Dispatcher::Instance()->Encrypt($prodi) . '&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__);
      $return['Pesan'] = $msg[0][1];
      $return['css'] = $msg[0][2];

      $return['dataKuota'] = $dataKuotaAdmisi;
      $return['start'] = $startRec+1;

      $return['dataSemester'] = $resSem;
      $return['dataTahunAjaran'] = $resThnAjaran;
      $return['dataProdi'] = $resProdi;
      $return['search']['thnAjaran'] = $thnAjaran;
      $return['search']['semester'] = $semester;
      $return['search']['prodi'] = $prodi;
      return $return;
   }
   
   function ParseTemplate($data = NULL) {
      $search = $data['search'];
	  $resSem = &$data['dataSemester'];
	  $resThnAjaran = &$data['dataTahunAjaran'];
	  $resProdi = &$data['dataProdi'];
	  $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']==$search['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']==$search['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']==$search['prodi'])$this->mrTemplate->AddVar('prodi', 'IS_SELECTED', 'selected');
	  else $this->mrTemplate->AddVar('prodi', 'IS_SELECTED', '');
	  $this->mrTemplate->parseTemplate('prodi', 'a');
	  }
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'kuotaWaktuKuliah', 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', '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['dataKuota'])) {
         $this->mrTemplate->AddVar('data_kuota', 'KUOTA_EMPTY', 'YES');
      } else {
         $decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
         $encPage = Dispatcher::Instance()->Encrypt($decPage);
         $this->mrTemplate->AddVar('data_kuota', 'KUOTA_EMPTY', 'NO');
         $dataKuota = $data['dataKuota'];
      
//mulai bikin tombol delete
         $label = "Manajemen Kuota Waktu Kuliah";
         $urlDelete = Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'deleteKuotaWaktuKuliah', 'do', 'html');
         $urlReturn = Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'kuotaWaktuKuliah', '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($dataKuota); $i++) {
            $no = $i+$data['start'];
            $dataKuota[$i]['number'] = $no;
            if ($no % 2 != 0) $dataKuota[$i]['class_name'] = 'table-common-even';
            else $dataKuota[$i]['class_name'] = '';
            
            if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);            
            if($i == sizeof($dataKuota)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
            $idEnc = Dispatcher::Instance()->Encrypt($dataKuota[$i]['kuota_kelas_id']);
            $urlAccept = 'kuota_waktu_kuliah|deleteKuotaWaktuKuliah|do|html-cari-'.$cari;
            $urlReturn = 'kuota_waktu_kuliah|kuotaWaktuKuliah|view|html-cari-'.$cari;
            $dataKuota[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('kuota_waktu_kuliah', 'inputKuotaWaktuKuliah', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
            $dataKuota[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').'&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataKuota[$i]['kuota_kelas_id'].'&label='.$label.'&dataName='.$dataKuota[$i]['kuota_kelas_prodi_nama'].' - '.$dataKuota[$i]['kuota_waktu_kuliah_nama'];

            $this->mrTemplate->AddVars('data_kuota_item', $dataKuota[$i], 'KUOTA_');
            $this->mrTemplate->parseTemplate('data_kuota_item', 'a');    
         }
      }
   }
}
?>
