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


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

      $nimObj=new AppNimFormula();
      
      $msg = Messenger::Instance()->Receive(__FILE__);
		$return['Pesan'] = $msg[0][1];
      
      $idDec = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
      
      $nimKomponen=$nimObj->GetNimKomponen();
      $nimFormulaById=$nimObj->GetNimFormulaById($idDec);
       

      $return['nimFormulaById']=$nimFormulaById;
      $return['nimKomponen']=$nimKomponen;
      $return['dataId']=$idDec;
      return $return;
   }
   
   function ParseTemplate($data=null){
      $data['start']=1;
      $dataId=$data['dataId'];
      $nimKomponen=$data['nimKomponen'];
      $nimFormulaById=$data['nimFormulaById'];
      
      if ($data['Pesan']) {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $data['Pesan']);
		}
      
      $url_simpan=Dispatcher::Instance()->GetUrl('nim_formula','settingNimFormula','do','html');
      $url_return=Dispatcher::Instance()->GetUrl('nim_formula','nimFormula','view','html');
      
      
      if(empty($nimFormulaById[0]['formula'])){
         $formula=$_SESSION['formula'];
         
         $formulaEdit=explode('&',$formula);
         
         for($i=0;$i<sizeOf($formulaEdit);$i++){
            if($i==0){
               $formulaEdit[$i]="'".$formulaEdit[$i]."'";
            }else{
               $formulaEdit[$i]='&'.$formulaEdit[$i]."'";
            }
         }
         
         $indeks=sizeOf($formulaEdit);
         
         if(empty($indeks))
            $indeks=0;
            
         $formulaEdit=implode(", '",$formulaEdit);
         $formulaEditShow=str_replace('&','',$formulaEdit); 
         
         $formulaShow=str_replace('&','',$formula);
         
         $indeks='0';
      }else{
      
         $formula=$nimFormulaById[0]['formula'];

         $formulaEdit=explode('&',$formula);
         
         for($i=0;$i<sizeOf($formulaEdit);$i++){
            if($i==0){
               $formulaEdit[$i]="'".$formulaEdit[$i]."'";
            }else{
               $formulaEdit[$i]='&'.$formulaEdit[$i]."'";
            }
         }
         
         $indeks=sizeOf($formulaEdit);
         
         if(empty($indeks))
            $indeks=0;
         
         $formulaEdit=implode(", '",$formulaEdit);
         $formulaEditShow=str_replace('&','',$formulaEdit); 
         
         $formulaShow=str_replace('&','',$nimFormulaById[0]['formula']);   
      }
      $this->mrTemplate->AddVar('content', 'STR_FORMULA', $formulaEdit);
      $this->mrTemplate->AddVar('content', 'STR_FORMULA_SHOW', $formulaEditShow);
      $this->mrTemplate->AddVar('content', 'INDEKS',$indeks-1);     
      $this->mrTemplate->AddVar('content', 'FORMULA_SHOW', $formulaShow);
      $this->mrTemplate->AddVar('content', 'DATA_ID', $dataId);
      $this->mrTemplate->AddVar('content', 'FORMULA',$formula);    
      $this->mrTemplate->AddVar('content','URL_SIMPAN',$url_simpan);
      $this->mrTemplate->AddVar('content','URL_RETURN',$url_return);
      
      
      
      /*if($nimKomponen[9]['komp_nama']=="Separator")
         $separator=explode(",",$nimKomponen[9]['komp_value_format']);*/
      
      //unset($nimKomponen[9]);
      
      /*$nimKomponen[9]['komp_kode']=$separator[0];
      $nimKomponen[10]['komp_kode']=$separator[1];
      $nimKomponen[11]['komp_kode']=$separator[2];      
      $nimKomponen[9]['komp_nama']="Separator Titik";
      $nimKomponen[10]['komp_nama']="Separator Strip";
      $nimKomponen[11]['komp_nama']="Separator Garis Miring";*/


      if(!empty($nimKomponen)){
         for ($i=0;$i<sizeOf($nimKomponen);$i++){
            $no = $i+$data['start'];
            
            // jika komponen berupa separator
            if($nimKomponen[$i]['komp_nama']=="Separator") {
               $separator=explode(",",$nimKomponen[$i]['komp_value_format']);
               for ($j=0; $j<count($separator); $j++) {
                  if ($no % 2 != 0) $komponen_separator[$j]['class_name'] = 'table-common-even';
                  else $komponen_separator[$j]['class_name'] = '';

                  $komponen_separator[$j]['number'] = $no;
                  $komponen_separator[$j]['komp_kode'] = $separator[$j];
                  switch ($j) {
                     case 0: $komponen_separator[$j]['komp_nama'] = 'Separator Titik'; break;
                     case 1: $komponen_separator[$j]['komp_nama'] = 'Separator Strip'; break;
                     case 2: $komponen_separator[$j]['komp_nama'] = 'Separator Garis Miring'; break;
                  }
                  $this->mrTemplate->AddVars('data_komp_item', $komponen_separator[$j], 'KOMP_');
                  $this->mrTemplate->parseTemplate('data_komp_item', 'a');
                  
                  $no++;
               }
            }
            // end utk komponen separator
            
            $nimKomponen[$i]['number'] = $no;
            if ($no % 2 != 0) $nimKomponen[$i]['class_name'] = 'table-common-even';
            else $nimKomponen[$i]['class_name'] = '';
            
            if($i == 0) $this->mrTemplate->AddVar('content', 'FIRST_NUMBER', $no);				
            if($i == sizeof($nimKomponen)-1) $this->mrTemplate->AddVar('content', 'LAST_NUMBER', $no);
            
            //if($i<=8)
            if (!empty($nimKomponen[$i]['komp_kode'])) {
               $nimKomponen[$i]['komp_kode']='['.$nimKomponen[$i]['komp_kode'].']';
            
               $this->mrTemplate->AddVars('data_komp_item', $nimKomponen[$i], 'KOMP_');
               $this->mrTemplate->parseTemplate('data_komp_item', 'a');
            }
         }
      } 
   }
}
?>