PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/content/pagebreak/pagebreak.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 327 lines | 207 code | 53 blank | 67 comment | 48 complexity | be0c8b09a93a694d182f576240e3e101 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE.txt
  5. */
  6. // No direct access.
  7. defined('_JEXEC') or die;
  8. jimport('joomla.html.pagination');
  9. jimport('joomla.html.html.sliders');
  10. jimport('joomla.html.html.tabs');
  11. jimport('joomla.utilities.utility');
  12. /**
  13. * Page break plugin
  14. *
  15. * <b>Usage:</b>
  16. * <code><hr class="system-pagebreak" /></code>
  17. * <code><hr class="system-pagebreak" title="The page title" /></code>
  18. * or
  19. * <code><hr class="system-pagebreak" alt="The first page" /></code>
  20. * or
  21. * <code><hr class="system-pagebreak" title="The page title" alt="The first page" /></code>
  22. * or
  23. * <code><hr class="system-pagebreak" alt="The first page" title="The page title" /></code>
  24. *
  25. * @package Joomla.Plugin
  26. * @subpackage Content.pagebreak
  27. * @since 1.6
  28. */
  29. class plgContentPagebreak extends JPlugin
  30. {
  31. /**
  32. * Constructor
  33. *
  34. * @access protected
  35. * @param object $subject The object to observe
  36. * @param array $config An array that holds the plugin configuration
  37. * @since 1.5
  38. */
  39. public function __construct(& $subject, $config)
  40. {
  41. parent::__construct($subject, $config);
  42. $this->loadLanguage();
  43. }
  44. /**
  45. * @param string The context of the content being passed to the plugin.
  46. * @param object The article object. Note $article->text is also available
  47. * @param object The article params
  48. * @param int The 'page' number
  49. *
  50. * @return void
  51. * @since 1.6
  52. */
  53. public function onContentPrepare($context, &$row, &$params, $page = 0)
  54. {
  55. $canProceed = $context == 'com_content.article';
  56. if (!$canProceed) {
  57. return;
  58. }
  59. $style = $this->params->get('style', 'pages');
  60. // Expression to search for.
  61. $regex = '#<hr(.*)class="system-pagebreak"(.*)\/>#iU';
  62. $print = JRequest::getBool('print');
  63. $showall = JRequest::getBool('showall');
  64. if (!$this->params->get('enabled', 1)) {
  65. $print = true;
  66. }
  67. if ($print) {
  68. $row->text = preg_replace($regex, '<br />', $row->text);
  69. return true;
  70. }
  71. // Simple performance check to determine whether bot should process further.
  72. if (JString::strpos($row->text, 'class="system-pagebreak') === false) {
  73. return true;
  74. }
  75. $db = JFactory::getDbo();
  76. $view = JRequest::getString('view');
  77. $full = JRequest::getBool('fullview');
  78. if (!$page) {
  79. $page = 0;
  80. }
  81. if ($params->get('intro_only') || $params->get('popup') || $full || $view != 'article') {
  82. $row->text = preg_replace($regex, '', $row->text);
  83. return;
  84. }
  85. // Find all instances of plugin and put in $matches.
  86. $matches = array();
  87. preg_match_all($regex, $row->text, $matches, PREG_SET_ORDER);
  88. if (($showall && $this->params->get('showall', 1))) {
  89. $hasToc = $this->params->get('multipage_toc', 1);
  90. if ($hasToc) {
  91. // Display TOC.
  92. $page = 1;
  93. $this->_createToc($row, $matches, $page);
  94. } else {
  95. $row->toc = '';
  96. }
  97. $row->text = preg_replace($regex, '<br />', $row->text);
  98. return true;
  99. }
  100. // Split the text around the plugin.
  101. $text = preg_split($regex, $row->text);
  102. // Count the number of pages.
  103. $n = count($text);
  104. // We have found at least one plugin, therefore at least 2 pages.
  105. if ($n > 1) {
  106. $title = $this->params->get('title', 1);
  107. $hasToc = $this->params->get('multipage_toc', 1);
  108. // Adds heading or title to <site> Title.
  109. if ($title) {
  110. if ($page) {
  111. $page_text = $page + 1;
  112. if ($page && @$matches[$page-1][2]) {
  113. $attrs = JUtility::parseAttributes($matches[$page-1][1]);
  114. if (@$attrs['title']) {
  115. $row->page_title = $attrs['title'];
  116. }
  117. }
  118. }
  119. }
  120. // Reset the text, we already hold it in the $text array.
  121. $row->text = '';
  122. if ($style == 'pages') {
  123. // Display TOC.
  124. if ($hasToc) {
  125. $this->_createToc($row, $matches, $page);
  126. } else {
  127. $row->toc = '';
  128. }
  129. // traditional mos page navigation
  130. $pageNav = new JPagination($n, $page, 1);
  131. // Page counter.
  132. $row->text .= '<div class="pagenavcounter">';
  133. $row->text .= $pageNav->getPagesCounter();
  134. $row->text .= '</div>';
  135. // Page text.
  136. $text[$page] = str_replace('<hr id="system-readmore" />', '', $text[$page]);
  137. $row->text .= $text[$page];
  138. // $row->text .= '<br />';
  139. $row->text .= '<div class="pagination">';
  140. // Adds navigation between pages to bottom of text.
  141. if ($hasToc) {
  142. $this->_createNavigation($row, $page, $n);
  143. }
  144. // Page links shown at bottom of page if TOC disabled.
  145. if (!$hasToc) {
  146. $row->text .= $pageNav->getPagesLinks();
  147. }
  148. $row->text .= '</div>';
  149. }
  150. else {
  151. $t[] = $text[0];
  152. $t[] = (string) JHtml::_($style.'.start');
  153. foreach ($text as $key => $subtext) {
  154. if ($key >= 1) {
  155. $match= $matches[$key-1];
  156. $match = (array) JUtility::parseAttributes($match[0]);
  157. if (isset($match['alt'])) {
  158. $title = stripslashes($match["alt"]);
  159. } elseif (isset($match['title'])) {
  160. $title = stripslashes($match['title']);
  161. } else {
  162. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $key);
  163. }
  164. $t[] = (string) JHtml::_($style.'.panel', $match['title'], 'basic-details');
  165. }
  166. $t[] = (string) $subtext;
  167. }
  168. $t[] = (string) JHtml::_($style.'.end');
  169. $row->text = implode(' ', $t);
  170. }
  171. }
  172. return true;
  173. }
  174. /**
  175. * @return void
  176. * @return 1.6
  177. */
  178. protected function _createTOC(&$row, &$matches, &$page)
  179. {
  180. $heading = isset($row->title) ? $row->title : JText::_('PLG_CONTENT_PAGEBREAK_NO_TITLE');
  181. $limitstart = JRequest::getUInt('limitstart', 0);
  182. $showall = JRequest::getInt('showall', 0);
  183. // TOC header.
  184. $row->toc .= '<div id="article-index">';
  185. if($this->params->get('article_index')==1)
  186. {
  187. $headingtext= JText::_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX');
  188. if($this->params->get('article_index_text')) {
  189. htmlspecialchars($headingtext=$this->params->get('article_index_text'));
  190. }
  191. $row->toc .='<h3>'.$headingtext.'</h3>';
  192. }
  193. // TOC first Page link.
  194. $class = ($limitstart === 0 && $showall === 0) ? 'toclink active' : 'toclink';
  195. $row->toc .= '<ul>
  196. <li>
  197. <a href="'. JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart=') .'" class="'.$class.'">'
  198. . $heading .
  199. '</a>
  200. </li>
  201. ';
  202. $i = 2;
  203. foreach ($matches as $bot) {
  204. $link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($i-1));
  205. if (@$bot[0]) {
  206. $attrs2 = JUtility::parseAttributes($bot[0]);
  207. if (@$attrs2['alt']) {
  208. $title = stripslashes($attrs2['alt']);
  209. } elseif (@$attrs2['title']) {
  210. $title = stripslashes($attrs2['title']);
  211. } else {
  212. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
  213. }
  214. } else {
  215. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
  216. }
  217. $class = ($limitstart == $i-1) ? 'toclink active' : 'toclink';
  218. $row->toc .= '
  219. <li>
  220. <a href="'. $link .'" class="'.$class.'">'
  221. . $title .
  222. '</a>
  223. </li>
  224. ';
  225. $i++;
  226. }
  227. if ($this->params->get('showall')) {
  228. $link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=1&limitstart=');
  229. $class = ($showall == 1) ? 'toclink active' : 'toclink';
  230. $row->toc .= '
  231. <li>
  232. <a href="'. $link .'" class="'.$class.'">'
  233. . JText::_('PLG_CONTENT_PAGEBREAK_ALL_PAGES') .
  234. '</a>
  235. </li>
  236. ';
  237. }
  238. $row->toc .= '</ul></div>';
  239. }
  240. /**
  241. * @return void
  242. * @since 1.6
  243. */
  244. protected function _createNavigation(&$row, $page, $n)
  245. {
  246. $pnSpace = '';
  247. if (JText::_('JGLOBAL_LT') || JText::_('JGLOBAL_LT')) {
  248. $pnSpace = ' ';
  249. }
  250. if ($page < $n-1) {
  251. $page_next = $page + 1;
  252. $link_next = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($page_next));
  253. // Next >>
  254. $next = '<a href="'. $link_next .'">' . JText::_('JNEXT') . $pnSpace . JText::_('JGLOBAL_GT') . JText::_('JGLOBAL_GT') .'</a>';
  255. } else {
  256. $next = JText::_('JNEXT');
  257. }
  258. if ($page > 0) {
  259. $page_prev = $page - 1 == 0 ? '' : $page - 1;
  260. $link_prev = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($page_prev));
  261. // << Prev
  262. $prev = '<a href="'. $link_prev .'">'. JText::_('JGLOBAL_LT') . JText::_('JGLOBAL_LT') . $pnSpace . JText::_('JPREV') .'</a>';
  263. } else {
  264. $prev = JText::_('JPREV');
  265. }
  266. $row->text .= '<ul><li>' . $prev . ' </li><li>' . $next .'</li></ul>';
  267. }
  268. }