<?php
require_once(__DIR__."/../includes/sysinit.php");
class Category 
{
	// Properties of the class:
	var $hddnCategoryId, $edtCategoryName;
	
	function __construct(){}
	function __destruct(){}
	
	function addCategory()
	{
		if (empty($this->hddnCategoryId))
		{
			if ($this->checkCategoryExists())
				SableSystem::ajaxResponse(false, "", "This category has already been added.", 5, "edtCategoryName", false, 
										"edtCategoryName");
			$this->loadCategory();
		}
		else
		{
			$this->updateCategory();
		}
	}
	
	function loadCategory()
	{
		global $sableDB;
		$insertQuery = "INSERT INTO category (category_name) VALUES ('".$this->edtCategoryName."')";
		$res=$sableDB->updateQuery($insertQuery);
		//Check if the category details were successfully added:
		if ($res)
		{
			$this->setCategoryId();
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'New category added: '.$this->edtCategoryName, 
											$this->hddnCategoryId);
			SableSystem::ajaxResponse(true, '/category/', 'The category was successfully added.', 0, "", 0, "");
		}
		else
			SableSystem::ajaxResponse(false, "", "Add of a new category failed. Please try again or contact support.", 5, 
									"edtCategoryName", false, "edtCategoryName");
	}
	
	function updateCategory()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnCategoryId);
		$updateQuery = "UPDATE category set category_name = '".$this->edtCategoryName."' where category_id = '".$code."'";
		$res=$sableDB->updateQuery($updateQuery);
		//Check if the details were successfully updated:
		if ($res)
		{
			SableDBFunctions::loadAuditDetail($_SESSION['user']['user_code'], 'Category details modified: '.$this->edtCategoryName, $code);
			SableSystem::ajaxResponse(true, '/category/', 'The Category was successfully updated.', 0, "", 0, "");
		}
		else 
			SableSystem::ajaxResponse(false, "", "Modification of the category failed. Please try again or contact support.", 5,
									 "edtCategoryName", false, "edtCategoryName");
	}
	
	function checkCategoryExists()
	{
	   global $sableDB;
		  $sql = "select category_id from category where category_id = '".$this->hddnCategoryId."'";
		  $cid = $sableDB->getValue($sql);
		  if (!empty($cid)) return true; else return false;
	}
	
	function viewCategory()
	{
		$url = '/category/view.php?'.SableSystem::sableDecode($this->hddnCategoryId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function editCategory()
	{
		$url = '/category/edit.php?'.SableSystem::sableDecode($this->hddnCategoryId);
		SableSystem::ajaxPageRedirect($url);
	}
	
	function deleteCategory()
	{
		global $sableDB;
		$code = SableSystem::sableDecode($this->hddnCategoryId);
		$cnt = $sableDB->getValue("select count(*) from category where category_id = ".$code,2,"CNT");
		if ($cnt <= 0)
			SableSystem::ajaxResponse(false, "", "Delete failed - invalid category selected.", 5, "edtCategoryName", 
									false, "edtCategoryName");
		else
		{
			$sql = "delete from category where category_id = '".$code."'";
			$res = $sableDB->updateQuery($sql);
			if ($res)
			{
				SableDBFunctions::loadAuditDetail($_SESSION["user"]["user_code"], "category record deleted.", $code);
				SableSystem::ajaxPageRedirect('/category/','The category record was successfully deleted.');
			}
			else
				SableSystem::ajaxResponse(false, "", 
				"System error: The category failed to delete. Please try again later or contact support.", 
				5, "edtCategoryName", false, "edtCategoryName");
		}
	}
	
	private function setCategoryId()
	{
		global $sableDB;
		$sql = "select category_id from category_name = '".$this->edtCategoryName."'";
		$this->hddnAccessId = $sableDB->getValue($sql);
	}
} //classCategory
?>
