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

class Postal_data
{
	// Properties of the class:
	var $hddnPostId, $edtSuburbName, $edtBoxCode, $edtStrCode, $edtRegionName;

	private $sableDB, $uc, $ts, $ip;

	function __construct()
	{
		$this->sableDB = new SableDB;
	}

	function __destruct()
	{
		unset($this->sableDB);
	}

	function addPostal_data()
	{
		$this->uc = $_SESSION['user']['user_code'];
		$this->ts = date('Y-m-d H:i:s');
		$this->ip = SableGeneralFunctions::getRealIpAddr();

		if (empty($this->hddnPostId))
		{
			if ($this->checkPostal_dataExists())
			SableSystem::ajaxResponse(false, "", "This Postal_data has already been added.", 
				5, "hddnPostId", false, "hddnPostId");
			$this->loadPostal_data();
		}
		else
		{
			$this->updatePostal_data();
		}
	}

	function loadPostal_data()
	{
		$insertQuery = "INSERT INTO postal_data (
						suburb_name, box_code, str_code, region_name
						) VALUES (
						'".SableGeneralFunctions::formatName($this->edtSuburbName)."',
						'".SableGeneralFunctions::formatName($this->edtBoxCode)."',
						'".SableGeneralFunctions::formatName($this->edtStrCode)."',
						'".SableGeneralFunctions::formatName($this->edtRegionName)."')";
		$res=$this->sableDB->updateQuery($insertQuery);
		//Check if the Postal_data details were successfully added:
		if ($res)
		{
			$this->setPostal_dataId();
			SableDBFunctions::loadAuditDetail($this->uc, 'New Postal_data added: '.$this->hddnPostId, $this->hddnPostId);
			SableSystem::ajaxResponse(true, '/postal_data/', 'The Postal_data was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Postal_data failed. Please try again or contact support.", 
				5, "hddnPostId", false, "hddnPostId");
	}

	function updatePostal_data()
	{
		$code = SableSystem::sableDecode($this->hddnPostId);
		$updateQuery = "UPDATE postal_data set 
						suburb_name = '".SableGeneralFunctions::formatName($this->edtSuburbName)."', 
						box_code = '".SableGeneralFunctions::formatName($this->edtBoxCode)."', 
						str_code = '".SableGeneralFunctions::formatName($this->edtStrCode)."', 
						region_name = '".SableGeneralFunctions::formatName($this->edtRegionName)."' 
						where post_id = '{$code}'";
		$res=$this->sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($this->uc, 
				'Postal_data details modified: '.$this->hddnPostId, $code);
			SableSystem::ajaxResponse(true, '/postal_data/', 'The Postal_data was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Postal_data failed. Please try again or contact support.", 
				5, "hddnPostId", false, "hddnPostId");
	}

	private function getPostal_dataId()
	{
		$sql = "select post_id from postal_data where post_id = '{$this->hddnPostId}'";
		return $this->sableDB->getValue($sql);
	}

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

	private function setPostal_dataId()
	{
		$this->hddnPostId = $this->getPostal_dataId();
	}

	function viewPostal_data()
	{
		$url = '/postal_data/view.php?'.SableSystem::sableDecode($this->hddnPostId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editPostal_data()
	{
		$url = '/postal_data/edit.php?'.SableSystem::sableDecode($this->hddnPostId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deletePostal_data()
	{
		$code = SableSystem::sableDecode($this->hddnPostId);
		$cnt = $this->sableDB->getValue("select count(*) from postal_data where post_id = {$code}");
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Postal_data selected.", 
				5, "hddnPostId", false, "hddnPostId");
		else
		{
			$sql = "delete from postal_data where post_id = '{$code}'";
			$res = $this->sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($this->uc, 'Postal_data record deleted.', $code);
				SableSystem::ajaxPageRedirect('/postal_data/','The Postal_data record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Postal_data failed to delete. Please try again later or contact support.", 
				5, "hddnPostId", false, "hddnPostId");
		}
	}

} //classPostal_data
