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

class ViewPengadaanBarang extends HtmlResponse
{
   function TemplateModule()
   {
      $this->SetTemplateBasedir(GTFWConfiguration::GetValue('application','docroot').
         'module/pb_pengadaan_barang/template');
      $this->SetTemplateFile('view_pengadaan_barang.html');
   }
   
   function ProcessRequest()
   {
		$Obj = new PengadaanBarang();
		
		$msg = Messenger::Instance()->Receive(__FILE__);

		$this->Data = $msg[0][0];
		$this->Pesan = $msg[0][1];
		$this->css = $msg[0][2];
		
		$data = $_POST->AsArray();
		if(empty($data) AND empty($_GET['kode'])){
			$data['pengadaanSumberdanaId'] = 'All';
			$data['nomorPengadaan'] = '';
			$return['search'] = $data;
		}elseif(isset($data)){
			$return['search'] = $data;
		}elseif(isset($_GET['kode'])){
			$data['pengadaanSumberdanaId'] = $_GET['kode']->Raw();
			$data['nomorPengadaan'] = $_GET['nomor']->Raw();
			$return['search'] = $data;
		}
		
      //$comboTahun = $Obj->GetComboTahun(2000, date('Y'), false);
      //Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'tahun_pengadaan', array('pengadaanTahun', $comboTahun, $data['pengadaanTahun'], true, ''), Messenger::CurrentRequest);
		
		$comboSumberDana = $Obj->GetComboSumberDana();
      Messenger::Instance()->SendToComponent('combobox', 'Combobox', 'view', 'html', 'sumber_dana', array('pengadaanSumberdanaId', $comboSumberDana, $data['pengadaanSumberdanaId'], true, ''), Messenger::CurrentRequest);
		
		$label = "Pengadaan Barang Inventaris";
		$urlDelete = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarangDelete', 'do', 'json');
		$urlReturn = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html');
		Messenger::Instance()->Send('confirm', 'confirmDelete', 'do', 'html', array($label, $urlDelete, $urlReturn),Messenger::NextRequest);
		
		$totalData = $Obj->GetDataPengadaanBarangCount($data);
		$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, Dispatcher::Instance()->mType . '&nomor=' . Dispatcher::Instance()->Encrypt($data['nomorPengadaan']). '&kode=' . Dispatcher::Instance()->Encrypt($data['pengadaanSumberdanaId']).'&cari=' . Dispatcher::Instance()->Encrypt(1));
		Messenger::Instance()->SendToComponent('paging', 'Paging', 'view', 'html', 'paging_top', array($itemViewed, $totalData, $url, $currPage), Messenger::CurrentRequest);
		
		if(isset($totalData)){
			$result = $Obj->GetDataPengadaanBarang($data, $startRec, $itemViewed);
		}
		
		// set url here
      $return['url']['search'] = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html');
      $return['url']['delete'] = Dispatcher::Instance()->GetUrl('confirm', 'confirmDelete', 'do', 'html');
      $return['url']['add'] = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarangInput', 'view', 'html');
      $return['url']['detail'] = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarangDetil', 'popup', 'html');
		
		$return['start'] = $startRec+1;
		$return['data'] = $result;		
		return $return;
   }
   
   function ParseTemplate($data = NULL)
   {
		// parse message here
		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);
		}
		
      // parse url here
		$this->mrTemplate->AddVars('content', $data['url'], 'URL_');
		
		// parse search here
		$this->mrTemplate->AddVars('content', $data['search'], '');
		
		if (count($data['data']) === 0)
		{
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'YES');
			return;
		}	else {
			$this->mrTemplate->AddVar('data', 'DATA_EMPTY', 'NO');
			
			$i = $data['start'];
			foreach ($data['data'] as $value)
			{
				$value['nomor'] = $i;
				$value['class_name'] = ($i % 2) ? 'table-common-even' : '';
				
				$value['url_detail'] = $data['url']['detail'] . "&id=". $value['pengadaanId'];
				$value['url_edit'] = $data['url']['add'] . "&id=". $value['pengadaanId'];
				
				$this->mrTemplate->AddVars('data_item', $value, '');
				$this->mrTemplate->parseTemplate('data_item', 'a');
								
				$i++;
			}
      }
   }
}
?>