PaginatedList.php
9.92 KB
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
<?php
/**
* A decorator that wraps around a data list in order to provide pagination.
*
* @package framework
* @subpackage view
*/
class PaginatedList extends SS_ListDecorator {
protected $request;
protected $getVar = 'start';
protected $pageLength = 10;
protected $pageStart;
protected $totalItems;
protected $limitItems = true;
/**
* Constructs a new paginated list instance around a list.
*
* @param SS_List $list The list to paginate. The getRange method will
* be used to get the subset of objects to show.
* @param array|ArrayAccess Either a map of request parameters or
* request object that the pagination offset is read from.
*/
public function __construct(SS_List $list, $request = array()) {
if (!is_array($request) && !$request instanceof ArrayAccess) {
throw new Exception('The request must be readable as an array.');
}
$this->request = $request;
parent::__construct($list);
}
/**
* Returns the GET var that is used to set the page start. This defaults
* to "start".
*
* If there is more than one paginated list on a page, it is neccesary to
* set a different get var for each using {@link setPaginationGetVar()}.
*
* @return string
*/
public function getPaginationGetVar() {
return $this->getVar;
}
/**
* Sets the GET var used to set the page start.
*
* @param string $var
*/
public function setPaginationGetVar($var) {
$this->getVar = $var;
return $this;
}
/**
* Returns the number of items displayed per page. This defaults to 10.
*
* @return int.
*/
public function getPageLength() {
return $this->pageLength;
}
/**
* Set the number of items displayed per page.
*
* @param int $length
*/
public function setPageLength($length) {
$this->pageLength = $length;
return $this;
}
/**
* Sets the current page.
*
* @param int $page
*/
public function setCurrentPage($page) {
$this->pageStart = ($page - 1) * $this->getPageLength();
return $this;
}
/**
* Returns the offset of the item the current page starts at.
*
* @return int
*/
public function getPageStart() {
if ($this->pageStart === null) {
if(
$this->request
&& isset($this->request[$this->getPaginationGetVar()])
&& $this->request[$this->getPaginationGetVar()] > 0
) {
$this->pageStart = (int)$this->request[$this->getPaginationGetVar()];
} else {
$this->pageStart = 0;
}
}
return $this->pageStart;
}
/**
* Sets the offset of the item that current page starts at. This should be
* a multiple of the page length.
*
* @param int $start
*/
public function setPageStart($start) {
$this->pageStart = $start;
return $this;
}
/**
* Returns the total number of items in the unpaginated list.
*
* @return int
*/
public function getTotalItems() {
if ($this->totalItems === null) {
$this->totalItems = count($this->list);
}
return $this->totalItems;
}
/**
* Sets the total number of items in the list. This is useful when doing
* custom pagination.
*
* @param int $items
*/
public function setTotalItems($items) {
$this->totalItems = $items;
return $this;
}
/**
* Sets the page length, page start and total items from a query object's
* limit, offset and unlimited count. The query MUST have a limit clause.
*
* @param SQLQuery $query
*/
public function setPaginationFromQuery(SQLQuery $query) {
if ($limit = $query->getLimit()) {
$this->setPageLength($limit['limit']);
$this->setPageStart($limit['start']);
$this->setTotalItems($query->unlimitedRowCount());
}
return $this;
}
/**
* Returns whether or not the underlying list is limited to the current
* pagination range when iterating.
*
* By default the limit method will be called on the underlying list to
* extract the subset for the current page. In some situations, if the list
* is custom generated and already paginated you don't want to additionally
* limit the list. You can use {@link setLimitItems} to control this.
*
* @return bool
*/
public function getLimitItems() {
return $this->limitItems;
}
/**
* @param bool $limit
*/
public function setLimitItems($limit) {
$this->limitItems = (bool) $limit;
return $this;
}
/**
* @return IteratorIterator
*/
public function getIterator() {
if($this->limitItems) {
$tmptList = clone $this->list;
return new IteratorIterator(
$tmptList->limit($this->getPageLength(), $this->getPageStart())
);
} else {
return new IteratorIterator($this->list);
}
}
/**
* Returns a set of links to all the pages in the list. This is useful for
* basic pagination.
*
* By default it returns links to every page, but if you pass the $max
* parameter the number of pages will be limited to that number, centered
* around the current page.
*
* @param int $max
* @return SS_List
*/
public function Pages($max = null) {
$result = new ArrayList();
if ($max) {
$start = ($this->CurrentPage() - floor($max / 2)) - 1;
$end = $this->CurrentPage() + floor($max / 2);
if ($start < 0) {
$start = 0;
$end = $max;
}
if ($end > $this->TotalPages()) {
$end = $this->TotalPages();
$start = max(0, $end - $max);
}
} else {
$start = 0;
$end = $this->TotalPages();
}
for ($i = $start; $i < $end; $i++) {
$result->push(new ArrayData(array(
'PageNum' => $i + 1,
'Link' => HTTP::setGetVar($this->getPaginationGetVar(), $i * $this->getPageLength()),
'CurrentBool' => $this->CurrentPage() == ($i + 1)
)));
}
return $result;
}
/**
* Returns a summarised pagination which limits the number of pages shown
* around the current page for visually balanced.
*
* Example: 25 pages total, currently on page 6, context of 4 pages
* [prev] [1] ... [4] [5] [[6]] [7] [8] ... [25] [next]
*
* Example template usage:
* <code>
* <% if MyPages.MoreThanOnePage %>
* <% if MyPages.NotFirstPage %>
* <a class="prev" href="$MyPages.PrevLink">Prev</a>
* <% end_if %>
* <% loop MyPages.PaginationSummary(4) %>
* <% if CurrentBool %>
* $PageNum
* <% else %>
* <% if Link %>
* <a href="$Link">$PageNum</a>
* <% else %>
* ...
* <% end_if %>
* <% end_if %>
* <% end_loop %>
* <% if MyPages.NotLastPage %>
* <a class="next" href="$MyPages.NextLink">Next</a>
* <% end_if %>
* <% end_if %>
* </code>
*
* @param int $context The number of pages to display around the current
* page. The number should be event, as half the number of each pages
* are displayed on either side of the current one.
* @return SS_List
*/
public function PaginationSummary($context = 4) {
$result = new ArrayList();
$current = $this->CurrentPage();
$total = $this->TotalPages();
// Make the number even for offset calculations.
if ($context % 2) {
$context--;
}
// If the first or last page is current, then show all context on one
// side of it - otherwise show half on both sides.
if ($current == 1 || $current == $total) {
$offset = $context;
} else {
$offset = floor($context / 2);
}
$left = max($current - $offset, 1);
$range = range($current - $offset, $current + $offset);
if ($left + $context > $total) {
$left = $total - $context;
}
for ($i = 0; $i < $total; $i++) {
$link = HTTP::setGetVar($this->getPaginationGetVar(), $i * $this->getPageLength());
$num = $i + 1;
$emptyRange = $num != 1 && $num != $total && (
$num == $left - 1 || $num == $left + $context + 1
);
if ($emptyRange) {
$result->push(new ArrayData(array(
'PageNum' => null,
'Link' => null,
'CurrentBool' => false
)));
} elseif ($num == 1 || $num == $total || in_array($num, $range)) {
$result->push(new ArrayData(array(
'PageNum' => $num,
'Link' => $link,
'CurrentBool' => $current == $num
)));
}
}
return $result;
}
/**
* @return int
*/
public function CurrentPage() {
return floor($this->getPageStart() / $this->getPageLength()) + 1;
}
/**
* @return int
*/
public function TotalPages() {
return ceil($this->getTotalItems() / $this->getPageLength());
}
/**
* @return bool
*/
public function MoreThanOnePage() {
return $this->TotalPages() > 1;
}
/**
* @return bool
*/
public function NotFirstPage() {
return $this->CurrentPage() != 1;
}
/**
* @return bool
*/
public function NotLastPage() {
return $this->CurrentPage() < $this->TotalPages();
}
/**
* Returns the number of the first item being displayed on the current
* page. This is useful for things like "displaying 10-20".
*
* @return int
*/
public function FirstItem() {
return ($start = $this->getPageStart()) ? $start + 1 : 1;
}
/**
* Returns the number of the last item being displayed on this page.
*
* @return int
*/
public function LastItem() {
if ($start = $this->getPageStart()) {
return min($start + $this->getPageLength(), $this->getTotalItems());
} else {
return min($this->getPageLength(), $this->getTotalItems());
}
}
/**
* Returns a link to the first page.
*
* @return string
*/
public function FirstLink() {
return HTTP::setGetVar($this->getPaginationGetVar(), 0);
}
/**
* Returns a link to the last page.
*
* @return string
*/
public function LastLink() {
return HTTP::setGetVar($this->getPaginationGetVar(), ($this->TotalPages() - 1) * $this->getPageLength());
}
/**
* Returns a link to the next page, if there is another page after the
* current one.
*
* @return string
*/
public function NextLink() {
if ($this->NotLastPage()) {
return HTTP::setGetVar($this->getPaginationGetVar(), $this->getPageStart() + $this->getPageLength());
}
}
/**
* Returns a link to the previous page, if the first page is not currently
* active.
*
* @return string
*/
public function PrevLink() {
if ($this->NotFirstPage()) {
return HTTP::setGetVar($this->getPaginationGetVar(), $this->getPageStart() - $this->getPageLength());
}
}
}