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

class Country
{
	// Properties of the class:
	var $hddnCountryId, $edtCountryIso, $edtCountryName, $edtCountryNicename, $edtCountryIso3, $edtCountryNumcode, $edtCountryPhonecode;

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

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

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

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

		if (empty($this->hddnCountryId))
		{
			if ($this->checkCountryExists())
			SableSystem::ajaxResponse(false, "", "This Country has already been added.", 
				5, "hddnCountryId", false, "hddnCountryId");
			$this->loadCountry();
		}
		else
		{
			$this->updateCountry();
		}
	}

	function loadCountry()
	{
		$insertQuery = "INSERT INTO country (
						country_iso, country_name, country_nicename, country_iso3, country_numcode, country_phonecode
						) VALUES (
						'".SableGeneralFunctions::formatName($this->edtCountryIso)."',
						'".SableGeneralFunctions::formatName($this->edtCountryName)."',
						'".SableGeneralFunctions::formatName($this->edtCountryNicename)."',
						'".SableGeneralFunctions::formatName($this->edtCountryIso3)."',
						'".SableGeneralFunctions::formatName($this->edtCountryNumcode)."',
						'".SableGeneralFunctions::formatName($this->edtCountryPhonecode)."')";
		$res=$this->sableDB->updateQuery($insertQuery);
		//Check if the Country details were successfully added:
		if ($res)
		{
			$this->setCountryId();
			SableDBFunctions::loadAuditDetail($this->uc, 'New Country added: '.$this->hddnCountryId, $this->hddnCountryId);
			SableSystem::ajaxResponse(true, '/country/', 'The Country was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Country failed. Please try again or contact support.", 
				5, "hddnCountryId", false, "hddnCountryId");
	}

	function updateCountry()
	{
		$code = SableSystem::sableDecode($this->hddnCountryId);
		$updateQuery = "UPDATE country set 
						country_iso = '".SableGeneralFunctions::formatName($this->edtCountryIso)."', 
						country_name = '".SableGeneralFunctions::formatName($this->edtCountryName)."', 
						country_nicename = '".SableGeneralFunctions::formatName($this->edtCountryNicename)."', 
						country_iso3 = '".SableGeneralFunctions::formatName($this->edtCountryIso3)."', 
						country_numcode = '".SableGeneralFunctions::formatName($this->edtCountryNumcode)."', 
						country_phonecode = '".SableGeneralFunctions::formatName($this->edtCountryPhonecode)."' 
						where country_id = '{$code}'";
		$res=$this->sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($this->uc, 
				'Country details modified: '.$this->hddnCountryId, $code);
			SableSystem::ajaxResponse(true, '/country/', 'The Country was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Country failed. Please try again or contact support.", 
				5, "hddnCountryId", false, "hddnCountryId");
	}

	private function getCountryId()
	{
		$sql = "select country_id from country where country_id = '{$this->hddnCountryId}'";
		return $this->sableDB->getValue($sql);
	}

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

	private function setCountryId()
	{
		$this->hddnCountryId = $this->getCountryId();
	}

	function viewCountry()
	{
		$url = '/country/view.php?'.SableSystem::sableDecode($this->hddnCountryId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editCountry()
	{
		$url = '/country/edit.php?'.SableSystem::sableDecode($this->hddnCountryId);
		SableSystem::ajaxPageRedirect($url);
	}

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

} //classCountry
