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

class ProcessJenisKdp {
	var $_POST;
	var $Obj;
	var $pageView;
	var $pageInput;
	var $pageUpdate;
	var $error = 0;
	var $pesanError = "";
	 
	//css hanya dipakai di view
	var $cssDone = "notebox-done";
	var $cssFail = "notebox-warning";
	var $cssWarn = "notebox-alert";
	
	function __construct() {
		$this->Obj = new JenisKdp();
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('kdp_ref_jenis_kdp', 'JenisKdp', 'view', 'html'); //landing pageView 
		$this->pageInput = Dispatcher::Instance()->GetUrl('kdp_ref_jenis_kdp', 'InputJenisKdp', 'view', 'html'); //landing pageInput 
		$this->pageUpdate = Dispatcher::Instance()->GetUrl('kdp_ref_jenis_kdp', 'UpdateJenisKdp', 'view', 'html'); //landing pageUpdate 
	}
	 
	function CekFieldKosong(){
		if($_POST['kode_jenis'] == ""){
			$this->error += 1;
			$this->pesanError .= " - Kode Jenis KDP masih Kosong <br>";
		}
		if($_POST['nama_jenis'] == ""){
			$this->error += 1;
			$this->pesanError .= " - Nama Jenis KDP masih Kosong <br>";
		}
	}
	 
	function CekKode(){
		$status = $this->Obj->CekKode($_POST['kode_jenis']);
		if($status == true){
			return "ada";
		} else {
			return "tidak";
		}
	}
	 
	function Add() {
		if(isset($_POST['btnsimpan'])) {
			$this->CekFieldKosong();
			
			//CEK KODE GANDA
			$kodeAsli = $this->_POST['kode_jenis_asli'];
			$kodeField = $this->_POST['kode_jenis'];
			if ($kodeField <> $kodeAsli){
				$kodeGanda = $this->CekKode();
				if ($kodeGanda == "ada"){
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','InputJenisKdp','view','html', array($this->_POST,'Kode Jenis Ganda/Sudah Ada', $this->cssFail),Messenger::NextRequest);
					return $this->pageInput;
				}
			}
			//END CEK KODE GANDA
			
			if($this->error != 0){
				Messenger::Instance()->Send('kdp_ref_jenis_kdp', 'InputJenisKdp', 'view', 'html', array($this->_POST,$this->pesanError, $this->cssWarn),Messenger::NextRequest);
				return $this->pageInput; //landing pageInput
			} else {
				$add = $this->Obj->DoAddJenisKdp($this->_POST['kode_jenis'],$this->_POST['nama_jenis']);
				if ($add == true){
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','JenisKdp','view','html', array($this->_POST,'Penambahan Berhasil Dilakukan',$this->cssDone),Messenger::NextRequest);
				} else {
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','JenisKdp','view','html', array($this->_POST,'Gagal Menambah Data',$this->cssFail),Messenger::NextRequest);
				}
				return $this->pageView; //landing pageView
			}
		}
		return $this->pageView;
	}
	 
	function Delete(){
		if (is_array($this->_POST['idDelete'])){ //Delete Banyak
			$arrId = $this->_POST['idDelete'];
			$delete = $this->Obj->DoDeleteBanyak($arrId);
		} else {
			$id = $this->_POST['idDelete']; //Delete Satuan
			$delete = $this->Obj->DoDeleteSatuan($id);
		}
		
		if($delete === true){
			Messenger::Instance()->Send('kdp_ref_jenis_kdp', 'JenisKdp', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} else {
			Messenger::Instance()->Send('kdp_ref_jenis_kdp', 'JenisKdp', 'view', 'html', array($this->_POST,'Penghapusan Data Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
		}
		
		return $this->pageView;
	}
	 	 
	function Update(){
		if(isset($_POST['btnsimpan'])) {
			$this->CekFieldKosong();
			
			//CEK KODE GANDA
			$kodeAsli = $this->_POST['kode_jenis_asli'];
			$kodeField = $this->_POST['kode_jenis'];	
			if ($kodeField <> $kodeAsli){
				$kodeGanda = $this->CekKode();
				if ($kodeGanda == "ada"){
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','UpdateJenisKdp','view','html', array($this->_POST,'Kode Jenis Ganda', $this->cssFail),Messenger::NextRequest);
					return $this->pageUpdate;
				}
			}
			//END CEK KODE GANDA
				
			if($this->error != 0){
				Messenger::Instance()->Send('kdp_ref_jenis_kdp', 'UpdateJenisKdp', 'view', 'html', array($this->_POST,$this->pesanError, $this->cssWarn),Messenger::NextRequest);
				return $this->pageUpdate; //landing pageUpdate
			} else {
				$update = $this->Obj->DoUpdateJenisKdp($this->_POST['kode_jenis'],$this->_POST['nama_jenis'],$this->_POST['kode_jenis_asli']);
				if ($update == true){
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','JenisKdp','view','html', array($this->_POST,'Update Berhasil Dilakukan',$this->cssDone),Messenger::NextRequest);
				} else {
					Messenger::Instance()->Send('kdp_ref_jenis_kdp','JenisKdp','view','html', array($this->_POST,'Gagal Update Data',$this->cssFail),Messenger::NextRequest);
				}
				return $this->pageView; //landing pageView
			}
		}
		return $this->pageView;
	}
	
}
?>
