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

class Area
{
	// Properties of the class:
	var $hddnAreaId, $edtAreaName;
	function __construct(){}
	function __destruct(){}

	function addArea()
	{
		if (empty($this->hddnAreaId))
		{
			if ($this->checkAreaExists())
			SableSystem::ajaxResponse(false, "", "This Area has already been added.", 
				5, "edtAreaName", false, "edtAreaName");
			$this->loadArea();
		}
		else
		{
			$this->updateArea();
		}
	}

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

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

	private function getAreaId()
	{
		global $sableDB;
		$sql = "select area_id from area where area_name = '".$this->edtAreaName."'";
		return $sableDB->getValue($sql);
	}

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

	private function setAreaId()
	{
		$this->hddnAreaId = $this->getAreaId();
	}

	function viewArea()
	{
		$url = '/area/view.php?'.SableSystem::sableDecode($this->hddnAreaId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editArea()
	{
		$url = '/area/edit.php?'.SableSystem::sableDecode($this->hddnAreaId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classArea
?>
