PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/content/pagebreak/pagebreak.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 368 lines | 247 code | 54 blank | 67 comment | 41 complexity | e9f1acce4a714f90eef76927912a73d4 MD5 | raw file
  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. defined('_JEXEC') or die;
  10. jimport('joomla.utilities.utility');
  11. /**
  12. * Page break plugin
  13. *
  14. * <b>Usage:</b>
  15. * <code><hr class="system-pagebreak" /></code>
  16. * <code><hr class="system-pagebreak" title="The page title" /></code>
  17. * or
  18. * <code><hr class="system-pagebreak" alt="The first page" /></code>
  19. * or
  20. * <code><hr class="system-pagebreak" title="The page title" alt="The first page" /></code>
  21. * or
  22. * <code><hr class="system-pagebreak" alt="The first page" title="The page title" /></code>
  23. *
  24. * @package Joomla.Plugin
  25. * @subpackage Content.pagebreak
  26. * @since 1.6
  27. */
  28. class PlgContentPagebreak extends JPlugin
  29. {
  30. /**
  31. * Load the language file on instantiation.
  32. *
  33. * @var boolean
  34. * @since 3.1
  35. */
  36. protected $autoloadLanguage = true;
  37. /**
  38. * @param string The context of the content being passed to the plugin.
  39. * @param object The article object. Note $article->text is also available
  40. * @param object The article params
  41. * @param integer The 'page' number
  42. *
  43. * @return void
  44. * @since 1.6
  45. */
  46. public function onContentPrepare($context, &$row, &$params, $page = 0)
  47. {
  48. $canProceed = $context == 'com_content.article';
  49. if (!$canProceed)
  50. {
  51. return;
  52. }
  53. $style = $this->params->get('style', 'pages');
  54. // Expression to search for.
  55. $regex = '#<hr(.*)class="system-pagebreak"(.*)\/>#iU';
  56. $input = JFactory::getApplication()->input;
  57. $print = $input->getBool('print');
  58. $showall = $input->getBool('showall');
  59. if (!$this->params->get('enabled', 1))
  60. {
  61. $print = true;
  62. }
  63. if ($print)
  64. {
  65. $row->text = preg_replace($regex, '<br />', $row->text);
  66. return true;
  67. }
  68. // Simple performance check to determine whether bot should process further.
  69. if (JString::strpos($row->text, 'class="system-pagebreak') === false)
  70. {
  71. return true;
  72. }
  73. $view = $input->getString('view');
  74. $full = $input->getBool('fullview');
  75. if (!$page)
  76. {
  77. $page = 0;
  78. }
  79. if ($params->get('intro_only') || $params->get('popup') || $full || $view != 'article')
  80. {
  81. $row->text = preg_replace($regex, '', $row->text);
  82. return;
  83. }
  84. // Find all instances of plugin and put in $matches.
  85. $matches = array();
  86. preg_match_all($regex, $row->text, $matches, PREG_SET_ORDER);
  87. if (($showall && $this->params->get('showall', 1)))
  88. {
  89. $hasToc = $this->params->get('multipage_toc', 1);
  90. if ($hasToc)
  91. {
  92. // Display TOC.
  93. $page = 1;
  94. $this->_createToc($row, $matches, $page);
  95. }
  96. else
  97. {
  98. $row->toc = '';
  99. }
  100. $row->text = preg_replace($regex, '<br />', $row->text);
  101. return true;
  102. }
  103. // Split the text around the plugin.
  104. $text = preg_split($regex, $row->text);
  105. // Count the number of pages.
  106. $n = count($text);
  107. // We have found at least one plugin, therefore at least 2 pages.
  108. if ($n > 1)
  109. {
  110. $title = $this->params->get('title', 1);
  111. $hasToc = $this->params->get('multipage_toc', 1);
  112. // Adds heading or title to <site> Title.
  113. if ($title)
  114. {
  115. if ($page)
  116. {
  117. if ($page && @$matches[$page - 1][2])
  118. {
  119. $attrs = JUtility::parseAttributes($matches[$page - 1][1]);
  120. if (@$attrs['title'])
  121. {
  122. $row->page_title = $attrs['title'];
  123. }
  124. }
  125. }
  126. }
  127. // Reset the text, we already hold it in the $text array.
  128. $row->text = '';
  129. if ($style == 'pages')
  130. {
  131. // Display TOC.
  132. if ($hasToc)
  133. {
  134. $this->_createToc($row, $matches, $page);
  135. }
  136. else
  137. {
  138. $row->toc = '';
  139. }
  140. // traditional mos page navigation
  141. $pageNav = new JPagination($n, $page, 1);
  142. // Page counter.
  143. $row->text .= '<div class="pagenavcounter">';
  144. $row->text .= $pageNav->getPagesCounter();
  145. $row->text .= '</div>';
  146. // Page text.
  147. $text[$page] = str_replace('<hr id="system-readmore" />', '', $text[$page]);
  148. $row->text .= $text[$page];
  149. // $row->text .= '<br />';
  150. $row->text .= '<div class="pager">';
  151. // Adds navigation between pages to bottom of text.
  152. if ($hasToc)
  153. {
  154. $this->_createNavigation($row, $page, $n);
  155. }
  156. // Page links shown at bottom of page if TOC disabled.
  157. if (!$hasToc)
  158. {
  159. $row->text .= $pageNav->getPagesLinks();
  160. }
  161. $row->text .= '</div>';
  162. }
  163. else
  164. {
  165. $t[] = $text[0];
  166. $t[] = (string) JHtml::_($style.'.start', 'article'.$row->id.'-'.$style);
  167. foreach ($text as $key => $subtext)
  168. {
  169. if ($key >= 1)
  170. {
  171. $match = $matches[$key - 1];
  172. $match = (array) JUtility::parseAttributes($match[0]);
  173. if (isset($match['alt']))
  174. {
  175. $title = stripslashes($match['alt']);
  176. }
  177. elseif (isset($match['title']))
  178. {
  179. $title = stripslashes($match['title']);
  180. }
  181. else
  182. {
  183. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $key + 1);
  184. }
  185. $t[] = (string) JHtml::_($style.'.panel', $title, 'article'.$row->id.'-'.$style.$key);
  186. }
  187. $t[] = (string) $subtext;
  188. }
  189. $t[] = (string) JHtml::_($style.'.end');
  190. $row->text = implode(' ', $t);
  191. }
  192. }
  193. return true;
  194. }
  195. /**
  196. * @return void
  197. * @return 1.6
  198. */
  199. protected function _createTOC(&$row, &$matches, &$page)
  200. {
  201. $heading = isset($row->title) ? $row->title : JText::_('PLG_CONTENT_PAGEBREAK_NO_TITLE');
  202. $input = JFactory::getApplication()->input;
  203. $limitstart = $input->getUInt('limitstart', 0);
  204. $showall = $input->getInt('showall', 0);
  205. // TOC header.
  206. $row->toc .= '<div class="pull-right article-index">';
  207. if ($this->params->get('article_index') == 1)
  208. {
  209. $headingtext = JText::_('PLG_CONTENT_PAGEBREAK_ARTICLE_INDEX');
  210. if ($this->params->get('article_index_text'))
  211. {
  212. htmlspecialchars($headingtext = $this->params->get('article_index_text'));
  213. }
  214. $row->toc .= '<h3>' . $headingtext . '</h3>';
  215. }
  216. // TOC first Page link.
  217. $class = ($limitstart === 0 && $showall === 0) ? 'toclink active' : 'toclink';
  218. $row->toc .= '<ul class="nav nav-tabs nav-stacked">
  219. <li class="'.$class.'">
  220. <a href="'. JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart=') .'" class="'.$class.'">'
  221. . $heading .
  222. '</a>
  223. </li>
  224. ';
  225. $i = 2;
  226. foreach ($matches as $bot)
  227. {
  228. $link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($i - 1));
  229. if (@$bot[0])
  230. {
  231. $attrs2 = JUtility::parseAttributes($bot[0]);
  232. if (@$attrs2['alt'])
  233. {
  234. $title = stripslashes($attrs2['alt']);
  235. }
  236. elseif (@$attrs2['title'])
  237. {
  238. $title = stripslashes($attrs2['title']);
  239. }
  240. else
  241. {
  242. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
  243. }
  244. }
  245. else
  246. {
  247. $title = JText::sprintf('PLG_CONTENT_PAGEBREAK_PAGE_NUM', $i);
  248. }
  249. $class = ($limitstart == $i - 1) ? 'toclink active' : 'toclink';
  250. $row->toc .= '
  251. <li>
  252. <a href="'. $link .'" class="'.$class.'">'
  253. . $title .
  254. '</a>
  255. </li>
  256. ';
  257. $i++;
  258. }
  259. if ($this->params->get('showall'))
  260. {
  261. $link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=1&limitstart=');
  262. $class = ($showall == 1) ? 'toclink active' : 'toclink';
  263. $row->toc .= '
  264. <li>
  265. <a href="'. $link .'" class="'.$class.'">'
  266. . JText::_('PLG_CONTENT_PAGEBREAK_ALL_PAGES') .
  267. '</a>
  268. </li>
  269. ';
  270. }
  271. $row->toc .= '</ul></div>';
  272. }
  273. /**
  274. * @return void
  275. * @since 1.6
  276. */
  277. protected function _createNavigation(&$row, $page, $n)
  278. {
  279. $pnSpace = '';
  280. if (JText::_('JGLOBAL_LT') || JText::_('JGLOBAL_LT'))
  281. {
  282. $pnSpace = ' ';
  283. }
  284. if ($page < $n - 1)
  285. {
  286. $page_next = $page + 1;
  287. $link_next = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($page_next));
  288. // Next >>
  289. $next = '<a href="'. $link_next .'">' . JText::_('JNEXT') . $pnSpace . JText::_('JGLOBAL_GT') . JText::_('JGLOBAL_GT') .'</a>';
  290. }
  291. else
  292. {
  293. $next = JText::_('JNEXT');
  294. }
  295. if ($page > 0)
  296. {
  297. $page_prev = $page - 1 == 0 ? '' : $page - 1;
  298. $link_prev = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catid).'&showall=&limitstart='. ($page_prev));
  299. // << Prev
  300. $prev = '<a href="'. $link_prev .'">'. JText::_('JGLOBAL_LT') . JText::_('JGLOBAL_LT') . $pnSpace . JText::_('JPREV') .'</a>';
  301. }
  302. else
  303. {
  304. $prev = JText::_('JPREV');
  305. }
  306. $row->text .= '<ul><li>' . $prev . ' </li><li>' . $next .'</li></ul>';
  307. }
  308. }