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

class ViewJurusan extends HtmlResponse{
   function TemplateModule() {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/jurusan/template');
      $this->SetTemplateFile('view_jurusan.html');
   }
   function ProcessRequest() {
      $jurObj = new AppJurusanToAdmisi();
      if($_POST || isset($_GET['cari'])) {
         if((isset($_POST['jur_kode']))or(isset($_POST['jur_fak_kode']))or(isset($_POST['jur_nama_resmi']))or(isset($_POST['jur_nama_singkat']))){
            $jurKode = $_POST['jur_kode'];
            $jurFakKode=$_POST['jur_fak_kode'];
            $jurNamaResmi=$_POST['jur_nama_resmi'];
            $jurNamaSingkat=$_POST['jur_nama_singkat'];
         } elseif((isset($_GET['jur_kode']))or(isset($_GET['jur_fak_kode']))or(isset($_GET['jur_nama_resmi']))or(isset($_GET['jur_nama_singkat']))) {
            $jurKode = Dispatcher::Instance()->Decrypt($_GET['jur_kode']);
            $jurFakKode=Dispatcher::Instance()->Decrypt($_GET['jur_fak_kode']);
            $jurNamaResmi=Dispatcher::Instance()->Decrypt($_GET['jur_nama_resmi']);
            $jurNamaSingkat=Dispatcher::Instance()->Decrypt($_GET['jur_nama_singkat']);
         } else {
            $jurKode = '';
            $jurFakKode='';
            $jurNamaResmi='';
            $jurNamaSingkat='';
         }
      }
      
      if($jurFakKode=='all'){
         $jurFakKode='';
      }
      
      
      $totalData = $jurObj->GetCountDataJurusan($jurKode,$jurFakKode,$jurNamaResmi,$jurNamaSingkat);
      $itemViewed = 20;
      $currPage = 1;
      $startRec = 0 ;
      if(isset($_GET['page'])) {
         $currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
         $startRec =($currPage-1) * $itemViewed;
      }
      $dataJurusan=$jurObj->GetDataJurusan($jurKode,
                                           $jurFakKode,
                                           $jurNamaResmi,
                                           $jurNamaSingkat,
                                           $startRec,
                                           $itemViewed);
                                       
      $dataFakultas=$jurObj->GetDataFakultas();                      
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'fakultas',
            array('jur_fak_kode',$dataFakultas,$jurFakKode,'true',''), Messenger::CurrentRequest);
            
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
             Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, 
             Dispatcher::Instance()->mType . 
             '&jur_kode=' . Dispatcher::Instance()->Encrypt($jurKode) . 
             '&jur_fak_kode=' . Dispatcher::Instance()->Encrypt($jurFakKode) . 
             '&jur_nama_resmi=' . Dispatcher::Instance()->Encrypt($jurNamaResmi) . 
             '&jur_nama_singkat=' . Dispatcher::Instance()->Encrypt($jurNamaSingkat) . 
             '&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['dataJurusan'] = $dataJurusan;
      $return['start'] = $startRec+1;
      
      $return['search']['jur_kode'] = $jurKode;
      $return['search']['jur_fak_kode']=$jurFakKode;
      $return['search']['jur_nama_resmi']=$jurNamaResmi;
      $return['search']['jur_nama_singkat']=$jurNamaSingkat;
      return $return;
   }

   function ParseTemplate($data = NULL) {
      $search = $data['search'];
      $this->mrTemplate->AddVar('content', 'JUR_KODE', $search['jur_kode']);
      $this->mrTemplate->AddVar('content', 'JUR_FAK_KODE', $search['jur_fak_kode']);
      $this->mrTemplate->AddVar('content', 'JUR_NAMA_RESMI', $search['jur_nama_resmi']);
      $this->mrTemplate->AddVar('content', 'JUR_NAMA_SINGKAT', $search['jur_nama_singkat']);
      $this->mrTemplate->AddVar('content', 'URL_SEARCH', Dispatcher::Instance()->GetUrl('jurusan', 'jurusan', 'view', 'html'));
      $this->mrTemplate->AddVar('content', 'URL_ADD', Dispatcher::Instance()->GetUrl('jurusan', 'inputJurusan', '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['dataJurusan'])) {
         $this->mrTemplate->AddVar('data_jurusan', 'JURUSAN_EMPTY', 'YES');
      } else {
         $decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
         $encPage = Dispatcher::Instance()->Encrypt($decPage);
         $this->mrTemplate->AddVar('data_jurusan', 'JURUSAN_EMPTY', 'NO');
         $dataJurusan = $data['dataJurusan'];
        
         $label = "Data Jurusan";
         $urlDelete = Dispatcher::Instance()->GetUrl('jurusan', 'deleteJurusan', 'do', 'html');
         $urlReturn = Dispatcher::Instance()->GetUrl('jurusan', 'jurusan', '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($dataJurusan); $i++) {
            $no = $i+$data['start'];
            $dataJurusan[$i]['number'] = $no;
            
            if ($no % 2 != 0) $dataJurusan[$i]['class_name'] = 'table-common-even';
            else $dataJurusan[$i]['class_name'] = '';
            
            if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);   
            
            if($i == sizeof($dataJurusan)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
            
            $idEnc = Dispatcher::Instance()->Encrypt($dataJurusan[$i]['jur_kode']);
            
            $urlAccept = 'jurusan|deleteJurusan|do|html-cari-'.$cari;
            $urlReturn = 'jurusan|jurusan|view|html-cari-'.$cari;
            
            $dataJurusan[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('jurusan', 'inputJurusan', 'view', 'html') . 
                                          '&dataId=' . $idEnc . 
                                          '&page=' . $encPage . 
                                          '&cari='.$cari;
            
            $dataJurusan[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
                                             '&urlDelete='. $urlAccept.
                                             '&urlReturn='.$urlReturn.
                                             '&id='.$dataJurusan[$i]['jur_kode'].
                                             '&label='.$label.
                                             '&dataName='.$dataJurusan[$i]['jur_nama_resmi'].
                                             ' ('.$dataJurusan[$i]['jur_nama_singkat'].')';   
                                             
            $this->mrTemplate->AddVars('data_jurusan_item', $dataJurusan[$i], 'JUR_');
            $this->mrTemplate->parseTemplate('data_jurusan_item', 'a');    
         }
      }
   }
}
?>
