<?php
require_once(__DIR__."/../includes/sysinit.php");

class Building
{
	// Properties of the class:
	var $hddnBuildingId, $selClientId, $selAreaId, $edtBuildingName, $txtBuildingNeeds;
	function __construct(){}
	function __destruct(){}

	function addBuilding()
	{
		if (empty($this->hddnBuildingId))
		{
			if ($this->checkBuildingExists())
			SableSystem::ajaxResponse(false, "", "This Building has already been added.", 
				5, "edtBuildingName", false, "edtBuildingName");
			$this->loadBuilding();
		}
		else
		{
			$this->updateBuilding();
		}
	}

	function loadBuilding()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO building (
						client_id, area_id, building_name, building_notes, user_code, building_ip, building_date, last_update
						) VALUES (
						'".$this->selClientId."',
						'".$this->selAreaId."',
						'".addslashes($this->edtBuildingName)."',
						'".addslashes($this->txtBuildingNeeds)."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."',
						'".date('Y-m-d H:i:s')."',
						'".date('Y-m-d H:i:s')."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Building details were successfully added:
		if ($res)
		{
			$this->setBuildingId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Building added: '.$this->edtBuildingName, $this->hddnBuildingId);
			SableSystem::ajaxResponse(true, '/building/', 'The Building was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Building failed. Please try again or contact support.", 
				5, "edtBuildingName", false, "edtBuildingName");
	}

	function updateBuilding()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnBuildingId);
		$updateQuery = "UPDATE building set 
						client_id = '".$this->selClientId."', 
						area_id = '".$this->selAreaId."', 
						building_name = '".addslashes($this->edtBuildingName)."', 
						building_notes = '".addslashes($this->txtBuildingNeeds)."',
						building_notes = ".$buildNotes.", 
						user_code = '".$_SESSION['user']['user_code']."', 
						building_ip = '".SableGeneralFunctions::getRealIpAddr()."', 
						building_date = '".date('Y-m-d H:i:s')."', 
						last_update = '".date('Y-m-d H:i:s')."' 
						where building_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Building details modified: '.$this->edtBuildingName, $code);
			SableSystem::ajaxResponse(true, '/building/', 'The Building was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Building failed. Please try again or contact support.", 
				5, "edtBuildingName", false, "edtBuildingName");
	}

	private function getBuildingId()
	{
		global $sableDB;
		$sql = "select building_id from building where building_name = '".$this->edtBuildingName."' and client_id = ".$this->selClientId;
		return $sableDB->getValue($sql);
	}

	private function checkBuildingExists()
	{
		$cid = $this->getBuildingId();
		if (!empty($cid)) return true; else return false;
	}

	private function setBuildingId()
	{
		$this->hddnBuildingId = $this->getBuildingId();
	}

	function viewBuilding()
	{
		$url = '/building/view.php?'.SableSystem::sableDecode($this->hddnBuildingId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editBuilding()
	{
		$url = '/building/edit.php?'.SableSystem::sableDecode($this->hddnBuildingId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteBuilding()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnBuildingId);
		$cnt = $sableDB->getValue("select count(*) from building where building_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Building selected.", 
				5, "edtBuildingName", false, "edtBuildingName");
		else
		{
			$sql = "delete from building where building_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Building record deleted.', $code);
				SableSystem::ajaxPageRedirect('/building/','The Building record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Building failed to delete. Please try again later or contact support.", 
				5, "edtBuildingName", false, "edtBuildingName");
		}
	}
	
	function viewClient()
	{
		global $sableDB;
		$this->selClientId = $sableDB->getValue('select client_id from building where building_id = '.$this->hddnBuildingId);
		$url = '/client/view.php?'.SableSystem::sableDecode($this->selClientId);
		SableSystem::ajaxPageRedirect($url);
	}	

} //classBuilding
?>
