[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/modules/mod_articles_category/src/Helper/ -> ArticlesCategoryHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  mod_articles_category
   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\ArticlesCategory\Site\Helper;
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Date\Date;
  16  use Joomla\CMS\Factory;
  17  use Joomla\CMS\HTML\HTMLHelper;
  18  use Joomla\CMS\Plugin\PluginHelper;
  19  use Joomla\CMS\Router\Route;
  20  use Joomla\Component\Content\Administrator\Extension\ContentComponent;
  21  use Joomla\Component\Content\Site\Helper\RouteHelper;
  22  use Joomla\String\StringHelper;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Helper for mod_articles_category
  30   *
  31   * @since  1.6
  32   */
  33  abstract class ArticlesCategoryHelper
  34  {
  35      /**
  36       * Get a list of articles from a specific category
  37       *
  38       * @param   \Joomla\Registry\Registry  &$params  object holding the models parameters
  39       *
  40       * @return  mixed
  41       *
  42       * @since  1.6
  43       */
  44      public static function getList(&$params)
  45      {
  46          $app     = Factory::getApplication();
  47          $factory = $app->bootComponent('com_content')->getMVCFactory();
  48  
  49          // Get an instance of the generic articles model
  50          $articles = $factory->createModel('Articles', 'Site', ['ignore_request' => true]);
  51  
  52          // Set application parameters in model
  53          $input     = $app->input;
  54          $appParams = $app->getParams();
  55          $articles->setState('params', $appParams);
  56  
  57          $articles->setState('list.start', 0);
  58          $articles->setState('filter.published', ContentComponent::CONDITION_PUBLISHED);
  59  
  60          // Set the filters based on the module params
  61          $articles->setState('list.limit', (int) $params->get('count', 0));
  62          $articles->setState('load_tags', $params->get('show_tags', 0) || $params->get('article_grouping', 'none') === 'tags');
  63  
  64          // Access filter
  65          $access     = !ComponentHelper::getParams('com_content')->get('show_noauth');
  66          $authorised = Access::getAuthorisedViewLevels(Factory::getUser()->get('id'));
  67          $articles->setState('filter.access', $access);
  68  
  69          // Prep for Normal or Dynamic Modes
  70          $mode = $params->get('mode', 'normal');
  71  
  72          switch ($mode) {
  73              case 'dynamic':
  74                  $option = $input->get('option');
  75                  $view   = $input->get('view');
  76  
  77                  if ($option === 'com_content') {
  78                      switch ($view) {
  79                          case 'category':
  80                          case 'categories':
  81                              $catids = array($input->getInt('id'));
  82                              break;
  83                          case 'article':
  84                              if ($params->get('show_on_article_page', 1)) {
  85                                  $article_id = $input->getInt('id');
  86                                  $catid      = $input->getInt('catid');
  87  
  88                                  if (!$catid) {
  89                                      // Get an instance of the generic article model
  90                                      $article = $factory->createModel('Article', 'Site', ['ignore_request' => true]);
  91  
  92                                      $article->setState('params', $appParams);
  93                                      $article->setState('filter.published', 1);
  94                                      $article->setState('article.id', (int) $article_id);
  95                                      $item   = $article->getItem();
  96                                      $catids = array($item->catid);
  97                                  } else {
  98                                      $catids = array($catid);
  99                                  }
 100                              } else {
 101                                  // Return right away if show_on_article_page option is off
 102                                  return;
 103                              }
 104                              break;
 105  
 106                          default:
 107                              // Return right away if not on the category or article views
 108                              return;
 109                      }
 110                  } else {
 111                      // Return right away if not on a com_content page
 112                      return;
 113                  }
 114  
 115                  break;
 116  
 117              default:
 118                  $catids = $params->get('catid');
 119                  $articles->setState('filter.category_id.include', (bool) $params->get('category_filtering_type', 1));
 120                  break;
 121          }
 122  
 123          // Category filter
 124          if ($catids) {
 125              if ($params->get('show_child_category_articles', 0) && (int) $params->get('levels', 0) > 0) {
 126                  // Get an instance of the generic categories model
 127                  $categories = $factory->createModel('Categories', 'Site', ['ignore_request' => true]);
 128                  $categories->setState('params', $appParams);
 129                  $levels = $params->get('levels', 1) ?: 9999;
 130                  $categories->setState('filter.get_children', $levels);
 131                  $categories->setState('filter.published', 1);
 132                  $categories->setState('filter.access', $access);
 133                  $additional_catids = array();
 134  
 135                  foreach ($catids as $catid) {
 136                      $categories->setState('filter.parentId', $catid);
 137                      $recursive = true;
 138                      $items     = $categories->getItems($recursive);
 139  
 140                      if ($items) {
 141                          foreach ($items as $category) {
 142                              $condition = (($category->level - $categories->getParent()->level) <= $levels);
 143  
 144                              if ($condition) {
 145                                  $additional_catids[] = $category->id;
 146                              }
 147                          }
 148                      }
 149                  }
 150  
 151                  $catids = array_unique(array_merge($catids, $additional_catids));
 152              }
 153  
 154              $articles->setState('filter.category_id', $catids);
 155          }
 156  
 157          // Ordering
 158          $ordering = $params->get('article_ordering', 'a.ordering');
 159  
 160          switch ($ordering) {
 161              case 'random':
 162                  $articles->setState('list.ordering', Factory::getDbo()->getQuery(true)->rand());
 163                  break;
 164  
 165              case 'rating_count':
 166              case 'rating':
 167                  $articles->setState('list.ordering', $ordering);
 168                  $articles->setState('list.direction', $params->get('article_ordering_direction', 'ASC'));
 169  
 170                  if (!PluginHelper::isEnabled('content', 'vote')) {
 171                      $articles->setState('list.ordering', 'a.ordering');
 172                  }
 173  
 174                  break;
 175  
 176              default:
 177                  $articles->setState('list.ordering', $ordering);
 178                  $articles->setState('list.direction', $params->get('article_ordering_direction', 'ASC'));
 179                  break;
 180          }
 181  
 182          // Filter by multiple tags
 183          $articles->setState('filter.tag', $params->get('filter_tag', array()));
 184  
 185          $articles->setState('filter.featured', $params->get('show_front', 'show'));
 186          $articles->setState('filter.author_id', $params->get('created_by', array()));
 187          $articles->setState('filter.author_id.include', $params->get('author_filtering_type', 1));
 188          $articles->setState('filter.author_alias', $params->get('created_by_alias', array()));
 189          $articles->setState('filter.author_alias.include', $params->get('author_alias_filtering_type', 1));
 190          $excluded_articles = $params->get('excluded_articles', '');
 191  
 192          if ($excluded_articles) {
 193              $excluded_articles = explode("\r\n", $excluded_articles);
 194              $articles->setState('filter.article_id', $excluded_articles);
 195  
 196              // Exclude
 197              $articles->setState('filter.article_id.include', false);
 198          }
 199  
 200          $date_filtering = $params->get('date_filtering', 'off');
 201  
 202          if ($date_filtering !== 'off') {
 203              $articles->setState('filter.date_filtering', $date_filtering);
 204              $articles->setState('filter.date_field', $params->get('date_field', 'a.created'));
 205              $articles->setState('filter.start_date_range', $params->get('start_date_range', '1000-01-01 00:00:00'));
 206              $articles->setState('filter.end_date_range', $params->get('end_date_range', '9999-12-31 23:59:59'));
 207              $articles->setState('filter.relative_date', $params->get('relative_date', 30));
 208          }
 209  
 210          // Filter by language
 211          $articles->setState('filter.language', $app->getLanguageFilter());
 212  
 213          $items = $articles->getItems();
 214  
 215          // Display options
 216          $show_date        = $params->get('show_date', 0);
 217          $show_date_field  = $params->get('show_date_field', 'created');
 218          $show_date_format = $params->get('show_date_format', 'Y-m-d H:i:s');
 219          $show_category    = $params->get('show_category', 0);
 220          $show_hits        = $params->get('show_hits', 0);
 221          $show_author      = $params->get('show_author', 0);
 222          $show_introtext   = $params->get('show_introtext', 0);
 223          $introtext_limit  = $params->get('introtext_limit', 100);
 224  
 225          // Find current Article ID if on an article page
 226          $option = $input->get('option');
 227          $view   = $input->get('view');
 228  
 229          if ($option === 'com_content' && $view === 'article') {
 230              $active_article_id = $input->getInt('id');
 231          } else {
 232              $active_article_id = 0;
 233          }
 234  
 235          // Prepare data for display using display options
 236          foreach ($items as &$item) {
 237              $item->slug = $item->id . ':' . $item->alias;
 238  
 239              if ($access || \in_array($item->access, $authorised)) {
 240                  // We know that user has the privilege to view the article
 241                  $item->link = Route::_(RouteHelper::getArticleRoute($item->slug, $item->catid, $item->language));
 242              } else {
 243                  $menu      = $app->getMenu();
 244                  $menuitems = $menu->getItems('link', 'index.php?option=com_users&view=login');
 245  
 246                  if (isset($menuitems[0])) {
 247                      $Itemid = $menuitems[0]->id;
 248                  } elseif ($input->getInt('Itemid') > 0) {
 249                      // Use Itemid from requesting page only if there is no existing menu
 250                      $Itemid = $input->getInt('Itemid');
 251                  }
 252  
 253                  $item->link = Route::_('index.php?option=com_users&view=login&Itemid=' . $Itemid);
 254              }
 255  
 256              // Used for styling the active article
 257              $item->active      = $item->id == $active_article_id ? 'active' : '';
 258              $item->displayDate = '';
 259  
 260              if ($show_date) {
 261                  $item->displayDate = HTMLHelper::_('date', $item->$show_date_field, $show_date_format);
 262              }
 263  
 264              if ($item->catid) {
 265                  $item->displayCategoryLink  = Route::_(RouteHelper::getCategoryRoute($item->catid, $item->category_language));
 266                  $item->displayCategoryTitle = $show_category ? '<a href="' . $item->displayCategoryLink . '">' . $item->category_title . '</a>' : '';
 267              } else {
 268                  $item->displayCategoryTitle = $show_category ? $item->category_title : '';
 269              }
 270  
 271              $item->displayHits       = $show_hits ? $item->hits : '';
 272              $item->displayAuthorName = $show_author ? $item->author : '';
 273  
 274              if ($show_introtext) {
 275                  $item->introtext = HTMLHelper::_('content.prepare', $item->introtext, '', 'mod_articles_category.content');
 276                  $item->introtext = self::_cleanIntrotext($item->introtext);
 277              }
 278  
 279              $item->displayIntrotext = $show_introtext ? self::truncate($item->introtext, $introtext_limit) : '';
 280              $item->displayReadmore  = $item->alternative_readmore;
 281          }
 282  
 283          return $items;
 284      }
 285  
 286      /**
 287       * Strips unnecessary tags from the introtext
 288       *
 289       * @param   string  $introtext  introtext to sanitize
 290       *
 291       * @return mixed|string
 292       *
 293       * @since  1.6
 294       */
 295      public static function _cleanIntrotext($introtext)
 296      {
 297          $introtext = str_replace(array('<p>', '</p>'), ' ', $introtext);
 298          $introtext = strip_tags($introtext, '<a><em><strong><joomla-hidden-mail>');
 299          $introtext = trim($introtext);
 300  
 301          return $introtext;
 302      }
 303  
 304      /**
 305       * Method to truncate introtext
 306       *
 307       * The goal is to get the proper length plain text string with as much of
 308       * the html intact as possible with all tags properly closed.
 309       *
 310       * @param   string   $html       The content of the introtext to be truncated
 311       * @param   integer  $maxLength  The maximum number of characters to render
 312       *
 313       * @return  string  The truncated string
 314       *
 315       * @since   1.6
 316       */
 317      public static function truncate($html, $maxLength = 0)
 318      {
 319          $baseLength = \strlen($html);
 320  
 321          // First get the plain text string. This is the rendered text we want to end up with.
 322          $ptString = HTMLHelper::_('string.truncate', $html, $maxLength, $noSplit = true, $allowHtml = false);
 323  
 324          for ($maxLength; $maxLength < $baseLength;) {
 325              // Now get the string if we allow html.
 326              $htmlString = HTMLHelper::_('string.truncate', $html, $maxLength, $noSplit = true, $allowHtml = true);
 327  
 328              // Now get the plain text from the html string.
 329              $htmlStringToPtString = HTMLHelper::_('string.truncate', $htmlString, $maxLength, $noSplit = true, $allowHtml = false);
 330  
 331              // If the new plain text string matches the original plain text string we are done.
 332              if ($ptString === $htmlStringToPtString) {
 333                  return $htmlString;
 334              }
 335  
 336              // Get the number of html tag characters in the first $maxlength characters
 337              $diffLength = \strlen($ptString) - \strlen($htmlStringToPtString);
 338  
 339              // Set new $maxlength that adjusts for the html tags
 340              $maxLength += $diffLength;
 341  
 342              if ($baseLength <= $maxLength || $diffLength <= 0) {
 343                  return $htmlString;
 344              }
 345          }
 346  
 347          return $html;
 348      }
 349  
 350      /**
 351       * Groups items by field
 352       *
 353       * @param   array   $list             list of items
 354       * @param   string  $fieldName        name of field that is used for grouping
 355       * @param   string  $direction        ordering direction
 356       * @param   null    $fieldNameToKeep  field name to keep
 357       *
 358       * @return  array
 359       *
 360       * @since   1.6
 361       */
 362      public static function groupBy($list, $fieldName, $direction, $fieldNameToKeep = null)
 363      {
 364          $grouped = array();
 365  
 366          if (!\is_array($list)) {
 367              if ($list === '') {
 368                  return $grouped;
 369              }
 370  
 371              $list = array($list);
 372          }
 373  
 374          foreach ($list as $key => $item) {
 375              if (!isset($grouped[$item->$fieldName])) {
 376                  $grouped[$item->$fieldName] = array();
 377              }
 378  
 379              if ($fieldNameToKeep === null) {
 380                  $grouped[$item->$fieldName][$key] = $item;
 381              } else {
 382                  $grouped[$item->$fieldName][$key] = $item->$fieldNameToKeep;
 383              }
 384  
 385              unset($list[$key]);
 386          }
 387  
 388          $direction($grouped);
 389  
 390          return $grouped;
 391      }
 392  
 393      /**
 394       * Groups items by date
 395       *
 396       * @param   array   $list             list of items
 397       * @param   string  $direction        ordering direction
 398       * @param   string  $type             type of grouping
 399       * @param   string  $monthYearFormat  date format to use
 400       * @param   string  $field            date field to group by
 401       *
 402       * @return  array
 403       *
 404       * @since   1.6
 405       */
 406      public static function groupByDate($list, $direction = 'ksort', $type = 'year', $monthYearFormat = 'F Y', $field = 'created')
 407      {
 408          $grouped = array();
 409  
 410          if (!\is_array($list)) {
 411              if ($list === '') {
 412                  return $grouped;
 413              }
 414  
 415              $list = array($list);
 416          }
 417  
 418          foreach ($list as $key => $item) {
 419              switch ($type) {
 420                  case 'month_year':
 421                      $month_year = StringHelper::substr($item->$field, 0, 7);
 422  
 423                      if (!isset($grouped[$month_year])) {
 424                          $grouped[$month_year] = array();
 425                      }
 426  
 427                      $grouped[$month_year][$key] = $item;
 428                      break;
 429  
 430                  default:
 431                      $year = StringHelper::substr($item->$field, 0, 4);
 432  
 433                      if (!isset($grouped[$year])) {
 434                          $grouped[$year] = array();
 435                      }
 436  
 437                      $grouped[$year][$key] = $item;
 438                      break;
 439              }
 440  
 441              unset($list[$key]);
 442          }
 443  
 444          $direction($grouped);
 445  
 446          if ($type === 'month_year') {
 447              foreach ($grouped as $group => $items) {
 448                  $date                      = new Date($group);
 449                  $formatted_group           = $date->format($monthYearFormat);
 450                  $grouped[$formatted_group] = $items;
 451  
 452                  unset($grouped[$group]);
 453              }
 454          }
 455  
 456          return $grouped;
 457      }
 458  
 459      /**
 460       * Groups items by tags
 461       *
 462       * @param   array   $list       list of items
 463       * @param   string  $direction  ordering direction
 464       *
 465       * @return  array
 466       *
 467       * @since   3.9.0
 468       */
 469      public static function groupByTags($list, $direction = 'ksort')
 470      {
 471          $grouped  = array();
 472          $untagged = array();
 473  
 474          if (!$list) {
 475              return $grouped;
 476          }
 477  
 478          foreach ($list as $item) {
 479              if ($item->tags->itemTags) {
 480                  foreach ($item->tags->itemTags as $tag) {
 481                      $grouped[$tag->title][] = $item;
 482                  }
 483              } else {
 484                  $untagged[] = $item;
 485              }
 486          }
 487  
 488          $direction($grouped);
 489  
 490          if ($untagged) {
 491              $grouped['MOD_ARTICLES_CATEGORY_UNTAGGED'] = $untagged;
 492          }
 493  
 494          return $grouped;
 495      }
 496  }


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