[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Content.pagebreak
   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\Factory;
  14  use Joomla\CMS\HTML\HTMLHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Pagination\Pagination;
  17  use Joomla\CMS\Plugin\CMSPlugin;
  18  use Joomla\CMS\Plugin\PluginHelper;
  19  use Joomla\CMS\Utility\Utility;
  20  use Joomla\Component\Content\Site\Helper\RouteHelper;
  21  use Joomla\String\StringHelper;
  22  
  23  // phpcs:disable PSR1.Files.SideEffects
  24  \defined('_JEXEC') or die;
  25  // phpcs:enable PSR1.Files.SideEffects
  26  
  27  /**
  28   * Page break plugin
  29   *
  30   * <strong>Usage:</strong>
  31   * <code><hr class="system-pagebreak" /></code>
  32   * <code><hr class="system-pagebreak" title="The page title" /></code>
  33   * or
  34   * <code><hr class="system-pagebreak" alt="The first page" /></code>
  35   * or
  36   * <code><hr class="system-pagebreak" title="The page title" alt="The first page" /></code>
  37   * or
  38   * <code><hr class="system-pagebreak" alt="The first page" title="The page title" /></code>
  39   *
  40   * @since  1.6
  41   */
  42  class PlgContentPagebreak extends CMSPlugin
  43  {
  44      /**
  45       * The navigation list with all page objects if parameter 'multipage_toc' is active.
  46       *
  47       * @var    array
  48       * @since  4.0.0
  49       */
  50      protected $list = array();
  51  
  52      /**
  53       * Plugin that adds a pagebreak into the text and truncates text at that point
  54       *
  55       * @param   string   $context  The context of the content being passed to the plugin.
  56       * @param   object   &$row     The article object.  Note $article->text is also available
  57       * @param   mixed    &$params  The article params
  58       * @param   integer  $page     The 'page' number
  59       *
  60       * @return  void
  61       *
  62       * @since   1.6
  63       */
  64      public function onContentPrepare($context, &$row, &$params, $page = 0)
  65      {
  66          $canProceed = $context === 'com_content.article';
  67  
  68          if (!$canProceed) {
  69              return;
  70          }
  71  
  72          $style = $this->params->get('style', 'pages');
  73  
  74          // Expression to search for.
  75          $regex = '#<hr(.*)class="system-pagebreak"(.*)\/?>#iU';
  76  
  77          $input = Factory::getApplication()->input;
  78  
  79          $print = $input->getBool('print');
  80          $showall = $input->getBool('showall');
  81  
  82          if (!$this->params->get('enabled', 1)) {
  83              $print = true;
  84          }
  85  
  86          if ($print) {
  87              $row->text = preg_replace($regex, '<br>', $row->text);
  88  
  89              return;
  90          }
  91  
  92          // Simple performance check to determine whether bot should process further.
  93          if (StringHelper::strpos($row->text, 'class="system-pagebreak') === false) {
  94              if ($page > 0) {
  95                  throw new Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
  96              }
  97  
  98              return;
  99          }
 100  
 101          $view = $input->getString('view');
 102          $full = $input->getBool('fullview');
 103  
 104          if (!$page) {
 105              $page = 0;
 106          }
 107  
 108          if ($full || $view !== 'article' || $params->get('intro_only') || $params->get('popup')) {
 109              $row->text = preg_replace($regex, '', $row->text);
 110  
 111              return;
 112          }
 113  
 114          // Load plugin language files only when needed (ex: not needed if no system-pagebreak class exists).
 115          $this->loadLanguage();
 116  
 117          // Find all instances of plugin and put in $matches.
 118          $matches = array();
 119          preg_match_all($regex, $row->text, $matches, PREG_SET_ORDER);
 120  
 121          if ($showall && $this->params->get('showall', 1)) {
 122              $hasToc = $this->params->get('multipage_toc', 1);
 123  
 124              if ($hasToc) {
 125                  // Display TOC.
 126                  $page = 1;
 127                  $this->_createToc($row, $matches, $page);
 128              } else {
 129                  $row->toc = '';
 130              }
 131  
 132              $row->text = preg_replace($regex, '<br>', $row->text);
 133  
 134              return;
 135          }
 136  
 137          // Split the text around the plugin.
 138          $text = preg_split($regex, $row->text);
 139  
 140          if (!isset($text[$page])) {
 141              throw new Exception(Text::_('JERROR_PAGE_NOT_FOUND'), 404);
 142          }
 143  
 144          // Count the number of pages.
 145          $n = count($text);
 146  
 147          // We have found at least one plugin, therefore at least 2 pages.
 148          if ($n > 1) {
 149              $title  = $this->params->get('title', 1);
 150              $hasToc = $this->params->get('multipage_toc', 1);
 151  
 152              // Adds heading or title to <site> Title.
 153              if ($title && $page && isset($matches[$page - 1][0])) {
 154                  $attrs = Utility::parseAttributes($matches[$page - 1][0]);
 155  
 156                  if (isset($attrs['title'])) {
 157                      $row->page_title = $attrs['title'];
 158                  }
 159              }
 160  
 161              // Reset the text, we already hold it in the $text array.
 162              $row->text = '';
 163  
 164              if ($style === 'pages') {
 165                  // Display TOC.
 166                  if ($hasToc) {
 167                      $this->_createToc($row, $matches, $page);
 168                  } else {
 169                      $row->toc = '';
 170                  }
 171  
 172                  // Traditional mos page navigation
 173                  $pageNav = new Pagination($n, $page, 1);
 174  
 175                  // Flag indicates to not add limitstart=0 to URL
 176                  $pageNav->hideEmptyLimitstart = true;
 177  
 178                  // Page counter.
 179                  $row->text .= '<div class="pagenavcounter">';
 180                  $row->text .= $pageNav->getPagesCounter();
 181                  $row->text .= '</div>';
 182  
 183                  // Page text.
 184                  $text[$page] = str_replace('<hr id="system-readmore" />', '', $text[$page]);
 185                  $row->text .= $text[$page];
 186  
 187                  // $row->text .= '<br>';
 188                  $row->text .= '<div class="pager">';
 189  
 190                  // Adds navigation between pages to bottom of text.
 191                  if ($hasToc) {
 192                      $this->_createNavigation($row, $page, $n);
 193                  }
 194  
 195                  // Page links shown at bottom of page if TOC disabled.
 196                  if (!$hasToc) {
 197                      $row->text .= $pageNav->getPagesLinks();
 198                  }
 199  
 200                  $row->text .= '</div>';
 201              } else {
 202                  $t[] = $text[0];
 203  
 204                  if ($style === 'tabs') {
 205                      $t[] = (string) HTMLHelper::_('uitab.startTabSet', 'myTab', ['active' => 'article' . $row->id . '-' . $style . '0', 'view' => 'tabs']);
 206                  } else {
 207                      $t[] = (string) HTMLHelper::_('bootstrap.startAccordion', 'myAccordion', array('active' => 'article' . $row->id . '-' . $style . '0'));
 208                  }
 209  
 210                  foreach ($text as $key => $subtext) {
 211                      $index = 'article' . $row->id . '-' . $style . $key;
 212  
 213                      if ($key >= 1) {
 214                          $match = $matches[$key - 1];
 215                          $match = (array) Utility::parseAttributes($match[0]);
 216  
 217                          if (isset($match['alt'])) {
 218                              $title = stripslashes($match['alt']);
 219                          } elseif (isset($match['title'])) {
 220                              $title = stripslashes($match['title']);
 221                          } else {
 222                              $title = Text::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $key + 1);
 223                          }
 224  
 225                          if ($style === 'tabs') {
 226                              $t[] = (string) HTMLHelper::_('uitab.addTab', 'myTab', $index, $title);
 227                          } else {
 228                              $t[] = (string) HTMLHelper::_('bootstrap.addSlide', 'myAccordion', $title, $index);
 229                          }
 230  
 231                          $t[] = (string) $subtext;
 232  
 233                          if ($style === 'tabs') {
 234                              $t[] = (string) HTMLHelper::_('uitab.endTab');
 235                          } else {
 236                              $t[] = (string) HTMLHelper::_('bootstrap.endSlide');
 237                          }
 238                      }
 239                  }
 240  
 241                  if ($style === 'tabs') {
 242                      $t[] = (string) HTMLHelper::_('uitab.endTabSet');
 243                  } else {
 244                      $t[] = (string) HTMLHelper::_('bootstrap.endAccordion');
 245                  }
 246  
 247                  $row->text = implode(' ', $t);
 248              }
 249          }
 250      }
 251  
 252      /**
 253       * Creates a Table of Contents for the pagebreak
 254       *
 255       * @param   object   &$row      The article object.  Note $article->text is also available
 256       * @param   array    &$matches  Array of matches of a regex in onContentPrepare
 257       * @param   integer  &$page     The 'page' number
 258       *
 259       * @return  void
 260       *
 261       * @since  1.6
 262       */
 263      protected function _createToc(&$row, &$matches, &$page)
 264      {
 265          $heading     = $row->title ?? Text::_('PLG_CONTENT_PAGEBREAK_NO_TITLE');
 266          $input       = Factory::getApplication()->input;
 267          $limitstart  = $input->getUint('limitstart', 0);
 268          $showall     = $input->getInt('showall', 0);
 269          $headingtext = '';
 270  
 271          if ($this->params->get('article_index', 1) == 1) {
 272              $headingtext = Text::_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX');
 273  
 274              if ($this->params->get('article_index_text')) {
 275                  $headingtext = htmlspecialchars($this->params->get('article_index_text'), ENT_QUOTES, 'UTF-8');
 276              }
 277          }
 278  
 279          // TOC first Page link.
 280          $this->list[1]         = new stdClass();
 281          $this->list[1]->link   = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language);
 282          $this->list[1]->title  = $heading;
 283          $this->list[1]->active = ($limitstart === 0 && $showall === 0);
 284  
 285          $i = 2;
 286  
 287          foreach ($matches as $bot) {
 288              if (@$bot[0]) {
 289                  $attrs2 = Utility::parseAttributes($bot[0]);
 290  
 291                  if (@$attrs2['alt']) {
 292                      $title = stripslashes($attrs2['alt']);
 293                  } elseif (@$attrs2['title']) {
 294                      $title = stripslashes($attrs2['title']);
 295                  } else {
 296                      $title = Text::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
 297                  }
 298              } else {
 299                  $title = Text::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
 300              }
 301  
 302              $this->list[$i]         = new stdClass();
 303              $this->list[$i]->link   = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language) . '&limitstart=' . ($i - 1);
 304              $this->list[$i]->title  = $title;
 305              $this->list[$i]->active = ($limitstart === $i - 1);
 306  
 307              $i++;
 308          }
 309  
 310          if ($this->params->get('showall')) {
 311              $this->list[$i]         = new stdClass();
 312              $this->list[$i]->link   = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language) . '&showall=1';
 313              $this->list[$i]->title  = Text::_('PLG_CONTENT_PAGEBREAK_ALL_PAGES');
 314              $this->list[$i]->active = ($limitstart === $i - 1);
 315          }
 316  
 317          $list = $this->list;
 318          $path = PluginHelper::getLayoutPath('content', 'pagebreak', 'toc');
 319          ob_start();
 320          include $path;
 321          $row->toc = ob_get_clean();
 322      }
 323  
 324      /**
 325       * Creates the navigation for the item
 326       *
 327       * @param   object  &$row  The article object.  Note $article->text is also available
 328       * @param   int     $page  The page number
 329       * @param   int     $n     The total number of pages
 330       *
 331       * @return  void
 332       *
 333       * @since   1.6
 334       */
 335      protected function _createNavigation(&$row, $page, $n)
 336      {
 337          $links = array(
 338              'next' => '',
 339              'previous' => '',
 340          );
 341  
 342          if ($page < $n - 1) {
 343              $links['next'] = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language) . '&limitstart=' . ($page + 1);
 344          }
 345  
 346          if ($page > 0) {
 347              $links['previous'] = RouteHelper::getArticleRoute($row->slug, $row->catid, $row->language);
 348  
 349              if ($page > 1) {
 350                  $links['previous'] .= '&limitstart=' . ($page - 1);
 351              }
 352          }
 353  
 354          $path = PluginHelper::getLayoutPath('content', 'pagebreak', 'navigation');
 355          ob_start();
 356          include $path;
 357          $row->text .= ob_get_clean();
 358      }
 359  }


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