/
PaginatedList.php
560 lines (505 loc) · 14.5 KB
/
PaginatedList.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
namespace SilverStripe\ORM;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\View\ArrayData;
use ArrayAccess;
use Exception;
use IteratorIterator;
use Traversable;
/**
* A decorator that wraps around a data list in order to provide pagination.
*
* @template TList of SS_List
* @template T
* @extends ListDecorator<TList, T>
*/
class PaginatedList extends 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 TList<T> $list The list to paginate. The getRange method will
* be used to get the subset of objects to show.
* @param array|ArrayAccess $request Either a map of request parameters or
* request object that the pagination offset is read from.
* @throws Exception
*/
public function __construct(SS_List $list, $request = [])
{
if (!is_array($request) && !$request instanceof ArrayAccess) {
throw new Exception('The request must be readable as an array.');
}
$this->setRequest($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 necessary 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
* @return $this
*/
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. Set to zero to disable paging.
*
* @param int $length
* @return $this
*/
public function setPageLength($length)
{
$this->pageLength = (int)$length;
return $this;
}
/**
* Sets the current page.
*
* @param int $page Page index beginning with 1
* @return $this
*/
public function setCurrentPage($page)
{
$this->pageStart = ((int)$page - 1) * $this->getPageLength();
return $this;
}
/**
* Returns the offset of the item the current page starts at.
*
* @return int
*/
public function getPageStart()
{
$request = $this->getRequest();
if ($this->pageStart === null) {
if ($request
&& isset($request[$this->getPaginationGetVar()])
&& $request[$this->getPaginationGetVar()] > 0
) {
$this->pageStart = (int)$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
* @return $this
*/
public function setPageStart($start)
{
$this->pageStart = (int)$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
* @return $this
*/
public function setTotalItems($items)
{
$this->totalItems = (int)$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 SQLSelect $query
* @return $this
*/
public function setPaginationFromQuery(SQLSelect $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
* @return $this
*/
public function setLimitItems($limit)
{
$this->limitItems = (bool)$limit;
return $this;
}
public function getIterator(): Traversable
{
$pageLength = $this->getPageLength();
if ($this->limitItems && $pageLength) {
$tmptList = clone $this->list;
return new IteratorIterator(
$tmptList->limit($pageLength, $this->getPageStart())
);
} else {
return new IteratorIterator($this->list);
}
}
/**
* @return array
*/
public function toArray()
{
$result = [];
// Use getIterator()
foreach ($this as $record) {
$result[] = $record;
}
return $result;
}
/**
* 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([
'PageNum' => $i + 1,
'Link' => HTTP::setGetVar(
$this->getPaginationGetVar(),
$i * $this->getPageLength(),
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
),
'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);
$right = min($current + $offset, $total);
$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(),
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
);
$num = $i + 1;
$emptyRange = $num != 1 && $num != $total && (
$num == $left - 1 || $num == $right + 1
);
if ($emptyRange) {
$result->push(new ArrayData([
'PageNum' => null,
'Link' => null,
'CurrentBool' => false
]));
} elseif ($num == 1 || $num == $total || in_array($num, $range ?? [])) {
$result->push(new ArrayData([
'PageNum' => $num,
'Link' => $link,
'CurrentBool' => $current == $num
]));
}
}
return $result;
}
/**
* @return int
*/
public function CurrentPage()
{
$pageLength = $this->getPageLength();
return $pageLength
? floor($this->getPageStart() / $pageLength) + 1
: 1;
}
/**
* @return int
*/
public function TotalPages()
{
$pageLength = $this->getPageLength();
return $pageLength
? ceil($this->getTotalItems() / $pageLength)
: min($this->getTotalItems(), 1);
}
/**
* @return bool
*/
public function MoreThanOnePage()
{
return $this->TotalPages() > 1;
}
/**
* @return bool
*/
public function FirstPage()
{
return $this->CurrentPage() == 1;
}
/**
* @return bool
*/
public function NotFirstPage()
{
return !$this->FirstPage();
}
/**
* @return bool
*/
public function LastPage()
{
return $this->CurrentPage() >= $this->TotalPages();
}
/**
* @return bool
*/
public function NotLastPage()
{
return !$this->LastPage();
}
/**
* 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()
{
$pageLength = $this->getPageLength();
if (!$pageLength) {
return $this->getTotalItems();
} elseif ($start = $this->getPageStart()) {
return min($start + $pageLength, $this->getTotalItems());
} else {
return min($pageLength, $this->getTotalItems());
}
}
/**
* Returns a link to the first page.
*
* @return string
*/
public function FirstLink()
{
return HTTP::setGetVar(
$this->getPaginationGetVar(),
0,
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
);
}
/**
* Returns a link to the last page.
*
* @return string
*/
public function LastLink()
{
return HTTP::setGetVar(
$this->getPaginationGetVar(),
($this->TotalPages() - 1) * $this->getPageLength(),
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
);
}
/**
* 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(),
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
);
}
}
/**
* 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(),
($this->request instanceof HTTPRequest) ? $this->request->getURL(true) : null
);
}
}
/**
* Returns the total number of items in the list
*/
public function TotalItems()
{
return $this->getTotalItems();
}
/**
* Set the request object for this list
*
* @param HTTPRequest|ArrayAccess $request
*/
public function setRequest($request)
{
$this->request = $request;
}
/**
* Get the request object for this list
*/
public function getRequest()
{
return $this->request;
}
}