/
pagination.php
446 lines (397 loc) · 14.9 KB
/
pagination.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
440
441
442
443
444
445
446
<?php
/**
* Provides helper methods for dealing with Page Navigation content.
*
* The default implementation assumes the following styles:
*
* div.pagination { width: 100%; margin: 0 auto; text-align: center; }
* div.pagination ul, div.pagination li { display: inline; margin: 0; padding: 0; }
* div.pagination li.current { font-weight: bold; }
*
* @package minPHP
* @subpackage minPHP.helpers.pagination
*/
Loader::load(HELPERDIR . "html" . DS . "html.php");
class Pagination extends Html {
/**
* @var string The string to use as the end of line character
*/
private $eol = "\n";
/**
* @var boolean Whether or not to return output from various pagination methods
*/
private $return_output = false;
/**
* @var array Format settings
*/
private $settings;
/**
* @var array All get parameters for this request
*/
private $get;
/**
* Sets default settings
*
* @param array $get The GET parameters for the current request
* @param array $format Format settings to overwrite default settings with (optional)
*/
public function __construct(array $get=array(), array $format=array()) {
// Load the language for the pagination
Language::loadLang("pagination", null, dirname(__FILE__) . DS . "language" . DS);
$this->setGet($get);
$this->settings = array(
// Wrapper to surround the link set
'wrapper' => array(
'tag' => "div",
'attributes' => array('class'=>"pagination")
),
'navigation' => array(
// First page link
'first' => array(
'tag' => "li",
'name' => Language::_("Pagination.first_link", true),
'attributes' => array(),
'link_attributes' => array(),
'show' => "if_needed", // options: if_needed, never, always
'disabled' => "disabled" // class to use if show and not needed
),
// Last page link
'last' => array(
'tag' => "li",
'name' => Language::_("Pagination.last_link", true),
'attributes' => array(),
'link_attributes' => array(),
'show' => "if_needed", // options: if_needed, never, always
'disabled' => "disabled" // class to use if show and not needed
),
// Next page link
'next' => array(
'tag' => "li",
'name' => Language::_("Pagination.next_link", true),
'attributes' => array(),
'link_attributes' => array(),
'show' => "if_needed", // options: if_needed, never, always
'disabled' => "disabled" // class to use if show and not needed
),
// Previous page link
'prev' => array(
'tag' => "li",
'name' => Language::_("Pagination.prev_link", true),
'attributes' => array(),
'link_attributes' => array(),
'show' => "if_needed", // options: if_needed, never, always
'disabled' => "disabled" // class to use if show and not needed
),
// Surround for the set of links
'surround' => array(
'tag' => "ul",
'attributes' => array(),
'link_attributes' => array()
),
// The currently active link
'current' => array(
'tag' => "li",
'attributes' => array('class'=>"current"),
'link_attributes' => array(),
'link' => false // disable linking
),
// All numeric links
'numerical' => array(
'tag' => "li",
'attributes' => array(),
'link_attributes' => array()
)
),
'merge_get' => true, // merge get params from URI with those set in 'params'
'show' => "if_needed", // options: if_needed, never, always
'pages_to_show' => 5, // max number of numerical pages shown in the pagination
'total_pages' => 0, // total number of pages (used instead of total results/result_per_page settings)
'total_results' => 0, // total number of results in the pagination set
'results_per_page' => 1, // number of result items per page
'uri' => "",
'uri_labels' => array( // tags that will be substituted with their appropriate value
'page' => "page",
'per_page' => "per_page"
),
'params' => array() // key => value pairs of additional uri query parameters (if set, overrides $get params)
);
$this->settings = $this->mergeArrays($this->settings, $format);
}
/**
* Extends one array using another to overwrite existing values. Recursively merges
* data.
*
* @param array $arr1 The array (default) to be merged into
* @param array $arr2 The array to merge into $arr1
* @return array The merged arrays
*/
private function mergeArrays(array $arr1, array $arr2) {
foreach($arr2 as $key => $value) {
if (array_key_exists($key, $arr1) && is_array($value))
$arr1[$key] = $this->mergeArrays($arr1[$key], $arr2[$key]);
else
$arr1[$key] = $value;
}
return $arr1;
}
/**
* Set all GET parameters for this pagination instance
*
* @param array $get An array of GET parameters
*/
public function setGet(array $get) {
// Remove all numeric indexed get parameters, only want key/value pairs
foreach ($get as $key => $value) {
// Ensure that the key is both numeric and an integer
if ((string)(int)$key == $key)
unset($get[$key]);
}
$this->get = $get;
}
/**
* Sets the end of line character to use
*
* @param string $eol The end of line character to use
*/
public function setEol($eol) {
$this->eol = $this->_($eol, true);
}
/**
* Sets the format settings
*
* @param array $format The format settings to overwrite
*/
public function setSettings($format) {
if (is_array($format))
$this->settings = $this->mergeArrays($this->settings, $format);
}
/**
* Returns whether or not pagination should be shown
*
* @return boolean True if pagination should be shown, false otherwise
*/
public function hasPages() {
$pages = 0;
if (isset($this->settings['total_pages']) && $this->settings['total_pages'] > 0)
$pages = $this->settings['total_pages'];
else
$pages = ceil($this->settings['total_results'] / $this->settings['results_per_page']);
if ($this->settings['show'] == "never" || ($pages <= 1 && $this->settings['show'] == "if_needed"))
return false;
return true;
}
/**
* Builds the content of the pagination and optionally outputs it.
*
* @return string The HTML for the pagination, void if output enabled
*/
public function build() {
// Set data to return, because we don't want to echo until we have everything built
$output = $this->return_output;
$this->setOutput(true);
// Merge get params with param settings if set to
if ($this->settings['merge_get'])
$this->settings['params'] = $this->mergeArrays($this->get, (array)$this->settings['params']);
if (isset($this->settings['total_pages']) && $this->settings['total_pages'] > 0)
$pages = $this->settings['total_pages'];
else
$pages = ceil($this->settings['total_results'] / $this->settings['results_per_page']);
// Ensure nav should be shown
if (!$this->hasPages())
return null;
// Set the wrapper tag
$html = $this->openTag($this->settings['wrapper']);
// Begin with surround tag
$html .= $this->openTag($this->settings['navigation']['surround']);
$show = $this->settings['pages_to_show'];
$per_page = (isset($settings['per_page']) && !empty($settings['per_page'])) ? $settings['per_page'] : $this->settings['results_per_page'];
$current_page = $this->currentPage();
$page_label = $this->settings['uri_labels']['page'];
$per_page_label = $this->settings['uri_labels']['per_page'];
$settings[$page_label] = $current_page;
$settings[$per_page_label] = $per_page;
if ($pages > 0) {
if ($this->settings['pages_to_show'] > 0) {
$current_page = min(max(1, $current_page), $pages);
$start = $current_page - floor($show/2);
$end = $current_page + floor($show/2) - ($show%2 == 0 ? 1 : 0);
if ($start < 1) {
$start = 1;
$end = min($pages, $show);
}
if ($end > $pages) {
$end = $pages;
$start = max($end - $show + 1, 1);
}
$prev = max($current_page - 1, $start);
$next = min($current_page + 1, $end);
$prev_needed = $current_page > 1;
$next_needed = $current_page < $pages;
// build first, prev links, merge with disabled settings if not needed but shown
if ($this->settings['navigation']['first']['show'] == "always" || ($this->settings['navigation']['first']['show'] == "if_needed" && $prev_needed)) {
if (!$prev_needed)
$this->settings['navigation']['first']['attributes']['class'] = (isset($this->settings['navigation']['first']['attributes']['class']) ? $this->settings['navigation']['first']['attributes']['class'] : "") . " " . $this->settings['navigation']['first']['disabled'];
$html .= $this->createNavItem($this->settings['navigation']['first'], 1);
}
if ($this->settings['navigation']['prev']['show'] == "always" || ($this->settings['navigation']['prev']['show'] == "if_needed" && $prev_needed)) {
if (!$prev_needed)
$this->settings['navigation']['prev']['attributes']['class'] = (isset($this->settings['navigation']['prev']['attributes']['class']) ? $this->settings['navigation']['prev']['attributes']['class'] : "") . " " . $this->settings['navigation']['prev']['disabled'];
$html .= $this->createNavItem($this->settings['navigation']['prev'], $prev);
}
// build page number links
for ($i=$start; $i<=$end; $i++) {
if ($current_page == $i)
$html .= $this->createNavItem($this->settings['navigation']['current'], $i);
else
$html .= $this->createNavItem($this->settings['navigation']['numerical'], $i);
}
// build next, last links, merge with disabled settings if not needed but shown
if ($this->settings['navigation']['next']['show'] == "always" || ($this->settings['navigation']['next']['show'] == "if_needed" && $next_needed)) {
if (!$next_needed)
$this->settings['navigation']['next']['attributes']['class'] = (isset($this->settings['navigation']['next']['attributes']['class']) ? $this->settings['navigation']['next']['attributes']['class'] : "") . " " . $this->settings['navigation']['next']['disabled'];
$html .= $this->createNavItem($this->settings['navigation']['next'], $next);
}
if ($this->settings['navigation']['last']['show'] == "always" || ($this->settings['navigation']['last']['show'] == "if_needed" && $next_needed)) {
if (!$next_needed)
$this->settings['navigation']['last']['attributes']['class'] = (isset($this->settings['navigation']['last']['attributes']['class']) ? $this->settings['navigation']['last']['attributes']['class'] : "") . " " . $this->settings['navigation']['last']['disabled'];
$html .= $this->createNavItem($this->settings['navigation']['last'], $pages);
}
}
}
// Close surround tag
$html .= $this->closeTag($this->settings['navigation']['surround']);
// Close the wrapper tag
$html .= $this->closeTag($this->settings['wrapper']);
// Restore the original output type
$this->setOutput($output);
return $this->output($html);
}
/**
* Finds the current page based on the current URI and/or query parameters
*
* @return int The current page
*/
private function currentPage() {
$page = 1;
$uri = $this->getUri();
$temp = explode("/", $uri);
$index = null;
// Look for the index partition where the page label is located
foreach ($temp as $i => $value) {
if ($value == "[" . $this->settings['uri_labels']['page'] . "]") {
$index = $i;
$temp = explode("/", $_SERVER['REQUEST_URI']);
break;
}
}
// Parse the page number out of the partition
if ($index && isset($temp[$index]))
$page = $temp[$index];
elseif (isset($this->get[$this->settings['uri_labels']['page']]))
$page = $this->get[$this->settings['uri_labels']['page']];
return $page;
}
/**
* Creates a page nav item
*
* @param array $nav_item Navigation settings for a specific nav link
* @param string $page The page number or symbol
* @return string The HTML for the nav item, void if output enabled
*/
private function createNavItem($nav_item, $page) {
return $this->output($this->openTag($nav_item) . (isset($nav_item['link']) && !$nav_item['link'] ? $page : $this->createLink($nav_item, $page)) . $this->closeTag($nav_item));
}
/**
* Opens a new tag
*
* @param array $tag The tag setting to open
* @return string The HTML for an open tag, void if output enabled
*/
private function openTag($tag) {
$html = "";
if (is_array($tag) && isset($tag['tag']))
$html .= "<" . $this->_($tag['tag'], true) . $this->buildAttributes($tag['attributes']) . ">" . $this->eol;
return $this->output($html);
}
/**
* Closes a tag
*
* @param string $tag The tag setting to close
* @return string The HTML for a closing tag, void if output enabled
*/
private function closeTag($tag) {
$html = "";
if (is_array($tag) && isset($tag['tag']))
$html .= "</" . $this->_($tag['tag'], true) . ">" . $this->eol;
return $this->output($html);
}
/**
* Creates a new link
*
* @param array $link The link settings
* @param integer $page The page number
* @return string The HTML for the <a> tag, void if output enabled
*/
private function createLink($link, $page) {
$html = "";
if (is_array($link)) {
$link['link_attributes']['href'] = $this->getPageUri($page);
$html .= "<a" . $this->buildAttributes($link['link_attributes']) . ">" .
(isset($link['name']) ? $this->_($link['name'], true) : $this->_($page, true)) .
"</a>" . $this->eol;
}
return $this->output($html);
}
private function getUri() {
// Build all query params
$query = null;
if (is_array($this->settings['params'])) {
// If settings contain no parameters, use those set by $this->get
if (empty($this->settings['params']) && is_array($this->get)) {
foreach ($this->get as $key => $value) {
if (is_numeric($key))
continue;
$this->settings['params'][$key] = $value;
}
}
foreach ($this->settings['params'] as $key => $value) {
$query .= ($query == null ? "?" : "&") . $key . "=" . $value;
}
}
// Build the URI
return $this->settings['uri'] . (substr($this->settings['uri'], -1) != "/" ? "/" : "") . $query;
}
/**
* Create the URI for the current page number, replacing any labels as needed.
*
* @param int $page The page number
* @return string The completed URI
*/
public function getPageUri($page) {
$uri = $this->getUri();
// Replace the page tag with the page value
return str_replace(array("[" . $this->settings['uri_labels']['page'] . "]", "[" . $this->settings['uri_labels']['per_page'] . "]"), array($page, $this->settings['results_per_page']), $uri);
}
/**
* Set whether to return $output generated by these methods, or to echo it out instead
*
* @param boolean $return True to return output from these form methods, false to echo results instead
*/
public function setOutput($return) {
if ($return)
$this->return_output = true;
else
$this->return_output = false;
}
/**
* Handles whether to output or return $html
*
* @param string $html The HTML to output/return
* @return string The HTML given, void if output enabled
*/
private function output($html) {
if ($this->return_output)
return $html;
echo $html;
}
}