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

class ViewNimFormula extends HtmlResponse {
   function TemplateModule() {
		$this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').'module/nim_formula/template');
		$this->SetTemplateFile('view_nim_formula.html');
	}
   
   function ProcessRequest(){
      unset($_SESSION['formula']);
      $nimObj=new AppNimFormula();
      $msg = Messenger::Instance()->Receive(__FILE__);
 
      $dataNimFormula=$nimObj->GetNimFormula();
      $totalData=$nimObj->GetCountNimFormula();
      
      $itemViewed = 20;
		$currPage = 1;
		$startRec = 0 ;
      
      if(isset($_GET['page'])) {
			$currPage = (string)$_GET['page']->StripHtmlTags()->SqlString()->Raw();
			$startRec =($currPage-1) * $itemViewed;
		}
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 
         Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, 
         '&cari=' . Dispatcher::Instance()->Encrypt(1));
      
      Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', 
         array($itemViewed,$totalData, $url, $currPage), Messenger::CurrentRequest);
      
      $return['pesan'] = $msg[0][1];
		$return['css'] = $msg[0][2];
      $return['dataNimFormula']=$dataNimFormula;
      $return['start'] = $startRec+1;
      return $return;
   }
   
   function ParseTemplate($data=null){
   
      $dataNimFormula=$data['dataNimFormula'];
       
      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']);
		}
      
      $url_add=Dispatcher::Instance()->GetUrl('nim_formula','settingNimFormula', 'view', 'html');
      $this->mrTemplate->AddVar('content', 'URL_ADD', $url_add);
      
      if (empty($dataNimFormula)) {
			$this->mrTemplate->AddVar('data_formula', 'FORMULA_EMPTY', 'YES');
		} else {
			$decPage = Dispatcher::Instance()->Decrypt($_REQUEST['page']);
			$encPage = Dispatcher::Instance()->Encrypt($decPage);
         
			$this->mrTemplate->AddVar('data_formula', 'FORMULA_EMPTY', 'NO');

        
         $label = "Data Nim Formula";
         
			$urlDelete = Dispatcher::Instance()->GetUrl('nim_formula', 'deleteNimFormula', 'do', 'html');
			$urlReturn = Dispatcher::Instance()->GetUrl('nim_formula', 'nimFormula', '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($dataNimFormula); $i++) {
         
				$no = $i+$data['start'];
				$dataNimFormula[$i]['number'] = $no;
            
				if ($no % 2 != 0) $dataNimFormula[$i]['class_name'] = 'table-common-even';
				else $dataNimFormula[$i]['class_name'] = '';
				
				if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
				if($i == sizeof($dataNimFormula)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
				
            $dataNimFormula[$i]['formula'] =str_replace('&','',$dataNimFormula[$i]['formula']);
            
            $idEnc = Dispatcher::Instance()->Encrypt($dataNimFormula[$i]['formula_id']);
           
            $url_aktif = Dispatcher::Instance()->GetUrl('nim_formula', 
               'aktifasiNimFormula', 'Do', 'html') . '&dataId=' . $idEnc ;
            
            $this->mrTemplate->AddVar('nim_aktif', 'FORMULA_URL_AKTIF', $url_aktif);    
            
            if($dataNimFormula[$i]['formula_aktif']=='ya')
               $this->mrTemplate->SetAttribute('nim_aktif', 'visibility', 'hidden');
            else
            	$this->mrTemplate->SetAttribute('nim_aktif', 'visibility', 'visible');
               
            $urlAccept = 'nim_formula|deleteNimFormula|do|html';
            $urlReturn = 'nim_formula|nimFormula|view|html-cari-'.$cari;
            
				$dataNimFormula[$i]['url_edit'] = Dispatcher::Instance()->GetUrl('nim_formula', 
               'settingNimFormula', 'view', 'html') . '&dataId=' . $idEnc . '&page=' . $encPage ;				
 
            $dataNimFormula[$i]['url_delete'] =  Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html').
               '&urlDelete='. $urlAccept.'&urlReturn='.$urlReturn.'&id='.$dataNimFormula[$i]['formula_id'].'&label='.$label.'&dataName='.$dataNimFormula[$i]['formula'];     
				
            $this->mrTemplate->AddVars('data_formula_item', $dataNimFormula[$i], 'FORMULA_');
				$this->mrTemplate->parseTemplate('data_formula_item', 'a');	 
			}
		}
   }
}
?>