1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace phpMyFAQ; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Pagination handler class. |
7
|
|
|
* |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public License, |
11
|
|
|
* v. 2.0. If a copy of the MPL was not distributed with this file, You can |
12
|
|
|
* obtain one at http://mozilla.org/MPL/2.0/. |
13
|
|
|
* |
14
|
|
|
* @package phpMyFAQ |
15
|
|
|
* |
16
|
|
|
* @author Anatoliy Belsky <[email protected]> |
17
|
|
|
* @author Thorsten Rinne <[email protected]> |
18
|
|
|
* @copyright 2009-2019 phpMyFAQ Team |
19
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
20
|
|
|
* |
21
|
|
|
* @link https://www.phpmyfaq.de |
22
|
|
|
* @since 2009-09-27 |
23
|
|
|
*/ |
24
|
|
|
if (!defined('IS_VALID_PHPMYFAQ')) { |
25
|
|
|
exit(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* PMF_Pagination. |
30
|
|
|
* |
31
|
|
|
* @package phpMyFAQ |
32
|
|
|
* |
33
|
|
|
* @author Anatoliy Belsky <[email protected]> |
34
|
|
|
* @author Thorsten Rinne <[email protected]> |
35
|
|
|
* @copyright 2009-2019 phpMyFAQ Team |
36
|
|
|
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
37
|
|
|
* |
38
|
|
|
* @link https://www.phpmyfaq.de |
39
|
|
|
* @since 2009-09-27 |
40
|
|
|
*/ |
41
|
|
|
class Pagination |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* Template vars. |
45
|
|
|
*/ |
46
|
|
|
const TPL_VAR_LINK_URL = '{LINK_URL}'; |
47
|
|
|
const TPL_VAR_LINK_TEXT = '{LINK_TEXT}'; |
48
|
|
|
const TPL_VAR_LAYOUT_CONTENT = '{LAYOUT_CONTENT}'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Base url used for links. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $baseUrl = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Total items count. |
59
|
|
|
* |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
protected $total = 0; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Items per page count. |
66
|
|
|
* |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected $perPage = 0; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Number of adjacent links. |
73
|
|
|
* |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
protected $adjacents = 4; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Default link template. |
80
|
|
|
* Possible variables are {LINK}, {TITLE}, {TEXT}. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $linkTpl = '<li class="page-item"><a class="page-link" href="{LINK_URL}">{LINK_TEXT}</a></li>'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Current page link template. |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $currentPageLinkTpl = '<li class="page-item active"><a class="page-link" href="{LINK_URL}">{LINK_TEXT}</a></li>'; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Next page link template. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
protected $nextPageLinkTpl = '<li class="page-item"><a class="page-link" href="{LINK_URL}">→</a></li>'; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Previous page link template. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected $prevPageLinkTpl = '<li class="page-item"><a class="page-link" href="{LINK_URL}">←</a></li>'; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* First page link template. |
109
|
|
|
* |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
protected $firstPageLinkTpl = '<li class="page-item"><a class="page-link" href="{LINK_URL}">⇤</a></li>'; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Last page link template. |
116
|
|
|
* |
117
|
|
|
* @var string |
118
|
|
|
*/ |
119
|
|
|
protected $lastPageLinkTpl = '<li class="page-item"><a class="page-link" href="{LINK_URL}">⇥</a></li>'; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Layout template. |
123
|
|
|
* |
124
|
|
|
* @var string |
125
|
|
|
*/ |
126
|
|
|
protected $layoutTpl = '<nav class="pmf-pagination"><ul class="pagination justify-content-center">{LAYOUT_CONTENT}</ul></nav>'; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Current page index. |
130
|
|
|
* |
131
|
|
|
* @var int |
132
|
|
|
*/ |
133
|
|
|
protected $currentPage = 0; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Param name to associate the page numbers to. |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
*/ |
140
|
|
|
protected $pageParamName = 'page'; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* SEO name. |
144
|
|
|
* |
145
|
|
|
* @var string |
146
|
|
|
*/ |
147
|
|
|
protected $seoName = ''; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Use rewritten URLs without GET variables. |
151
|
|
|
* |
152
|
|
|
* @var bool |
153
|
|
|
*/ |
154
|
|
|
protected $useRewrite = false; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Rewritten URL format for page param. |
158
|
|
|
* |
159
|
|
|
* @var string |
160
|
|
|
*/ |
161
|
|
|
protected $rewriteUrl = ''; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @var PMF_Configuration |
165
|
|
|
*/ |
166
|
|
|
private $_config; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Constructor. |
170
|
|
|
* |
171
|
|
|
* We read in the current page from the baseUrl, so if it contains |
172
|
|
|
* no pageParamName, first page is asumed |
173
|
|
|
* |
174
|
|
|
* @param Configuration $config |
175
|
|
|
* @param array $options initialization options, |
176
|
|
|
* possible options: |
177
|
|
|
* - baseUrl (default "") |
178
|
|
|
* - total |
179
|
|
|
* - perPage |
180
|
|
|
* - linkTpl |
181
|
|
|
* - currentPageLinkTpl |
182
|
|
|
* - nextPageLinkTpl |
183
|
|
|
* - prevPageLinkTpl |
184
|
|
|
* - firstPageLinkTpl |
185
|
|
|
* - lastPageLinkTpl |
186
|
|
|
* - layoutTpl |
187
|
|
|
* - pageParamName (default "page") |
188
|
|
|
* - useRewrite |
189
|
|
|
* |
190
|
|
|
* @return PMF_Pagination |
191
|
|
|
*/ |
192
|
|
|
public function __construct(Configuration $config, Array $options = null) |
193
|
|
|
{ |
194
|
|
|
$this->_config = $config; |
|
|
|
|
195
|
|
|
|
196
|
|
|
if (isset($options['baseUrl'])) { |
197
|
|
|
$this->baseUrl = $options['baseUrl']; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (isset($options['total'])) { |
201
|
|
|
$this->total = $options['total']; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
if (isset($options['perPage'])) { |
205
|
|
|
$this->perPage = $options['perPage']; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (isset($options['linkTpl'])) { |
209
|
|
|
$this->linkTpl = $options['linkTpl']; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (isset($options['currentPageLinkTpl'])) { |
213
|
|
|
$this->currentPageLinkTpl = $options['currentPageLinkTpl']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (isset($options['nextPageLinkTpl'])) { |
217
|
|
|
$this->nextPageLinkTpl = $options['nextPageLinkTpl']; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if (isset($options['prevPageLinkTpl'])) { |
221
|
|
|
$this->prevPageLinkTpl = $options['prevPageLinkTpl']; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (isset($options['firstPageLinkTpl'])) { |
225
|
|
|
$this->firstPageLinkTpl = $options['firstPageLinkTpl']; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if (isset($options['lastPageLinkTpl'])) { |
229
|
|
|
$this->lastPageLinkTpl = $options['lastPageLinkTpl']; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
if (isset($options['layoutTpl'])) { |
233
|
|
|
$this->layoutTpl = $options['layoutTpl']; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
if (isset($options['pageParamName'])) { |
237
|
|
|
$this->pageParamName = $options['pageParamName']; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
if (isset($options['seoName'])) { |
241
|
|
|
$this->seoName = $options['seoName']; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if (isset($options['useRewrite']) && isset($options['rewriteUrl'])) { |
245
|
|
|
$this->useRewrite = $options['useRewrite']; |
246
|
|
|
$this->rewriteUrl = $options['rewriteUrl']; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// Let this call to be last cuz it needs some options to be set before |
250
|
|
|
$this->currentPage = $this->getCurrentPageFromUrl($this->baseUrl); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Returns the current page URL. |
255
|
|
|
* |
256
|
|
|
* @param string $url URL |
257
|
|
|
* |
258
|
|
|
* @return int |
259
|
|
|
*/ |
260
|
|
|
protected function getCurrentPageFromUrl($url) |
261
|
|
|
{ |
262
|
|
|
$page = 1; |
263
|
|
|
|
264
|
|
|
if (!empty($url)) { |
265
|
|
|
$match = []; |
266
|
|
|
if (Strings::preg_match('$&(amp;|)'.$this->pageParamName.'=(\d+)$', $url, $match)) { |
267
|
|
|
$page = isset($match[2]) ? $match[2] : $page; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $page; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Render full pagination string. |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function render() |
280
|
|
|
{ |
281
|
|
|
$content = []; |
282
|
|
|
$pages = ceil($this->total/$this->perPage); |
283
|
|
|
$adjacents = floor($this->adjacents/2) >= 1 ? floor($this->adjacents/2) : 1; |
284
|
|
|
|
285
|
|
|
for ($page = 1; $page <= $pages; ++$page) { |
286
|
|
View Code Duplication |
if ($page > $this->adjacents && $page < $this->currentPage - $adjacents) { |
287
|
|
|
$content[] = '<li class="disabled"><a>…</a></li>'; |
288
|
|
|
$page = $this->currentPage - $adjacents - 1; |
289
|
|
|
continue; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
View Code Duplication |
if ($page > $this->currentPage + $adjacents && $page <= $pages - $this->adjacents) { |
293
|
|
|
$content[] = '<li class="disabled"><a>…</a></li>'; |
294
|
|
|
$page = $pages - $this->adjacents; |
295
|
|
|
continue; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
$link = $this->renderUrl($this->baseUrl, $page); |
299
|
|
|
|
300
|
|
|
if ($page == $this->currentPage) { |
301
|
|
|
$template = $this->currentPageLinkTpl; |
302
|
|
|
} else { |
303
|
|
|
$template = $this->linkTpl; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
$content[] = $this->renderLink($template, $link, $page); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if (1 < $this->currentPage) { |
310
|
|
|
array_unshift( |
311
|
|
|
$content, |
312
|
|
|
$this->renderLink( |
313
|
|
|
$this->prevPageLinkTpl, |
314
|
|
|
$this->renderUrl($this->baseUrl, $this->currentPage - 1), |
315
|
|
|
$this->currentPage - 1 |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
array_unshift( |
319
|
|
|
$content, |
320
|
|
|
$this->renderLink( |
321
|
|
|
$this->firstPageLinkTpl, |
322
|
|
|
$this->renderUrl($this->baseUrl, 1), |
323
|
|
|
1 |
324
|
|
|
) |
325
|
|
|
); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if ($page - 1 > $this->currentPage) { |
329
|
|
|
array_push( |
330
|
|
|
$content, |
331
|
|
|
$this->renderLink( |
332
|
|
|
$this->nextPageLinkTpl, |
333
|
|
|
$this->renderUrl($this->baseUrl, $this->currentPage + 1), |
334
|
|
|
$this->currentPage + 1 |
335
|
|
|
) |
336
|
|
|
); |
337
|
|
|
array_push( |
338
|
|
|
$content, |
339
|
|
|
$this->renderLink( |
340
|
|
|
$this->lastPageLinkTpl, |
341
|
|
|
$this->renderUrl($this->baseUrl, $page - 1), |
342
|
|
|
$page - 1 |
343
|
|
|
) |
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $this->renderLayout(implode(' ', $content)); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Render url for a given page. |
352
|
|
|
* |
353
|
|
|
* @param string $url url |
354
|
|
|
* @param int $page page number |
355
|
|
|
* |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
|
|
protected function renderUrl($url, $page) |
359
|
|
|
{ |
360
|
|
|
if ($this->useRewrite) { |
361
|
|
|
$url = sprintf($this->rewriteUrl, $page); |
362
|
|
|
} else { |
363
|
|
|
$cleanedUrl = Strings::preg_replace(array('$&(amp;|)'.$this->pageParamName.'=(\d+)$'), '', $url); |
364
|
|
|
$url = sprintf('%s&%s=%d', $cleanedUrl, $this->pageParamName, $page); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
return $url; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Render a link. |
372
|
|
|
* |
373
|
|
|
* @param string $tpl link template |
374
|
|
|
* @param string $url url value for template container |
375
|
|
|
* @param string $linkText text value for template container |
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
protected function renderLink($tpl, $url, $linkText) |
380
|
|
|
{ |
381
|
|
|
$search = array(self::TPL_VAR_LINK_URL, self::TPL_VAR_LINK_TEXT); |
382
|
|
|
$replace = array($url, $linkText); |
383
|
|
|
|
384
|
|
|
return str_replace($search, $replace, $tpl); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Render the whole pagination layout. |
389
|
|
|
* |
390
|
|
|
* @param string $content layout contents |
391
|
|
|
* |
392
|
|
|
* @return string |
393
|
|
|
*/ |
394
|
|
|
protected function renderLayout($content) |
395
|
|
|
{ |
396
|
|
|
return str_replace(self::TPL_VAR_LAYOUT_CONTENT, $content, $this->layoutTpl); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..