<?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 rseventsModelEventElement extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
		global $mainframe;
		
		$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 c.CategoryName LIKE '%$category_filter%'";
		else
		$cat_filter="";
		
		if($location_filter)
		$loc_filter="AND l.LocationName LIKE '%$location_filter%'";
		else
		$loc_filter="";
		
		$sortOrder=JRequest::getVar('filter_order_Dir','asc');
		$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.published , l.LocationName , c.CategoryName , COUNT(IdSubscription) as subscribers  "
						."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 "
						."WHERE e.EventName LIKE '%$filter%' $cat_filter $loc_filter "
						."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('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');

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

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

		//echo $this->_db->_errorMsg;
	}
	function getData()
	{
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState('limitstart'), $this->getState('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()
	{
		// 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('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
	
	function getEvent()
	{
		$idEvent= JRequest::getVar('cid');
		if(is_array($idEvent)) $idEvent=$idEvent[0];
		$row= & JTable::getInstance('Rsevents_Events','Table');
		$row->load($idEvent);
		if(empty($row->EventStartDate))
		{
			$row->EventStartDate = time();
			$row->EventStartDateH='20';
			$row->EventStartDateM='00';
		} else 
		{
			$row->EventStartDateH = date('H',$row->EventStartDate);
			$row->EventStartDateM = date('i',$row->EventStartDate);
		}
		
		if(empty($row->EventEndDate)) 
		{
			$row->EventEndDate = $row->EventStartDate;
			$row->EventEndDateH = '22';
			$row->EventEndDateM = '00';
		} else 
		{
			$row->EventEndDateH = date('H',$row->EventEndDate);
			$row->EventEndDateM = date('i',$row->EventEndDate);
		}
		
		return $row;
	}
	
	function getFiles()
	{
		$db = JFactory::getDBO();
		$idEvent = JRequest::getVar('cid');
		if(is_array($idEvent)) $idEvent = $idEvent[0];
		$db->setQuery("SELECT * FROM #__rsevents_files WHERE IdEvent=$idEvent");
		$rows = $db->loadObjectList();
		return $rows;
	}
	
	
	function copy($IdEvent)
	{
		$db = JFactory::getDBO();
		$row= & JTable::getInstance('Rsevents_Events','Table');
		$row->load($IdEvent);
		$row->published=0;
		$row->EventName=JText::_('RSE_COPY_OF').$row->EventName;
		$row->IdEvent=null;
		$row->store();
		return true;
	}
	
	function getExtras()
	{
		$db=& JFactory::getDBO();
		$idEvent = JRequest::getVar('cid');
		if(is_array($idEvent)) $idEvent = $idEvent[0];
		$db->setQuery("SELECT IdExtra, IdEvent,ExtraName,ExtraValue FROM #__rsevents_extras WHERE IdEvent=".$idEvent);
		$rows = $db->loadObjectList();
		return $rows;
	}
	
	function getExtra()
	{
		$db=& JFactory::getDBO();
		$cid = JRequest::getVar('cid');
		$idEvent = JRequest::getVar('IdEvent',0);
		$row= & JTable::getInstance('Rsevents_Extras','Table');
		$row->load($cid);
		$row->IdEvent = $idEvent;
		return $row;
	}
	
	
	
 
}