[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_finder
   6   *
   7   * @copyright   (C) 2011 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\Site\Model;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Multilanguage;
  16  use Joomla\CMS\MVC\Model\ListModel;
  17  use Joomla\Component\Finder\Administrator\Indexer\Helper;
  18  use Joomla\Database\DatabaseQuery;
  19  use Joomla\String\StringHelper;
  20  use Joomla\Utilities\ArrayHelper;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Suggestions model class for the Finder package.
  28   *
  29   * @since  2.5
  30   */
  31  class SuggestionsModel extends ListModel
  32  {
  33      /**
  34       * Context string for the model type.
  35       *
  36       * @var    string
  37       * @since  2.5
  38       */
  39      protected $context = 'com_finder.suggestions';
  40  
  41      /**
  42       * Method to get an array of data items.
  43       *
  44       * @return  array  An array of data items.
  45       *
  46       * @since   2.5
  47       */
  48      public function getItems()
  49      {
  50          // Get the items.
  51          $items = parent::getItems();
  52  
  53          // Convert them to a simple array.
  54          foreach ($items as $k => $v) {
  55              $items[$k] = $v->term;
  56          }
  57  
  58          return $items;
  59      }
  60  
  61      /**
  62       * Method to build a database query to load the list data.
  63       *
  64       * @return  DatabaseQuery  A database query
  65       *
  66       * @since   2.5
  67       */
  68      protected function getListQuery()
  69      {
  70          $user   = Factory::getUser();
  71          $groups = ArrayHelper::toInteger($user->getAuthorisedViewLevels());
  72          $lang   = Helper::getPrimaryLanguage($this->getState('language'));
  73  
  74          // Create a new query object.
  75          $db = $this->getDatabase();
  76          $termIdQuery = $db->getQuery(true);
  77          $termQuery = $db->getQuery(true);
  78  
  79          // Limit term count to a reasonable number of results to reduce main query join size
  80          $termIdQuery->select('ti.term_id')
  81              ->from($db->quoteName('#__finder_terms', 'ti'))
  82              ->where('ti.term LIKE ' . $db->quote($db->escape(StringHelper::strtolower($this->getState('input')), true) . '%', false))
  83              ->where('ti.common = 0')
  84              ->where('ti.language IN (' . $db->quote($lang) . ', ' . $db->quote('*') . ')')
  85              ->order('ti.links DESC')
  86              ->order('ti.weight DESC');
  87  
  88          $termIds = $db->setQuery($termIdQuery, 0, 100)->loadColumn();
  89  
  90          // Early return on term mismatch
  91          if (!count($termIds)) {
  92              return $termIdQuery;
  93          }
  94  
  95          // Select required fields
  96          $termQuery->select('DISTINCT(t.term)')
  97              ->from($db->quoteName('#__finder_terms', 't'))
  98              ->whereIn('t.term_id', $termIds)
  99              ->order('t.links DESC')
 100              ->order('t.weight DESC');
 101  
 102          // Join mapping table for term <-> link relation
 103          $mappingTable = $db->quoteName('#__finder_links_terms', 'tm');
 104          $termQuery->join('INNER', $mappingTable . ' ON tm.term_id = t.term_id');
 105  
 106          // Join links table
 107          $termQuery->join('INNER', $db->quoteName('#__finder_links', 'l') . ' ON (tm.link_id = l.link_id)')
 108              ->where('l.access IN (' . implode(',', $groups) . ')')
 109              ->where('l.state = 1')
 110              ->where('l.published = 1');
 111  
 112          return $termQuery;
 113      }
 114  
 115      /**
 116       * Method to get a store id based on model the configuration state.
 117       *
 118       * This is necessary because the model is used by the component and
 119       * different modules that might need different sets of data or different
 120       * ordering requirements.
 121       *
 122       * @param   string  $id  An identifier string to generate the store id. [optional]
 123       *
 124       * @return  string  A store id.
 125       *
 126       * @since   2.5
 127       */
 128      protected function getStoreId($id = '')
 129      {
 130          // Add the search query state.
 131          $id .= ':' . $this->getState('input');
 132          $id .= ':' . $this->getState('language');
 133  
 134          // Add the list state.
 135          $id .= ':' . $this->getState('list.start');
 136          $id .= ':' . $this->getState('list.limit');
 137  
 138          return parent::getStoreId($id);
 139      }
 140  
 141      /**
 142       * Method to auto-populate the model state.  Calling getState in this method will result in recursion.
 143       *
 144       * @param   string  $ordering   An optional ordering field.
 145       * @param   string  $direction  An optional direction (asc|desc).
 146       *
 147       * @return  void
 148       *
 149       * @since   2.5
 150       */
 151      protected function populateState($ordering = null, $direction = null)
 152      {
 153          // Get the configuration options.
 154          $app = Factory::getApplication();
 155          $input = $app->input;
 156          $params = ComponentHelper::getParams('com_finder');
 157          $user = Factory::getUser();
 158  
 159          // Get the query input.
 160          $this->setState('input', $input->request->get('q', '', 'string'));
 161  
 162          // Set the query language
 163          if (Multilanguage::isEnabled()) {
 164              $lang = Factory::getLanguage()->getTag();
 165          } else {
 166              $lang = Helper::getDefaultLanguage();
 167          }
 168  
 169          $this->setState('language', $lang);
 170  
 171          // Load the list state.
 172          $this->setState('list.start', 0);
 173          $this->setState('list.limit', 10);
 174  
 175          // Load the parameters.
 176          $this->setState('params', $params);
 177  
 178          // Load the user state.
 179          $this->setState('user.id', (int) $user->get('id'));
 180      }
 181  }


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