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

require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/gedung/business/Ruang.class.php';

require_once GTFWConfiguration::GetValue( 'application', 'docroot').
   'module/gedung/business/AppGedungAkademik.class.php';

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

	var $return;

	function __construct() {
		$this->Obj = new Ruang;
		$this->_POST = $_POST->AsArray();
		$this->pageView = Dispatcher::Instance()->GetUrl('gedung', 'listRuang', 'view', 'html');
		$this->pageInput = Dispatcher::Instance()->GetUrl('gedung', 'inputRuang', 'view', 'html');
	}
   
   function CheckUploadedImage()
   {
   	  $maxSize = GTFWConfiguration::GetValue('application','max_upload_size');
      $fileName = strrev($_FILES['foto']['name']);
      $fileExtPos = strpos($fileName, '.');
      $fileExt = strtolower(strrev(substr($fileName, 0, $fileExtPos)));
      $_FILES['foto']['name']= strrev(substr($fileName, $fileExtPos + 1));
      $_FILES['foto']['ext']= $fileExt;
      if
      (
         $_FILES['foto']['error'] !== 0 ||
         strpos($_FILES['foto']['type'], 'image') !== 0 ||
      	 ($_FILES['foto']['size']/pow(1024,2)) > $maxSize ||
         !in_array($fileExt, array('gif','jpg','jpeg','png','bmp')) AND
         $fileName !== ''
      ){
      	 if (($_FILES['foto']['size']/pow(1024,2)) > $maxSize) $this->errmsg = '<br>Ukuran file tidak boleh lebih dari '.$maxSize.' MB';
         return false;
      }else return true;
   }
   
   function Check ()
   {
      if (isset($this->_POST['btnbalik'])) return $this->pageView.'&lantaiId='.$this->_POST['lantaiId'];
      if (!isset($this->_POST['kode'])) return $this->pageInput;
      unset($this->_POST['btnsimpan']);
      
      $msg = array();
		/*strpos('/'.implode('/', $this->_POST), '//') !== false*/
      if ($this->_POST['kode'] =="" || $this->_POST['nama'] =="" || $this->_POST['luas'] =="" || $this->_POST['kapasitas'] =="" || $this->_POST['unit_id'] =="")
         $msg[] = 'Isian bertanda * (bintang) tidak boleh kosong!';
      $kode_ruang = $this->Obj->CekKodeRuang($this->_POST['kode']);
      if (empty($_GET['id'])) {
         if(!empty($kode_ruang))
            $msg[] = 'Kode ruang sudah ada didalam databse, silahkan gunakan kode lain.';
      }
      #if ($this->_POST['kode'] !== '' AND intval($this->_POST['kode']) == 0)
         #$list[] = 'Kode Ruangan';
      if ($this->_POST['kapasitas'] !== '' AND intval($this->_POST['kapasitas']) == 0)
         $list[] = 'Kapasitas Ruangan';
      if ($this->_POST['luas'] !== '' AND intval($this->_POST['luas']) == 0)
         $list[] = 'Luas Ruangan';
      /*if ($this->_POST['jmlKursi'] !== '' AND intval($this->_POST['jmlKursi']) == 0)
         $list[] = 'Jumlah Kursi';
      if ($this->_POST['jmlMeja'] !== '' AND intval($this->_POST['jmlMeja']) == 0)
         $list[] = 'Jumlah Meja';*/
      if (isset($list))
      {
         $tmp = "Field berikut harap diisi dengan bilangan numeris!\n";
         foreach ($list as $val)
            $tmp .= "\t\t<li style='margin-left: 2em; list-style: circle'>$val</li>\n";
         $msg[] = "$tmp\t";
      }
      
      if (!$this->CheckUploadedImage())
         $msg[] = 'Proses upload gambar gagal!'.$this->errmsg;
      
      // check uploaded image
      // ---------
      
      if (count($msg) > 0)
      {
         $msg = array($this->_POST, $msg, '');
         Messenger::Instance()->Send('gedung', 'inputRuang', 'view', 'html', $msg, Messenger::NextRequest);
         
         $return = $this->pageInput;
         if (isset($_GET['id'])) $return .= "&id=".$_GET['id'];
         return $return;
      }
      
      return true;
   }
   
	public function addRuangSia(){
		//-- check connection sia 2
		$Obj = new AppGedung();
		$Obj2 = new AppGedungAkademik();
		$dataLast = $Obj->GetDataRuangLastId();
		$insertRuangSia = $Obj2->addEruang($dataLast);
		if($insertRuangSia===false){
			return false;
		} else {
			if($this->_POST['jnsRuangId']=='2'){
				$dataLab = $Obj2->addElaboratorium($dataLast);
				if($dataLab===false){
					return false;
				}
			}
			return true;
		}
	}
	
	public function updateRuangSia(){
		//-- check connection sia 2
		$Obj2 = new AppGedungAkademik();
		if($this->_POST['jnsRuangId']=='2'){
			$checkId = $Obj2->CheckIdLab($_GET['id']->Integer()->Raw());
			if(empty($checkId)){
				$dat['ruId'] = $_GET['id']->Integer()->Raw();
				$dataLab = $Obj2->addElaboratorium($dat);
				if($dataLab===false){
					return false;
				}
			}
		} else {
			$checkId = $Obj2->CheckIdLab($_GET['id']->Integer()->Raw());
			if(!empty($checkId)){
				$delete = $Obj2->deleteElaboratorium($checkId[0]);
				if($delete===false){
					return false;
				}
			}
		}
		$updateEruang = $Obj2->updateEruang($_GET['id']->Integer()->Raw(), $this->_POST);
		if($updateEruang===false){
			return false;
		}
		return true;
	}
	
   function Add()
   {
      $check = $this->Check();
      if ($check !== true) return $check;
	  
      $result = $this->Obj->AddRuang($this->_POST);
      
      if ($result === 0) {
         for($j=0; $j<sizeof($this->_POST['fasjml_']); $j++) {
            if(!empty($this->_POST['fasjml_'][$j]))
               $arr_jumlah[] = $this->_POST['fasjml_'][$j];
         }
         
         for($f =0; $f<sizeof($arr_jumlah); $f++) {
            if(!empty($arr_jumlah[$f])) {
               $insert_fas = $this->Obj->UpdateFasilitasRuang($this->Obj->GetMaxIdRuang(), $this->_POST['fas_id'][$f], $arr_jumlah[$f]);
            }
         }
         
         #insert unit pengguna ruang
         for($u=0; $u<count($this->_POST['data']['peng_ruang']['id']); $u++) {
            $ins_peng_ruang = $this->Obj->DoAddUnitPenggunaRuang($this->_POST['data']['peng_ruang']['id'][$u], $this->Obj->GetMaxIdRuang());
         }
         $msg = array(1=>'Penambahan Data Berhasil Dilakukan.', $this->cssDone);
			
			if(GTFWConfiguration::GetValue( 'application', 'sia')===true){
				$insert = $this->addRuangSia();
				if($insert===false){
					$msg = array(1=>'Proses penambahan data ruang pada aplikasi SIA gagal!', $this->cssFail);
					Messenger::Instance()->Send('gedung', 'listRuang', 'view', 'html', $msg, Messenger::NextRequest).'&lantaiId='.$this->_POST['lantaiId'];
					return $this->pageView.'&lantaiId='.$this->_POST['lantaiId'];
				}
			}
      }
      else $msg = array(1=>'Proses Penambahan Data Gagal!', $this->cssFail);
      Messenger::Instance()->Send('gedung', 'listRuang', 'view', 'html', $msg, Messenger::NextRequest).'&lantaiId='.$this->_POST['lantaiId'];
      
      return $this->pageView.'&lantaiId='.$this->_POST['lantaiId'];
   }
   
   function Update()
   {
      $check = $this->Check();
      if ($check !== true) return $check;  
		
      //post fasilitas gedung
      //delete untuk updare
      if(!empty($this->_POST['id_fas_'])) {
         for($d=0; $d<sizeof($this->_POST['id_fas_']); $d++){
            if(!empty($this->_POST['id_fas_'][$d]))
               $this->Obj->DeleteFasilitasRuang($_GET['id']->Integer()->Raw(),$this->_POST['id_fas_'][$d]);
         }
      }
      
      for($j=0; $j<sizeof($this->_POST['fasjml_']); $j++) {
         if(!empty($this->_POST['fasjml_'][$j]))
            $arr_jumlah[] = $this->_POST['fasjml_'][$j];
      }
      
      for($f =0; $f<sizeof($arr_jumlah); $f++) {
         if(!empty($arr_jumlah[$f])) {
            $insert_fas = $this->Obj->UpdateFasilitasRuang($_GET['id']->Integer()->Raw(), $this->_POST['fas_id'][$f], $arr_jumlah[$f]);
         }
      }
      
      $result = $this->Obj->UpdateRuang($_GET['id']->Integer()->Raw(), $this->_POST);
      if ($result === 0) {			
			#update pengguna ruang
         $delete_dulu_peng_ruang = $this->Obj->DeletePenggunaRuang($_GET['id']->Integer()->Raw());
         for($u=0; $u<count($this->_POST['data']['peng_ruang']['id']); $u++) {
            $ins_peng_ruang = $this->Obj->DoAddUnitPenggunaRuang($this->_POST['data']['peng_ruang']['id'][$u], $_GET['id']->Integer()->Raw());
         }
         
         $msg = array(1=>'Perubahan Data Berhasil Dilakukan.', $this->cssDone);
			if(GTFWConfiguration::GetValue('application', 'sia')===true){
				$insertRuangSia = $this->updateRuangSia();
				if($insertRuangSia===false){
					$msg = array(1=>'Proses perubahan data ruang pada aplikasi SIA gagal!', $this->cssFail);
					Messenger::Instance()->Send('gedung', 'listRuang', 'view', 'html', $msg, Messenger::NextRequest).'&lantaiId='.$this->_POST['lantaiId'];
					return $this->pageView.'&lantaiId='.$this->_POST['lantaiId'];
				}
			}
      }
      else $msg = array(1=>'Tidak Berhasil Mengubah Data!', $this->cssFail);
      Messenger::Instance()->Send('gedung', 'listRuang', 'view', 'html', $msg, Messenger::NextRequest).'&lantaiId='.$this->_POST['lantaiId'];
      
      return $this->pageView.'&lantaiId='.$this->_POST['lantaiId'];
   }
   
   function Delete()
   {
		$result = 0;
		if(GTFWConfiguration::GetValue('application', 'sia')===true){
			$Obj2 = new AppGedungAkademik();
			$result = $Obj2->deleteEruang($this->_POST['idDelete']);
		}
		
		if($result === 0){
			$result2 = $this->Obj->DeleteRuang($this->_POST['idDelete']);
         if ($result2 === 0){
				$msg = array(1=>'Penghapusan Data Berhasil Dilakukan.', $this->cssDone);
			} else {
				$msg = array(1=>'Proses Penghapusan Data Gagal!', $this->cssFail);
			}
      } else { $msg = array(1=>'Proses penghapusan data ruang pada aplikasi SIA gagal!', $this->cssFail); }
      Messenger::Instance()->Send('gedung', 'listRuang', 'view', 'html', $msg, Messenger::NextRequest).'&lantaiId='.$_GET['lantaiId'];
      
      return $this->pageView.'&lantaiId='.$_GET['lantaiId'];
   }
}
?>