[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_tags_similar/src/Helper/ -> TagsSimilarHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_tags_similar
   6   *
   7   * @copyright   (C) 2013 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\Module\TagsSimilar\Site\Helper;
  12  
  13  use Joomla\CMS\Component\ComponentHelper;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Helper\ContentHelper;
  16  use Joomla\CMS\Helper\TagsHelper;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\Component\Tags\Site\Helper\RouteHelper;
  19  use Joomla\Database\ParameterType;
  20  use Joomla\Registry\Registry;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('_JEXEC') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Helper for mod_tags_similar
  28   *
  29   * @since  3.1
  30   */
  31  abstract class TagsSimilarHelper
  32  {
  33      /**
  34       * Get a list of tags
  35       *
  36       * @param   Registry  &$params  Module parameters
  37       *
  38       * @return  array
  39       */
  40      public static function getList(&$params)
  41      {
  42          $app    = Factory::getApplication();
  43          $option = $app->input->get('option');
  44          $view   = $app->input->get('view');
  45  
  46          // For now assume com_tags and com_users do not have tags.
  47          // This module does not apply to list views in general at this point.
  48          if ($option === 'com_tags' || $view === 'category' || $option === 'com_users') {
  49              return array();
  50          }
  51  
  52          $db         = Factory::getDbo();
  53          $user       = Factory::getUser();
  54          $groups     = $user->getAuthorisedViewLevels();
  55          $matchtype  = $params->get('matchtype', 'all');
  56          $ordering   = $params->get('ordering', 'count');
  57          $tagsHelper = new TagsHelper();
  58          $prefix     = $option . '.' . $view;
  59          $id         = $app->input->getInt('id');
  60          $now        = Factory::getDate()->toSql();
  61          $nullDate   = $db->getNullDate();
  62  
  63          // This returns a comma separated string of IDs.
  64          $tagsToMatch = $tagsHelper->getTagIds($id, $prefix);
  65  
  66          if (!$tagsToMatch) {
  67              return array();
  68          }
  69  
  70          $tagsToMatch = explode(',', $tagsToMatch);
  71          $tagCount    = \count($tagsToMatch);
  72  
  73          $query = $db->getQuery(true);
  74          $query
  75              ->select(
  76                  [
  77                      $db->quoteName('m.core_content_id'),
  78                      $db->quoteName('m.content_item_id'),
  79                      $db->quoteName('m.type_alias'),
  80                      'COUNT( ' . $db->quoteName('tag_id') . ') AS ' . $db->quoteName('count'),
  81                      $db->quoteName('ct.router'),
  82                      $db->quoteName('cc.core_title'),
  83                      $db->quoteName('cc.core_alias'),
  84                      $db->quoteName('cc.core_catid'),
  85                      $db->quoteName('cc.core_language'),
  86                      $db->quoteName('cc.core_params'),
  87                  ]
  88              )
  89              ->from($db->quoteName('#__contentitem_tag_map', 'm'))
  90              ->join(
  91                  'INNER',
  92                  $db->quoteName('#__tags', 't'),
  93                  $db->quoteName('m.tag_id') . ' = ' . $db->quoteName('t.id')
  94              )
  95              ->join(
  96                  'INNER',
  97                  $db->quoteName('#__ucm_content', 'cc'),
  98                  $db->quoteName('m.core_content_id') . ' = ' . $db->quoteName('cc.core_content_id')
  99              )
 100              ->join(
 101                  'INNER',
 102                  $db->quoteName('#__content_types', 'ct'),
 103                  $db->quoteName('m.type_alias') . ' = ' . $db->quoteName('ct.type_alias')
 104              )
 105              ->whereIn($db->quoteName('m.tag_id'), $tagsToMatch)
 106              ->whereIn($db->quoteName('t.access'), $groups)
 107              ->where($db->quoteName('cc.core_state') . ' = 1')
 108              ->extendWhere(
 109                  'AND',
 110                  [
 111                      $db->quoteName('cc.core_access') . ' IN (' . implode(',', $query->bindArray($groups)) . ')',
 112                      $db->quoteName('cc.core_access') . ' = 0',
 113                  ],
 114                  'OR'
 115              )
 116              ->extendWhere(
 117                  'AND',
 118                  [
 119                      $db->quoteName('m.content_item_id') . ' <> :currentId',
 120                      $db->quoteName('m.type_alias') . ' <> :prefix',
 121                  ],
 122                  'OR'
 123              )
 124              ->bind(':currentId', $id, ParameterType::INTEGER)
 125              ->bind(':prefix', $prefix)
 126              ->extendWhere(
 127                  'AND',
 128                  [
 129                      $db->quoteName('cc.core_publish_up') . ' IS NULL',
 130                      $db->quoteName('cc.core_publish_up') . ' = :nullDateUp',
 131                      $db->quoteName('cc.core_publish_up') . ' <= :nowDateUp',
 132                  ],
 133                  'OR'
 134              )
 135              ->bind(':nullDateUp', $nullDate)
 136              ->bind(':nowDateUp', $now)
 137              ->extendWhere(
 138                  'AND',
 139                  [
 140                      $db->quoteName('cc.core_publish_down') . ' IS NULL',
 141                      $db->quoteName('cc.core_publish_down') . ' = :nullDateDown',
 142                      $db->quoteName('cc.core_publish_down') . ' >= :nowDateDown',
 143                  ],
 144                  'OR'
 145              )
 146              ->bind(':nullDateDown', $nullDate)
 147              ->bind(':nowDateDown', $now);
 148  
 149          // Optionally filter on language
 150          $language = ComponentHelper::getParams('com_tags')->get('tag_list_language_filter', 'all');
 151  
 152          if ($language !== 'all') {
 153              if ($language === 'current_language') {
 154                  $language = ContentHelper::getCurrentLanguage();
 155              }
 156  
 157              $query->whereIn($db->quoteName('cc.core_language'), [$language, '*'], ParameterType::STRING);
 158          }
 159  
 160          $query->group(
 161              [
 162                  $db->quoteName('m.core_content_id'),
 163                  $db->quoteName('m.content_item_id'),
 164                  $db->quoteName('m.type_alias'),
 165                  $db->quoteName('ct.router'),
 166                  $db->quoteName('cc.core_title'),
 167                  $db->quoteName('cc.core_alias'),
 168                  $db->quoteName('cc.core_catid'),
 169                  $db->quoteName('cc.core_language'),
 170                  $db->quoteName('cc.core_params'),
 171              ]
 172          );
 173  
 174          if ($matchtype === 'all' && $tagCount > 0) {
 175              $query->having('COUNT( ' . $db->quoteName('tag_id') . ')  = :tagCount')
 176                  ->bind(':tagCount', $tagCount, ParameterType::INTEGER);
 177          } elseif ($matchtype === 'half' && $tagCount > 0) {
 178              $tagCountHalf = ceil($tagCount / 2);
 179              $query->having('COUNT( ' . $db->quoteName('tag_id') . ')  >= :tagCount')
 180                  ->bind(':tagCount', $tagCountHalf, ParameterType::INTEGER);
 181          }
 182  
 183          if ($ordering === 'count' || $ordering === 'countrandom') {
 184              $query->order($db->quoteName('count') . ' DESC');
 185          }
 186  
 187          if ($ordering === 'random' || $ordering === 'countrandom') {
 188              $query->order($query->rand());
 189          }
 190  
 191          $maximum = (int) $params->get('maximum', 5);
 192  
 193          if ($maximum > 0) {
 194              $query->setLimit($maximum);
 195          }
 196  
 197          $db->setQuery($query);
 198  
 199          try {
 200              $results = $db->loadObjectList();
 201          } catch (\RuntimeException $e) {
 202              $results = [];
 203              $app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
 204          }
 205  
 206          foreach ($results as $result) {
 207              $result->link = RouteHelper::getItemRoute(
 208                  $result->content_item_id,
 209                  $result->core_alias,
 210                  $result->core_catid,
 211                  $result->core_language,
 212                  $result->type_alias,
 213                  $result->router
 214              );
 215  
 216              $result->core_params = new Registry($result->core_params);
 217          }
 218  
 219          return $results;
 220      }
 221  }


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