[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_articles_news/src/Helper/ -> ArticlesNewsHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_articles_news
   6   *
   7   * @copyright   (C) 2010 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\ArticlesNews\Site\Helper;
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Application\SiteApplication;
  15  use Joomla\CMS\Component\ComponentHelper;
  16  use Joomla\CMS\Factory;
  17  use Joomla\CMS\HTML\HTMLHelper;
  18  use Joomla\CMS\Language\Text;
  19  use Joomla\CMS\Router\Route;
  20  use Joomla\CMS\Uri\Uri;
  21  use Joomla\Component\Content\Site\Helper\RouteHelper;
  22  use Joomla\Database\DatabaseAwareInterface;
  23  use Joomla\Database\DatabaseAwareTrait;
  24  use Joomla\Registry\Registry;
  25  
  26  // phpcs:disable PSR1.Files.SideEffects
  27  \defined('_JEXEC') or die;
  28  // phpcs:enable PSR1.Files.SideEffects
  29  
  30  /**
  31   * Helper for mod_articles_news
  32   *
  33   * @since  1.6
  34   */
  35  class ArticlesNewsHelper implements DatabaseAwareInterface
  36  {
  37      use DatabaseAwareTrait;
  38  
  39      /**
  40       * Get a list of the latest articles from the article model.
  41       *
  42       * @param   Registry         $params  Object holding the models parameters
  43       * @param   SiteApplication  $app     The app
  44       *
  45       * @return  mixed
  46       *
  47       * @since 4.2.0
  48       */
  49      public function getArticles(Registry $params, SiteApplication $app)
  50      {
  51          /** @var \Joomla\Component\Content\Site\Model\ArticlesModel $model */
  52          $model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);
  53  
  54          // Set application parameters in model
  55          $appParams = $app->getParams();
  56          $model->setState('params', $appParams);
  57  
  58          $model->setState('list.start', 0);
  59          $model->setState('filter.published', 1);
  60  
  61          // Set the filters based on the module params
  62          $model->setState('list.limit', (int) $params->get('count', 5));
  63  
  64          // This module does not use tags data
  65          $model->setState('load_tags', false);
  66  
  67          // Access filter
  68          $access     = !ComponentHelper::getParams('com_content')->get('show_noauth');
  69          $authorised = Access::getAuthorisedViewLevels($app->getIdentity() ? $app->getIdentity()->id : 0);
  70          $model->setState('filter.access', $access);
  71  
  72          // Category filter
  73          $model->setState('filter.category_id', $params->get('catid', array()));
  74  
  75          // Filter by language
  76          $model->setState('filter.language', $app->getLanguageFilter());
  77  
  78          // Filter by tag
  79          $model->setState('filter.tag', $params->get('tag', array()));
  80  
  81          // Featured switch
  82          $featured = $params->get('show_featured', '');
  83  
  84          if ($featured === '') {
  85              $model->setState('filter.featured', 'show');
  86          } elseif ($featured) {
  87              $model->setState('filter.featured', 'only');
  88          } else {
  89              $model->setState('filter.featured', 'hide');
  90          }
  91  
  92          // Filter by id in case it should be excluded
  93          if (
  94              $params->get('exclude_current', true)
  95              && $app->input->get('option') === 'com_content'
  96              && $app->input->get('view') === 'article'
  97          ) {
  98              // Exclude the current article from displaying in this module
  99              $model->setState('filter.article_id', $app->input->get('id', 0, 'UINT'));
 100              $model->setState('filter.article_id.include', false);
 101          }
 102  
 103          // Set ordering
 104          $ordering = $params->get('ordering', 'a.publish_up');
 105          $model->setState('list.ordering', $ordering);
 106  
 107          if (trim($ordering) === 'rand()') {
 108              $model->setState('list.ordering', $this->getDatabase()->getQuery(true)->rand());
 109          } else {
 110              $direction = $params->get('direction', 1) ? 'DESC' : 'ASC';
 111              $model->setState('list.direction', $direction);
 112              $model->setState('list.ordering', $ordering);
 113          }
 114  
 115          // Check if we should trigger additional plugin events
 116          $triggerEvents = $params->get('triggerevents', 1);
 117  
 118          // Retrieve Content
 119          $items = $model->getItems();
 120  
 121          foreach ($items as &$item) {
 122              $item->readmore = \strlen(trim($item->fulltext));
 123              $item->slug     = $item->id . ':' . $item->alias;
 124  
 125              if ($access || \in_array($item->access, $authorised)) {
 126                  // We know that user has the privilege to view the article
 127                  $item->link     = Route::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
 128                  $item->linkText = Text::_('MOD_ARTICLES_NEWS_READMORE');
 129              } else {
 130                  $item->link = new Uri(Route::_('index.php?option=com_users&view=login', false));
 131                  $item->link->setVar('return', base64_encode(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language)));
 132                  $item->linkText = Text::_('MOD_ARTICLES_NEWS_READMORE_REGISTER');
 133              }
 134  
 135              $item->introtext = HTMLHelper::_('content.prepare', $item->introtext, '', 'mod_articles_news.content');
 136  
 137              // Remove any images belongs to the text
 138              if (!$params->get('image')) {
 139                  $item->introtext = preg_replace('/<img[^>]*>/', '', $item->introtext);
 140              }
 141  
 142              // Show the Intro/Full image field of the article
 143              if ($params->get('img_intro_full') !== 'none') {
 144                  $images = json_decode($item->images);
 145                  $item->imageSrc = '';
 146                  $item->imageAlt = '';
 147                  $item->imageCaption = '';
 148  
 149                  if ($params->get('img_intro_full') === 'intro' && !empty($images->image_intro)) {
 150                      $item->imageSrc = htmlspecialchars($images->image_intro, ENT_COMPAT, 'UTF-8');
 151                      $item->imageAlt = htmlspecialchars($images->image_intro_alt, ENT_COMPAT, 'UTF-8');
 152  
 153                      if ($images->image_intro_caption) {
 154                          $item->imageCaption = htmlspecialchars($images->image_intro_caption, ENT_COMPAT, 'UTF-8');
 155                      }
 156                  } elseif ($params->get('img_intro_full') === 'full' && !empty($images->image_fulltext)) {
 157                      $item->imageSrc = htmlspecialchars($images->image_fulltext, ENT_COMPAT, 'UTF-8');
 158                      $item->imageAlt = htmlspecialchars($images->image_fulltext_alt, ENT_COMPAT, 'UTF-8');
 159  
 160                      if ($images->image_intro_caption) {
 161                          $item->imageCaption = htmlspecialchars($images->image_fulltext_caption, ENT_COMPAT, 'UTF-8');
 162                      }
 163                  }
 164              }
 165  
 166              if ($triggerEvents) {
 167                  $item->text = '';
 168                  $app->triggerEvent('onContentPrepare', array('com_content.article', &$item, &$params, 0));
 169  
 170                  $results                 = $app->triggerEvent('onContentAfterTitle', array('com_content.article', &$item, &$params, 0));
 171                  $item->afterDisplayTitle = trim(implode("\n", $results));
 172  
 173                  $results                    = $app->triggerEvent('onContentBeforeDisplay', array('com_content.article', &$item, &$params, 0));
 174                  $item->beforeDisplayContent = trim(implode("\n", $results));
 175  
 176                  $results                   = $app->triggerEvent('onContentAfterDisplay', array('com_content.article', &$item, &$params, 0));
 177                  $item->afterDisplayContent = trim(implode("\n", $results));
 178              } else {
 179                  $item->afterDisplayTitle    = '';
 180                  $item->beforeDisplayContent = '';
 181                  $item->afterDisplayContent  = '';
 182              }
 183          }
 184  
 185          return $items;
 186      }
 187  
 188      /**
 189       * Get a list of the latest articles from the article model
 190       *
 191       * @param   \Joomla\Registry\Registry  &$params  object holding the models parameters
 192       *
 193       * @return  mixed
 194       *
 195       * @since 1.6
 196       *
 197       * @deprecated 5.0 Use the none static function getArticles
 198       */
 199      public static function getList(&$params)
 200      {
 201          return (new self())->getArticles($params, Factory::getApplication());
 202      }
 203  }


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