Actions

J1.5 talk

Creating a search plugin

From Joomla! Documentation

Revision as of 04:05, 2 June 2011 by JulienV (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

the php file presented here is written in a procedural way.

For joomla 1.5, it's better to write it using a class, as explained here: http://docs.joomla.org/Creating_a_Plugin_for_Joomla_1.5

<?php
//First start with information about the Plugin and yourself. For example:
/**
 * @version		$Id: nameofplugin.php versionnumber date author
 * @copyright	        Copyright
 * @license		License, for example GNU/GPL
 * All other information you would like to add
 */
 
//To prevent accessing the document directly, enter this code:
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
// Import library dependencies
jimport('joomla.plugin.plugin');
 
//Load the language file. Replace 'nameofplugin' with the name of your plugin.
JPlugin::loadLanguage( 'plg_search_nameofplugin' );
 
class plgSearchnameofplugin extends JPlugin {
 
	/**
	 * plugin 'areas', i.e. it's differents search sections
	 * for this example, we us only one section, but you could have several (e.g: articles, categories, author...)
	 * @var array
	 */
	var $_areas  = array(
		'mypluginsearcha' => 'PLG_MYSEARCHPLUGIN_AREA_A'
	); 
 
	/**
	 * Handles onSearchAreas Event
	 *
	 * @return array
	 */
	function onSearchAreas()
	{
		return $this->_areas;
	}
 
	/** 
	 * Handles onSearchAreas event
	 * 
	 * @param string $text the text to search
	 * @param string $phrase the type of search (exact/all/any)
	 * @param string $ordering ordering of the results (alpha/newest/...)
	 * @param array $areas areas to be search
	 * @return array matches
	 */
	function onSearch( $text, $phrase='', $ordering='', $areas=null )
	{
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser(); 
 
		//If the array is not correct, return it:
		if (is_array( $areas )) {
			if (!array_intersect( $areas, array_keys( $this->_areas ) )) {
				return array();
			}
		}
 
		//Define the parameters. First get the right plugin; 'search' (the group), 'nameofplugin'. 
		$plugin =& JPluginHelper::getPlugin('search', 'nameofplugin');
 
		//Then load the parameters of the plugin.
		$pluginParams = new JParameter( $plugin->params );
 
		//Now define the parameters like this:
		$limit = $pluginParams->def( 'nameofparameter', defaultsetting );
 
		//Use the function trim to delete spaces in front of or at the back of the searching terms
		$text = trim( $text );
 
		//Return Array when nothing was filled in.
		if ($text == '') {
			return array();
		}
 
		// After this, you have to add the database part. This will be the most difficult part, because this changes per situation.
		// In the coding examples later on you will find some of the examples used by Joomla! 1.5 core Search Plugins.
		// in case of multiple sectionS, just repeat and adapt for each, merging each section results, 
		// and filtering with an if statement: if (!$areas || in_array('mypluginsearcha', $areas))) {...}
		// It will look something like this, if you have only one section 
		$wheres = array();
		switch ($phrase) {
 
			//search exact
			case 'exact':
				$text		= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
				$wheres2 	= array();
				$wheres2[] 	= 'LOWER(a.name) LIKE '.$text;
				$where 		= '(' . implode( ') OR (', $wheres2 ) . ')';
				break;
 
			//search all or any
			case 'all':
			case 'any':
 
			//set default
			default:
				$words 	= explode( ' ', $text );
				$wheres = array();
				foreach ($words as $word)
				{
					$word		= $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
					$wheres2 	= array();
					$wheres2[] 	= 'LOWER(a.name) LIKE '.$word;
					$wheres[] 	= implode( ' OR ', $wheres2 );
				}
				$where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
				break;
		}
 
		//ordering of the results
		switch ( $ordering ) {
 
			//alphabetic, ascending
			case 'alpha':
				$order = 'a.name ASC';
				break;
 
			//oldest first
			case 'oldest':
 
			//popular first
			case 'popular':
 
			//newest first
			case 'newest':
 
			//default setting: alphabetic, ascending
			default:
				$order = 'a.name ASC';
		}
 
		//replace nameofplugin
		$searchNameofplugin = JText::_( 'Nameofplugin' );
 
		//the database query; differs per situation! It will look something like this:
		$query = 'SELECT a.name AS title,'
		. ' CONCAT_WS( " / ", '. $db->Quote($searchNameofplugin) .', b.title )AS section,'
		. ' "1" AS browsernav'
		. ' FROM #__nameofplugin AS a'
		. ' INNER JOIN #__categories AS b ON b.id = a.catid'
		. ' WHERE ( '. $where .' )'
		. ' AND a.published = 1'
		. ' AND b.access <= '. (int) $user->get( 'aid' )
		. ' ORDER BY '. $order
		;
 
		//Set query
		$db->setQuery( $query, 0, $limit );
		$rows = $db->loadObjectList();
 
		//The 'output' of the displayed link
		foreach($rows as $key => $row) {
			$rows[$key]->href = 'index.php?option=com_newsfeeds&view=newsfeed&catid='.$row->catslug.'&id='.$row->slug;
		}
 
		//Return the search results in an array
		return $rows;
	}
}