Source for file searches.php

Documentation is available at searches.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_search
  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. /**
  13.  * Methods supporting a list of search terms.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_search
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * Constructor.
  22.      *
  23.      * @param   array  An optional associative array of configuration settings.
  24.      * @see     JController
  25.      * @since   1.6
  26.      */
  27.     public function __construct($config array())
  28.     {
  29.         if (empty($config['filter_fields']))
  30.         {
  31.             $config['filter_fields'array(
  32.                 'search_term''a.search_term',
  33.                 'hits''a.hits',
  34.             );
  35.         }
  36.  
  37.         parent::__construct($config);
  38.     }
  39.  
  40.     /**
  41.      * Method to auto-populate the model state.
  42.      *
  43.      * Note. Calling getState in this method will result in recursion.
  44.      *
  45.      * @param   string  $ordering   An optional ordering field.
  46.      * @param   string  $direction  An optional direction (asc|desc).
  47.      *
  48.      * @return  void 
  49.      *
  50.      * @since   1.6
  51.      */
  52.     protected function populateState($ordering null$direction null)
  53.     {
  54.         // Load the filter state.
  55.         $search $this->getUserStateFromRequest($this->context . '.filter.search''filter_search'false'string'false);
  56.         $this->setState('filter.search'$search);
  57.  
  58.         $showResults $this->getUserStateFromRequest($this->context . '.filter.results''filter_results'null'int'false);
  59.         $this->setState('filter.results'$showResults);
  60.  
  61.         // Load the parameters.
  62.         $params JComponentHelper::getParams('com_search');
  63.         $this->setState('params'$params);
  64.  
  65.         // List state information.
  66.         parent::populateState('a.hits''asc');
  67.     }
  68.  
  69.     /**
  70.      * Method to get a store id based on model configuration state.
  71.      *
  72.      * This is necessary because the model is used by the component and
  73.      * different modules that might need different sets of data or different
  74.      * ordering requirements.
  75.      *
  76.      * @param   string  $id    A prefix for the store id.
  77.      *
  78.      * @return  string  A store id.
  79.      * @since   1.6
  80.      */
  81.     protected function getStoreId($id '')
  82.     {
  83.         // Compile the store id.
  84.         $id .= ':' $this->getState('filter.search');
  85.         $id .= ':' $this->getState('filter.results');
  86.  
  87.         return parent::getStoreId($id);
  88.     }
  89.  
  90.     /**
  91.      * Build an SQL query to load the list data.
  92.      *
  93.      * @return  JDatabaseQuery 
  94.      * @since   1.6
  95.      */
  96.     protected function getListQuery()
  97.     {
  98.         // Create a new query object.
  99.         $db $this->getDbo();
  100.         $query $db->getQuery(true);
  101.  
  102.         // Select the required fields from the table.
  103.         $query->select(
  104.             $this->getState(
  105.                 'list.select',
  106.                 'a.*'
  107.             )
  108.         );
  109.         $query->from($db->quoteName('#__core_log_searches'' AS a');
  110.  
  111.         // Filter by access level.
  112.         if ($access $this->getState('filter.access'))
  113.         {
  114.             $query->where('a.access = ' . (int) $access);
  115.         }
  116.  
  117.         // Filter by search in title
  118.         $search $this->getState('filter.search');
  119.         if (!empty($search))
  120.         {
  121.             $search $db->quote('%' $db->escape($searchtrue'%');
  122.             $query->where('a.search_term LIKE ' $search);
  123.         }
  124.  
  125.         // Add the list ordering clause.
  126.         $query->order($db->escape($this->getState('list.ordering''a.hits')) ' ' $db->escape($this->getState('list.direction''ASC')));
  127.  
  128.         //echo nl2br(str_replace('#__','jos_',$query));
  129.         return $query;
  130.     }
  131.  
  132.     /**
  133.      * Override the parnet getItems to inject optional data.
  134.      *
  135.      * @return  mixed  An array of objects on success, false on failure.
  136.      * @since   1.6
  137.      */
  138.     public function getItems()
  139.     {
  140.         $items parent::getItems();
  141.  
  142.         // Determine if number of results for search item should be calculated
  143.         // by default it is `off` as it is highly query intensive
  144.         if ($this->getState('filter.results'))
  145.         {
  146.             JPluginHelper::importPlugin('search');
  147.             $app JFactory::getApplication();
  148.  
  149.             if (!class_exists('JSite'))
  150.             {
  151.                 // This fools the routers in the search plugins into thinking it's in the frontend
  152.                 JLoader::register('JSite'JPATH_COMPONENT '/helpers/site.php');
  153.             }
  154.  
  155.             foreach ($items as &$item)
  156.             {
  157.                 $results $app->triggerEvent('onContentSearch'array($item->search_term));
  158.                 $item->returns 0;
  159.                 foreach ($results as $result)
  160.                 {
  161.                     $item->returns += count($result);
  162.                 }
  163.             }
  164.         }
  165.  
  166.         return $items;
  167.     }
  168.  
  169.     /**
  170.      * Method to reset the seach log table.
  171.      *
  172.      * @return  boolean 
  173.      * @since   1.6
  174.      */
  175.     public function reset()
  176.     {
  177.         $db $this->getDbo();
  178.         $db->setQuery(
  179.             'DELETE FROM #__core_log_searches'
  180.         );
  181.  
  182.         try
  183.         {
  184.             $db->execute();
  185.         }
  186.         catch (RuntimeException $e)
  187.         {
  188.             $this->setError($e->getMessage());
  189.             return false;
  190.         }
  191.  
  192.         return true;
  193.     }
  194. }

Documentation generated on Tue, 19 Nov 2013 15:12:45 +0100 by phpDocumentor 1.4.3