<?php
class Category
{
	var $bLoaded=false;
	var $cat_id = 0;
	var $cat_name = '';
	var $is_active = 0;
	function Category($db_row=false)
	{
		if($db_row)
		{
			$this->cat_id = $db_row->cat_id;
			$this->cat_name = $db_row->cat_name;
			$this->is_active = $db_row->is_active;
			$this->bLoaded=true;
		}
	}
	function &getByID($id)
	{
		global $db;
		$sql = 'SELECT * FROM '.$db->prefix('arcade_cats').' WHERE cat_id='.$id;
		$ret = false;
		if($result = $db->query($sql))
		{
			if($row = $db->fetch_object($result))
			{
				$ret = new Category($row);
			}
		}
		return $ret;
	}
	function &getAllWhere($arrConditions=false, $order_by='', $limit=0, $offset=0)
	{
		global $db;
		$sql = 'SELECT * FROM '.$db->prefix('arcade_cats');
		if($arrConditions && count($arrConditions) > 0)	
		{
			$sql .= " WHERE ";
			for ($i=0; $i<count($arrConditions); $i++)
			{
				if ($i < count($arrConditions)-1)
					$sql .= $arrConditions[$i].' AND ';
				else
					$sql .= $arrConditions[$i].' ';
			}
		}
		if($order_by!='')
		{
			$sql .= " ORDER BY $order_by ";
		}
		$ret = array();
		if($result = $db->query($sql, intval($limit), intval($offset)))
		{
			while($row = $db->fetch_object($result))
			{
				$ret[] = new Category($row);
			}
		}
		return $ret;
	}
	function store()
	{
		global $db;
		$sql = '';
		if($this->bLoaded)
		{
			$sql = "UPDATE ".$db->prefix('arcade_cats')." SET ";
			$sql .= " cat_name='$this->cat_name',";
			$sql .= " is_active='$this->is_active'";
			$sql .= " WHERE cat_id='$this->cat_id'";
		}
		else
		{
			$sql = "INSERT INTO ".$db->prefix('arcade_cats')." SET ";
			$sql .= " cat_id='$this->cat_id',";
			$sql .= " cat_name='$this->cat_name',";
			$sql .= " is_active='$this->is_active'";
		}
		$result = $db->query($sql);
		if($result) $this->cat_id = $db->getInsertId();
		return $result;
	}
	function delete()
	{
		global $db;
		$sql = 'DELETE FROM '.$db->prefix('arcade_cats').' WHERE cat_id='.$this->cat_id;
		$result = $db->query($sql);
		if($result) $this->bloaded = false;
		return $result;
	}
}
?>
