Joomla плагин поиска по своему компоненту

09.01.2016

простенький плагин, для поиска по самописному компоненту

sonews.php (Download)

 <?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Search.SoNews
 */

defined('_JEXEC') or die;

/**
 * SoNews search plugin.
 *
 * @since  1.6
 */
class PlgSearchSoNews extends JPlugin
{
  /**
   * Load the language file on instantiation.
   *
   * @var    boolean
   * @since  3.1
   */
  protected $autoloadLanguage = true;


  /**
   * Search content (categories).
   *
   * The SQL must return the following fields that are used in a common display
   * routine: href, title, section, created, text, browsernav.
   *
   * @param   string  $text      Target search string.
   * @param   string  $phrase    Matching option (possible values: exact|any|all).  Default is "any".
   * @param   string  $ordering  Ordering option (possible values: newest|oldest|popular|alpha|category).  Default is "newest".
   * @param   mixed   $areas     An array if the search is to be restricted to areas or null to search all areas.
   *
   * @return  array  Search results.
   *
   * @since   1.6
   */
  public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
  {

    $db = JFactory::getDbo();
    $searchText = $text;

/*    if (is_array($areas))
    {
      if (!array_intersect($areas, array_keys($this->onContentSearchAreas())))
      {
        return array();
      }
    }*/


    $limit = $this->params->def('search_limit', 50);
    $state = array();



    $text = trim($text);

    if ($text == '')
    {
      return array();
    }

    switch ($ordering)
    {
      case 'alpha':
        $order = 'a.title ASC';
      break;

      default:
        $order = 'a.title DESC';
      break;
    }

    $text = $db->quote('%' . $db->escape($text, true) . '%', false);
    $query = $db->getQuery(true);

    $query->select('a.id, a.title, a.short_text AS text')
          ->from('#__so_news_item AS a')
          ->where('published = 1 AND (a.title LIKE ' . $text . ' OR a.short_text LIKE ' . $text . ' OR a.full_text LIKE ' . $text . ')')
          ->order( $order );
    ;
    
    // echo $query->__toString().'<br />';


    $db->setQuery($query, 0, $limit);
    try
    {
      $rows = $db->loadObjectList();
    }
    catch (RuntimeException $e)
    {
      $rows = array();
      JFactory::getApplication()->enqueueMessage(JText::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
    }

/*echo '<pre>';
print_r($rows);
echo '</pre>';*/

    $return = array();
    foreach($rows as &$oRow)
	{
	  $oRow->href = 'index.php?option=com_sonews&view=item&id='.$oRow->id;
	}

    return $rows;
  }
}

Скачать


Категории: CMS Joomla 3.X, Joomla
Пометки: Joomla плагин для поиска по новостям / записям блога
Яндекс.Метрика