<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/mahasiswa/business/AppMahasiswa.class.php';
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .'main/function/date.php';
class ViewMahasiswaDataInduk extends HtmlResponse{

   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/mahasiswa/template');
      $this->SetTemplateFile('view_mahasiswa_data_induk.html');
   }
   
   function ProcessRequest(){
      $mhsObj=new AppMahasiswa();
      if(isset($_POST['tahun_masuk'])){
         if((isset($_POST['tahun_masuk']))or(isset($_POST['notes_nim']))or(isset($_POST['is_calon']))or(isset($_POST['program_studi']))or(isset($_POST['nama']))) {
            $tahunMasuk=$_POST['tahun_masuk'];
            $semsMasuk=$_POST['sems_masuk'];
            $notes_nim=$_POST['notes_nim'];
            $isCalon=$_POST['is_calon'];
            $prodi=$_POST['program_studi'];
            $nama=$_POST['nama'];
         }elseif(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="";
         }
   
         $_SESSION['tahunMasuk']=$tahunMasuk->Raw();
         $_SESSION['semsMasuk']=$semsMasuk->Raw();
         $_SESSION['isCalon']=$isCalon->Raw();
         $_SESSION['notes_nim']=$notes_nim->Raw();
         $_SESSION['prodi']=$prodi->Raw();
         $_SESSION['nama']=$nama->Raw();
      }
      $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->GetCountMahasiswaDataInduk($tahunMasuk,$notes_nim,$isCalon,$prodi,$nama,$semsMasuk);
      $dataMhs=$mhsObj->GetMahasiswaDataInduk($tahunMasuk,$notes_nim,$isCalon,$prodi,$nama,$semsMasuk,$startRec,$itemViewed);
      $dataTahun=$mhsObj->GetTahunMasuk();
      $jumlahMhs=$mhsObj->GetTotalMahasiswa();

      

         $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__);

      $return['search']['tahunMasuk'] = $tahunMasuk;
      $return['search']['isCalon']=$isCalon;
      $return['search']['semsMasuk'] = $semsMasuk;
      $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['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'];
      $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('mahasiswa', 'mahasiswaDatainduk', 'view', 'html'));   
         
     $this->mrTemplate->AddVar('content', 'URL_DATA_RINGKAS', 
         Dispatcher::Instance()->GetUrl('mahasiswa', 'mahasiswa', 'view', 'html'));
         
      $this->mrTemplate->AddVar('content', 'URL_ADD', 
         Dispatcher::Instance()->GetUrl('mahasiswa', 'inputMahasiswa', 'view', 'html', true));
         
      if(GTFWConfiguration::GetValue( 'application', 'use_gtum')==true){
         $this->mrTemplate->SetAttribute('import_um', 'visibility', 'visible');
         $this->mrTemplate->AddVar('import_um', 'URL_IMPORT_UM', Dispatcher::Instance()->GetUrl('mahasiswa', 'importMahasiswaFromUm', 'view', 'html'));
      }else{
         $this->mrTemplate->SetAttribute('import_um', 'visibility', 'hidden');
      }
      
      $this->mrTemplate->AddVar('content', 'URL_IMPORT', 
         Dispatcher::Instance()->GetUrl('mahasiswa', 'importMahasiswa', 'view', 'html'));      
      
      $url_export = Dispatcher::Instance()->GetUrl('mahasiswa', 'daftarMahasiswaDataInduk', 'view', 'xls') . 
                  '&tahunMasuk=' . $search['tahunMasuk'] . '&semsMasuk=' . $search['semsMasuk'] . 
                  '&isCalon='. $search['isCalon']. '&notes_nim=' . $search['notes_nim'] . '&prodi=' . $search['program_studi'] . '&nama=' . $search['nama'];
      
      $this->mrTemplate->AddVar('content', 'URL_EXPORT', $url_export);
         
      $this->mrTemplate->AddVar('content', 'URL_PRINT', 
         Dispatcher::Instance()->GetUrl('mahasiswa', 'daftarMahasiswaDataInduk', 'print', 'html'));      
      
      $this->mrTemplate->AddVar('content', 'URL_PRINT_DETIL', 
         Dispatcher::Instance()->GetUrl('mahasiswa', 'detilDaftarMahasiswa', 'print', 'html'));
         

      $label = "Data Mahasiswa";
      
      $urlDelete = Dispatcher::Instance()->GetUrl('mahasiswa', 'deleteMahasiswa', 'do', 'html').'&cari='.$cari;
      $urlReturn = Dispatcher::Instance()->GetUrl('mahasiswa', 'mahasiswa', 'view', 'html').'&cari='.$cari;
      
      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'));     
         


      $this->mrTemplate->AddVar('content', $checked, 'checked');

      if($pesan) {
         $this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
         $this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $pesan);
         $this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $css);
      }

      if(empty($data['dataProdi'])){
         $this->mrTemplate->SetAttribute('empty_box', 'visibility', 'visible');
         $this->mrTemplate->SetAttribute('empty_periode', '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_mahasiswa', '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 = 'mahasiswa|deleteMahasiswa|do|html-cari-'.$cari;
            $urlReturn = 'mahasiswa|mahasiswa|view|html-cari-'.$cari;

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

            $dataMhs[$i]['url_detail'] = Dispatcher::Instance()->GetUrl('mahasiswa', 'detailMahasiswa', 'view', 'html') . 
               '&dataId=' . $idEnc . '&page=' . $encPage . '&cari='.$cari;            
            
            $dataMhs[$i]['url_print'] = Dispatcher::Instance()->GetUrl('mahasiswa', 'detailMahasiswa', '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]['mhs_tgl_lhr']=ConvertDate($dataMhs[$i]['mhs_tgl_lhr'],"YYYY-MM-DD","DD-MM-YYYY");          
            $this->mrTemplate->AddVars('data_mahasiswa_item', $dataMhs[$i], 'MHS_');
            $this->mrTemplate->parseTemplate('data_mahasiswa_item', 'a');    
         }
      }else{
         $this->mrTemplate->AddVar('data_mahasiswa', 'MHS_EMPTY', 'YES');
      }
   }
}
?>