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

//set Label for module
require_once GTFWConfiguration::GetValue( 'application', 'docroot') .
   'module/label/response/Label.proc.class.php';

class ViewUsulanList extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue( 'application', 'docroot').
         'module/approval_usulan_barang/template');
      $this->SetTemplateFile('view_usulan_list.html');
   }
   
   function ProcessRequest()
   {
      $Obj = new Usulan();
      $tmp = $Obj->GetDataUserByUsername($_SESSION['username']);
      $return['roleId'] = $tmp['role_id'];
      
      // inisialisasi messaging
		$msg = Messenger::Instance()->Receive(__FILE__);
      $this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
      // ---------
      
      // inisialisasi filter
      $filter = array();
      
      if (isset($_POST['btncari'])) $filter = $_POST->AsArray();
      elseif (isset($_GET['page']) && is_array($this->Data)) $filter = $this->Data;
      
      Messenger::Instance()->Send('approval_usulan_barang', 'usulanList', 'view', 'html', array($filter), Messenger::UntilFetched);
      $return['filter'] = $filter;
      // ---------
      
      // render Combo Box
      $comboUnit = $Obj->GetComboUnit();
		
      if (count($comboUnit) === 1)
      {
         $filter['unit'] = $comboUnit[key($comboUnit)]['id'];
         Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('unit', $comboUnit, $filter['unit'], false, ''), Messenger::CurrentRequest);
      }
      else Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'unit', array('unit', $comboUnit, $filter['unit'], true, ''), Messenger::CurrentRequest);
      // ---------
      
		$comboStatus = array(array(id=>0, name=>'belum diproses'), array(id=>1, name=>'sudah sebagian'), array(id=>2, name=>'sudah diproses'));		
		
		Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'status', array('status', $comboStatus, $filter['status'], true, ''), Messenger::CurrentRequest);
      // Inisialisasi komponen paging
      $totalData = $Obj->GetDataGridCount($filter);
      $itemViewed = 10;
      if (isset($_GET['page'])) $currPage = $_GET['page']->Integer()->Raw();
      if (!isset($currPage) OR $currPage < 1) $currPage = 1;
      $startRec = ($currPage - 1) * $itemViewed;
      
      $url = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);

      Messenger::Instance()->SendToComponent('paging', 'paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
      $return['start'] = $startRec+1;
      // ---------

      // Generate data structure
      if ($totalData > 0) $return['dataGrid'] = $Obj->GetDataGrid($filter, $startRec, $itemViewed);
      else $return['dataGrid'] = array();
      // ---------
      //print_r($return['dataGrid']);exit;
      // inisialisasi url
      $return['url']['search'] = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
      $return['url']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html');
      $return['url']['add'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanInput', 'view', 'html');
      $return['url']['detail'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanDetail', 'view', 'html');
      $return['url']['approval'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanApproval', 'view', 'html');
      $return['url']['cetak'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanList', 'print', 'html');
		 $return['url']['rtf'] = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'rtfUsulanList', 'print', 'html');

      // ---------
      
      // Inisialisasi komponen confirmDelete
		$label = "Usulan Barang";
		$urlDelete = Dispatcher::Instance()->GetUrl('approval_usulan_barang', 'usulanDelete', 'do', 'html');
		$urlReturn = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, Dispatcher::Instance()->mSubModule, Dispatcher::Instance()->mAction, Dispatcher::Instance()->mType);
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
      // ---------
      
      return $return;
   }
   
   function TemplateLabeling(){
      $ObjLabel = new GetModuleLabel();
      $arrLabel = $ObjLabel->GetLabel();
      
      //patTemplate name yang memiliki label2 dinamic
      $arrContent=array("content");
      
      for($i=0;$i<count($arrContent);$i++){
         for($j=0;$j<count($arrLabel);$j++){
            $this->mrTemplate->AddVar($arrContent[$i],$arrLabel[$j]['labelCode'],$arrLabel[$j]['labelName']);
         }
      }
   }
   
   function ParseTemplate($data = NULL)
   {
      //set Label
      $this->TemplateLabeling();
      
      // Render message box
      if($this->Pesan)
      {
			$this->mrTemplate->SetAttribute('warning_box', 'visibility', 'visible');
			$this->mrTemplate->AddVar('warning_box', 'ISI_PESAN', $this->Pesan);
			$this->mrTemplate->AddVar('warning_box', 'CLASS_PESAN', $this->css);
		}
      // ---------
      
      // Render URL
      $this->mrTemplate->AddVars('content', $data['url'], 'URL_');
      $this->mrTemplate->AddVars('link_approval', $data['url'], 'URL_');
      $this->mrTemplate->SetAttribute('link_approval', 'visibility', 'visible');
      // ---------
      
      // Render Filter Box
      $this->mrTemplate->AddVars('content', $data['filter'], '');
      // ---------
      
      // Render data grid
		if (count($data['dataGrid']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}
		else $this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
      
		$i = $data['start'];
      $status[0] = 'belum diproses';
      $status[1] = 'sudah sebagian';
      $status[2] = 'sudah diproses';
      foreach ($data['dataGrid'] as $value)
      {
         $isProcessed = $value['status'];
			$value['nomor'] = $i;
         $value['status'] = $status[$value['status']];
			$value['class_name'] = ($i % 2) ? 'table-common-even' : '';
         $value['url_edit'] = $data['url']['add'] . "&id=". $value['id'];
         $value['url_detail'] = $data['url']['detail'] . "&id=". $value['id'];
         $value['tglUsulan'] = date('d F Y', strtotime($value['usulanBrgMstTglUsulan']));
         
         $this->mrTemplate->AddVars('data_item', $value, '');
         if (!$isProcessed OR $value['jumlahItem'] == 0)
         {
            $this->mrTemplate->AddVar('data_item_edit', 'URL_EDIT', $value['url_edit']);
            $this->mrTemplate->SetAttribute('data_item_edit', 'visibility', 'visible');
            $this->mrTemplate->parseTemplate('data_item', 'a');
            $this->mrTemplate->SetAttribute('data_item_edit', 'visibility', 'hidden');
         }
         else $this->mrTemplate->parseTemplate('data_item', 'a');
         
         $i++;
      }
      // ---------
   }
}
?>