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

class AreaType
{
	// Properties of the class:
	var $hddnAreaTypeId, $edtAreaTypeName;
	function __construct(){}
	function __destruct(){}

	function addAreaType()
	{
		if (empty($this->hddnAreaTypeId))
		{
			if ($this->checkAreaTypeExists())
			SableSystem::ajaxResponse(false, "", "This Area Type has already been added.", 
				5, "edtAreaTypeName", false, "edtAreaTypeName");
			$this->loadAreaType();
		}
		else
		{
			$this->updateAreaType();
		}
	}

	function loadAreaType()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO area_type (
						area_type_name
						) VALUES (
						'".addslashes($this->edtAreaTypeName)."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Area Type details were successfully added:
		if ($res)
		{
			$this->setAreaTypeId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Area Type added: '.$this->edtAreaTypeName, $this->hddnAreaTypeId);
			SableSystem::ajaxResponse(true, '/area_type/', 'The Area Type was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Area Type failed. Please try again or contact support.", 
				5, "edtAreaTypeName", false, "edtAreaTypeName");
	}

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

	private function getAreaTypeId()
	{
		global $sableDB;
		$sql = "select area_type_id from area_type where area_type_name = '".$this->edtAreaTypeName."'";
		return $sableDB->getValue($sql);
	}

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

	private function setAreaTypeId()
	{
		$this->hddnAreaTypeId = $this->getAreaTypeId();
	}

	function viewAreaType()
	{
		$url = '/area_type/view.php?'.SableSystem::sableDecode($this->hddnAreaTypeId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editAreaType()
	{
		$url = '/area_type/edit.php?'.SableSystem::sableDecode($this->hddnAreaTypeId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classAreaType
?>
