-
Notifications
You must be signed in to change notification settings - Fork 74
/
Pagination.class.php
439 lines (409 loc) · 12.7 KB
/
Pagination.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* Pagination
*
* Supplies an API for setting pagination details, and renders the resulting
* pagination markup (html) through the included render.inc.php file.
*
* @note The SEO methods (canonical/rel) were written following Google's
* suggested patterns. Namely, the canoical url excludes any
* peripheral parameters that don't relate to the pagination
* series. Whereas the prev/next rel link tags include any params
* found in the request.
* @link https://github.com/onassar/PHP-Pagination
* @author Oliver Nassar <[email protected]>
* @todo add setter parameter type and range checks w/ exceptions
* @example
* <code>
* // source inclusion
* require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
*
* // determine page (based on <_GET>)
* $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
*
* // instantiate with page and records as constructor parameters
* $pagination = (new Pagination($page, 200));
* $markup = $pagination->parse();
* </code>
* @example
* <code>
* // source inclusion
* require_once APP . '/vendors/PHP-Pagination/Pagination.class.php';
*
* // determine page (based on <_GET>)
* $page = isset($_GET['page']) ? ((int) $_GET['page']) : 1;
*
* // instantiate; set current page; set number of records
* $pagination = (new Pagination());
* $pagination->setCurrent($page);
* $pagination->setTotal(200);
*
* // grab rendered/parsed pagination markup
* $markup = $pagination->parse();
* </code>
*/
class Pagination
{
/**
* _variables
*
* Sets default variables for the rendering of the pagination markup.
*
* @var array
* @access protected
*/
protected $_variables = array(
'classes' => array('clearfix', 'pagination'),
'crumbs' => 5,
'rpp' => 10,
'key' => 'page',
'target' => '',
'next' => 'Next »',
'previous' => '« Previous',
'alwaysShowPagination' => false,
'clean' => false
);
/**
* __construct
*
* @access public
* @param integer $current (default: null)
* @param integer $total (default: null)
* @return void
*/
public function __construct($current = null, $total = null)
{
// current instantiation setting
if (is_null($current) === false) {
$this->setCurrent($current);
}
// total instantiation setting
if (is_null($total) === false) {
$this->setTotal($total);
}
// Pass along get (for link generation)
$this->_variables['get'] = $_GET;
}
/**
* _check
*
* Checks the current (page) and total (records) parameters to ensure
* they've been set. Throws an exception otherwise.
*
* @access protected
* @return void
*/
protected function _check()
{
if (isset($this->_variables['current']) === false) {
throw new Exception('Pagination::current must be set.');
} elseif (isset($this->_variables['total']) === false) {
throw new Exception('Pagination::total must be set.');
}
}
/**
* addClasses
*
* Sets the classes to be added to the pagination div node.
* Useful with Twitter Bootstrap (eg. pagination-centered, etc.)
*
* @see http://twitter.github.com/bootstrap/components.html#pagination
* @access public
* @param mixed $classes
* @return void
*/
public function addClasses($classes)
{
$this->_variables['classes'] = array_merge(
$this->_variables['classes'],
(array) $classes
);
}
/**
* alwaysShowPagination
*
* Tells the rendering engine to show the pagination links even if there
* aren't any pages to paginate through.
*
* @access public
* @return void
*/
public function alwaysShowPagination()
{
$this->_variables['alwaysShowPagination'] = true;
}
/**
* getCanonicalUrl
*
* @access public
* @return string
*/
public function getCanonicalUrl()
{
$target = $this->_variables['target'];
if (empty($target) === true) {
$target = $_SERVER['PHP_SELF'];
}
$page = (int) $this->_variables['current'];
if ($page !== 1) {
return 'http://' . ($_SERVER['HTTP_HOST']) . ($target) . $this->getPageParam();
}
return 'http://' . ($_SERVER['HTTP_HOST']) . ($target);
}
/**
* getPageParam
*
* @access public
* @param boolean|integer $page (default: false)
* @return string
*/
public function getPageParam($page = false)
{
if ($page === false) {
$page = (int) $this->_variables['current'];
}
$key = $this->_variables['key'];
return '?' . ($key) . '=' . ((int) $page);
}
/**
* getPageUrl
*
* @access public
* @param boolean|integer $page (default: false)
* @return string
*/
public function getPageUrl($page = false)
{
$target = $this->_variables['target'];
if (empty($target) === true) {
$target = $_SERVER['PHP_SELF'];
}
return 'http://' . ($_SERVER['HTTP_HOST']) . ($target) . ($this->getPageParam($page));
}
/**
* getRelPrevNextLinkTags
*
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=1663744
* @see http://googlewebmastercentral.blogspot.ca/2011/09/pagination-with-relnext-and-relprev.html
* @see http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394
* @access public
* @return array
*/
public function getRelPrevNextLinkTags()
{
// generate path
$target = $this->_variables['target'];
if (empty($target) === true) {
$target = $_SERVER['PHP_SELF'];
}
$key = $this->_variables['key'];
$params = $this->_variables['get'];
$params[$key] = 'pgnmbr';
$href = ($target) . '?' . http_build_query($params);
$href = preg_replace(
array('/=$/', '/=&/'),
array('', '&'),
$href
);
$href = 'http://' . ($_SERVER['HTTP_HOST']) . $href;
// Pages
$currentPage = (int) $this->_variables['current'];
$numberOfPages = (
(int) ceil(
$this->_variables['total'] /
$this->_variables['rpp']
)
);
// On first page
if ($currentPage === 1) {
// There is a page after this one
if ($numberOfPages > 1) {
$href = str_replace('pgnmbr', 2, $href);
return array(
'<link rel="next" href="' . ($href) . '" />'
);
}
return array();
}
// Store em
$prevNextTags = array(
'<link rel="prev" href="' . (str_replace('pgnmbr', $currentPage - 1, $href)) . '" />'
);
// There is a page after this one
if ($numberOfPages > $currentPage) {
array_push(
$prevNextTags,
'<link rel="next" href="' . (str_replace('pgnmbr', $currentPage + 1, $href)) . '" />'
);
}
return $prevNextTags;
}
/**
* parse
*
* Parses the pagination markup based on the parameters set and the
* logic found in the render.inc.php file.
*
* @access public
* @return void
*/
public function parse()
{
// ensure required parameters were set
$this->_check();
// bring variables forward
foreach ($this->_variables as $_name => $_value) {
$$_name = $_value;
}
// buffer handling
ob_start();
include 'render.inc.php';
$_response = ob_get_contents();
ob_end_clean();
return $_response;
}
/**
* setClasses
*
* @see http://twitter.github.com/bootstrap/components.html#pagination
* @access public
* @param mixed $classes
* @return void
*/
public function setClasses($classes)
{
$this->_variables['classes'] = (array) $classes;
}
/**
* setClean
*
* Sets the pagination to exclude page numbers, and only output
* previous/next markup. The counter-method of this is self::setFull.
*
* @access public
* @return void
*/
public function setClean()
{
$this->_variables['clean'] = true;
}
/**
* setCrumbs
*
* Sets the maximum number of 'crumbs' (eg. numerical page items)
* available.
*
* @access public
* @param integer $crumbs
* @return void
*/
public function setCrumbs($crumbs)
{
$this->_variables['crumbs'] = $crumbs;
}
/**
* setCurrent
*
* Sets the current page being viewed.
*
* @access public
* @param integer $current
* @return void
*/
public function setCurrent($current)
{
$this->_variables['current'] = (int)$current;
}
/**
* setFull
*
* See self::setClean for documentation.
*
* @access public
* @return void
*/
public function setFull()
{
$this->_variables['clean'] = false;
}
/**
* setKey
*
* Sets the key of the <_GET> array that contains, and ought to contain,
* paging information (eg. which page is being viewed).
*
* @access public
* @param string $key
* @return void
*/
public function setKey($key)
{
$this->_variables['key'] = $key;
}
/**
* setNext
*
* Sets the copy of the next anchor.
*
* @access public
* @param string $str
* @return void
*/
public function setNext($str)
{
$this->_variables['next'] = $str;
}
/**
* setPrevious
*
* Sets the copy of the previous anchor.
*
* @access public
* @param string $str
* @return void
*/
public function setPrevious($str)
{
$this->_variables['previous'] = $str;
}
/**
* setRPP
*
* Sets the number of records per page (used for determining total
* number of pages).
*
* @access public
* @param integer $rpp
* @return void
*/
public function setRPP($rpp)
{
$this->_variables['rpp'] = (int)$rpp;
}
/**
* setTarget
*
* Sets the leading path for anchors.
*
* @access public
* @param string $target
* @return void
*/
public function setTarget($target)
{
$this->_variables['target'] = $target;
}
/**
* setTotal
*
* Sets the total number of records available for pagination.
*
* @access public
* @param integer $total
* @return void
*/
public function setTotal($total)
{
$this->_variables['total'] = (int)$total;
}
}