Source for file pagebreak.php

Documentation is available at pagebreak.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Content.pagebreak
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. jimport('joomla.utilities.utility');
  13.  
  14. /**
  15.  * Page break plugin
  16.  *
  17.  * <b>Usage:</b>
  18.  * <code><hr class="system-pagebreak" /></code>
  19.  * <code><hr class="system-pagebreak" title="The page title" /></code>
  20.  * or
  21.  * <code><hr class="system-pagebreak" alt="The first page" /></code>
  22.  * or
  23.  * <code><hr class="system-pagebreak" title="The page title" alt="The first page" /></code>
  24.  * or
  25.  * <code><hr class="system-pagebreak" alt="The first page" title="The page title" /></code>
  26.  *
  27.  * @package     Joomla.Plugin
  28.  * @subpackage  Content.pagebreak
  29.  * @since       1.6
  30.  */
  31. class PlgContentPagebreak extends JPlugin
  32. {
  33.     /**
  34.      * Load the language file on instantiation.
  35.      *
  36.      * @var    boolean 
  37.      * @since  3.1
  38.      */
  39.     protected $autoloadLanguage = true;
  40.  
  41.     /**
  42.      * Plugin that adds a pagebreak into the text and truncates text at that point
  43.      *
  44.      * @param   string   $context  The context of the content being passed to the plugin.
  45.      * @param   object   &$row     The article object.  Note $article->text is also available
  46.      * @param   mixed    &$params  The article params
  47.      * @param   integer  $page     The 'page' number
  48.      *
  49.      * @return  mixed  Always returns void or true
  50.      *
  51.      * @since   1.6
  52.      */
  53.     public function onContentPrepare($context&$row&$params$page 0)
  54.     {
  55.         $canProceed $context == 'com_content.article';
  56.  
  57.         if (!$canProceed)
  58.         {
  59.             return;
  60.         }
  61.  
  62.         $style $this->params->get('style''pages');
  63.  
  64.         // Expression to search for.
  65.         $regex '#<hr(.*)class="system-pagebreak"(.*)\/>#iU';
  66.  
  67.         $input JFactory::getApplication()->input;
  68.  
  69.         $print $input->getBool('print');
  70.         $showall $input->getBool('showall');
  71.  
  72.         if (!$this->params->get('enabled'1))
  73.         {
  74.             $print true;
  75.         }
  76.  
  77.         if ($print)
  78.         {
  79.             $row->text preg_replace($regex'<br />'$row->text);
  80.  
  81.             return true;
  82.         }
  83.  
  84.         // Simple performance check to determine whether bot should process further.
  85.         if (JString::strpos($row->text'class="system-pagebreak'=== false)
  86.         {
  87.             return true;
  88.         }
  89.  
  90.         $view $input->getString('view');
  91.         $full $input->getBool('fullview');
  92.  
  93.         if (!$page)
  94.         {
  95.             $page 0;
  96.         }
  97.  
  98.         if ($params->get('intro_only'|| $params->get('popup'|| $full || $view != 'article')
  99.         {
  100.             $row->text preg_replace($regex''$row->text);
  101.  
  102.             return;
  103.         }
  104.  
  105.         // Find all instances of plugin and put in $matches.
  106.         $matches array();
  107.         preg_match_all($regex$row->text$matchesPREG_SET_ORDER);
  108.  
  109.         if (($showall && $this->params->get('showall'1)))
  110.         {
  111.             $hasToc $this->params->get('multipage_toc'1);
  112.  
  113.             if ($hasToc)
  114.             {
  115.                 // Display TOC.
  116.                 $page 1;
  117.                 $this->_createToc($row$matches$page);
  118.             }
  119.             else
  120.             {
  121.                 $row->toc '';
  122.             }
  123.  
  124.             $row->text preg_replace($regex'<br />'$row->text);
  125.  
  126.             return true;
  127.         }
  128.  
  129.         // Split the text around the plugin.
  130.         $text preg_split($regex$row->text);
  131.  
  132.         // Count the number of pages.
  133.         $n count($text);
  134.  
  135.         // We have found at least one plugin, therefore at least 2 pages.
  136.         if ($n 1)
  137.         {
  138.             $title    $this->params->get('title'1);
  139.             $hasToc $this->params->get('multipage_toc'1);
  140.  
  141.             // Adds heading or title to <site> Title.
  142.             if ($title)
  143.             {
  144.                 if ($page)
  145.                 {
  146.                     if ($page && @$matches[$page 1][2])
  147.                     {
  148.                         $attrs JUtility::parseAttributes($matches[$page 1][1]);
  149.  
  150.                         if (@$attrs['title'])
  151.                         {
  152.                             $row->page_title $attrs['title'];
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.  
  158.             // Reset the text, we already hold it in the $text array.
  159.             $row->text '';
  160.  
  161.             if ($style == 'pages')
  162.             {
  163.                 // Display TOC.
  164.                 if ($hasToc)
  165.                 {
  166.                     $this->_createToc($row$matches$page);
  167.                 }
  168.                 else
  169.                 {
  170.                     $row->toc '';
  171.                 }
  172.  
  173.                 // Traditional mos page navigation
  174.                 $pageNav new JPagination($n$page1);
  175.  
  176.                 // Page counter.
  177.                 $row->text .= '<div class="pagenavcounter">';
  178.                 $row->text .= $pageNav->getPagesCounter();
  179.                 $row->text .= '</div>';
  180.  
  181.                 // Page text.
  182.                 $text[$pagestr_replace('<hr id="system-readmore" />'''$text[$page]);
  183.                 $row->text .= $text[$page];
  184.  
  185.                 // $row->text .= '<br />';
  186.                 $row->text .= '<div class="pager">';
  187.  
  188.                 // Adds navigation between pages to bottom of text.
  189.                 if ($hasToc)
  190.                 {
  191.                     $this->_createNavigation($row$page$n);
  192.                 }
  193.  
  194.                 // Page links shown at bottom of page if TOC disabled.
  195.                 if (!$hasToc)
  196.                 {
  197.                     $row->text .= $pageNav->getPagesLinks();
  198.                 }
  199.  
  200.                 $row->text .= '</div>';
  201.             }
  202.             else
  203.             {
  204.                 $t[$text[0];
  205.  
  206.                 $t[= (string) JHtml::_($style '.start''article' $row->id '-' $style);
  207.  
  208.                 foreach ($text as $key => $subtext)
  209.                 {
  210.                     if ($key >= 1)
  211.                     {
  212.                         $match $matches[$key 1];
  213.                         $match = (array) JUtility::parseAttributes($match[0]);
  214.  
  215.                         if (isset($match['alt']))
  216.                         {
  217.                             $title    stripslashes($match['alt']);
  218.                         }
  219.                         elseif (isset($match['title']))
  220.                         {
  221.                             $title    stripslashes($match['title']);
  222.                         }
  223.                         else
  224.                         {
  225.                             $title    JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM'$key 1);
  226.                         }
  227.  
  228.                         $t[= (string) JHtml::_($style '.panel'$title'article' $row->id '-' $style $key);
  229.                     }
  230.  
  231.                     $t[= (string) $subtext;
  232.                 }
  233.  
  234.                 $t[= (string) JHtml::_($style '.end');
  235.  
  236.                 $row->text implode(' '$t);
  237.             }
  238.         }
  239.  
  240.         return true;
  241.     }
  242.  
  243.     /**
  244.      * Creates a Table of Contents for the pagebreak
  245.      *
  246.      * @param   object   &$row      The article object.  Note $article->text is also available
  247.      * @param   array    &$matches  Array of matches of a regex in onContentPrepare
  248.      * @param   integer  &$page     The 'page' number
  249.      *
  250.      * @return  void 
  251.      *
  252.      * @since  1.6
  253.      */
  254.     protected function _createTOC(&$row&$matches&$page)
  255.     {
  256.         $heading = isset($row->title$row->title JText::_('PLG_CONTENT_PAGEBREAK_NO_TITLE');
  257.         $input JFactory::getApplication()->input;
  258.         $limitstart $input->getUInt('limitstart'0);
  259.         $showall $input->getInt('showall'0);
  260.  
  261.         // TOC header.
  262.         $row->toc .= '<div class="pull-right article-index">';
  263.  
  264.         if ($this->params->get('article_index'== 1)
  265.         {
  266.             $headingtext JText::_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX');
  267.  
  268.             if ($this->params->get('article_index_text'))
  269.             {
  270.                 htmlspecialchars($headingtext $this->params->get('article_index_text'));
  271.             }
  272.  
  273.             $row->toc .= '<h3>' $headingtext '</h3>';
  274.         }
  275.  
  276.         // TOC first Page link.
  277.         $class ($limitstart === && $showall === 0'toclink active' 'toclink';
  278.         $row->toc .= '<ul class="nav nav-tabs nav-stacked">
  279.         <li class="' $class '">
  280.  
  281.             <a href="' JRoute::_(ContentHelperRoute::getArticleRoute($row->slug$row->catid'&showall=&limitstart=''" class="' $class '">'
  282.             . $heading .
  283.             '</a>
  284.  
  285.         </li>
  286.         ';
  287.  
  288.         $i 2;
  289.  
  290.         foreach ($matches as $bot)
  291.         {
  292.             $link JRoute::_(ContentHelperRoute::getArticleRoute($row->slug$row->catid'&showall=&limitstart=' ($i 1));
  293.  
  294.             if (@$bot[0])
  295.             {
  296.                 $attrs2 JUtility::parseAttributes($bot[0]);
  297.  
  298.                 if (@$attrs2['alt'])
  299.                 {
  300.                     $title    stripslashes($attrs2['alt']);
  301.                 }
  302.                 elseif (@$attrs2['title'])
  303.                 {
  304.                     $title    stripslashes($attrs2['title']);
  305.                 }
  306.                 else
  307.                 {
  308.                     $title    JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM'$i);
  309.                 }
  310.             }
  311.             else
  312.             {
  313.                 $title    JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM'$i);
  314.             }
  315.  
  316.             $class ($limitstart == $i 1'toclink active' 'toclink';
  317.             $row->toc .= '
  318.                 <li>
  319.  
  320.                     <a href="' $link '" class="' $class '">'
  321.                     . $title .
  322.                     '</a>
  323.  
  324.                 </li>
  325.                 ';
  326.             $i++;
  327.         }
  328.  
  329.         if ($this->params->get('showall'))
  330.         {
  331.             $link JRoute::_(ContentHelperRoute::getArticleRoute($row->slug$row->catid'&showall=1&limitstart=');
  332.             $class ($showall == 1'toclink active' 'toclink';
  333.             $row->toc .= '
  334.             <li>
  335.  
  336.                     <a href="' $link '" class="' $class '">'
  337.                     . JText::_('PLG_CONTENT_PAGEBREAK_ALL_PAGES'.
  338.                     '</a>
  339.  
  340.             </li>
  341.             ';
  342.         }
  343.  
  344.         $row->toc .= '</ul></div>';
  345.     }
  346.  
  347.     /**
  348.      * Creates the navigation for the item
  349.      *
  350.      * @param   object  &$row  The article object.  Note $article->text is also available
  351.      * @param   int     $page  The total number of pages
  352.      * @param   int     $n     The page number
  353.      *
  354.      * @return  void 
  355.      *
  356.      * @since   1.6
  357.      */
  358.     protected function _createNavigation(&$row$page$n)
  359.     {
  360.         $pnSpace '';
  361.  
  362.         if (JText::_('JGLOBAL_LT'|| JText::_('JGLOBAL_LT'))
  363.         {
  364.             $pnSpace ' ';
  365.         }
  366.  
  367.         if ($page $n 1)
  368.         {
  369.             $page_next $page 1;
  370.  
  371.             $link_next JRoute::_(ContentHelperRoute::getArticleRoute($row->slug$row->catid'&showall=&limitstart=' ($page_next));
  372.  
  373.             // Next >>
  374.             $next '<a href="' $link_next '">' JText::_('JNEXT'$pnSpace JText::_('JGLOBAL_GT'JText::_('JGLOBAL_GT''</a>';
  375.         }
  376.         else
  377.         {
  378.             $next JText::_('JNEXT');
  379.         }
  380.  
  381.         if ($page 0)
  382.         {
  383.             $page_prev $page == '' $page 1;
  384.  
  385.             $link_prev JRoute::_(ContentHelperRoute::getArticleRoute($row->slug$row->catid'&showall=&limitstart=' ($page_prev));
  386.  
  387.             // << Prev
  388.             $prev '<a href="' $link_prev '">' JText::_('JGLOBAL_LT'JText::_('JGLOBAL_LT'$pnSpace JText::_('JPREV''</a>';
  389.         }
  390.         else
  391.         {
  392.             $prev JText::_('JPREV');
  393.         }
  394.  
  395.         $row->text .= '<ul><li>' $prev ' </li><li>' $next '</li></ul>';
  396.     }
  397. }

Documentation generated on Tue, 19 Nov 2013 15:10:04 +0100 by phpDocumentor 1.4.3