[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_articles_latest/src/Helper/ -> ArticlesLatestHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_articles_latest
   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\ArticlesLatest\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\Router\Route;
  18  use Joomla\Component\Content\Site\Helper\RouteHelper;
  19  use Joomla\Component\Content\Site\Model\ArticlesModel;
  20  use Joomla\Database\DatabaseAwareInterface;
  21  use Joomla\Database\DatabaseAwareTrait;
  22  use Joomla\Registry\Registry;
  23  use Joomla\Utilities\ArrayHelper;
  24  
  25  // phpcs:disable PSR1.Files.SideEffects
  26  \defined('_JEXEC') or die;
  27  // phpcs:enable PSR1.Files.SideEffects
  28  
  29  /**
  30   * Helper for mod_articles_latest
  31   *
  32   * @since  1.6
  33   */
  34  class ArticlesLatestHelper implements DatabaseAwareInterface
  35  {
  36      use DatabaseAwareTrait;
  37  
  38      /**
  39       * Retrieve a list of article
  40       *
  41       * @param   Registry       $params  The module parameters.
  42       * @param   ArticlesModel  $model   The model.
  43       *
  44       * @return  mixed
  45       *
  46       * @since   4.2.0
  47       */
  48      public function getArticles(Registry $params, SiteApplication $app)
  49      {
  50          // Get the Dbo and User object
  51          $db   = $this->getDatabase();
  52          $user = $app->getIdentity();
  53  
  54          /** @var ArticlesModel $model */
  55          $model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);
  56  
  57          // Set application parameters in model
  58          $model->setState('params', $app->getParams());
  59  
  60          $model->setState('list.start', 0);
  61          $model->setState('filter.published', 1);
  62  
  63          // Set the filters based on the module params
  64          $model->setState('list.limit', (int) $params->get('count', 5));
  65  
  66          // This module does not use tags data
  67          $model->setState('load_tags', false);
  68  
  69          // Access filter
  70          $access     = !ComponentHelper::getParams('com_content')->get('show_noauth');
  71          $authorised = Access::getAuthorisedViewLevels($user->get('id'));
  72          $model->setState('filter.access', $access);
  73  
  74          // Category filter
  75          $model->setState('filter.category_id', $params->get('catid', array()));
  76  
  77          // State filter
  78          $model->setState('filter.condition', 1);
  79  
  80          // User filter
  81          $userId = $user->get('id');
  82  
  83          switch ($params->get('user_id')) {
  84              case 'by_me':
  85                  $model->setState('filter.author_id', (int) $userId);
  86                  break;
  87              case 'not_me':
  88                  $model->setState('filter.author_id', $userId);
  89                  $model->setState('filter.author_id.include', false);
  90                  break;
  91  
  92              case 'created_by':
  93                  $model->setState('filter.author_id', $params->get('author', array()));
  94                  break;
  95  
  96              case '0':
  97                  break;
  98  
  99              default:
 100                  $model->setState('filter.author_id', (int) $params->get('user_id'));
 101                  break;
 102          }
 103  
 104          // Filter by language
 105          $model->setState('filter.language', $app->getLanguageFilter());
 106  
 107          // Featured switch
 108          $featured = $params->get('show_featured', '');
 109  
 110          if ($featured === '') {
 111              $model->setState('filter.featured', 'show');
 112          } elseif ($featured) {
 113              $model->setState('filter.featured', 'only');
 114          } else {
 115              $model->setState('filter.featured', 'hide');
 116          }
 117  
 118          // Set ordering
 119          $order_map = array(
 120              'm_dsc'  => 'a.modified DESC, a.created',
 121              'mc_dsc' => 'a.modified',
 122              'c_dsc'  => 'a.created',
 123              'p_dsc'  => 'a.publish_up',
 124              'random' => $db->getQuery(true)->rand(),
 125          );
 126  
 127          $ordering = ArrayHelper::getValue($order_map, $params->get('ordering'), 'a.publish_up');
 128          $dir      = 'DESC';
 129  
 130          $model->setState('list.ordering', $ordering);
 131          $model->setState('list.direction', $dir);
 132  
 133          $items = $model->getItems();
 134  
 135          foreach ($items as &$item) {
 136              $item->slug    = $item->id . ':' . $item->alias;
 137  
 138              if ($access || \in_array($item->access, $authorised)) {
 139                  // We know that user has the privilege to view the article
 140                  $item->link = Route::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
 141              } else {
 142                  $item->link = Route::_('index.php?option=com_users&view=login');
 143              }
 144          }
 145  
 146          return $items;
 147      }
 148  
 149      /**
 150       * Retrieve a list of articles
 151       *
 152       * @param   Registry       $params  The module parameters.
 153       * @param   ArticlesModel  $model   The model.
 154       *
 155       * @return  mixed
 156       *
 157       * @since   1.6
 158       *
 159       * @deprecated 5.0 Use the none static function getArticles
 160       */
 161      public static function getList(Registry $params, ArticlesModel $model)
 162      {
 163          return (new self())->getArticles($params, Factory::getApplication());
 164      }
 165  }


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