<?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
*/

// no direct access
defined('_JEXEC') or die('Restricted access');


class modRseventsHelper
{

	/**
	 * Method to get the events
	 *
	 * @access public
	 * @return array
	 */
	function getList(&$params)
	{
		global $mainframe;

		$db			=& JFactory::getDBO();
		$rseventsConfig = $mainframe->getUserState('rseventsConfig');
		
		if(file_exists(JPATH_SITE.DS.'components'.DS.'com_rsevents'.DS.'helpers'.DS.'rsevents.php'))
			require_once(JPATH_SITE.DS.'components'.DS.'com_rsevents'.DS.'helpers'.DS.'rsevents.php');
		else return;
		
		$where = "";
		
		$current_time	= time();
		$locationIds	= $params->get('locids','');
		$categoryIds = $params->get('catids','');
		$repeatingEvents = $params->get('repeatevents','0');
		$itid			= $params->get('itemid','');
		$limit		 = (int)$params->get('evnumber');
		if($limit == 0) $limit= 1000000;
		
		$ordering = $params->get('ordering','0');
		$order = ($ordering == 0) ? 'DESC' : 'ASC';
		
		$target = ($params->get('target','0') == 0) ? '_blank' : '_self';
		
		if(is_array($locationIds))
		$locids = implode(',', $locationIds);
		else $locids = $locationIds;
		if(is_array($categoryIds))
		$catids = implode(',', $categoryIds);
		else $catids = $categoryIds;
		
		$where .= " AND e.EventStartDate >= '".$current_time."' ";
		
		if(!empty($locationIds))
			$where .= " AND l.IdLocation IN (".$locids.") ";
		if(!empty($categoryIds))
			$where .= " AND c.IdCategory IN (".$catids.") ";
			
			$where .= " AND e.published = 1 AND e.EventType = 1 ";
		
		if($repeatingEvents == 1 ) $where .= " AND e.IdParent = 0 ";
			else $where .= "";
		
		$db->setQuery("SELECT ConfigValue FROM #__rsevents_config WHERE ConfigName = 'global.dateformat'");
		$dateFormat = $db->loadResult();
		
		$query = "SELECT e.IdEvent , e.EventName ,e.EventEnableRegistration, l.IdLocation , l.LocationName , e.EventStartDate , e.EventEndDate , e.EventIcon ,  c.IdCategory , c.CategoryName 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 WHERE 1=1 ".$where." GROUP BY e.IdEvent ORDER BY e.EventStartDate ".$order." LIMIT ".$limit;
				
		$db->setQuery($query);
		$rows = $db->loadObjectList();
		
		
		$Itemid = JRequest::getVar('Itemid','','request');
		if(!empty($Itemid))
		{
			if($itid == '' || empty($itid)) $itemId =  '&Itemid='.$Itemid;
				else $itemId = '&Itemid='.$itid;
		} else $itemId='';
		
		$i		= 0;
		$lists	= array();
		if(!empty($rows))
		foreach ( $rows as $row )
		{
			$categnames = array();
			$layout = $params->get('layout','{EventName} - {EventLocation}');
			
			$small_icon = str_replace('.jpg','_'.$rseventsConfig['event.icon.small'].'.jpg',$row->EventIcon);
			if($rseventsConfig['enable.event.icon'] && $row->EventIcon != '')
				$eventIcon = '<a href="'.$eventLink.'"><img style="vertical-align:middle;padding-bottom:2px;" src="'.JURI::root().'components/com_rsevents/images/thumbs/'.$small_icon.'" /></a>';
			else 
				$eventIcon = '';
			
			$db->setQuery("SELECT c.CategoryName , c.IdCategory FROM #__rsevents_categories c LEFT JOIN #__rsevents_events_cat ec ON ec.IdCategory = c.IdCategory WHERE ec.IdEvent =".$row->IdEvent);
			$categs = $db->loadObjectList();
			foreach($categs as $categ)
				$categnames[] = '<a target="'.$target.'" href="'.html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=listcategoryevents&cid='.$categ->IdCategory.$itemId)).'">'.$categ->CategoryName.'</a>';
			
			$categnames = implode(',',$categnames);
			
			if($row->EventEnableRegistration == 1)
			$eventsubscribe = '<a href="'.JRoute::_('index.php?option=com_rsevents&view=events&layout=subscribe&cid='.$row->IdEvent.$itemId,false).'">'.JText::_('RSE_MENU_EVENT_SUBSCRIBE').'</a>';
			else
				$eventsubscribe = '';
			
			$row->EventName = htmlspecialchars( $row->EventName, ENT_COMPAT, 'UTF-8');
			$eventlink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=events&layout=show&cid='.$row->IdEvent.$itemId));
			$locationlink = html_entity_decode(JRoute::_('index.php?option=com_rsevents&view=locations&layout=show&cid='.$row->IdLocation.'&IdEvent='.$row->IdEvent.$itemId));
			$row->EventName = '<a target="'.$target.'" href="'.$eventlink.'">'.$row->EventName.'</a>';
			$row->CategoryName = $categnames;
			$row->LocationName = '<a target="'.$target.'" href="'.$locationlink.'">'.$row->LocationName.'</a>';
			$row->EventStartDate = rseventsHelper::translateDate(date($dateFormat,$row->EventStartDate)); 
			$row->EventEndDate   = rseventsHelper::translateDate(date($dateFormat,$row->EventEndDate));
			
			$bad  = array('{EventName}','{EventLocation}','{EventCategory}','{EventStartDate}','{EventEndDate}','{EventIcon}','{EventSubscribe}');
			$good = array($row->EventName , $row->LocationName , $row->CategoryName , $row->EventStartDate , $row->EventEndDate, $eventIcon,$eventsubscribe);
			
			$layout = str_replace($bad,$good,$layout);
			
			$lists[$i]->layout = $layout;

			$i++;
		}
		

		return $lists;
		
		
	}
}