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

class Position
{
	// Properties of the class:
	var $hddnPositionId, $edtPositionTitle, $edtPositionDesc, $selAccessId;
	function __construct(){}
	function __destruct(){}

	function addPosition()
	{
		if (empty($this->hddnPositionId))
		{
			if ($this->checkPositionExists())
			SableSystem::ajaxResponse(false, "", "This Position has already been added.", 
				5, "edtPositionTitle", false, "edtPositionTitle");
			$this->loadPosition();
		}
		else
		{
			$this->updatePosition();
		}
	}

	function loadPosition()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO position (
						position_title, position_desc, access_id
						) VALUES (
						'".addslashes($this->edtPositionTitle)."',
'".addslashes($this->edtPositionDesc)."',
'".$this->selAccessId."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Position details were successfully added:
		if ($res)
		{
			$this->setPositionId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Position added: '.$this->edtPositionTitle, $this->hddnPositionId);
			SableSystem::ajaxResponse(true, '/position/', 'The Position was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Position failed. Please try again or contact support.", 
				5, "edtPositionTitle", false, "edtPositionTitle");
	}

	function updatePosition()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnPositionId);
		$updateQuery = "UPDATE position set 
						position_title = '".addslashes($this->edtPositionTitle)."', 
						position_desc = '".addslashes($this->edtPositionDesc)."', 
						access_id = '".$this->selAccessId."' 
						where position_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Position details modified: '.$this->edtPositionTitle, $code);
			SableSystem::ajaxResponse(true, '/position/', 'The Position was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Position failed. Please try again or contact support.", 
				5, "edtPositionTitle", false, "edtPositionTitle");
	}

	private function checkPositionExists()
	{
		global $sableDB;
		$sql = "select position_id from position where position_title = '".addslashes($this->edtPositionTitle)."' and 
				access_id = ".$this->selAccessId;
		$cid = $sableDB->getValue($sql);
		if (!empty($cid)) return true; else return false;
	}

	function viewPosition()
	{
		$url = '/position/view.php?'.SableSystem::sableDecode($this->hddnPositionId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editPosition()
	{
		$url = '/position/edit.php?'.SableSystem::sableDecode($this->hddnPositionId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deletePosition()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnPositionId);
		$cnt = $sableDB->getValue("select count(*) from position where position_id = ".$code,2,"CNT");
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Position selected.", 
				5, "edtPositionTitle", false, "edtPositionTitle");
		else
		{
			$sql = "delete from position where position_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "
				'Position record deleted.", $code);
				SableSystem::ajaxPageRedirect('/position/','The Position record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Position failed to delete. Please try again later or contact support.", 
				5, "edtPositionTitle", false, "edtPositionTitle");
		}
	}
	
	private function setPositionId()
	{
		global $sableDB;
		$sql = "select position_id from position where position_title = '".addslashes($this->edtPositionTitle)."' and 
				access_id = ".$this->selAccessId;
		$this->hddnPositionId = $sableDB->getValue($sql);
	}	

} //classPosition
?>
