Source for file categories.php

Documentation is available at categories.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Search.categories
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC') or die;
  11.  
  12. require_once JPATH_SITE . '/components/com_content/helpers/route.php';
  13.  
  14. /**
  15.  * Categories Search plugin
  16.  *
  17.  * @package     Joomla.Plugin
  18.  * @subpackage  Search.categories
  19.  * @since       1.6
  20.  */
  21. class PlgSearchCategories extends JPlugin
  22. {
  23.     /**
  24.      * Load the language file on instantiation.
  25.      *
  26.      * @var    boolean 
  27.      * @since  3.1
  28.      */
  29.     protected $autoloadLanguage = true;
  30.  
  31.     /**
  32.      * @return array An array of search areas
  33.      */
  34.     public function onContentSearchAreas()
  35.     {
  36.         static $areas = array(
  37.             'categories' => 'PLG_SEARCH_CATEGORIES_CATEGORIES'
  38.         );
  39.         return $areas;
  40.     }
  41.  
  42.     /**
  43.      * Categories Search method
  44.      *
  45.      * The sql must return the following fields that are
  46.      * used in a common display routine: href, title, section, created, text,
  47.      * browsernav
  48.      *
  49.      * @param string Target search string
  50.      * @param string mathcing option, exact|any|all
  51.      * @param string ordering option, newest|oldest|popular|alpha|category
  52.      * @param mixed  An array if restricted to areas, null if search all
  53.      */
  54.     public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
  55.     {
  56.         $db = JFactory::getDbo();
  57.         $user = JFactory::getUser();
  58.         $app = JFactory::getApplication();
  59.         $groups = implode(',', $user->getAuthorisedViewLevels());
  60.         $searchText = $text;
  61.  
  62.         if (is_array($areas))
  63.         {
  64.             if (!array_intersect($areas, array_keys($this->onContentSearchAreas())))
  65.             {
  66.                 return array();
  67.             }
  68.         }
  69.  
  70.         $sContent = $this->params->get('search_content', 1);
  71.         $sArchived = $this->params->get('search_archived', 1);
  72.         $limit = $this->params->def('search_limit', 50);
  73.         $state = array();
  74.         if ($sContent)
  75.         {
  76.             $state[] = 1;
  77.         }
  78.         if ($sArchived)
  79.         {
  80.             $state[] = 2;
  81.         }
  82.  
  83.         if (empty($state))
  84.         {
  85.             return array();
  86.         }
  87.  
  88.         $text = trim($text);
  89.         if ($text == '')
  90.         {
  91.             return array();
  92.         }
  93.  
  94.         /* TODO: The $where variable does not seem to be used at all
  95.         switch ($phrase)
  96.         {
  97.             case 'exact':
  98.                 $text = $db->quote('%' . $db->escape($text, true) . '%', false);
  99.                 $wheres2 = array();
  100.                 $wheres2[] = 'a.title LIKE ' . $text;
  101.                 $wheres2[] = 'a.description LIKE ' . $text;
  102.                 $where = '(' . implode(') OR (', $wheres2) . ')';
  103.                 break;
  104.  
  105.             case 'any':
  106.             case 'all';
  107.             default:
  108.                 $words = explode(' ', $text);
  109.                 $wheres = array();
  110.                 foreach ($words as $word)
  111.                 {
  112.                     $word = $db->quote('%' . $db->escape($word, true) . '%', false);
  113.                     $wheres2 = array();
  114.                     $wheres2[] = 'a.title LIKE ' . $word;
  115.                     $wheres2[] = 'a.description LIKE ' . $word;
  116.                     $wheres[] = implode(' OR ', $wheres2);
  117.                 }
  118.                 $where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')';
  119.                 break;
  120.         }
  121.         */
  122.  
  123.         switch ($ordering)
  124.         {
  125.             case 'alpha':
  126.                 $order = 'a.title ASC';
  127.                 break;
  128.  
  129.             case 'category':
  130.             case 'popular':
  131.             case 'newest':
  132.             case 'oldest':
  133.             default:
  134.                 $order = 'a.title DESC';
  135.         }
  136.  
  137.         $text = $db->quote('%' . $db->escape($text, true) . '%', false);
  138.         $query = $db->getQuery(true);
  139.  
  140.         //sqlsrv changes
  141.         $case_when = ' CASE WHEN ';
  142.         $case_when .= $query->charLength('a.alias', '!=', '0');
  143.         $case_when .= ' THEN ';
  144.         $a_id = $query->castAsChar('a.id');
  145.         $case_when .= $query->concatenate(array($a_id, 'a.alias'), ':');
  146.         $case_when .= ' ELSE ';
  147.         $case_when .= $a_id . ' END as slug';
  148.         $query->select('a.title, a.description AS text, \'\' AS created, \'2\' AS browsernav, a.id AS catid, ' . $case_when)
  149.             ->from('#__categories AS a')
  150.             ->where(
  151.                 '(a.title LIKE ' . $text . ' OR a.description LIKE ' . $text . ') AND a.published IN (' . implode(',', $state) . ') AND a.extension = ' . $db->quote('com_content')
  152.                     . 'AND a.access IN (' . $groups . ')'
  153.             )
  154.             ->group('a.id, a.title, a.description, a.alias')
  155.             ->order($order);
  156.         if ($app->isSite() && JLanguageMultilang::isEnabled())
  157.         {
  158.             $query->where('a.language in (' . $db->quote(JFactory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
  159.         }
  160.  
  161.         $db->setQuery($query, 0, $limit);
  162.         $rows = $db->loadObjectList();
  163.  
  164.         $return = array();
  165.         if ($rows)
  166.         {
  167.             $count = count($rows);
  168.             for ($i = 0; $i < $count; $i++)
  169.             {
  170.                 $rows[$i]->href = ContentHelperRoute::getCategoryRoute($rows[$i]->slug);
  171.                 $rows[$i]->section = JText::_('JCATEGORY');
  172.             }
  173.  
  174.             foreach ($rows as $category)
  175.             {
  176.                 if (searchHelper::checkNoHTML($category, $searchText, array('name', 'title', 'text')))
  177.                 {
  178.                     $return[] = $category;
  179.                 }
  180.             }
  181.         }
  182.  
  183.         return $return;
  184.     }
  185. }

Documentation generated on Tue, 19 Nov 2013 14:55:16 +0100 by phpDocumentor 1.4.3