[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Model/ -> SearchesModel.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Finder\Administrator\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  15  use Joomla\CMS\MVC\Model\ListModel;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Methods supporting a list of search terms.
  23   *
  24   * @since  4.0.0
  25   */
  26  class SearchesModel extends ListModel
  27  {
  28      /**
  29       * Constructor.
  30       *
  31       * @param   array                $config   An optional associative array of configuration settings.
  32       * @param   MVCFactoryInterface  $factory  The factory.
  33       *
  34       * @see     \Joomla\CMS\MVC\Model\BaseDatabaseModel
  35       * @since   4.0.0
  36       */
  37      public function __construct($config = array(), MVCFactoryInterface $factory = null)
  38      {
  39          if (empty($config['filter_fields'])) {
  40              $config['filter_fields'] = array(
  41                  'searchterm', 'a.searchterm',
  42                  'hits', 'a.hits',
  43              );
  44          }
  45  
  46          parent::__construct($config, $factory);
  47      }
  48  
  49      /**
  50       * Method to auto-populate the model state.
  51       *
  52       * Note. Calling getState in this method will result in recursion.
  53       *
  54       * @param   string  $ordering   An optional ordering field.
  55       * @param   string  $direction  An optional direction (asc|desc).
  56       *
  57       * @return  void
  58       *
  59       * @since   4.0.0
  60       */
  61      protected function populateState($ordering = 'a.hits', $direction = 'asc')
  62      {
  63          // Special state for toggle results button.
  64          $this->setState('show_results', $this->getUserStateFromRequest($this->context . '.show_results', 'show_results', 1, 'int'));
  65  
  66          // Load the parameters.
  67          $params = ComponentHelper::getParams('com_finder');
  68          $this->setState('params', $params);
  69  
  70          // List state information.
  71          parent::populateState($ordering, $direction);
  72      }
  73  
  74      /**
  75       * Method to get a store id based on model configuration state.
  76       *
  77       * This is necessary because the model is used by the component and
  78       * different modules that might need different sets of data or different
  79       * ordering requirements.
  80       *
  81       * @param   string  $id  A prefix for the store id.
  82       *
  83       * @return  string  A store id.
  84       *
  85       * @since   4.0.0
  86       */
  87      protected function getStoreId($id = '')
  88      {
  89          // Compile the store id.
  90          $id .= ':' . $this->getState('show_results');
  91          $id .= ':' . $this->getState('filter.search');
  92  
  93          return parent::getStoreId($id);
  94      }
  95  
  96      /**
  97       * Build an SQL query to load the list data.
  98       *
  99       * @return  \Joomla\Database\DatabaseQuery
 100       *
 101       * @since   4.0.0
 102       */
 103      protected function getListQuery()
 104      {
 105          // Create a new query object.
 106          $db = $this->getDatabase();
 107          $query = $db->getQuery(true);
 108  
 109          // Select the required fields from the table.
 110          $query->select(
 111              $this->getState(
 112                  'list.select',
 113                  'a.*'
 114              )
 115          );
 116          $query->from($db->quoteName('#__finder_logging', 'a'));
 117  
 118          // Filter by search in title
 119          if ($search = $this->getState('filter.search')) {
 120              $search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
 121              $query->where($db->quoteName('a.searchterm') . ' LIKE ' . $search);
 122          }
 123  
 124          // Add the list ordering clause.
 125          $query->order($db->escape($this->getState('list.ordering', 'a.hits')) . ' ' . $db->escape($this->getState('list.direction', 'ASC')));
 126  
 127          return $query;
 128      }
 129  
 130      /**
 131       * Override the parent getItems to inject optional data.
 132       *
 133       * @return  mixed  An array of objects on success, false on failure.
 134       *
 135       * @since   4.0.0
 136       */
 137      public function getItems()
 138      {
 139          $items = parent::getItems();
 140  
 141          foreach ($items as $item) {
 142              if (is_resource($item->query)) {
 143                  $item->query = unserialize(stream_get_contents($item->query));
 144              } else {
 145                  $item->query = unserialize($item->query);
 146              }
 147          }
 148  
 149          return $items;
 150      }
 151  
 152      /**
 153       * Method to reset the search log table.
 154       *
 155       * @return  boolean
 156       *
 157       * @since   4.0.0
 158       */
 159      public function reset()
 160      {
 161          $db = $this->getDatabase();
 162  
 163          try {
 164              $db->truncateTable('#__finder_logging');
 165          } catch (\RuntimeException $e) {
 166              $this->setError($e->getMessage());
 167  
 168              return false;
 169          }
 170  
 171          return true;
 172      }
 173  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer