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

class Audit
{
	// Properties of the class:
	var $hddnAuditId, $edtItemId, $edtAuditDesc, $edtAuditCode, $selUserCode, $edtAuditTime, $edtIpAddress;
	function __construct(){}
	function __destruct(){}

	function addAudit()
	{
		if (empty($this->hddnAuditId))
		{
			if ($this->checkAuditExists())
			SableSystem::ajaxResponse(false, "", "This Audit has already been added.", 
				5, "edtAuditDesc", false, "edtAuditDesc");
			$this->loadAudit();
		}
		else
		{
			$this->updateAudit();
		}
	}

	function loadAudit()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO audit (
						item_id, audit_desc, audit_code, user_code, audit_time, ip_address
						) VALUES (
						'".$this->edtItemId."',
'".addslashes($this->edtAuditDesc)."',
'".addslashes($this->edtAuditCode)."',
'".$this->selUserCode."',
'".addslashes($this->edtAuditTime)."',
'".addslashes($this->edtIpAddress)."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Audit details were successfully added:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Audit added: '.$this->edtAuditDesc, 0);
			SableSystem::ajaxResponse(true, '/audit/', 'The Audit was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Audit failed. Please try again or contact support.", 
				5, "edtAuditDesc", false, "edtAuditDesc");
	}

	function updateAudit()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnAuditId);
		$updateQuery = "UPDATE audit set 
						item_id = '".$this->edtItemId."', 
						audit_desc = '".addslashes($this->edtAuditDesc)."', 
						audit_code = '".addslashes($this->edtAuditCode)."', 
						user_code = '".$this->selUserCode."', 
						audit_time = '".addslashes($this->edtAuditTime)."', 
						ip_address = '".addslashes($this->edtIpAddress)."' 
						where audit_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Audit details modified: '.$this->edtAuditDesc, 0);
			SableSystem::ajaxResponse(true, '/audit/', 'The Audit was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Audit failed. Please try again or contact support.", 
				5, "edtAuditDesc", false, "edtAuditDesc");
	}

	function checkAuditExists()
	{
		global $sableDB;
		$sql = "select audit_id from audit where audit_id = '".$this->hddnAuditId."'";
		$cid = $sableDB->getValue($sql);
		if (!empty($cid)) return true; else return false;
	}

	function viewAudit()
	{
		$url = '/audit/view.php?'.SableSystem::sableDecode($this->hddnAuditId);
		SableSystem::ajaxPageRedirect($url);
	}

} //classAudit
?>
