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