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

class AreaSize
{
	// Properties of the class:
	var $hddnAreaSizeId, $selAreaTypeId, $edtAreaSizeName;
	function __construct(){}
	function __destruct(){}

	function addAreaSize()
	{
		if (empty($this->hddnAreaSizeId))
		{
			if ($this->checkAreaSizeExists())
			SableSystem::ajaxResponse(false, "", "This Area Size has already been added.", 
				5, "edtAreaSizeName", false, "edtAreaSizeName");
			$this->loadAreaSize();
		}
		else
		{
			$this->updateAreaSize();
		}
	}

	function loadAreaSize()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO area_size (
						area_type_id, area_size_name
						) VALUES (
						'".$this->selAreaTypeId."',
						'".addslashes($this->edtAreaSizeName)."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Area Size details were successfully added:
		if ($res)
		{
			$this->setAreaSizeId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Area Size added: '.$this->edtAreaSizeName, $this->hddnAreaSizeId);
			SableSystem::ajaxResponse(true, '/area_size/', 'The Area Size was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Area Size failed. Please try again or contact support.", 
				5, "edtAreaSizeName", false, "edtAreaSizeName");
	}

	function updateAreaSize()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAreaSizeId);
		$updateQuery = "UPDATE area_size set 
						area_type_id = '".$this->selAreaTypeId."', 
						area_size_name = '".addslashes($this->edtAreaSizeName)."' 
						where area_size_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Area Size details modified: '.$this->edtAreaSizeName, $code);
			SableSystem::ajaxResponse(true, '/area_size/', 'The Area Size was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Area Size failed. Please try again or contact support.", 
				5, "edtAreaSizeName", false, "edtAreaSizeName");
	}

	private function getAreaSizeId()
	{
		global $sableDB;
		$sql = "select area_size_id from area_size where area_size_id = '".$this->hddnAreaSizeId."'";
		return $sableDB->getValue($sql);
	}

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

	private function setAreaSizeId()
	{
		$this->hddnAreaSizeId = $this->getAreaSizeId();
	}

	function viewAreaSize()
	{
		$url = '/area_size/view.php?'.SableSystem::sableDecode($this->hddnAreaSizeId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editAreaSize()
	{
		$url = '/area_size/edit.php?'.SableSystem::sableDecode($this->hddnAreaSizeId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteAreaSize()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAreaSizeId);
		$cnt = $sableDB->getValue("select count(*) from area_size where area_size_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Area Size selected.", 
				5, "edtAreaSizeName", false, "edtAreaSizeName");
		else
		{
			$sql = "delete from area_size where area_size_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Area Size record deleted.', $code);
				SableSystem::ajaxPageRedirect('/area_size/','The Area Size record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Area Size failed to delete. Please try again later or contact support.", 
				5, "edtAreaSizeName", false, "edtAreaSizeName");
		}
	}

} //classAreaSize
?>
