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

class Vehicle
{
	// Properties of the class:
	var $hddnVehicleId, $edtVehicleReg, $edtVehicleName, $edtVehicleDesc, $cbVehicleActive;
	function __construct(){}
	function __destruct(){}

	function addVehicle()
	{
		if (empty($this->hddnVehicleId))
		{
			if ($this->checkVehicleExists())
			SableSystem::ajaxResponse(false, "", "This Vehicle has already been added.", 
				5, "edtVehicleName", false, "edtVehicleName");
			$this->loadVehicle();
		}
		else
		{
			$this->updateVehicle();
		}
	}

	function loadVehicle()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO vehicle (
						vehicle_reg, vehicle_name, vehicle_desc, vehicle_active, vehicle_timestamp, vehicle_user, vehicle_ip, last_update
						) VALUES (
						'".addslashes($this->edtVehicleReg)."',
						'".addslashes($this->edtVehicleName)."',
						'".addslashes($this->edtVehicleDesc)."',
						".$this->cbVehicleActive.",
						'".date('Y-m-d H:i:s')."',
						'".$_SESSION['user']['user_code']."',
						'".SableGeneralFunctions::getRealIpAddr()."',
						'".date('Y-m-d H:i:s')."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the Vehicle details were successfully added:
		if ($res)
		{
			$this->setVehicleId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New Vehicle added: '.$this->edtVehicleName, $this->hddnVehicleId);
			SableSystem::ajaxResponse(true, '/vehicle/', 'The Vehicle was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new Vehicle failed. Please try again or contact support.", 
				5, "edtVehicleName", false, "edtVehicleName");
	}

	function updateVehicle()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnVehicleId);
		$updateQuery = "UPDATE vehicle set 
						vehicle_reg = '".addslashes($this->edtVehicleReg)."', 
						vehicle_name = '".addslashes($this->edtVehicleName)."', 
						vehicle_desc = '".addslashes($this->edtVehicleDesc)."', 
						vehicle_active = ".$this->cbVehicleActive.", 
						vehicle_timestamp = '".date('Y-m-d H:i:s')."', 
						vehicle_user = '".$_SESSION['user']['user_code']."', 
						vehicle_ip = '".SableGeneralFunctions::getRealIpAddr()."', 
						last_update = '".date('Y-m-d H:i:s')."' 
						where vehicle_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 
				'Vehicle details modified: '.$this->edtVehicleName, $code);
			SableSystem::ajaxResponse(true, '/vehicle/', 'The Vehicle was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the Vehicle failed. Please try again or contact support.", 
				5, "edtVehicleName", false, "edtVehicleName");
	}

	private function getVehicleId()
	{
		global $sableDB;
		$sql = "select vehicle_id from vehicle where vehicle_id = '".$this->hddnVehicleId."'";
		return $sableDB->getValue($sql);
	}

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

	private function setVehicleId()
	{
		$this->hddnVehicleId = $this->getVehicleId();
	}

	function viewVehicle()
	{
		$url = '/vehicle/view.php?'.SableSystem::sableDecode($this->hddnVehicleId);
		SableSystem::ajaxPageRedirect($url);
	}

	function editVehicle()
	{
		$url = '/vehicle/edit.php?'.SableSystem::sableDecode($this->hddnVehicleId);
		SableSystem::ajaxPageRedirect($url);
	}

	function deleteVehicle()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnVehicleId);
		$cnt = $sableDB->getValue("select count(*) from vehicle where vehicle_id = ".$code);
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid Vehicle selected.", 
				5, "edtVehicleName", false, "edtVehicleName");
		else
		{
			$sql = "delete from vehicle where vehicle_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], 'Vehicle record deleted.', $code);
				SableSystem::ajaxPageRedirect('/vehicle/','The Vehicle record was successfully deleted.');
			}
			else
			SableSystem::ajaxResponse(false, "", "
			System error: The Vehicle failed to delete. Please try again later or contact support.", 
				5, "edtVehicleName", false, "edtVehicleName");
		}
	}

} //classVehicle
?>
