PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/content/pagebreak/pagebreak.php

https://github.com/joebushi/joomla
PHP | 262 lines | 178 code | 43 blank | 41 comment | 33 complexity | a910b167805b55c1545144c1117257b3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. // No direct access.
  8. defined('_JEXEC') or die;
  9. /**
  10. * Page break plugin
  11. *
  12. * <b>Usage:</b>
  13. * <code><hr class="system-pagebreak" /></code>
  14. * <code><hr class="system-pagebreak" title="The page title" /></code>
  15. * or
  16. * <code><hr class="system-pagebreak" alt="The first page" /></code>
  17. * or
  18. * <code><hr class="system-pagebreak" title="The page title" alt="The first page" /></code>
  19. * or
  20. * <code><hr class="system-pagebreak" alt="The first page" title="The page title" /></code>
  21. *
  22. * @package Joomla
  23. * @subpackage plg_pagebreak
  24. */
  25. class plgContentPagebreak extends JPlugin
  26. {
  27. public function onPrepareContent(&$row, &$params, $page=0)
  28. {
  29. // Expression to search for.
  30. $regex = '#<hr(.*)class=\"system-pagebreak\"(.*)\/>#iU';
  31. $print = JRequest::getBool('print');
  32. $showall = JRequest::getBool('showall');
  33. if (!$this->params->get('enabled', 1)) {
  34. $print = true;
  35. }
  36. if ($print) {
  37. $row->text = preg_replace($regex, '<BR/>', $row->text);
  38. return true;
  39. }
  40. // Simple performance check to determine whether bot should process further.
  41. if (JString::strpos($row->text, 'class="system-pagebreak') === false) {
  42. return true;
  43. }
  44. $db = &JFactory::getDbo();
  45. $full = JRequest::getBool('fullview');
  46. if (!$page) {
  47. $page = 0;
  48. }
  49. if ($params->get('intro_only') || $params->get('popup') || $full) {
  50. $row->text = preg_replace($regex, '', $row->text);
  51. return;
  52. }
  53. // Find all instances of plugin and put in $matches.
  54. $matches = array();
  55. preg_match_all($regex, $row->text, $matches, PREG_SET_ORDER);
  56. if (($showall && $this->params->get('showall', 1)))
  57. {
  58. $hasToc = $this->params->get('multipage_toc', 1);
  59. if ($hasToc)
  60. {
  61. // Display TOC.
  62. $page = 1;
  63. $this->_createToc($row, $matches, $page);
  64. }
  65. else {
  66. $row->toc = '';
  67. }
  68. $row->text = preg_replace($regex, '<BR/>', $row->text);
  69. return true;
  70. }
  71. // Split the text around the plugin.
  72. $text = preg_split($regex, $row->text);
  73. // Count the number of pages.
  74. $n = count($text);
  75. // We have found at least one plugin, therefore at least 2 pages.
  76. if ($n > 1)
  77. {
  78. $title = $this->params->get('title', 1);
  79. $hasToc = $this->params->get('multipage_toc', 1);
  80. // Adds heading or title to <site> Title.
  81. if ($title)
  82. {
  83. if ($page)
  84. {
  85. $page_text = $page + 1;
  86. if ($page && @$matches[$page-1][2])
  87. {
  88. $attrs = JUtility::parseAttributes($matches[$page-1][1]);
  89. if (@$attrs['title']) {
  90. $row->page_title = $attrs['title'];
  91. }
  92. }
  93. }
  94. }
  95. // Reset the text, we already hold it in the $text array.
  96. $row->text = '';
  97. // Display TOC.
  98. if ($hasToc) {
  99. $this->_createToc($row, $matches, $page);
  100. } else {
  101. $row->toc = '';
  102. }
  103. // Traditional mos page navigation.
  104. jimport('joomla.html.pagination');
  105. $pageNav = new JPagination($n, $page, 1);
  106. // Page counter.
  107. $row->text .= '<div class="pagenavcounter">';
  108. $row->text .= $pageNav->getPagesCounter();
  109. $row->text .= '</div>';
  110. // Page text.
  111. $text[$page] = str_replace("<hr id=\"\"system-readmore\"\" />", "", $text[$page]);
  112. $row->text .= $text[$page];
  113. $row->text .= '<br />';
  114. $row->text .= '<div class="pagenavbar">';
  115. // Adds navigation between pages to bottom of text.
  116. if ($hasToc) {
  117. $this->_createNavigation($row, $page, $n);
  118. }
  119. // Page links shown at bottom of page if TOC disabled.
  120. if (!$hasToc) {
  121. $row->text .= $pageNav->getPagesLinks();
  122. }
  123. $row->text .= '</div><br />';
  124. }
  125. return true;
  126. }
  127. protected function _createTOC(&$row, &$matches, &$page)
  128. {
  129. $heading = $row->title;
  130. // TOC header.
  131. $row->toc = '
  132. <table cellpadding="0" cellspacing="0" class="contenttoc" align="right">
  133. <tr>
  134. <th>'
  135. . JText::_('Article Index') .
  136. '</th>
  137. </tr>
  138. ';
  139. // TOC first Page link.
  140. $row->toc .= '
  141. <tr>
  142. <td>
  143. <a href="'. JRoute::_('&showall=&limitstart=') .'" class="toclink">'
  144. . $heading .
  145. '</a>
  146. </td>
  147. </tr>
  148. ';
  149. $i = 2;
  150. foreach ($matches as $bot)
  151. {
  152. $link = JRoute::_('&showall=&limitstart='. ($i-1));
  153. if (@$bot[0])
  154. {
  155. $attrs2 = JUtility::parseAttributes($bot[0]);
  156. if (@$attrs2['alt']) {
  157. $title = stripslashes($attrs2['alt']);
  158. } elseif (@$attrs2['title']) {
  159. $title = stripslashes($attrs2['title']);
  160. } else {
  161. $title = JText::sprintf('Page #', $i);
  162. }
  163. }
  164. else {
  165. $title = JText::sprintf('Page #', $i);
  166. }
  167. $row->toc .= '
  168. <tr>
  169. <td>
  170. <a href="'. $link .'" class="toclink">'
  171. . $title .
  172. '</a>
  173. </td>
  174. </tr>
  175. ';
  176. $i++;
  177. }
  178. if ($this->params->get('showall'))
  179. {
  180. $link = JRoute::_('&showall=1&limitstart=');
  181. $row->toc .= '
  182. <tr>
  183. <td>
  184. <a href="'. $link .'" class="toclink">'
  185. . JText::_('All Pages') .
  186. '</a>
  187. </td>
  188. </tr>
  189. ';
  190. }
  191. $row->toc .= '</table>';
  192. }
  193. protected function _createNavigation(&$row, $page, $n)
  194. {
  195. $pnSpace = "";
  196. if (JText::_('LT') || JText::_('GT')) $pnSpace = " ";
  197. if ($page < $n-1)
  198. {
  199. $page_next = $page + 1;
  200. $link_next = JRoute::_('&limitstart='. ($page_next));
  201. // Next >>
  202. $next = '<a href="'. $link_next .'">' . JText::_('Next') . $pnSpace . JText::_('GT') . JText::_('GT') .'</a>';
  203. }
  204. else {
  205. $next = JText::_('Next');
  206. }
  207. if ($page > 0)
  208. {
  209. $page_prev = $page - 1 == 0 ? "" : $page - 1;
  210. $link_prev = JRoute::_( '&limitstart='. ($page_prev));
  211. // << Prev
  212. $prev = '<a href="'. $link_prev .'">'. JText::_('LT') . JText::_('LT') . $pnSpace . JText::_('Prev') .'</a>';
  213. }
  214. else {
  215. $prev = JText::_('Prev');
  216. }
  217. $row->text .= '<div>' . $prev . ' - ' . $next .'</div>';
  218. }
  219. }