<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

defined('_JEXEC') or die();
 
jimport( 'joomla.application.component.model' );

class rseventsModelCategories extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
	
		global $mainframe;
		$db =& JFactory::getDBO();
		
		$sortColumn=$mainframe->getUserStateFromRequest('com_rsevents.categories.sortColumn','filter_order','ordering');
		$sortOrder=$mainframe->getUserStateFromRequest('com_rsevents.categories.sortOrder','filter_order_Dir','asc');
		
		$mainframe->setUserState('com_rsevents.categories.sortColumn',$sortColumn);
		$mainframe->setUserState('com_rsevents.categories.sortOrder',$sortOrder);
		
		$filter=JRequest::getVar('rs_filter');

		
		$this->_query="SELECT IdCategory as RecordId, CategoryName , CategoryDescription as CatDes , published , ordering FROM #__rsevents_categories WHERE CategoryName LIKE '%".$db->getEscaped($filter)."%' ORDER BY $sortColumn $sortOrder";
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest($option.'.categories.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.categories.limitstart', 'limitstart', 0, 'int');

		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState($option.'.categories.limit', $limit);
		$this->setState($option.'.categories.limitstart', $limitstart);
		

		//echo $this->_db->_errorMsg;
	}
	function getData()
	{
		global $option;
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState($option.'.categories.limitstart'), $this->getState($option.'.categories.limit'));
		}
		return $this->_data;
	}
	function getTotal()
	{
		// Load the content if it doesn't already exist
		if (empty($this->_total))
		{
			$this->_total = $this->_getListCount($this->_query);	
		}
		return $this->_total;
	}
	function getPagination()
	{
		global $option;
		// Load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState($option.'.categories.limitstart'), $this->getState($option.'.categories.limit'));
		}
		return $this->_pagination;
	}
	
	function getCategories()
	{
		$idCategory= JRequest::getVar('cid');
		if(is_array($idCategory)) $idCategory=$idCategory[0];
		$row= & JTable::getInstance('Rsevents_Categories','Table');
		$row->load($idCategory);
		return $row;
	}
	
 
}