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

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

	var $return;
	var $decId;
	var $encId;

	function __construct() {
		$this->Obj = new Kodefikasi;
		$this->_POST = $_POST->AsArray();
		$this->decId = $_GET['id']->Integer()->Raw();
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('kodefikasi_barang', 'kodefikasi', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('kodefikasi_barang', 'inputBarang', 'view', 'html');
	}
   
   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView;
      if (trim($this->_POST['subKelompok']) == '' OR trim($this->_POST['kode']) == '' OR trim($this->_POST['nama']) == '')
         $error = 'Isian bertanda bintang tidak boleh kosong!';
      elseif (!is_numeric($this->_POST['kode']))
         $error = 'Kode harus berupa karakter numeris!';
      elseif ($this->Obj->CekKodeBarang($this->decId, $this->_POST['kode'], $this->_POST['subKelompok']))
         $error = 'Kode barang sudah digunakan';
      
      if (isset($error))
      {
         $msg = array($this->_POST, $error, '');
         Messenger::Instance()->Send('kodefikasi_barang', 'inputBarang', 'view', 'html', $msg, Messenger::NextRequest);
         
         $return = $this->pageInput;
         if (isset($_GET['id'])) $return .= "&id=".$_GET['id']->Integer()->Raw();
         return $return;
      }
      else return true;
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
	  
	  $files = $_FILES['fotoBarang'];
			if(trim($files['name']) != ""){
				$file = $this->UploadFile($files);
				$this->_POST['fotoBarang']=$file;}
			else{
				$file = $this->_POST['hidden_file'];
			}

      $result = $this->Obj->UpdateBarang($this->_POST);
      if ($result === 0)
         $msg = array(1=>'Perubahan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Tidak Berhasil Mengubah Data!', $this->cssFail);
      Messenger::Instance()->Send('kodefikasi_barang', 'kodefikasi', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Add()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
      
	  $files = $_FILES['fotoBarang'];
	  if(trim($files['name']) != ""){
	  $file =  $this->UploadFile($files);
	  $this->_POST['fotoBarang']=$file;} 
	  
      $result = $this->Obj->AddBarang($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('kodefikasi_barang', 'kodefikasi', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function Delete()
   {
      $result = $this->Obj->DeleteBarang($this->_POST['idDelete']);
      if ($result === 0)
         $msg = array(1=>'Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
      else $msg = array(1=>'Proses Penghapusan Data Gagal!', $this->cssFail);
      Messenger::Instance()->Send('kodefikasi_barang', 'kodefikasi', 'view', 'html', $msg, Messenger::NextRequest);
      
      return $this->pageView;
   }
   
   function UploadFile($files){	
   
		   $pisah=explode(".",$files['name']);
		   $h=(sizeof($pisah))-1;
		   $extensi=$pisah[$h];
		   
			if 	($files!='') {
                $file = $this->_POST['kode'].'_'.date("Y-m-d_H-i-s_") . substr(md5(microtime()), 0, 3).'.'.$extensi;
					@move_uploaded_file($files['tmp_name'], GTFWConfiguration::GetValue( 'application', 
				'docroot')."document/barang/".$file);
			}
			else {$file='';}
			
			return $file;
	}
}
?>