<?php
class Ruang extends Database
{
   protected $mSqlFile= 'module/gedung/business/Ruang.sql.php';

   function __construct($connectionNumber=0) {
      parent::__construct($connectionNumber);
      $gId = $this->GetGedungId();
      $data = $this->GetDataGedungById($gId);

      if ($data)
      {
         $this->gId = $gId;
         $this->gedungKode = $data['gedungKode'];
         $this->gedungNama = $data['gedungNama'];
      }
      else $this->gId = false;
      #$this->setDebugOn();
   }
	
	function GetDataUserByUsername($name)
   {
      if (isset($this->mUserByUserName[$name])) return $this->mUserByUserName[$name];
      $result = $this->Open($this->mSqlQueries['get_data_user_by_user_name'], array($name));  
      $this->mUserByUserName[$name] = $result[0];
      
		return $result[0];
   }
	
	function GetComboGedungOld(){
		$User = $this->GetDataUserByUsername($_SESSION['username']);		
		$array = array(
			$User['unitkerjaId'], $User['unitkerjaId'], $User['role_id']
		);
		return $this->Open($this->mSqlQueries['get_combo_gedung'],$array);
	}
	
	# multiunit
	//show data gedung based on kode sistem
	function GetComboGedung(){
		$User = $this->GetDataUserByUsername($_SESSION['username']);
		$array = array(
				$User['unitkerjaKodeSistem'],
				$User['unitkerjaKodeSistem'].".%",
				$User['unitkerjaKodeSistem'],
				$User['unitkerjaKodeSistem'].".%",
				$User['unitkerjaKodeSistem'],
				$User['unitkerjaKodeSistem'].".%"
		);
		return $this->Open($this->mSqlQueries['get_combo_gedung'],$array);
	}
	
	function GetComboLantai($id){
		return $this->Open($this->mSqlQueries['get_combo_lantai'],array($id));
	}
	
   function GetGedungId()
   {
      if (!isset($_SESSION['mod=gedung']['sub=ruang']['gId']) AND !isset($_GET['gId']))
         return false;

      if (isset($_GET['gId']))
      {
         $gId = $_GET['gId']->Integer()->Raw();
         $_SESSION['mod=gedung']['sub=ruang']['gId'] = $gId;
      }
      else $gId = $_SESSION['mod=gedung']['sub=ruang']['gId'];

      return $gId;
   }
	
	public function GetDataRuangLastId(){      
      $result = $this->Open($this->mSqlQueries['get_data_ruang_last_insert'], array());
		return $result[0];
	}
	
   function GetDataGedungById($gId)
   {
      $result = $this->Open($this->mSqlQueries['get_data_gedung_by_id'], array($gId));

      if (empty($result)) return false;
      else return $result[0];
   }

   function GetListCount($filter)
   {
      extract($filter);
      $arg = array
      (
         $this->gId,
         $jnsRuangId, $jnsRuangId,
         $tipeRuangId, $tipeRuangId,
         "%$key%", "%$key%",
			$lantaiId
      );

      $result = $this->Open($this->mSqlQueries['get_list_count'], $arg);
      return $result[0]['total'];
   }
   
   function GetCount(){
   	$result = $this->Open($this->mSqlQueries['get_count'],array());
   	return $result[0]['total'];
   }

   # multiunit
   function GetList($filter, $start = 0, $limit = 20)
   {
		extract($filter);
		$user = $this->GetDataUserByUsername($_SESSION['username']);
		$arg = array
      (
         $this->gId,
         $jnsRuangId, $jnsRuangId,
         $tipeRuangId, $tipeRuangId,
         "%$key%", "%$key%",
		 $lantaiId,
      	 $user['unitkerjaKodeSistem'],
      	 $user['unitkerjaKodeSistem'].".%",
         $start, $limit
      );

      $result = $this->Open($this->mSqlQueries['get_list'], $arg);
      return $result;
   }
	
	function GetGedungLantai($lantaiId)
   {
      $result = $this->Open($this->mSqlQueries['get_gedung_lantai'], array($lantaiId));
      return $result;
   }

   function GetListByGedungIdCount($gId)
   {
      $result = $this->Open($this->mSqlQueries['get_list_by_gedung_id_count'], array($gId));
      return $result[0]['total'];
   }

   function GetListByGedungId($gId, $start = 0, $limit = 20)
   {
      $result = $this->Open($this->mSqlQueries['get_list_by_gedung_id'], array($gId, $start, $limit));
      return $result;
   }

   function GetLastRuangId()
   {
      $result = $this->Open($this->mSqlQueries['get_last_ruang_id'], array());
      return intval($result[0]['id']);
   }

   function GetDataRuangById($id)
   {
      $result = $this->Open($this->mSqlQueries['get_data_by_id'], array($id));
      if (empty($result)) return false;
      else return $result[0];
   }

   function GetFasilitasByRuangId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_fasilitas_by_ruang_id'], array($id, $id, $id));

      /*$return = array();
      foreach ($result as $value)
         $return[] = $value['id'];*/
      return $result;
   }

   function GetKonstruksiByRuangId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_konstruksi_by_ruang_id'], array($id));

      $return = array();
      foreach ($result as $value)
         $return[$value['id']] = $value['value'];
      return $return;
   }

   function GetFasilitasList()
   {
      return $this->Open($this->mSqlQueries['get_fasilitas_list'], array());
   }

   function GetKonstruksiRuangList()
   {
      return $this->Open($this->mSqlQueries['get_konstruksi_ruang_list'], array());
   }

   //untuk print rtf ruang
   function GetDataPrintRuang($idGedung,$jnsRuangId, $tipeRuangId, $key, $lantaiId)
   {
      return $this->Open($this->mSqlQueries['get_data_print_ruang'], array($idGedung,
																		   $jnsRuangId,$jnsRuangId,
																		   $tipeRuangId,$tipeRuangId,
																		   '%'.$key.'%', '%'.$key.'%', $lantaiId));
   }

   // Combo Box
   function ComboJnsRuang()
   {
      $return = $this->Open($this->mSqlQueries['combo_jenis_ruang'], array());

      if (empty($return)) $return = array('id'=>0, 'name'=>'Data jenis ruang belum ada');
      return $return;
   }

   function ComboTipeRuang()
   {
      $return = $this->Open($this->mSqlQueries['combo_tipe_ruang'], array());

      if (empty($return)) $return = array('id'=>0, 'name'=>'Data tipe belum ada');
      return $return;
   }

   function ComboKondisiRuang()
   {
      $return = $this->Open($this->mSqlQueries['combo_kondisi_ruang'], array());

      if (empty($return)) $return = array('id'=>0, 'name'=>'Data kondisi ruang belum ada');
      return $return;
   }

   function ComboPicRuangan()
   {
      $return = $this->Open($this->mSqlQueries['combo_pic_ruangan'], array());

      if (empty($return)) $return = array('id'=>0, 'name'=>'Data PIC belum ada');
      return $return;
   }

   function ComboJnsKonstruksi()
   {
      return $this->Open($this->mSqlQueries['combo_jenis_konstruksi'], array());
   }

   // Do
   function AddRuang($data)
   {
      extract($data);
      unset($data);
      $return = 0;

      // upload foto
      $foto = $this->UploadFoto();
      if ($foto === false) $return |= 8;
      if ($foto == false) $foto = $oldFoto;

      // tabel ruang
      $userId = trim(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());

      $result = $this->Execute
      (
         $this->mSqlQueries['add_ruang'], array
         (
            $kode,
            $nama,
            $this->gId,
            $jnsRuangId,
            $kapasitas,
            $luas,
            $panjang,
            $lebar,
            $tipeRuangId,
            #$jmlKursi,
            #$jmlMeja,
            $kondisiRuangId,
            $unit_id,
            $picId,
            $foto,
				$warna_cat,
				$merk_cat,
				$nomor_cat,
            $userId,
            $lantaiId
         )
      );
      if (!$result) return 1;

      $id = $this->GetLastRuangId();
      // tabel ruang_fasilitas
      #$result = $this->UpdateFasilitasRuang($id, $fasilitas);
      #if (!$result) $return |= 2;

      // tabel ruang_konstruksi
      $result = $this->UpdateKonstruksiRuang($id, $konstruksi);
      if (!$result) $return |= 4;
      
      return $return;
   }

   function UpdateRuang($id, $data)
   {
      extract($data);
      unset($data);
      $return = 0;

      // upload foto
      $foto = $this->UploadFoto();
      if ($foto === false) $return |= 8;
      if ($foto == false) $foto = $oldFoto;

      // tabel ruang
      $userId = trim(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId());
      $result = $this->Execute
      (
         $this->mSqlQueries['update_ruang'], array
         (
            $kode,
            $nama,
            $this->gId,
            $jnsRuangId,
            $kapasitas,
            $luas,
            $panjang,
            $lebar,
            $tipeRuangId,
            #$jmlKursi,
            #$jmlMeja,
            $kondisiRuangId,
            $unit_id,
            $picId,
            $foto,
				$warna_cat,
				$merk_cat,
				$nomor_cat,
            $userId,
            $id
         )
      );
      if (!$result) return 1;

      // tabel ruang_fasilitas
      #$result = $this->UpdateFasilitasRuang($id, $fasilitas);
      #if (!$result) $return |= 2;

      // tabel ruang_konstruksi
      $result = $this->UpdateKonstruksiRuang($id, $konstruksi);
      if (!$result) $return |= 4;

      return $return;
   }

   /*function UpdateFasilitasRuang ($id, $fasilitasList)
   {
      if (count($fasilitasList) < 1) return true;

      $query = implode(", ", array_fill(0, count($fasilitasList), '(%d, %d)'));
      $query = str_replace('%REPLACE%', $query, $this->mSqlQueries['add_fasilitas_ruang']);

      foreach ($fasilitasList as $value)
      {
         $input[] = $id;
         $input[] = $value;
      }

      $result = $this->Execute($this->mSqlQueries['delete_fasilitas_ruang'], array($id));
      if (!$result) return false;
      return $this->Execute($query, $input);
   }*/

   function DeleteFasilitasRuang ($id_ruang, $fas_id) {
      $ret = $this->Execute($this->mSqlQueries['delete_fasilitas_ruang'], array($id_ruang, $fas_id));
      return $ret;
   }

   function GetMaxIdRuang() {
      $res = $this->Open($this->mSqlQueries['get_max_id_ruang'], array());
      return $res[0]['id'];
   }

   function UpdateFasilitasRuang ($id_ruang, $fas_id, $jml) {
      $ret = $this->Execute($this->mSqlQueries['add_fasilitas_ruang'], array($id_ruang, $fas_id, $jml));
      return $ret;
   }

   function UpdateKonstruksiRuang ($id, $konstruksiList)
   {
      if (count($konstruksiList) < 1) return true;

      $query = implode(", ", array_fill(0, count($konstruksiList), '(%d, %d, %d)'));
      $query = str_replace('%REPLACE%', $query, $this->mSqlQueries['add_konstruksi_ruang']);

      foreach ($konstruksiList as $key=>$value)
      {
         $input[] = $id;
         $input[] = $key;
         $input[] = $value;
      }
      $asd = eval('return sprintf($query,'.implode(',',$input).');');

      $result = $this->Execute($this->mSqlQueries['delete_konstruksi_ruang'], array($id));
      if (!$result) return false;

      return $this->Execute($query, $input);
   }

   function DeleteRuang($arrayId)
   {
      $query = implode(", ", array_fill(0, count($arrayId), '%d'));
      $query = str_replace('%REPLACE%', $query, $this->mSqlQueries['delete_ruang']);

      $result = $this->Execute($query, $arrayId);
      if (!$result) return 1;
      else return 0;
   }

   function UploadFoto()
   {
      $fileName = $_FILES['foto']['name'];
      if ($fileName === '') return '';

      $fileExt = $_FILES['foto']['ext'];
      $fileName .= "-".dechex(mt_rand(65536,1048575)).".$fileExt";

      if (move_uploaded_file($_FILES['foto']['tmp_name'], GTFW_APP_DIR."/images/ruang/$fileName"))
         return $fileName;
      else return false;
   }

   function GetInventarisByRuangId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_inventaris_ruang'], array($id));
      return $result[0];
   }

   function GetListInventarisByRuangId($id)
   {
      $result = $this->Open($this->mSqlQueries['get_list_inventaris_ruang'], array($id));
      return $result;
   }

   function CekKodeRuang($kode)
   {
      $result = $this->Open($this->mSqlQueries['cek_ruang'], array($kode));
      return $result[0]['id_ruang'];
   }

   #unit_pengguna_ruang
   function GetUnitPenggunaRuang($kode)
   {
      $result = $this->Open($this->mSqlQueries['get_unit_pengguna_ruang'], array($kode));
      return $result;
   }

   function DoAddUnitPenggunaRuang($unit, $ruang) {
      #$sql = sprintf($this->mSqlQueries['insert_unit_pengguna_ruang'], $unit, $ruang);
      #echo $sql; exit;
      $ret = $this->Execute($this->mSqlQueries['insert_unit_pengguna_ruang'], array($unit, $ruang));
      return $ret;
   }
   
   function DoUpdatePindahRuang($data) {
   	extract($data);
   	$arrData  = array(
   		$gedung,
   		$lantai,
   		trim(Security::Instance()->mAuthentication->GetCurrentUser()->GetUserId()),		
   		$ruangId
   	);
   
      $result = $this->Execute($this->mSqlQueries['update_pindah_ruang'], $arrData);
      return $result;
   }

   function DeletePenggunaRuang ($id_ruang) {
      $ret = $this->Execute($this->mSqlQueries['delete_pengguna_ruang'], array($id_ruang));
      return $ret;
   }
}
?>
