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

class Address
{
	// Properties of the class:
	var $hddnAddressId, $hddnClientId, $hddnAddressTypeId, $hddnCountryId, $hddnPostId, $edtAddressStreet, $edtAddressSuburb, $edtAddressCity, 
		$edtAddressCounty, $edtAddressPostalCode, $edtAddressLng, $edtAddressLat;

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

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

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

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

		if (empty($this->hddnAddressId))
		{
			/*if ($this->checkAddressExists())
			SableSystem::ajaxResponse(false, "", "This Address has already been added.", 
				5, "hddnAddressId", false, "hddnAddressId");*/
			$this->loadAddress();
		}
		else
		{
			$this->updateAddress();
		}
	}

	function loadAddress()
	{
		$insertQuery = "INSERT INTO address (
						client_id, address_type_id, country_id, post_id, address_street, address_suburb, address_city, address_county, address_postal_code, address_lng, address_lat
						) VALUES (
						'{$this->hddnClientId}',
						'{$this->hddnAddressTypeId}',
						'{$this->hddnCountryId}',
						'{$this->hddnPostId}',
						'".SableGeneralFunctions::formatName($this->edtAddressStreet)."',
						'".SableGeneralFunctions::formatName($this->edtAddressSuburb)."',
						'".SableGeneralFunctions::formatName($this->edtAddressCity)."',
						'".SableGeneralFunctions::formatName($this->edtAddressCounty)."',
						'".SableGeneralFunctions::formatName($this->edtAddressPostalCode)."',
						'".SableGeneralFunctions::formatName($this->edtAddressLng)."',
						'".SableGeneralFunctions::formatName($this->edtAddressLat)."')";
		$res=$this->sableDB->updateQuery($insertQuery);
		//Check if the Address details were successfully added:
		if ($res)
		{
			$this->setAddressId();
			SableDBFunctions::loadAuditDetail($this->uc, 'New Address added: '.$this->hddnAddressId, $this->hddnAddressId);
			SableSystem::ajaxResponse(true, '/address/', 'The Address was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Address failed. Please try again or contact support.", 
				5, "hddnAddressId", false, "hddnAddressId");
	}

	function updateAddress()
	{
		$code = SableSystem::sableDecode($this->hddnAddressId);
		$updateQuery = "UPDATE address set 
						client_id = '{$this->hddnClientId}', 
						address_type_id = '{$this->hddnAddressTypeId}', 
						country_id = '{$this->hddnCountryId}', 
						post_id = '{$this->hddnPostId}', 
						address_street = '".SableGeneralFunctions::formatName($this->edtAddressStreet)."', 
						address_suburb = '".SableGeneralFunctions::formatName($this->edtAddressSuburb)."', 
						address_city = '".SableGeneralFunctions::formatName($this->edtAddressCity)."', 
						address_county = '".SableGeneralFunctions::formatName($this->edtAddressCounty)."', 
						address_postal_code = '".SableGeneralFunctions::formatName($this->edtAddressPostalCode)."', 
						address_lng = '".SableGeneralFunctions::formatName($this->edtAddressLng)."', 
						address_lat = '".SableGeneralFunctions::formatName($this->edtAddressLat)."' 
						where address_id = '{$code}'";
		$res=$this->sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($this->uc, 
				'Address details modified: '.$this->hddnAddressId, $code);
			SableSystem::ajaxResponse(true, '/address/', 'The Address was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Address failed. Please try again or contact support.", 
				5, "hddnAddressId", false, "hddnAddressId");
	}

	private function getAddressId()
	{
		$sql = "select address_id from address where address_id = '{$this->hddnAddressId}'";
		return $this->sableDB->getValue($sql);
	}

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

	private function setAddressId()
	{
		$this->hddnAddressId = $this->getAddressId();
	}

	function viewAddress()
	{
		$url = '/address/view.php?'.SableSystem::sableDecode($this->hddnAddressId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editAddress()
	{
		$url = '/address/edit.php?'.SableSystem::sableDecode($this->hddnAddressId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classAddress
