[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_related_items/src/Helper/ -> RelatedItemsHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_related_items
   6   *
   7   * @copyright   (C) 2006 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\RelatedItems\Site\Helper;
  12  
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Language\Multilanguage;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Router\Route;
  17  use Joomla\Component\Content\Administrator\Extension\ContentComponent;
  18  use Joomla\Component\Content\Site\Helper\RouteHelper;
  19  use Joomla\Database\ParameterType;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Helper for mod_related_items
  27   *
  28   * @since  1.5
  29   */
  30  abstract class RelatedItemsHelper
  31  {
  32      /**
  33       * Get a list of related articles
  34       *
  35       * @param   \Joomla\Registry\Registry  &$params  module parameters
  36       *
  37       * @return  array
  38       */
  39      public static function getList(&$params)
  40      {
  41          $db        = Factory::getDbo();
  42          $app       = Factory::getApplication();
  43          $input     = $app->input;
  44          $groups    = Factory::getUser()->getAuthorisedViewLevels();
  45          $maximum   = (int) $params->get('maximum', 5);
  46          $factory   = $app->bootComponent('com_content')->getMVCFactory();
  47  
  48          // Get an instance of the generic articles model
  49          /** @var \Joomla\Component\Content\Site\Model\ArticlesModel $articles */
  50          $articles = $factory->createModel('Articles', 'Site', ['ignore_request' => true]);
  51  
  52          // Set application parameters in model
  53          $articles->setState('params', $app->getParams());
  54  
  55          $option = $input->get('option');
  56          $view   = $input->get('view');
  57  
  58          if (!($option === 'com_content' && $view === 'article')) {
  59              return [];
  60          }
  61  
  62          $temp = $input->getString('id');
  63          $temp = explode(':', $temp);
  64          $id   = (int) $temp[0];
  65  
  66          $now      = Factory::getDate()->toSql();
  67          $related  = [];
  68          $query    = $db->getQuery(true);
  69  
  70          if ($id) {
  71              // Select the meta keywords from the item
  72              $query->select($db->quoteName('metakey'))
  73                  ->from($db->quoteName('#__content'))
  74                  ->where($db->quoteName('id') . ' = :id')
  75                  ->bind(':id', $id, ParameterType::INTEGER);
  76              $db->setQuery($query);
  77  
  78              try {
  79                  $metakey = trim($db->loadResult());
  80              } catch (\RuntimeException $e) {
  81                  $app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
  82  
  83                  return array();
  84              }
  85  
  86              // Explode the meta keys on a comma
  87              $keys  = explode(',', $metakey);
  88              $likes = [];
  89  
  90              // Assemble any non-blank word(s)
  91              foreach ($keys as $key) {
  92                  $key = trim($key);
  93  
  94                  if ($key) {
  95                      $likes[] = $db->escape($key);
  96                  }
  97              }
  98  
  99              if (\count($likes)) {
 100                  // Select other items based on the metakey field 'like' the keys found
 101                  $query->clear()
 102                      ->select($db->quoteName('a.id'))
 103                      ->from($db->quoteName('#__content', 'a'))
 104                      ->where($db->quoteName('a.id') . ' != :id')
 105                      ->where($db->quoteName('a.state') . ' = ' . ContentComponent::CONDITION_PUBLISHED)
 106                      ->whereIn($db->quoteName('a.access'), $groups)
 107                      ->bind(':id', $id, ParameterType::INTEGER);
 108  
 109                  $binds  = [];
 110                  $wheres = [];
 111  
 112                  foreach ($likes as $keyword) {
 113                      $binds[] = '%' . $keyword . '%';
 114                  }
 115  
 116                  $bindNames = $query->bindArray($binds, ParameterType::STRING);
 117  
 118                  foreach ($bindNames as $keyword) {
 119                      $wheres[] = $db->quoteName('a.metakey') . ' LIKE ' . $keyword;
 120                  }
 121  
 122                  $query->extendWhere('AND', $wheres, 'OR')
 123                      ->extendWhere('AND', [ $db->quoteName('a.publish_up') . ' IS NULL', $db->quoteName('a.publish_up') . ' <= :nowDate1'], 'OR')
 124                      ->extendWhere(
 125                          'AND',
 126                          [
 127                              $db->quoteName('a.publish_down') . ' IS NULL',
 128                              $db->quoteName('a.publish_down') . ' >= :nowDate2'
 129                          ],
 130                          'OR'
 131                      )
 132                      ->bind([':nowDate1', ':nowDate2'], $now);
 133  
 134                  // Filter by language
 135                  if (Multilanguage::isEnabled()) {
 136                      $query->whereIn($db->quoteName('a.language'), [Factory::getLanguage()->getTag(), '*'], ParameterType::STRING);
 137                  }
 138  
 139                  $query->setLimit($maximum);
 140                  $db->setQuery($query);
 141  
 142                  try {
 143                      $articleIds = $db->loadColumn();
 144                  } catch (\RuntimeException $e) {
 145                      $app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED'), 'error');
 146  
 147                      return [];
 148                  }
 149  
 150                  if (\count($articleIds)) {
 151                      $articles->setState('filter.article_id', $articleIds);
 152                      $articles->setState('filter.published', 1);
 153                      $related = $articles->getItems();
 154                  }
 155  
 156                  unset($articleIds);
 157              }
 158          }
 159  
 160          if (\count($related)) {
 161              // Prepare data for display using display options
 162              foreach ($related as &$item) {
 163                  $item->slug  = $item->id . ':' . $item->alias;
 164                  $item->route = Route::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
 165              }
 166          }
 167  
 168          return $related;
 169      }
 170  }


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