[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/content/pagenavigation/ -> pagenavigation.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Content.pagenavigation
   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   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Access\Access;
  14  use Joomla\CMS\Factory;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  use Joomla\CMS\Plugin\PluginHelper;
  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   * Pagenavigation plugin class.
  27   *
  28   * @since  1.5
  29   */
  30  class PlgContentPagenavigation extends CMSPlugin
  31  {
  32      /**
  33       * If in the article view and the parameter is enabled shows the page navigation
  34       *
  35       * @param   string   $context  The context of the content being passed to the plugin
  36       * @param   object   &$row     The article object
  37       * @param   mixed    &$params  The article params
  38       * @param   integer  $page     The 'page' number
  39       *
  40       * @return  mixed  void or true
  41       *
  42       * @since   1.6
  43       */
  44      public function onContentBeforeDisplay($context, &$row, &$params, $page = 0)
  45      {
  46          $app   = Factory::getApplication();
  47          $view  = $app->input->get('view');
  48          $print = $app->input->getBool('print');
  49  
  50          if ($print) {
  51              return false;
  52          }
  53  
  54          if ($context === 'com_content.article' && $view === 'article' && $params->get('show_item_navigation')) {
  55              $db         = Factory::getDbo();
  56              $user       = Factory::getUser();
  57              $lang       = Factory::getLanguage();
  58              $now        = Factory::getDate()->toSql();
  59              $query      = $db->getQuery(true);
  60              $uid        = $row->id;
  61              $option     = 'com_content';
  62              $canPublish = $user->authorise('core.edit.state', $option . '.article.' . $row->id);
  63  
  64              /**
  65               * The following is needed as different menu items types utilise a different param to control ordering.
  66               * For Blogs the `orderby_sec` param is the order controlling param.
  67               * For Table and List views it is the `orderby` param.
  68               */
  69              $params_list = $params->toArray();
  70  
  71              if (array_key_exists('orderby_sec', $params_list)) {
  72                  $order_method = $params->get('orderby_sec', '');
  73              } else {
  74                  $order_method = $params->get('orderby', '');
  75              }
  76  
  77              // Additional check for invalid sort ordering.
  78              if ($order_method === 'front') {
  79                  $order_method = '';
  80              }
  81  
  82              if (in_array($order_method, ['date', 'rdate'])) {
  83                  // Get the order code
  84                  $orderDate = $params->get('order_date');
  85  
  86                  switch ($orderDate) {
  87                      // Use created if modified is not set
  88                      case 'modified':
  89                          $orderby = 'CASE WHEN ' . $db->quoteName('a.modified') . ' IS NULL THEN ' .
  90                              $db->quoteName('a.created') . ' ELSE ' . $db->quoteName('a.modified') . ' END';
  91                          break;
  92  
  93                      // Use created if publish_up is not set
  94                      case 'published':
  95                          $orderby = 'CASE WHEN ' . $db->quoteName('a.publish_up') . ' IS NULL THEN ' .
  96                              $db->quoteName('a.created') . ' ELSE ' . $db->quoteName('a.publish_up') . ' END';
  97                          break;
  98  
  99                      // Use created as default
 100                      default:
 101                          $orderby = $db->quoteName('a.created');
 102                          break;
 103                  }
 104  
 105                  if ($order_method === 'rdate') {
 106                      $orderby .= ' DESC';
 107                  }
 108              } else {
 109                  // Determine sort order.
 110                  switch ($order_method) {
 111                      case 'alpha':
 112                          $orderby = $db->quoteName('a.title');
 113                          break;
 114                      case 'ralpha':
 115                          $orderby = $db->quoteName('a.title') . ' DESC';
 116                          break;
 117                      case 'hits':
 118                          $orderby = $db->quoteName('a.hits');
 119                          break;
 120                      case 'rhits':
 121                          $orderby = $db->quoteName('a.hits') . ' DESC';
 122                          break;
 123                      case 'author':
 124                          $orderby = $db->quoteName(['a.created_by_alias', 'u.name']);
 125                          break;
 126                      case 'rauthor':
 127                          $orderby = $db->quoteName('a.created_by_alias') . ' DESC, ' .
 128                              $db->quoteName('u.name') . ' DESC';
 129                          break;
 130                      case 'front':
 131                          $orderby = $db->quoteName('f.ordering');
 132                          break;
 133                      default:
 134                          $orderby = $db->quoteName('a.ordering');
 135                          break;
 136                  }
 137              }
 138  
 139              $query->order($orderby);
 140  
 141              $case_when = ' CASE WHEN ' . $query->charLength($db->quoteName('a.alias'), '!=', '0')
 142                  . ' THEN ' . $query->concatenate([$query->castAsChar($db->quoteName('a.id')), $db->quoteName('a.alias')], ':')
 143                  . ' ELSE ' . $query->castAsChar('a.id') . ' END AS ' . $db->quoteName('slug');
 144  
 145              $case_when1 = ' CASE WHEN ' . $query->charLength($db->quoteName('cc.alias'), '!=', '0')
 146                  . ' THEN ' . $query->concatenate([$query->castAsChar($db->quoteName('cc.id')), $db->quoteName('cc.alias')], ':')
 147                  . ' ELSE ' . $query->castAsChar('cc.id') . ' END AS ' . $db->quoteName('catslug');
 148  
 149              $query->select($db->quoteName(['a.id', 'a.title', 'a.catid', 'a.language']))
 150                  ->select([$case_when, $case_when1])
 151                  ->from($db->quoteName('#__content', 'a'))
 152                  ->join('LEFT', $db->quoteName('#__categories', 'cc'), $db->quoteName('cc.id') . ' = ' . $db->quoteName('a.catid'));
 153  
 154              if ($order_method === 'author' || $order_method === 'rauthor') {
 155                  $query->select($db->quoteName(['a.created_by', 'u.name']));
 156                  $query->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('a.created_by'));
 157              }
 158  
 159              $query->where(
 160                  [
 161                      $db->quoteName('a.catid') . ' = :catid',
 162                      $db->quoteName('a.state') . ' = :state',
 163                  ]
 164              )
 165                  ->bind(':catid', $row->catid, ParameterType::INTEGER)
 166                  ->bind(':state', $row->state, ParameterType::INTEGER);
 167  
 168              if (!$canPublish) {
 169                  $query->whereIn($db->quoteName('a.access'), Access::getAuthorisedViewLevels($user->id));
 170              }
 171  
 172              $query->where(
 173                  [
 174                      '(' . $db->quoteName('publish_up') . ' IS NULL OR ' . $db->quoteName('publish_up') . ' <= :nowDate1)',
 175                      '(' . $db->quoteName('publish_down') . ' IS NULL OR ' . $db->quoteName('publish_down') . ' >= :nowDate2)',
 176                  ]
 177              )
 178                  ->bind(':nowDate1', $now)
 179                  ->bind(':nowDate2', $now);
 180  
 181              if ($app->isClient('site') && $app->getLanguageFilter()) {
 182                  $query->whereIn($db->quoteName('a.language'), [$lang->getTag(), '*'], ParameterType::STRING);
 183              }
 184  
 185              $db->setQuery($query);
 186              $list = $db->loadObjectList('id');
 187  
 188              // This check needed if incorrect Itemid is given resulting in an incorrect result.
 189              if (!is_array($list)) {
 190                  $list = [];
 191              }
 192  
 193              reset($list);
 194  
 195              // Location of current content item in array list.
 196              $location = array_search($uid, array_keys($list));
 197              $rows     = array_values($list);
 198  
 199              $row->prev = null;
 200              $row->next = null;
 201  
 202              if ($location - 1 >= 0) {
 203                  // The previous content item cannot be in the array position -1.
 204                  $row->prev = $rows[$location - 1];
 205              }
 206  
 207              if (($location + 1) < count($rows)) {
 208                  // The next content item cannot be in an array position greater than the number of array positions.
 209                  $row->next = $rows[$location + 1];
 210              }
 211  
 212              if ($row->prev) {
 213                  $row->prev_label = ($this->params->get('display', 0) == 0) ? Text::_('JPREV') : $row->prev->title;
 214                  $row->prev = RouteHelper::getArticleRoute($row->prev->slug, $row->prev->catid, $row->prev->language);
 215              } else {
 216                  $row->prev_label = '';
 217                  $row->prev = '';
 218              }
 219  
 220              if ($row->next) {
 221                  $row->next_label = ($this->params->get('display', 0) == 0) ? Text::_('JNEXT') : $row->next->title;
 222                  $row->next = RouteHelper::getArticleRoute($row->next->slug, $row->next->catid, $row->next->language);
 223              } else {
 224                  $row->next_label = '';
 225                  $row->next = '';
 226              }
 227  
 228              // Output.
 229              if ($row->prev || $row->next) {
 230                  // Get the path for the layout file
 231                  $path = PluginHelper::getLayoutPath('content', 'pagenavigation');
 232  
 233                  // Render the pagenav
 234                  ob_start();
 235                  include $path;
 236                  $row->pagination = ob_get_clean();
 237  
 238                  $row->paginationposition = $this->params->get('position', 1);
 239  
 240                  // This will default to the 1.5 and 1.6-1.7 behavior.
 241                  $row->paginationrelative = $this->params->get('relative', 0);
 242              }
 243          }
 244      }
 245  }


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