PageRenderTime 182ms CodeModel.GetById 60ms app.highlight 11ms RepoModel.GetById 99ms 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
  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
  8// No direct access.
  9defined('_JEXEC') or die;
 10
 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
 25 * @subpackage	plg_pagebreak
 26 */
 27class plgContentPagebreak extends JPlugin
 28{
 29
 30	public function onPrepareContent(&$row, &$params, $page=0)
 31	{
 32		// Expression to search for.
 33		$regex = '#<hr(.*)class=\"system-pagebreak\"(.*)\/>#iU';
 34
 35		$print = JRequest::getBool('print');
 36		$showall = JRequest::getBool('showall');
 37
 38		if (!$this->params->get('enabled', 1)) {
 39			$print = true;
 40		}
 41
 42		if ($print) {
 43			$row->text = preg_replace($regex, '<BR/>', $row->text);
 44			return true;
 45		}
 46
 47		// Simple performance check to determine whether bot should process further.
 48		if (JString::strpos($row->text, 'class="system-pagebreak') === false) {
 49			return true;
 50		}
 51
 52		$db = &JFactory::getDbo();
 53		$full = JRequest::getBool('fullview');
 54
 55		if (!$page) {
 56			$page = 0;
 57		}
 58
 59		if ($params->get('intro_only') || $params->get('popup') || $full) {
 60			$row->text = preg_replace($regex, '', $row->text);
 61			return;
 62		}
 63
 64		// Find all instances of plugin and put in $matches.
 65		$matches = array();
 66		preg_match_all($regex, $row->text, $matches, PREG_SET_ORDER);
 67
 68		if (($showall && $this->params->get('showall', 1)))
 69		{
 70			$hasToc = $this->params->get('multipage_toc', 1);
 71			if ($hasToc)
 72			{
 73				// Display TOC.
 74				$page = 1;
 75				$this->_createToc($row, $matches, $page);
 76			}
 77			else {
 78				$row->toc = '';
 79			}
 80			$row->text = preg_replace($regex, '<BR/>', $row->text);
 81			return true;
 82		}
 83
 84		// Split the text around the plugin.
 85		$text = preg_split($regex, $row->text);
 86
 87		// Count the number of pages.
 88		$n = count($text);
 89
 90		// We have found at least one plugin, therefore at least 2 pages.
 91		if ($n > 1)
 92		{
 93			$title	= $this->params->get('title', 1);
 94			$hasToc = $this->params->get('multipage_toc', 1);
 95			// Adds heading or title to <site> Title.
 96			if ($title)
 97			{
 98				if ($page)
 99				{
100					$page_text = $page + 1;
101					if ($page && @$matches[$page-1][2])
102					{
103						$attrs = JUtility::parseAttributes($matches[$page-1][1]);
104
105						if (@$attrs['title']) {
106							$row->page_title = $attrs['title'];
107						}
108					}
109				}
110			}
111
112			// Reset the text, we already hold it in the $text array.
113			$row->text = '';
114
115			// Display TOC.
116			if ($hasToc) {
117				$this->_createToc($row, $matches, $page);
118			} else {
119				$row->toc = '';
120			}
121
122			// Traditional mos page navigation.
123			jimport('joomla.html.pagination');
124			$pageNav = new JPagination($n, $page, 1);
125
126			// Page counter.
127			$row->text .= '<div class="pagenavcounter">';
128			$row->text .= $pageNav->getPagesCounter();
129			$row->text .= '</div>';
130
131			// Page text.
132			$text[$page] = str_replace("<hr id=\"\"system-readmore\"\" />", "", $text[$page]);
133			$row->text .= $text[$page];
134
135			$row->text .= '<br />';
136			$row->text .= '<div class="pagenavbar">';
137
138			// Adds navigation between pages to bottom of text.
139			if ($hasToc) {
140				$this->_createNavigation($row, $page, $n);
141			}
142
143			// Page links shown at bottom of page if TOC disabled.
144			if (!$hasToc) {
145				$row->text .= $pageNav->getPagesLinks();
146			}
147
148			$row->text .= '</div><br />';
149		}
150
151		return true;
152	}
153
154	protected function _createTOC(&$row, &$matches, &$page)
155	{
156
157		$heading = $row->title;
158
159		// TOC header.
160		$row->toc = '
161		<table cellpadding="0" cellspacing="0" class="contenttoc" align="right">
162		<tr>
163			<th>'
164			. JText::_('Article Index') .
165			'</th>
166		</tr>
167		';
168
169		// TOC first Page link.
170		$row->toc .= '
171		<tr>
172			<td>
173			<a href="'. JRoute::_('&showall=&limitstart=') .'" class="toclink">'
174			. $heading .
175			'</a>
176			</td>
177		</tr>
178		';
179
180		$i = 2;
181
182		foreach ($matches as $bot)
183		{
184			$link = JRoute::_('&showall=&limitstart='. ($i-1));
185
186
187			if (@$bot[0])
188			{
189				$attrs2 = JUtility::parseAttributes($bot[0]);
190
191				if (@$attrs2['alt']) {
192					$title	= stripslashes($attrs2['alt']);
193				} elseif (@$attrs2['title']) {
194					$title	= stripslashes($attrs2['title']);
195				} else {
196					$title	= JText::sprintf('Page #', $i);
197				}
198			}
199			else {
200				$title	= JText::sprintf('Page #', $i);
201			}
202
203			$row->toc .= '
204				<tr>
205					<td>
206					<a href="'. $link .'" class="toclink">'
207					. $title .
208					'</a>
209					</td>
210				</tr>
211				';
212			$i++;
213		}
214
215		if ($this->params->get('showall'))
216		{
217			$link = JRoute::_('&showall=1&limitstart=');
218			$row->toc .= '
219			<tr>
220				<td>
221					<a href="'. $link .'" class="toclink">'
222					. JText::_('All Pages') .
223					'</a>
224				</td>
225			</tr>
226			';
227		}
228		$row->toc .= '</table>';
229	}
230
231	protected function _createNavigation(&$row, $page, $n)
232	{
233		$pnSpace = "";
234		if (JText::_('LT') || JText::_('GT')) $pnSpace = " ";
235
236		if ($page < $n-1)
237		{
238			$page_next = $page + 1;
239
240			$link_next = JRoute::_('&limitstart='. ($page_next));
241			// Next >>
242			$next = '<a href="'. $link_next .'">' . JText::_('Next') . $pnSpace . JText::_('GT') . JText::_('GT') .'</a>';
243		}
244		else {
245			$next = JText::_('Next');
246		}
247
248		if ($page > 0)
249		{
250			$page_prev = $page - 1 == 0 ? "" : $page - 1;
251
252			$link_prev = JRoute::_( '&limitstart='. ($page_prev));
253			// << Prev
254			$prev = '<a href="'. $link_prev .'">'. JText::_('LT') . JText::_('LT') . $pnSpace . JText::_('Prev') .'</a>';
255		}
256		else {
257			$prev = JText::_('Prev');
258		}
259
260		$row->text .= '<div>' . $prev . ' - ' . $next .'</div>';
261	}
262}