<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/'.Dispatcher::Instance()->mModule.'/business/PegendalianBarang.class.php';

class ProcessPengendalian
{
	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	//css hanya dipake di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";

	var $return;

	function __construct() {
		$this->Obj = new PengendalianBarang;
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'pengendalianBarang', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl(Dispatcher::Instance()->mModule, 'inputPengendalianBarang', 'view', 'html');
	}
   
   function CheckUploadedFile($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 ||
         !in_array($fileExt, array('jpg','png','gif')) AND
         $fileName !== ''
      )
         return false;
      else return true;
   }
   
   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      if (empty($this->_POST)) return $this->pageInput;
      $msg = array();
      
      if (empty($this->_POST['barangPengendalian'])) $msg[] = "Belum ada barang inventarisasi yang dipilih.";
      else foreach ($this->_POST['barangPengendalian'] as $value)
         if ($value['isFixed'] == 0 && $value['pengBrgStatusId'] == 0){$msg[] = "Harap lengkapi data barang yang dipilih.";break;}
      if (!$this->_POST['pengBAPeng']) $msg[] = "Field BA Pengendalian harus diisi.";
      if (!$this->_POST['pengPIC']) $msg[] = "Data PIC tidak boleh kosong.";
      
      // cuma ngecek validitas file, proses move_uploaded_file ada di Business
      if (!$this->CheckUploadedFile('pengFile')) 
         $msg[] = 'File tidak valid atau proses upload gagal.';
      
      if (count($msg) > 0)
      {
         $msg = array($this->_POST, $msg, 'notebox-alert');
         Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'inputPengendalianBarang', '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;

      $result = $this->Obj->Add($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Penambahan data berhasil dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses Penambahan Data Gagal!", $this->cssFail);
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'pengendalianBarang', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
      
      $result = $this->Obj->Update($_GET['id']->Integer()->Raw(), $this->_POST);
      if ($result === 0)
         $msg = array(1=>'Pengubahan data berhasil dilakukan.', $this->cssDone);
      else $msg = array(1=>"Proses pengubahan data gagal!", $this->cssFail);
      Messenger::Instance()->Send(Dispatcher::Instance()->mModule, 'pengendalianBarang', '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(Dispatcher::Instance()->mModule, 'pengendalianBarang', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
}
?>