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

class ProcessPengadaanBarang
{
	var $pageView;
	var $pageInput;
	var $_POST;
	var $Obj;
	
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	function __construct() {
		$this->Obj = new PengadaanBarang();
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('pb_pengadaan_barang', 'pengadaanBarangInput', 'view', 'html');
	}

   function CheckUploadedImage($name)
   {
      $fileName = strrev(basename($_FILES[$name]['name']));
      $fileExtPos = strpos($fileName, '.');
      $fileExt = strtolower(strrev(substr($fileName, 0, $fileExtPos)));
      $_FILES[$name]['name']= strrev(substr($fileName, $fileExtPos + 1));
      $_FILES[$name]['ext']= $fileExt;
      if
      (
         $_FILES[$name]['error'] !== 0 ||
         strpos($_FILES[$name]['type'], 'image') !== 0 ||
         !in_array($fileExt, array('gif','jpg','jpeg','png','bmp')) AND
         $fileName !== ''
      )
         return false;
      else return true;
   }

   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      unset($this->_POST['btnsimpan']);
		
		$notNull = array
      (
         'nomorPengadaan',
         'pic'
      );
		
      $msg = array();
      foreach ($notNull as $val) if ($this->_POST[$val] == '')
         {$msg[] = 'Isian bertanda * (bintang) tidak boleh kosong!'; break;}
			
		if (count($msg) > 0)
      {
         $msg = array($this->_POST, $msg, '');
         Messenger::Instance()->Send('pb_pengadaan_barang', 'pengadaanBarangInput', 'view', 'html', $msg, Messenger::NextRequest);

         $return = $this->pageInput;
         if (isset($_GET['id'])) $return .= "&id=".$_GET['id'];
         return $return;
      }

      return true;
   }

   function Add()
   {
      $check = $this->Check();
		if ($check !== true) return $check;
		
		$error = array
      (
         'Penambahan data berhasil dilakukan.',
         'Penambahan data gagal dilakukan!',
			'Penambahan data detil barang gagal dilakukan!',
      );
		
		$result = $this->Obj->AddPengadaanBarang($this->_POST);
		
		if ($result === 0)
         $msg = array(1=>$error[$result], $this->cssDone);
      else $msg = array(1=>$error[$result], $this->cssFail);
      Messenger::Instance()->Send('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html', $msg, Messenger::NextRequest);

      return $this->pageView;
   }

   function Update()
   {
		$check = $this->Check();
		if ($check !== true) return $check;
		
		$error = array
      (
         'Perubahan data berhasil dilakukan.',
         'Perubahan data gagal dilakukan!',
         'Penghapusan data detil barang gagal dilakukan!',
			'Perubahan data detil barang gagal dilakukan!',
      );
		
		$id = $_GET['id']->Integer()->Raw();
		
		$result = $this->Obj->UpdatePengadaanBarang($this->_POST, $id);
		
		if ($result === 0)
         $msg = array(1=>$error[$result], $this->cssDone);
      else $msg = array(1=>$error[$result], $this->cssFail);
      Messenger::Instance()->Send('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html', $msg, Messenger::NextRequest);

      return $this->pageView;
   }

   function Delete()
   {
		$result = $this->Obj->Delete($this->_POST['idDelete']);
      if ($result === 0)
         $msg = array(1=>'Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>"Ada $result data yang tidak bisa dihapus.", $this->cssFail);
      Messenger::Instance()->Send('pb_pengadaan_barang', 'pengadaanBarang', 'view', 'html', $msg, Messenger::NextRequest);

      return $this->pageView;
   }
}
?>