<?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 rseventsModelArchive extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
		global $mainframe;
		$db =& JFactory::getDBO();
		
		$filter=JRequest::getVar('rs_filter');
		$category_filter=JRequest::getVar('rs_cat_filter');
		$location_filter=JRequest::getVar('rs_loc_filter');
		
		if($category_filter)
		$cat_filter="AND ec.IdCategory = ".$db->getEscaped($category_filter)." ";
		else
		$cat_filter="";
		
		if($location_filter)
		$loc_filter="AND l.LocationName LIKE '%".$db->getEscaped($location_filter)."%'";
		else
		$loc_filter="";
		
		$sortOrder=JRequest::getVar('filter_order_Dir','desc');
		$sortColumn=JRequest::getVar('filter_order','e.EventStartDate');
		
		$this->_query="SELECT e.IdEvent as RecordId, e.EventName ,e.EventEnableRegistration ,  e.IdLocation , e.EventStartDate , e.EventEndDate, e.IdUser , e.IdParent , e.published , l.LocationName , COUNT(IdSubscription) as subscribers  , u.username "
						."FROM #__rsevents_events e "
						."LEFT JOIN #__rsevents_locations l ON e.IdLocation = l.IdLocation "
						."LEFT JOIN #__rsevents_events_cat ec ON ec.IdEvent = e.IdEvent "
						."LEFT JOIN #__rsevents_categories c ON c.IdCategory = ec.IdCategory "
						."LEFT JOIN #__rsevents_subscriptions s ON e.IdEvent = s.IdEvent "
						."LEFT JOIN #__users u ON e.IdUser = u.id "
						."WHERE e.EventName LIKE '%".$db->getEscaped($filter)."%' $cat_filter $loc_filter AND e.published = '-1' "
						."GROUP BY e.IdEvent "
						."ORDER BY $sortColumn $sortOrder ";
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

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

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

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

		//echo $this->_db->_errorMsg;
	}
	function getData()
	{
		global $option;
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState($option.'.archieve.limitstart'), $this->getState($option.'.archieve.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.'.archieve.limitstart'), $this->getState($option.'.archieve.limit'));
		}
		return $this->_pagination;
	}	 
}