[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_finder/src/Service/HTML/ -> Query.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   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\Administrator\Service\HTML;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\Component\Finder\Administrator\Helper\LanguageHelper;
  16  use Joomla\Component\Finder\Administrator\Indexer\Query as IndexerQuery;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Query HTML behavior class for Finder.
  24   *
  25   * @since  2.5
  26   */
  27  class Query
  28  {
  29      /**
  30       * Method to get the explained (human-readable) search query.
  31       *
  32       * @param   IndexerQuery  $query  A IndexerQuery object to explain.
  33       *
  34       * @return  mixed  String if there is data to explain, null otherwise.
  35       *
  36       * @since   2.5
  37       */
  38      public static function explained(IndexerQuery $query)
  39      {
  40          $parts = array();
  41  
  42          // Process the required tokens.
  43          foreach ($query->included as $token) {
  44              if ($token->required && (!isset($token->derived) || $token->derived == false)) {
  45                  $parts[] = '<span class="query-required">' . Text::sprintf('COM_FINDER_QUERY_TOKEN_REQUIRED', $token->term) . '</span>';
  46              }
  47          }
  48  
  49          // Process the optional tokens.
  50          foreach ($query->included as $token) {
  51              if (!$token->required && (!isset($token->derived) || $token->derived == false)) {
  52                  $parts[] = '<span class="query-optional">' . Text::sprintf('COM_FINDER_QUERY_TOKEN_OPTIONAL', $token->term) . '</span>';
  53              }
  54          }
  55  
  56          // Process the excluded tokens.
  57          foreach ($query->excluded as $token) {
  58              if (!isset($token->derived) || $token->derived === false) {
  59                  $parts[] = '<span class="query-excluded">' . Text::sprintf('COM_FINDER_QUERY_TOKEN_EXCLUDED', $token->term) . '</span>';
  60              }
  61          }
  62  
  63          // Process the start date.
  64          if ($query->date1) {
  65              $date = Factory::getDate($query->date1)->format(Text::_('DATE_FORMAT_LC'));
  66              $datecondition = Text::_('COM_FINDER_QUERY_DATE_CONDITION_' . strtoupper($query->when1));
  67              $parts[] = '<span class="query-start-date">' . Text::sprintf('COM_FINDER_QUERY_START_DATE', $datecondition, $date) . '</span>';
  68          }
  69  
  70          // Process the end date.
  71          if ($query->date2) {
  72              $date = Factory::getDate($query->date2)->format(Text::_('DATE_FORMAT_LC'));
  73              $datecondition = Text::_('COM_FINDER_QUERY_DATE_CONDITION_' . strtoupper($query->when2));
  74              $parts[] = '<span class="query-end-date">' . Text::sprintf('COM_FINDER_QUERY_END_DATE', $datecondition, $date) . '</span>';
  75          }
  76  
  77          // Process the taxonomy filters.
  78          if (!empty($query->filters)) {
  79              // Get the filters in the request.
  80              $t = Factory::getApplication()->input->request->get('t', array(), 'array');
  81  
  82              // Process the taxonomy branches.
  83              foreach ($query->filters as $branch => $nodes) {
  84                  // Process the taxonomy nodes.
  85                  $lang = Factory::getLanguage();
  86  
  87                  foreach ($nodes as $title => $id) {
  88                      // Translate the title for Types
  89                      $key = LanguageHelper::branchPlural($title);
  90  
  91                      if ($lang->hasKey($key)) {
  92                          $title = Text::_($key);
  93                      }
  94  
  95                      // Don't include the node if it is not in the request.
  96                      if (!in_array($id, $t)) {
  97                          continue;
  98                      }
  99  
 100                      // Add the node to the explanation.
 101                      $parts[] = '<span class="query-taxonomy">'
 102                          . Text::sprintf('COM_FINDER_QUERY_TAXONOMY_NODE', $title, Text::_(LanguageHelper::branchSingular($branch)))
 103                          . '</span>';
 104                  }
 105              }
 106          }
 107  
 108          // Build the interpreted query.
 109          return count($parts) ? implode(Text::_('COM_FINDER_QUERY_TOKEN_GLUE'), $parts) : null;
 110      }
 111  
 112      /**
 113       * Method to get the suggested search query.
 114       *
 115       * @param   IndexerQuery  $query  A IndexerQuery object.
 116       *
 117       * @return  mixed  String if there is a suggestion, false otherwise.
 118       *
 119       * @since   2.5
 120       */
 121      public static function suggested(IndexerQuery $query)
 122      {
 123          $suggested = false;
 124  
 125          // Check if the query input is empty.
 126          if (empty($query->input)) {
 127              return $suggested;
 128          }
 129  
 130          // Check if there were any ignored or included keywords.
 131          if (count($query->ignored) || count($query->included)) {
 132              $suggested = $query->input;
 133  
 134              // Replace the ignored keyword suggestions.
 135              foreach (array_reverse($query->ignored) as $token) {
 136                  if (isset($token->suggestion)) {
 137                      $suggested = str_ireplace($token->term, $token->suggestion, $suggested);
 138                  }
 139              }
 140  
 141              // Replace the included keyword suggestions.
 142              foreach (array_reverse($query->included) as $token) {
 143                  if (isset($token->suggestion)) {
 144                      $suggested = str_ireplace($token->term, $token->suggestion, $suggested);
 145                  }
 146              }
 147  
 148              // Check if we made any changes.
 149              if ($suggested == $query->input) {
 150                  $suggested = false;
 151              }
 152          }
 153  
 154          return $suggested;
 155      }
 156  }


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