<?php
require_once(__DIR__."/../includes/sysinit.php");
class Access 
{
	// Properties of the class:
	var $hddnAccessId, $edtAccessName, $edtAccessLevel;
	
	function __construct(){}
	function __destruct(){}
	
	function addAccess()
	{
		if (empty($this->hddnAccessId))
		{
			if ($this->checkAccessExists())
			SableSystem::ajaxResponse(false, "", "This access has already been added.", 5, "edtAccessName", false, "edtAccessName");
			$this->loadAccess();
		}
		else
		{
			$this->updateAccess();
		}
	}
	
	function loadAccess()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO access (
						access_name,
						access_level) VALUES (
						'".$this->edtAccessName."',
						'".$this->edtAccessLevel."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the access details were successfully added:
		if ($res)
		{
			$this->setAccessId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New access added: '.$this->edtAccessName, $this->hddnAccessId);
			SableSystem::ajaxResponse(true, '/access/', 'The access was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new access failed. Please try again or contact support.", 5, 
									"edtAccessName", false, "edtAccessName");
	}
	
	function updateAccess()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAccessId);
		$updateQuery = "UPDATE access set 
						access_name = '".$this->edtAccessName."',
						access_level = '".$this->edtAccessLevel."' where access_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'Access details modified: '.$this->edtAccessName, $code);
			SableSystem::ajaxResponse(true, '/access/', 'The Access was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the access failed. Please try again or contact support.", 5, 
									"edtAccessName", false, "edtAccessName");
	}
	
	function checkAccessExists()
	{
		global $sableDB;
		$sql = "select access_id from access where access_name = '".$this->edtAccessName."' and access_level = '".$this->edtAccessLevel."'";
		$cid = $sableDB->getValue($sql);
		if (!empty($cid)) return true; else return false;
	}
	
	function viewAccess()
	{
		$url = '/access/view.php?'.SableSystem::sableDecode($this->hddnAccessId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function editAccess()
	{
		$url = '/access/edit.php?'.SableSystem::sableDecode($this->hddnAccessId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function copyAccess()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAccessId);
		$newId = $sableDB->duplicateRow("access", "access_id", $code);
		if($newId)
		{
			SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "access record duplicated.", $code);
			SableSystem::ajaxPageRedirect("/access/", "The access record was successfully duplicated.");
		}
		else
			SableSystem::ajaxResponse(false, "", "System error: The access failed to duplicate. Please try again later or contact support.", 
										5, "edtAccessName", false, "edtAccessName");
	}
	
	function deleteAccess()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAccessId);
		$cnt = $sableDB->getValue("select count(*) from access where access_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid access selected.", 5, "edtAccessName", false, "edtAccessName");
		else
		{
			$sql = "delete from access where access_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "access record deleted.", $code);
				SableSystem::ajaxPageRedirect('/access/','The access was successfully deleted.');
			}
			else
				SableSystem::ajaxResponse(false, "", 
					"System error: The access failed to delete. Please try again later or contact support.", 
					5, "edtAccessName", false, "edtAccessName");
		}
	}
	
	private function setAccessId()
	{
		global $sableDB;
		$sql = "select access_id from access where access_name = '".$this->edtAccessName."' and access_level = '".$this->edtAccessLevel."'";
		$this->hddnAccessId = $sableDB->getValue($sql);
	}
} //classAccess
?>
