<?php
require_once GTFWConfiguration::GetValue( 'application', 'docroot') . 'module/periode/business/AppPeriode.class.php';
class ProcessPeriode {

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

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

	function __construct() {
		$this->Obj = new AppPeriode();
		$this->_POST = $_POST->AsArray();
		$this->decId = Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
		$this->encId = Dispatcher::Instance()->Encrypt($this->decId);
		$this->pageView = Dispatcher::Instance()->GetUrl('periode', 'periode', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('periode', 'inputPeriode', 'view', 'html');
		$this->pageConfirm = Dispatcher::Instance()->GetUrl('periode', 'confirmAddPeriode', 'view', 'html')
         ."&dataId=" . Dispatcher::Instance()->Decrypt($_REQUEST['dataId']);
	}

	function Check() {
		if (isset($_POST['btnsimpan'])) {
         $periodeAkhir = $this->Obj->GetPeriodeAkhir();
         if($periodeAkhir[0]['periode']=="2"){
            $tahun=$periodeAkhir[0]['tahun']+1;
            $periode=$periodeAkhir[0]['periode']-1;
            $nextPeriode = $tahun.$periode;
         }else{
            $tahun=$periodeAkhir[0]['tahun'];
            $periode=$periodeAkhir[0]['periode']+1;
            $nextPeriode = $tahun.$periode;
         }
         $tahun=$periodeAkhir[0]['tahun']+1;
         $semester=$periodeAkhir[0]['periode']+1;
			if((trim($this->_POST['tahun']) == "") or (trim($this->_POST['semester']) == "")) {
				return "empty";
			} elseif(($this->_POST['tahun'].$this->_POST['semester'] < $periodeAkhir[0]['tahun'].$periodeAkhir[0]['periode'])
            or ($this->_POST['tahun'].$this->_POST['semester'] > $nextPeriode)) {
			   return "confirm";
			}else{
				return true;
			}
		}
		return false;
	}
   function Add() { 
		$cek = $this->Check();		
		if($cek === true) {   
			$addPeriode = $this->Obj->DoAddPeriode($this->_POST['tahun'],$this->_POST['semester'],'');
			if ($addPeriode === true) {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('periode', 'inputPeriode', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput;
		}elseif($cek == "confirm") {
			Messenger::Instance()->Send('periode', 'confirmAddPeriode', 'view', 'html', array($this->_POST['tahun'],$this->_POST['semester']),Messenger::NextRequest);
			return $this->pageConfirm;
		}
		return $this->pageView;
	}
	
	function AddConfirm() {    
			$addPeriode = $this->Obj->DoAddPeriode($_SESSION['tahun'],$_SESSION['semester'],'');
			if ($addPeriode === true) {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Penambahan data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Gagal Menambah Data', $this->cssFail),Messenger::NextRequest);
			}
		return $this->pageView;
	}

	function Update() {
		$cek = $this->Check();
		if($cek === true) {
			$updatePeriode = $this->Obj->DoUpdatePeriode($this->_POST['tahun'],$this->_POST['semester'], $this->decId);
			if ($updatePeriode === true) {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Perubahan Periode Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Perubahan Periode Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			}
		} elseif($cek == "empty") {
			Messenger::Instance()->Send('periode', 'inputPeriode', 'view', 'html', array($this->_POST,'Lengkapi Isian Data'),Messenger::NextRequest);
			return $this->pageInput . "&dataId=" . $this->encId;
		}elseif($cek == "confirm") {
			Messenger::Instance()->Send('periode', 'confirmAddPeriode', 'view', 'html', array($this->_POST['tahun'],$this->_POST['semester']),Messenger::NextRequest);
			return $this->pageConfirm;
		}
		return $this->pageView;
	}
	
	function UpdateConfirm() {
			$updatePeriode = $this->Obj->DoUpdatePeriode($_SESSION['tahun'],$_SESSION['semester'], $this->decId);
			if ($updatePeriode === true) {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Perubahan Periode Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
			} else {
				Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Perubahan Periode Gagal Dilakukan', $this->cssFail),Messenger::NextRequest);
			} 
		return $this->pageView;
	}
	
	function AktifPeriode(){
      $this->Obj->StartTrans();
      
      $nonAktifPeriode = $this->Obj->DoNonAktifPeriode('');
      $aktifPeriode = $this->Obj->DoAktifPeriode($this->decId);
      
      $aktif = $nonAktifPeriode && $aktifPeriode;
      $this->Obj->EndTrans($aktif);
      
      if($aktif === true){
         Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Periode Berhasil Diaktifkan', $this->cssDone),Messenger::NextRequest);
      }else{
         Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Periode Gagal Diaktifkan', $this->cssFail),Messenger::NextRequest);
      }
      return $this->pageView;
   }

	function Delete() {
		$arrId = $this->_POST['idDelete'];
		$deleteDataById=$this->Obj->DoDeletePeriodeById($arrId);
		
		if($deleteDataById==false){
   		    if(is_array($arrId)==false)
               $arr[0]=$arrId;
            else
               $arr=$arrId;
         $deleteArrData = $this->Obj->DoDeletePeriodeByArrayId($arr);
      }
		//print_r($this->_POST);
		if($deleteArrData === true) {
			Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} elseif($deleteDataById === true) {
			Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST,'Penghapusan Data Berhasil Dilakukan', $this->cssDone),Messenger::NextRequest);
		} else {
			//jika masuk disini, berarti PASTI ada salah satu atau lebih data yang gagal dihapus
			for($i=0;$i<sizeof($arr);$i++) {
				$deleteData = false;
				$deleteData = $this->Obj->DoDeletePeriodeById($arr[$i]);
				if($deleteData === true) $sukses += 1;
				else $gagal += 1;
			}
			Messenger::Instance()->Send('periode', 'periode', 'view', 'html', array($this->_POST, $gagal . ' Data Tidak Dapat Dihapus.', $this->cssFail),Messenger::NextRequest);
		}
		return $this->pageView;
	}
}
?>
