<?php
	require_once(__DIR__."/../includes/sysinit.php");
	class Legal
	{
	// Properties of the class:
var $hddnLegalId,
$edtLegalName,
$txtLegalText,
$cbLegalActive;
	function __construct(){}

	function __destruct(){}

	function addLegal()
	{
		if (empty($this->hddnLegalId))
		{
			if ($this->checkLegalExists())
SableSystem::ajaxResponse(false, "", "This Legal has already been added.", 5, "edtLegalName", false, "edtLegalName");
			$this->loadLegal();
		}
		else
		{
			$this->updateLegal();
		}
	}


	function loadLegal()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO legal (
						legal_name,
						legal_text,
						legal_active) VALUES (
						'".$this->edtLegalName."',
						'".htmlspecialchars(addslashes($this->txtLegalText),ENT_QUOTES)."',
						".$this->cbLegalActive.")";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Legal details were successfully added:
		if ($res)
		{
			$this->setLegalId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Legal added: '.$this->edtLegalName, $this->hddnLegalId);
			SableSystem::ajaxResponse(true, '/legal/', 'The Legal was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Legal failed. Please try again or contact support.", 5, "edtLegalName", 
									false, "edtLegalName");
}


	function updateLegal()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnLegalId);
		$updateQuery = "UPDATE legal set 
						legal_name = '".$this->edtLegalName."',
						legal_text = '".htmlspecialchars(addslashes($this->txtLegalText),ENT_QUOTES)."',
						legal_active = ".$this->cbLegalActive." where legal_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'Legal details modified: '.$this->edtLegalName, $code);
			SableSystem::ajaxResponse(true, '/legal/', 'The Legal was successfully updated.', 0, "", 0, "");
		}
		else 
SableSystem::ajaxResponse(false, "", "Modification of the Legal failed. Please try again or contact support.", 5, "edtLegalName", false, "edtLegalName");
	}


	function checkLegalExists()
	{
		global $sableDB;
		$sql = "select legal_id from legal where legal_name = '".$this->edtLegalName."'";
		$cid = $sableDB->getValue($sql);
		if (!empty($cid)) return true; else return false;
	}


	function viewLegal()
	{
		$url = '/legal/view.php?'.SableSystem::sableDecode($this->hddnLegalId);
		SableSystem::ajaxPageRedirect($url);
	}


	function editLegal()
	{
		$url = '/legal/edit.php?'.SableSystem::sableDecode($this->hddnLegalId);
		SableSystem::ajaxPageRedirect($url);
	}


	function deleteLegal()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnLegalId);
		$cnt = $sableDB->getValue("select count(*) from legal where legal_id = ".$code,2,"CNT");
		if ($cnt <= 0)
SableSystem::ajaxResponse(false, "", "Delete failed - invalid Legal selected.", 5, "edtLegalName", false, "edtLegalName");
		else
		{
			$sql = "delete from legal where legal_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "Legal record deleted.", $code);
				SableSystem::ajaxPageRedirect('/legal/','The Legal record was successfully deleted.');
			}
			else
				SableSystem::ajaxResponse(false, "", "System error: The Legal failed to delete. Please try again later or contact support.",
										 5, "edtLegalName", false, "edtLegalName");
		}
	}
	
	private function setLegalId()
	{
		global $sableDB;
		$sql = "select legal_id from legal where legal_name = '".$this->edtLegalName."'";
		$this->hddnLegalId = $sableDB->getValue($sql);
	}	


} //classLegal
?>
