<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/pendaftar/business/AppPendaftar.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'main/function/date.php';

class ViewPendaftar extends HtmlResponse{

	function TemplateModule() {		
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/pendaftar/template');
		$this->SetTemplateFile('view_pendaftar.html');
	}
	
   function ProcessRequest(){
        
      $mhsObj=new AppMahasiswa();
        
      if((isset($_POST['tahun_masuk']))or(isset($_POST['notes_nim']))or(isset($_POST['is_calon']))or(isset($_POST['program_studi']))or(isset($_POST['nama']))) {
         if(isset($_POST['tahun_masuk'])){
            $tahunMasuk=$_POST['tahun_masuk'];
            $semsMasuk=$_POST['sems_masuk'];
            $notes_nim=$_POST['notes_nim'];
            $isCalon=$_POST['is_calon'];
            $prodi=$_POST['program_studi'];
            $nama=$_POST['nama'];
         }
	  }
	  else
	  {
	  if(isset($_GET['tahun_masuk'])){
      
            $tahunMasuk = Dispatcher::Instance()->Decrypt($_GET['tahun_masuk']);
            $semsMasuk = Dispatcher::Instance()->Decrypt($_GET['sems_masuk']);
            $notes_nim=Dispatcher::Instance()->Decrypt($_GET['notes_nim']);
            $isCalon=Dispatcher::Instance()->Decrypt($_GET['is_calon']);
            $prodi=Dispatcher::Instance()->Decrypt($_GET['program_studi']);
            $nama=Dispatcher::Instance()->Decrypt($_GET['nama']);
            
         }else{
            $tahunMasuk="";
            $semsMasuk="";
            $notes_nim="";
            $isCalon="";
            $prodi="";
            $nama="";
         }
	   }
       
      $itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;

      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
			
		}
      
      if($prodi=='all'){
         $prodi='';
      }       
      
      if($isCalon=='all'){
         $isCalon='';
      }       
      
      if($tahunMasuk=='all'){
         $tahunMasuk='';
      }  
      
      if($semsMasuk=='all'){
         $semsMasuk='';
      }
      
      $totalData=$mhsObj->GetCountDataMahasiswa($tahunMasuk,$notes_nim,$isCalon,$prodi,$nama,$semsMasuk);
      //$dataMhs=$mhsObj->GetDataMahasiswa($tahunMasuk,$notes_nim,$isCalon,$prodi,$nama,$semsMasuk,$startRec,$itemViewed);
      $dataTahun=$mhsObj->GetTahunMasuk();
      $jumlahMhs=$mhsObj->GetTotalMahasiswa();
      
      $dataMhs = array(
              0=>array('number'=>1,'mhs_niu'=>'TL-21-03000','mhs_nama'=>'CARMIKO PRASMANA KAMAL','mhs_jkl'=>'L','mhs_prodi'=>'BEKASI','mhs_periode_masuk'=>'8/22/1992','mhs_pilihan_prodi'=>'NAUTIKA' ),
              1=>array('number'=>2,'mhs_niu'=>'TL-21-03001','mhs_nama'=>'KARTIKA ANINDYA ANUGRAHWATI','mhs_jkl'=>'P','mhs_prodi'=>'JAKARTA','mhs_periode_masuk'=>'12/1/1991','mhs_pilihan_prodi'=>'NAUTIKA'),
              2=>array('number'=>3,'mhs_niu'=>'TL-21-03002','mhs_nama'=>'DAVID EKA PUTRA','mhs_jkl'=>'L','mhs_prodi'=>'BEKASI','mhs_periode_masuk'=>'12/1/1992','mhs_pilihan_prodi'=>'NAUTIKA'),
              3=>array('number'=>4,'mhs_niu'=>'TL-21-03003','mhs_nama'=>'REIGI DIARMA RAHARJO','mhs_jkl'=>'L','mhs_prodi'=>'SUKABUMI','mhs_periode_masuk'=>'8/19/1992','mhs_pilihan_prodi'=>'NAUTIKA')
      );
      //print_r($dataJenisSmta);
      if(!empty($dataMhs)){
         $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
               Dispatcher::Instance()->mSubModule, 
               Dispatcher::Instance()->mAction, 
               Dispatcher::Instance()->mType . 
               '&tahun_masuk=' . Dispatcher::Instance()->Encrypt($tahunMasuk) . 
               '&sems_masuk=' . Dispatcher::Instance()->Encrypt($semsMasuk) . 
               '&notes_nim=' . Dispatcher::Instance()->Encrypt($notes_nim) . 
               '&program_studi=' . Dispatcher::Instance()->Encrypt($prodi) . 
               '&is_calon=' . Dispatcher::Instance()->Encrypt($isCalon) .
               '&nama=' . Dispatcher::Instance()->Encrypt($nama) . 
               '&cari=' . Dispatcher::Instance()->Encrypt(1));
               
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top',
            array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);   
         
      }
        
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun_masuk',
            array('tahun_masuk',$dataTahun,$tahunMasuk,'true',''), Messenger::CurrentRequest);     

      $dataSemester=$mhsObj->GetSemesterMasuk();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sems_masuk',
            array('sems_masuk',$dataSemester,$semsMasuk,'true',''), Messenger::CurrentRequest);
            
      $dataProdi=$mhsObj->GetDataProdi();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'program_studi',
            array('program_studi',$dataProdi,$prodi,'true',''), Messenger::CurrentRequest);
      
      $dataStatus=$mhsObj->GetStatusMahasiswa();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status_mahasiswa',
            array('is_calon',$dataStatus,$isCalon,'true',''), Messenger::CurrentRequest); 
            
                       
	   $msg = Messenger::Instance()->Receive(__FILE__);
//print_r($msg);
      $return['search']['tahunMasuk'] = $tahunMasuk;
      $return['search']['semsMasuk'] = $semsMasuk;
      $return['search']['isCalon']=$isCalon;
      $return['search']['notes_nim']=$notes_nim;
      $return['search']['program_studi']=$prodi;
      $return['search']['nama']=$nama;
   
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
		$return['tipePesan'] = $msg[0][3];
      
      $return['jumlahMhs']=$jumlahMhs;
		$return['dataMhs'] = $dataMhs;
      $return['dataTahun']=$dataTahun;
      $return['dataProdi']=$dataProdi;
		$return['start'] = $startRec+1;

      return $return;
   }

   function ParseTemplate($data=NULL){
		
      $search=$data['search'];
      $pesan=$data['pesan'];
      $tipePesan=$data['tipePesan'];
      $css=$data['css'];
      $dataMhs=$data['dataMhs'];
      $jumlahMhs=$data['jumlahMhs'];
      $startRec=$data['start'];
      
		
      $cari=$search['tahunMasuk'].'/'.
            $search['isCalon'].'/'.
            $search['notes_nim'].'/'.
            $search['program_studi'].'/'.
            $search['nama'];
      
      $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('pendaftar', 'pendaftar', 'view', 'html'));		
         
         
      $this->mrTemplate->AddVar('content', 'URL_EXPORT', 
         Dispatcher::Instance()->GetUrl('pendaftar', 'pendaftar', 'view', 'html'));             

      $label = "Data Pendaftar";
      
		$urlReturn = Dispatcher::Instance()->GetUrl('pendaftar', 'pendaftar', 'view', 'html').'&cari='.$cari;
      
	

      $this->mrTemplate->AddVar('content', $checked, 'checked');
              // print($tipePesan);
      if($pesan) {
         if($pesan['gagal']){

            $this->mrTemplate->AddVar('warning_box', 'URL_FAIL', Dispatcher::Instance()->GetUrl('pendaftar', 'hasilImportPendaftar', 'print', 'html').'&view=fail'); 
   			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'gagal');
   			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $pesan['gagal']);
   			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', 'notebox-warning');
   		}
   		if($pesan['berhasil']){
   			$this->mrTemplate->SetAttribute('done_box', 'visibility', 'visible');
   			$this->mrTemplate->AddVar('done_box', 'ISI_PESAN', $pesan['berhasil']);
   			$this->mrTemplate->AddVar('done_box', 'CLASS_PESAN', 'notebox-done');
         }
		}

      if(empty($data['dataProdi'])){
         $this->mrTemplate->SetAttribute('empty_box', 'visibility', 'visible');
         $this->mrTemplate->SetAttribute('empty_prodi', 'visibility', 'visible');
         $this->mrTemplate->AddVar('empty_prodi', 'ISI_PESAN_PRODI', 'Silahkan tambahkan Program Studi disini');
         $this->mrTemplate->AddVar('empty_prodi', 'URL_ADD', 
            Dispatcher::Instance()->GetUrl('program_studi', 'inputProgramStudi', 'view', 'html'));
      }
      if($jumlahMhs==0){
         $this->mrTemplate->SetAttribute('empty_box', 'visibility', 'visible');
         $this->mrTemplate->SetAttribute('empty_mhs', 'visibility', 'visible');
			$this->mrTemplate->AddVar('empty_mhs', 'ISI_PESAN_MHS', 'Silahkan tambahkan Mahasiswa disini');
         $this->mrTemplate->AddVar('empty_mhs', 'URL_ADD', 
            Dispatcher::Instance()->GetUrl('mahasiswa', 'inputMahasiswa', 'view', 'html'));
      }
            
      if(!empty($dataMhs)){
			$this->mrTemplate->AddVar('data_pendaftar', 'MHS_EMPTY', 'NO');

			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);

         for ($i=0; $i<sizeof($dataMhs); $i++) {

				$no = $i+$startRec;
				$dataMhs[$i]['number'] = $no;

				if ($no % 2 != 0) $dataMhs[$i]['class_name'] = 'table-common-even';
				else $dataMhs[$i]['class_name'] = '';	
			
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);		
		
				if($i == sizeof($dataMhs)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);


            $idEnc = Dispatcher::Instance()->Encrypt($dataMhs[$i]['mhs_id']);
            
			$urlAccept = 'pendaftar|deletePendaftar|do|html-cari-'.$cari;
            $urlReturn = 'pendaftar|pendaftar|view|html-cari-'.$cari;

			$dataMhs[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('pendaftar', 'inputPendaftar', 'view', 'html') . 
               '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;

            $dataMhs[$i]['url_detail'] = Dispatcher::Instance()->GetUrl('pendaftar', 'detailPendaftar', 'view', 'html') . 
               '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;            
            
            $dataMhs[$i]['url_print'] = Dispatcher::Instance()->GetUrl('pendaftar', 'detailPendaftar', 'view', 'html') . 
               '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;
     
            $dataMhs[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
               '&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataMhs[$i]['mhs_id'].
               '&label='.$label.'&dataName='.$dataMhs[$i]['mhs_nama'];
            
            $dataMhs[$i]['url_photo'] = Dispatcher::Instance()->GetUrl('pendaftar', 'InputPhotoPendaftar', 'view', 'html') . 
               '&dataId=' . $idEnc . '&dataIdMhs=' . $dataMhs[$i]['mhs_niu'] .'&page=' . $encPage . '&cari='.$cari;
               
            $dataMhs[$i]['mhs_thn_masuk']=$dataMhs[$i]['mhs_thn_masuk'].'/'.($dataMhs[$i]['mhs_thn_masuk']+1);
            
            $this->mrTemplate->AddVars('data_mahasiswa_item', $dataMhs[$i], 'MHS_');
				$this->mrTemplate->parseTemplate('data_mahasiswa_item', 'a');	 
			}
      }else{
			$this->mrTemplate->AddVar('data_pendaftar', 'MHS_EMPTY', 'YES');
      }
   }
}
?>