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

class Address_type
{
	// Properties of the class:
	var $hddnAddressTypeId, $edtAddressTypeName;

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

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

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

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

		if (empty($this->hddnAddressTypeId))
		{
			if ($this->checkAddress_typeExists())
			SableSystem::ajaxResponse(false, "", "This Address_type has already been added.", 
				5, "hddnAddressTypeId", false, "hddnAddressTypeId");
			$this->loadAddress_type();
		}
		else
		{
			$this->updateAddress_type();
		}
	}

	function loadAddress_type()
	{
		$insertQuery = "INSERT INTO address_type (
						address_type_name
						) VALUES (
						'".SableGeneralFunctions::formatName($this->edtAddressTypeName)."')";
		$res=$this->sableDB->updateQuery($insertQuery);
		//Check if the Address_type details were successfully added:
		if ($res)
		{
			$this->setAddress_typeId();
			SableDBFunctions::loadAuditDetail($this->uc, 'New Address_type added: '.$this->hddnAddressTypeId, $this->hddnAddressTypeId);
			SableSystem::ajaxResponse(true, '/address_type/', 'The Address_type was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Address_type failed. Please try again or contact support.", 
				5, "hddnAddressTypeId", false, "hddnAddressTypeId");
	}

	function updateAddress_type()
	{
		$code = SableSystem::sableDecode($this->hddnAddressTypeId);
		$updateQuery = "UPDATE address_type set 
						address_type_name = '".SableGeneralFunctions::formatName($this->edtAddressTypeName)."' 
						where address_type_id = '{$code}'";
		$res=$this->sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($this->uc, 
				'Address_type details modified: '.$this->hddnAddressTypeId, $code);
			SableSystem::ajaxResponse(true, '/address_type/', 'The Address_type was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Address_type failed. Please try again or contact support.", 
				5, "hddnAddressTypeId", false, "hddnAddressTypeId");
	}

	private function getAddress_typeId()
	{
		$sql = "select address_type_id from address_type where address_type_id = '{$this->hddnAddressTypeId}'";
		return $this->sableDB->getValue($sql);
	}

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

	private function setAddress_typeId()
	{
		$this->hddnAddressTypeId = $this->getAddress_typeId();
	}

	function viewAddress_type()
	{
		$url = '/address_type/view.php?'.SableSystem::sableDecode($this->hddnAddressTypeId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editAddress_type()
	{
		$url = '/address_type/edit.php?'.SableSystem::sableDecode($this->hddnAddressTypeId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classAddress_type
