2 namespace wcf\system\template\plugin
;
3 use wcf\system\exception\SystemException
;
4 use wcf\system\request\LinkHandler
;
5 use wcf\system\template\TemplateEngine
;
7 use wcf\util\StringUtil
;
10 * Template function plugin which generates sliding pagers.
13 * {pages pages=10 link='page-%d.html'}
14 * {pages page=8 pages=10 link='page-%d.html'}
16 * assign to variable 'output'; do not print:
17 * {pages page=8 pages=10 link='page-%d.html' assign='output'}
19 * assign to variable 'output' and do print also:
20 * {pages page=8 pages=10 link='page-%d.html' assign='output' print=true}
23 * @copyright 2001-2017 WoltLab GmbH
24 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
25 * @package WoltLabSuite\Core\System\Template\Plugin
27 class PagesFunctionTemplatePlugin
implements IFunctionTemplatePlugin
{
31 * CSS class name for <nav> element
34 protected $cssClassName = 'pagination';
37 * Inserts the page number into the link.
40 * @param integer $pageNo
41 * @return string final link
43 protected static function insertPageNumber($link, $pageNo) {
44 $startPos = mb_strpos($link, '%d');
45 if ($startPos !== null) $link = mb_substr($link, 0, $startPos) . $pageNo . mb_substr($link, $startPos +
2);
50 * Generates HTML code for a link.
53 * @param integer $pageNo
54 * @param integer $activePage
55 * @param integer $pages
58 protected function makeLink($link, $pageNo, $activePage, $pages) {
60 if ($activePage != $pageNo) {
61 return '<li><a href="'.self
::insertPageNumber($link, $pageNo).'" title="'.WCF
::getLanguage()->getDynamicVariable('wcf.page.pageNo', ['pageNo' => $pageNo]).'">'.StringUtil
::formatInteger($pageNo).'</a></li>'."\n";
64 return '<li class="active"><span>'.StringUtil
::formatInteger($pageNo).'</span><span class="invisible">'.WCF
::getLanguage()->getDynamicVariable('wcf.page.pagePosition', ['pageNo' => $pageNo, 'pages' => $pages]).'</span></li>'."\n";
69 * Generates HTML code for 'previous' link.
72 * @param integer $pageNo
75 protected function makePreviousLink($link, $pageNo) {
77 return '<li class="skip"><a href="'.self
::insertPageNumber($link, $pageNo - 1).'" title="'.WCF
::getLanguage()->getDynamicVariable('wcf.global.page.previous').'" class="icon icon24 fa-chevron-left jsTooltip"></a></li>'."\n";
80 return '<li class="skip disabled"><span class="icon icon24 fa-chevron-left"></span></li>'."\n";
85 * Generates HTML code for 'next' link.
88 * @param integer $pageNo
89 * @param integer $pages
92 protected function makeNextLink($link, $pageNo, $pages) {
93 if ($pageNo && $pageNo < $pages) {
94 return '<li class="skip"><a href="'.self
::insertPageNumber($link, $pageNo +
1).'" title="'.WCF
::getLanguage()->getDynamicVariable('wcf.global.page.next').'" class="icon icon24 fa-chevron-right jsTooltip"></a></li>'."\n";
97 return '<li class="skip disabled"><span class="icon icon24 fa-chevron-right"></span></li>'."\n";
104 public function execute($tagArgs, TemplateEngine
$tplObj) {
105 // needed params: controller, link, page, pages
106 if (!isset($tagArgs['link'])) throw new SystemException("missing 'link' argument in pages tag");
107 if (!isset($tagArgs['controller'])) throw new SystemException("missing 'controller' argument in pages tag");
108 if (!isset($tagArgs['pages'])) {
109 if (($tagArgs['pages'] = $tplObj->get('pages')) === null) {
110 throw new SystemException("missing 'pages' argument in pages tag");
116 if ($tagArgs['pages'] > 1) {
117 // create and encode route link
119 if (isset($tagArgs['id'])) $parameters['id'] = $tagArgs['id'];
120 if (isset($tagArgs['title'])) $parameters['title'] = $tagArgs['title'];
121 if (isset($tagArgs['object'])) $parameters['object'] = $tagArgs['object'];
122 if (isset($tagArgs['application'])) $parameters['application'] = $tagArgs['application'];
124 $link = StringUtil
::encodeHTML(LinkHandler
::getInstance()->getLink($tagArgs['controller'], $parameters, $tagArgs['link']));
126 if (!isset($tagArgs['page'])) {
127 if (($tagArgs['page'] = $tplObj->get('pageNo')) === null) {
128 $tagArgs['page'] = 0;
133 $html .= "<nav class=\"".$this->cssClassName
."\" data-link=\"".$link."\" data-pages=\"".$tagArgs['pages']."\">\n<ul>\n";
136 $html .= $this->makePreviousLink($link, $tagArgs['page']);
139 $html .= $this->makeLink($link, 1, $tagArgs['page'], $tagArgs['pages']);
141 // calculate page links
142 $maxLinks = static::SHOW_LINKS
- 4;
143 $linksBeforePage = $tagArgs['page'] - 2;
144 if ($linksBeforePage < 0) $linksBeforePage = 0;
145 $linksAfterPage = $tagArgs['pages'] - ($tagArgs['page'] +
1);
146 if ($linksAfterPage < 0) $linksAfterPage = 0;
147 if ($tagArgs['page'] > 1 && $tagArgs['page'] < $tagArgs['pages']) {
151 $half = $maxLinks / 2;
152 $left = $right = $tagArgs['page'];
153 if ($left < 1) $left = 1;
154 if ($right < 1) $right = 1;
155 if ($right > $tagArgs['pages'] - 1) $right = $tagArgs['pages'] - 1;
157 if ($linksBeforePage >= $half) {
161 $left -= $linksBeforePage;
162 $right +
= $half - $linksBeforePage;
165 if ($linksAfterPage >= $half) {
169 $right +
= $linksAfterPage;
170 $left -= $half - $linksAfterPage;
173 $right = intval(ceil($right));
174 $left = intval(ceil($left));
175 if ($left < 1) $left = 1;
176 if ($right > $tagArgs['pages']) $right = $tagArgs['pages'];
181 $html .= $this->makeLink($link, 2, $tagArgs['page'], $tagArgs['pages']);
184 $html .= '<li class="jumpTo"><a title="'.WCF
::getLanguage()->getDynamicVariable('wcf.page.jumpTo').'" class="jsTooltip">'.StringUtil
::HELLIP
.'</a></li>'."\n";
189 for ($i = $left +
1; $i < $right; $i++
) {
190 $html .= $this->makeLink($link, $i, $tagArgs['page'], $tagArgs['pages']);
194 if ($right < $tagArgs['pages']) {
195 if ($tagArgs['pages'] - $right < 2) {
196 $html .= $this->makeLink($link, $tagArgs['pages'] - 1, $tagArgs['page'], $tagArgs['pages']);
199 $html .= '<li class="jumpTo"><a title="'.WCF
::getLanguage()->getDynamicVariable('wcf.page.jumpTo').'" class="jsTooltip">'.StringUtil
::HELLIP
.'</a></li>'."\n";
204 $html .= $this->makeLink($link, $tagArgs['pages'], $tagArgs['page'], $tagArgs['pages']);
207 $html .= $this->makeNextLink($link, $tagArgs['page'], $tagArgs['pages']);
210 $html .= "</ul></nav>\n";
213 // assign html output to template var
214 if (isset($tagArgs['assign'])) {
215 $tplObj->assign($tagArgs['assign'], $html);
216 if (!isset($tagArgs['print']) ||
!$tagArgs['print']) return '';