1.5 Template Project

The Joomla! Documentation Working Group is running a project to develop detailed reference and tutorial material on Joomla! 1.5 templates.  There is a project page on the documentation wiki where you can see the work in progress and help us by contributing your knowledge.

Who's Online

We have 8 guests online

Help Site License

The Joomla! Help Site content is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution NonCommercial ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Home arrow Developer Manual arrow 4. Mambots arrow An onSearch Mambot

An onSearch Mambot PDF Print E-mail
Here is a code framework for a mambot that it triggered by the onSearch event (affectionately known as search bots).

* @version $Id $
* @package Joomla
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* Joomla! is free software and parts of it may contain or be derived from the
* GNU General Public License or other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
$_PLUGINS->registerFunction( 'onSearch', 'botSearchContacts' );
* Search method
* @param array Named 'text' element is the search term
function botSearchContacts( $text ) {
  global $database;
  $text = trim( $text );
  if ($text == '') {
    return array();
  $database->setQuery( "SELECT name AS title,"
    . " '' AS created,"
    . " misc AS text,"
    . " 'Contact' AS section,"
    . " CONCAT('index.php?option=com_contact&task=view&id=',id) AS href,"
    . " '2' AS browsernav"
    . " FROM #__contact_details"
    . " INNER JOIN #__categories AS b ON b.id=a.catid AND b.access <='$my->gid'"
    . " LEFT JOIN #__sections AS u ON u.id = a.sectionid"
    . " WHERE name LIKE '%$text%' OR misc LIKE '%$text%'"
    . "  AND published='1'"
    . " ORDER BY name"
  return $database->loadObjectList();
Firstly you have the usual header and security gate.

$_PLUGINS is a variable exposed to the mambot file when it is included. You don't have to declare it as a global.

It has a method called registerFunction in the form:
$_PLUGINS->registerFunction( 'event_name', 'function_name' );
The event available for searching is 'onSearch' so we use this as our event_name.

The function_name is the function that we want the Joomla script to execute when it triggers the on search event. You can call it anything you like as long as you ensure it is unique. For this example we have named the function botSearchContacts. Functions called by the onSearch event have a required arguments list:
function function_name( string 'search_text' )
The first argument is the text to search for. The rest of the function simply queries the database and returns an array of results. The query must return rows with the following fields:
A title for the search result
The date of the row
Where the row is from
The href attribute for the link to the item
Set to 2 to open in the current window.

Last Updated ( Thursday, 15 September 2005 )
< Prev   Next >