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

// ensure this file is being included by a parent file
defined( '_JEXEC' ) or die( 'Restricted access' );

global $mainframe;

$mainframe->registerEvent( 'onSearch', 'plgSearchRsevents' );
$mainframe->registerEvent( 'onSearchAreas', 'plgSearchRseventsAreas' );

/**
 * @return array An array of search areas
 */
function &plgSearchRseventsAreas()
{
	static $areas = array('rsevents' => 'Rsevents');
	return $areas;
}

/**
 * Content Search method
 * The sql must return the following fields that are used in a common display
 * routine: href, title, section, created, text, browsernav
 * @param string Target search string
 * @param string mathcing option, exact|any|all
 * @param string ordering option, newest|oldest|popular|alpha|category
 * @param mixed An array if the search it to be restricted to areas, null if search all
 */

function plgSearchRsevents( $text, $phrase='', $ordering='', $areas = null )
{
	$text = trim($text);
	
	if ($text == '') {
		return array();
	}
	
	if (is_array($areas)) {
		if (!array_intersect($areas, array_keys(plgSearchRseventsAreas()))) {
			return array();
		}
	}

	$db = & JFactory::getDBO();
	
	//get the display limit 
	$plugin	= & JPluginHelper::getPlugin('search', 'rsevents.search');
	$pluginParams = new JParameter($plugin->params);
	$limit = $pluginParams->def('search_limit', 50);
	$iid = $pluginParams->def('itemid');
	
	if (file_exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_rsevents'.DS.'rsevents.php')) 
	{		
		switch ($phrase) {
			case 'exact':
				$where = "LOWER(EventName) LIKE '%$text%'";
				break;
			case 'all':
			case 'any':
			default:
				$words = explode(' ', $text);
				$wheres = array();
				foreach ($words as $word) {
					$wheres2 = array();
					$wheres2[] = "LOWER(EventName) LIKE '%$word%'";
					$wheres2[] = "LOWER(EventDescription) LIKE '%$word%'";
					$wheres[] = implode(' OR ', $wheres2);
				}
				$where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
				break;
		}
		
		switch ($ordering) 
		{
			case 'oldest':
				$order = 'EventStartDate ASC';
				break;
			case 'newest':
			default:
				$order = 'EventStartDate DESC';
				break;
		}

		$query = "SELECT "
				. "\n  EventName      AS text"
				. "\n, EventStartDate AS created"
				. "\n, '2'          AS browsernav"
				. "\n, 'Rsevents'   AS section"
				. "\n, ''           AS href"
				. "\n, IdEvent"
				. "\nFROM #__rsevents_events "
				. "\nWHERE published='1' AND EventType='1'"
				. "\n AND ($where) "
				. "\nORDER BY $order";

		$db->setQuery($query, 0, $limit);
		$rows = $db->loadObjectList();

		$result = array();
		$cnt = count($rows);

		if(empty($iid))
		$Itemid = JRequest::getVar('Itemid','','request');
		else $Itemid = $iid;
		$itemId = ($Itemid == '') ? '' : '&Itemid='.$Itemid;
		
		if ($cnt > 0) 
		{			
			for ($i = 0; $i < $cnt; $i++) 
			{
				$link = 'index.php?option=com_rsevents&view=events&layout=show&cid=';
				
				$rows[$i]->href = JRoute::_($link . $rows[$i]->IdEvent.$itemId);
				
				$eventDetails = $rows[$i]->text;
				
				if ($eventDetails != '') {
					$rows[$i]->title = $eventDetails;
					$rows[$i]->text = $eventDetails;
					$result[] = $rows[$i];
				}
			}
		}
		unset($rows);
		
		return $result;
	}
	return array();
} 
?>