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

class ViewCetakDokumen extends HtmlResponse{
	function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/cetak_dokumen/template');
		$this->SetTemplateFile('view_cetak_dokumen.html');
	}
   
   function ProcessRequest(){
   

      $show=false;
      if(isset($_POST['btnreset'])){
         $periode['tahunMasuk']="";
         $periode['semsMasuk']="";
         $notes_nim="";
         $status="";
         $prodi="";
         $nama="";
         $show=false;
      }else{
         if((isset($_POST['tahun_masuk_awal']))or(isset($_POST['notes_nim']))or(isset($_POST['is_calon']))or(isset($_POST['program_studi']))or(isset($_POST['nama']))) {
               $tahunMasukAwal=$_POST['tahun_masuk_awal'];
               $tahunMasukAkhir=$_POST['tahun_masuk_akhir'];
               $semsMasukAwal=$_POST['sems_masuk_awal'];
               $semsMasukAkhir=$_POST['sems_masuk_akhir'];
               $fakultas=$_POST['fakultas'];
               $notes_nim=$_POST['notes_nim'];
               $status=$_POST['is_calon'];
               $prodi=$_POST['program_studi'];
               $nama=$_POST['nama'];
               $show=true;
            }elseif((isset($_GET['tahun_masuk'])) or (isset($_GET['notes_nim']))){
            
               $tahunMasukAwal = Dispatcher::Instance()->Decrypt($_GET['tahun_masuk_awal']);
               $tahunMasukAkhir = Dispatcher::Instance()->Decrypt($_GET['tahun_masuk_akhir']);
               $semsMasukAwal = Dispatcher::Instance()->Decrypt($_GET['sems_masuk_awal']);
               $semsMasukAkhir = Dispatcher::Instance()->Decrypt($_GET['sems_masuk_akhir']);
               $notes_nim=Dispatcher::Instance()->Decrypt($_GET['notes_nim']);
               $fakultas=Dispatcher::Instance()->Decrypt($_GET['fakultas']);
               $status=Dispatcher::Instance()->Decrypt($_GET['is_calon']);
               $prodi=Dispatcher::Instance()->Decrypt($_GET['program_studi']);
               $nama=Dispatcher::Instance()->Decrypt($_GET['nama']);
               $show=true;
            }
         }
      #print($show);
       
      #print_r($_POST);
      if($prodi=='all'){
         $prodi='';
      }       
      if($fakultas=='all'){
         $fakultas='';
      }       
      if($status=='all'){
         $status='';
      }      
      
      if($tahunMasukAwal=='all'){
         $tahunMasukAwal='';
      }  
      
      if($tahunMasukAkhir=='all'){
         $tahunMasukAkhir='';
      }  
         
      if($semsMasukAwal=='all'){
         $semsMasukAwal='';
      }

      if($semsMasukAkhir=='all'){
         $semsMasukAkhir='';
      } 
      
      $cetakObj=new AppCetakDokumen();
      
      $itemViewed = 10;
		$currPageWithNim = 1;
		$currPageWithoutNim = 1;
		$startRecWithNim = 0 ;
      $startRecWithoutNim=0;
      
      if(isset($_GET['page'])) {
      
         if($_GET['nim']=="1"){
            $currPageWithNim = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
            $startRecWithNim =($currPageWithNim-1) * $itemViewed;
            $show=true;
         }elseif($_GET['nim']=="0"){
            $currPageWithoutNim = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
            $startRecWithoutNim =($currPageWithoutNim-1) * $itemViewed;      
            $show=true;
         }
                        
		}
      
      $url_return=Dispatcher::Instance()->GetUrl('cetak_dokumen', 'cetakDokumen', 'view', 'html');
      
      $field='periode-prodi-nim-nama';
      $periode['tahun']['awal']=$tahunMasukAwal;
      $periode['tahun']['akhir']=$tahunMasukAkhir;
      $periode['semester']['awal']=$semsMasukAwal;
      $periode['semester']['akhir']=$semsMasukAkhir;
      $prodiFak['prodi']=$prodi;
      $prodiFak['fakProdi']=$fakultas;
     // print_r($periode);
      
      $param=array($periode,$prodiFak,$notes_nim,$nama,$status);
      
      Messenger::Instance()->SendToComponent('pencarian_mahasiswa', 'pencarianMahasiswa', 'view', 'html', 'pencarian', 
         array($url_return,$field,$reset=true,$param), Messenger::CurrentRequest);
      
      $totalDataWithNim=$cetakObj->GetCountDataMahasiswaWithNim($tahunMasukAwal,
                                                               $tahunMasukAkhir,
                                                               $notes_nim,
                                                               $status,
                                                               $prodi,
                                                               $nama,
                                                               $semsMasukAwal,
                                                               $semsMasukAkhir);
      
      $totalDataWithoutNim=$cetakObj->GetCountDataMahasiswaWithoutNim($tahunMasukAwal,
                                                                     $tahunMasukAkhir,
                                                                     $notes_nim,
                                                                     $status,
                                                                     $prodi,
                                                                     $nama,
                                                                     $semsMasukAwal,
                                                                     $semsMasukAkhir);
                        
                                                   
      $dataMhsWithNim=$cetakObj->GetDataMahasiswaWithNim($tahunMasukAwal,
                                                         $tahunMasukAkhir,
                                                         $notes_nim,
                                                         $status,
                                                         $prodi,
                                                         $nama,
                                                         $semsMasukAwal,
                                                         $semsMasukAkhir,
                                                         $startRecWithNim,
                                                         $itemViewed);
    
      $dataMhsWithoutNim=$cetakObj->GetDataMahasiswaWithoutNim($tahunMasukAwal,
                                                               $tahunMasukAkhir,
                                                               $notes_nim,
                                                               $status,
                                                               $prodi,
                                                               $nama,
                                                               $semsMasukAwal,
                                                               $semsMasukAkhir,
                                                               $startRecWithNim,
                                                               $itemViewed);

      $templateCetak=$cetakObj->GetTemplateCetak();
      $totalProdi=$cetakObj->GetTotalProdi();
      $totalMhs=$cetakObj->GetTotalMahasiswa();
      $totalMhs=$cetakObj->GetTotalMahasiswa();
      $totalTplAktif=$cetakObj->GetTotalTemplateAktif();

      if(!empty($dataMhsWithoutNim)){
         $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
               Dispatcher::Instance()->mSubModule, 
               Dispatcher::Instance()->mAction, 
               Dispatcher::Instance()->mType . 
               '&nim=' . 0 . 
               '&tahun_masuk_awal='.$tahunMasukAwal.
               '&sems_masuk_awal='.$semsMasukAwal.
               '&tahun_masuk_akhir='.$tahunMasukAkhir.
               '&sems_masuk_akhir='.$semsMasukAkhir.
               '&notes_nim='.$notes_nim.
               '&is_calon='.$status.
               '&program_studi='.$prodi.
               '&nama='.$nama.
               '&cari=' . Dispatcher::Instance()->Encrypt(1));
         
         Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_without_nim',
            array($itemViewed,$totalDataWithoutNim, $url, $currPageWithoutNim), 
            Messenger::CurrentRequest);
      }      
      
      if(!empty($dataMhsWithNim)){
         $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
               Dispatcher::Instance()->mSubModule, 
               Dispatcher::Instance()->mAction, 
               Dispatcher::Instance()->mType . 
               '&nim=' . 1 . 
               '&tahun_masuk='.$periode['tahunMasuk'].
               '&sems_masuk='.$periode['semsMasuk'].
               '&notes_nim='.$notes_nim.
               '&is_calon='.$status.
               '&program_studi='.$prodi.
               '&nama='.$nama.
               '&cari=' . Dispatcher::Instance()->Encrypt(1));
         
         Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_with_nim',
            array($itemViewed,$totalDataWithNim, $url, $currPageWithNim), 
            Messenger::CurrentRequest);
      }
      
      
      $return['templateCetak']=$templateCetak;
      $return['startWithoutNim'] = $startRecWithoutNim+1;
      $return['startWithNim'] = $startRecWithNim+1;
      $return['show']=$show;
      $return['dataMhsWithNim']=$dataMhsWithNim;
      $return['dataMhsWithoutNim']=$dataMhsWithoutNim;
      $return['totalTplAktif']=$totalTplAktif;
      $return['totalDataWithNim']=$totalDataWithNim;
      $return['totalDataWithoutNim']=$totalDataWithoutNim;
      $return['totalMhs']=$totalMhs;
      $return['totalProdi']=$totalProdi;
      return $return;
   }
   
   function Parsetemplate($data=null){
      $dataMhsWithNim=$data['dataMhsWithNim'];
      $dataMhsWithoutNim=$data['dataMhsWithoutNim'];
      $totalDataWithNim=$data['totalDataWithNim'];
      $totalDataWithoutNim=$data['totalDataWithoutNim'];
      $show=$data['show'];
      $startRecWithNim=$data['startWithNim'];
      $startRecWithoutNim=$data['startWithoutNim'];
      $templateCetak=$data['templateCetak'];
      $totalMhs=$data['totalMhs'];
      $totalProdi=$data['totalProdi'];
      $totalTplAktif=$data['totalTplAktif'];
      
      if($totalProdi==0){
         $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', 'programStudi', 'view', 'html'));
      }
      
      if($totalMhs==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', 'mahasiswa', 'view', 'html'));
      }     
      
      if($totalTplAktif==0){
         $this->mrTemplate->SetAttribute('empty_box', 'visibility', 'visible');
         $this->mrTemplate->SetAttribute('tpl_is_aktif', 'visibility', 'visible');
			$this->mrTemplate->AddVar('tpl_is_aktif', 'ISI_PESAN_MHS', 'Silahkan Aktifkan Template disini');
         $this->mrTemplate->AddVar('tpl_is_aktif', 'URL_TPL', 
            Dispatcher::Instance()->GetUrl('template_cetak', 'templateCetak', 'view', 'html'));
      }
      
      $totalData=$totalDataWithNim + $totalDataWithoutNim;

      if(empty($dataMhsWithNim)){
        $this->mrTemplate->AddVar('data_mahasiswa_with_nim', 'DATA_WITH_NIM_EMPTY', 'YES');
      }else{
         $this->mrTemplate->AddVar('data_mahasiswa_with_nim', 'DATA_WITH_NIM_EMPTY', 'NO');
      }      
      if(empty($dataMhsWithoutNim)){
        $this->mrTemplate->AddVar('data_mahasiswa_without_nim', 'DATA_WITHOUT_NIM_EMPTY', 'YES');
      }else{
         $this->mrTemplate->AddVar('data_mahasiswa_without_nim', 'DATA_WITHOUT_NIM_EMPTY', 'NO');
      }
      
      if(($totalData==1) and ($show==true)){
      
         $this->mrTemplate->AddVar('data_mahasiswa', 'MHS_SATU', 'YES');
         if ($totalDataWithNim==1){
            foreach($dataMhsWithNim as $dataNim){
               $this->mrTemplate->AddVars('data_detail', $dataNim, 'MHS_');
               $this->mrTemplate->parseTemplate('data_detail', 'a');
            }
         $idMhs = Dispatcher::Instance()->Encrypt($dataMhsWithNim[0]['mhs_id']);
         }else{
            foreach($dataMhsWithoutNim as $dataWithoutNim){
            
               $this->mrTemplate->AddVars('data_detail', $dataWithoutNim, 'MHS_');
               $this->mrTemplate->parseTemplate('data_detail', 'a');
            }         
            
         $idMhs = Dispatcher::Instance()->Encrypt($dataMhsWithoutNim[0]['mhs_id']);
         }
         

         $this->mrTemplate->AddVar('data_mahasiswa', 'MHS_SATU', 'YES');
         if (!empty($templateCetak))
            $this->mrTemplate->SetAttribute('toolbar', 'visibility', 'visible');
         else
            $this->mrTemplate->SetAttribute('toolbar', 'visibility', 'hidden');
         

         foreach($templateCetak as $dataTpl){
            $dataTpl['url_action']=Dispatcher::Instance()->GetUrl('cetak_dokumen', 'cetakDokumen', 'print', 'html') . 
               '&idMhs=' . $idMhs . '&idTpl=' . $dataTpl['tpl_id'];
             
            $dir = dirname(realpath($_SERVER['SCRIPT_FILENAME']));             
            $file = $dir . "/file/template_cetak/".$dataTpl['tpl_path'];

            if(!file_exists($file)){


               $this->mrTemplate->SetAttribute('tpl_not_found', 'visibility', 'visible');
               
               $this->mrTemplate->AddVars('tpl_not_found_item', $dataTpl);
               $this->mrTemplate->parseTemplate('tpl_not_found_item','a'); 
            }else{
               $this->mrTemplate->AddVars('template_cetak', $dataTpl);
               $this->mrTemplate->parseTemplate('template_cetak','a');
            }
         }
         
      }elseif(($totalData > 1)and($show==true)){
      
         $this->mrTemplate->AddVar('data_mahasiswa', 'MHS_SATU', 'NO');
         
         for ($i=0; $i<sizeof($dataMhsWithNim); $i++) {

				$no = $i+$startRecWithNim;
				$dataMhsWithNim[$i]['number'] = $no;

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


            $nim = Dispatcher::Instance()->Encrypt($dataMhsWithNim[$i]['mhs_niu']);
            $notes= Dispatcher::Instance()->Encrypt($dataMhsWithNim[$i]['mhs_no_test']);
            $nama=Dispatcher::Instance()->Encrypt($dataMhsWithNim[$i]['mhs_nama']);
            
            $dataId=empty($nim)?$notes:$nim;

            $dataMhsWithNim[$i]['url_pilih'] =  Dispatcher::Instance()->GetUrl('cetak_dokumen', 'cetakDokumen', 'view', 'html') . 
               '&notes_nim=' . $dataId . '&nama=' . $nama . '&page=1&cari='.$cari;
               
            $dataMhs[$i]['mhs_thn_masuk']=$dataMhsWithNim[$i]['mhs_thn_masuk'].'/'.($dataMhsWithNim[$i]['mhs_thn_masuk']+1);
            
            
            $this->mrTemplate->AddVars('data_mahasiswa_item_with_nim', $dataMhsWithNim[$i], 'MHS_');
				$this->mrTemplate->parseTemplate('data_mahasiswa_item_with_nim', 'a');	 
			}
         
         for ($i=0; $i<sizeof($dataMhsWithoutNim); $i++) {

				$no = $i+$startRecWithoutNim;
				$dataMhsWithoutNim[$i]['number'] = $no;

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


            $nim = Dispatcher::Instance()->Encrypt($dataMhsWithoutNim[$i]['mhs_niu']);
            $notes= Dispatcher::Instance()->Encrypt($dataMhsWithoutNim[$i]['mhs_no_test']);
            $nama=Dispatcher::Instance()->Encrypt($dataMhsWithoutNim[$i]['mhs_nama']);
            
            $dataId=empty($nim)?$notes:$nim;

            $dataMhsWithoutNim[$i]['url_pilih'] =  Dispatcher::Instance()->GetUrl('cetak_dokumen', 'cetakDokumen', 'view', 'html') . 
               '&notes_nim=' . $dataId . '&nama=' . $nama . '&page=1&cari='.$cari;
               
            $dataMhs[$i]['mhs_thn_masuk']=$dataMhsWithoutNim[$i]['mhs_thn_masuk'].'/'.($dataMhsWithoutNim[$i]['mhs_thn_masuk']+1);
            
            
            $this->mrTemplate->AddVars('data_mahasiswa_item_without_nim', $dataMhsWithoutNim[$i], 'MHS_');
				$this->mrTemplate->parseTemplate('data_mahasiswa_item_without_nim', 'a');	 
			}
      }
   
   }

}
?>