PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/content/pagebreak/pagebreak.php

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