-
Notifications
You must be signed in to change notification settings - Fork 126
/
GridFieldConfigurablePaginator.php
457 lines (397 loc) · 13.8 KB
/
GridFieldConfigurablePaginator.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
<?php
namespace Symbiote\GridFieldExtensions;
use Exception;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_FormAction;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridState_Data;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Limitable;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\UnsavedRelationList;
use SilverStripe\View\ArrayData;
/**
* GridFieldConfigurablePaginator paginates the {@link GridField} list and adds controls to the bottom of
* the {@link GridField}. The page sizes are configurable.
*/
class GridFieldConfigurablePaginator extends GridFieldPaginator
{
use Configurable;
/**
* Specifies default page sizes
*
* @config
* @var int
*/
private static $default_page_sizes = array(15, 30, 60);
/**
* @var GridField
*/
protected $gridField;
/**
* @var GridState_Data
*/
protected $gridFieldState;
/**
* @var int[]
*/
protected $pageSizes = array();
/**
* @param int $itemsPerPage How many items should be displayed per page
* @param int $pageSizes The page sizes to show in the dropdown
*/
public function __construct($itemsPerPage = null, $pageSizes = null)
{
$this->setPageSizes($pageSizes ?: $this->config()->get('default_page_sizes'));
if (!$itemsPerPage) {
$itemsPerPage = $this->pageSizes[0];
}
parent::__construct($itemsPerPage);
}
/**
* Get the total number of records in the list
*
* @return int
*/
public function getTotalRecords()
{
return (int) $this->getGridField()->getList()->count();
}
/**
* Get the first shown record number
*
* @return int
*/
public function getFirstShown()
{
$firstShown = $this->getGridPagerState()->firstShown ?: 1;
// Prevent visiting a page with an offset higher than the total number of items
if ($firstShown > $this->getTotalRecords()) {
$this->getGridPagerState()->firstShown = $firstShown = 1;
}
return $firstShown;
}
/**
* Set the first shown record number. Will be stored in the state.
*
* @param int $firstShown
* @return $this
*/
public function setFirstShown($firstShown = 1)
{
$this->getGridPagerState()->firstShown = (int) $firstShown;
return $this;
}
/**
* Get the last shown record number
*
* @return int
*/
public function getLastShown()
{
return min($this->getTotalRecords(), $this->getFirstShown() + $this->getItemsPerPage() - 1);
}
/**
* Get the total number of pages, given the current number of items per page. The total
* pages might be higher than <totalitems> / <itemsperpage> if the first shown record
* is half way through a standard page break point.
*
* @return int
*/
public function getTotalPages()
{
// Pages before
$pages = ceil(($this->getFirstShown() - 1) / $this->getItemsPerPage());
// Current page
$pages++;
// Pages after
$pages += ceil(($this->getTotalRecords() - $this->getLastShown()) / $this->getItemsPerPage());
return (int) $pages;
}
/**
* Get the page currently active. This is calculated by adding one to the previous number
* of pages calculated via the "first shown record" position.
*
* @return int
*/
public function getCurrentPage()
{
return (int) ceil(($this->getFirstShown() - 1) / $this->getItemsPerPage()) + 1;
}
/**
* Get the next page number
*
* @return int
*/
public function getNextPage()
{
return min($this->getTotalPages(), $this->getCurrentPage() + 1);
}
/**
* Get the previous page number
*
* @return int
*/
public function getPreviousPage()
{
return max(1, $this->getCurrentPage() - 1);
}
/**
* Set the page sizes to use in the "Show x" dropdown
*
* @param array $pageSizes
* @return $this
*/
public function setPageSizes(array $pageSizes)
{
$this->pageSizes = $pageSizes;
// Reset items per page
$this->setItemsPerPage(current($pageSizes ?? []));
return $this;
}
/**
* Get the sizes for the "Show x" dropdown
*
* @return array
*/
public function getPageSizes()
{
return $this->pageSizes;
}
/**
* Gets a list of page sizes for use in templates as a dropdown
*
* @return ArrayList
*/
public function getPageSizesAsList()
{
$pageSizes = ArrayList::create();
$perPage = $this->getItemsPerPage();
foreach ($this->getPageSizes() as $pageSize) {
$pageSizes->push(array(
'Size' => $pageSize,
'Selected' => $pageSize == $perPage
));
}
return $pageSizes;
}
/**
* Get the GridField used in this request
*
* @return GridField
* @throws Exception If the GridField has not been previously set
*/
public function getGridField()
{
if ($this->gridField) {
return $this->gridField;
}
throw new Exception('No GridField available yet for this request!');
}
/**
* Set the GridField so it can be used in other parts of the component during this request
*
* @param GridField $gridField
* @return $this
*/
public function setGridField(GridField $gridField)
{
$this->gridField = $gridField;
return $this;
}
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
{
$this->setGridField($gridField);
if ($actionName !== 'paginate') {
return;
}
$state = $this->getGridPagerState();
$state->firstShown = (int) $arguments['first-shown'];
$state->pageSize = $data[$gridField->getName()]['page-sizes'];
$this->setItemsPerPage($state->pageSize);
}
public function getManipulatedData(GridField $gridField, SS_List $dataList)
{
// Assign the GridField to the class so it can be used later in the request
$this->setGridField($gridField);
// Retain page sizes during actions provided by other components
$state = $this->getGridPagerState();
if (is_numeric($state->pageSize)) {
$this->setItemsPerPage($state->pageSize);
}
if (!($dataList instanceof Limitable) || ($dataList instanceof UnsavedRelationList)) {
return $dataList;
}
return $dataList->limit($this->getItemsPerPage(), $this->getFirstShown() - 1);
}
/**
* Add the configurable page size options to the template data
*
* {@inheritDoc}
*
* @param GridField $gridField
* @return ArrayData|null
*/
public function getTemplateParameters(GridField $gridField)
{
$state = $this->getGridPagerState();
if (is_numeric($state->pageSize)) {
$this->setItemsPerPage($state->pageSize);
}
$arguments = $this->getPagerArguments();
// Figure out which page and record range we're on
if (!$arguments['total-rows']) {
return null;
}
// Define a list of the FormActions that should be generated for pager controls (see getPagerActions())
$controls = array(
'first' => array(
'title' => 'First',
'args' => array('first-shown' => 1),
'extra-class' => 'btn btn-secondary btn--hide-text btn-sm font-icon-angle-double-left '
. 'ss-gridfield-pagination-action ss-gridfield-firstpage',
'disable-previous' => ($this->getCurrentPage() == 1)
),
'prev' => array(
'title' => 'Previous',
'args' => array('first-shown' => $arguments['first-shown'] - $this->getItemsPerPage()),
'extra-class' => 'btn btn-secondary btn--hide-text btn-sm font-icon-angle-left '
. 'ss-gridfield-pagination-action ss-gridfield-previouspage',
'disable-previous' => ($this->getCurrentPage() == 1)
),
'next' => array(
'title' => 'Next',
'args' => array('first-shown' => $arguments['first-shown'] + $this->getItemsPerPage()),
'extra-class' => 'btn btn-secondary btn--hide-text btn-sm font-icon-angle-right '
.'ss-gridfield-pagination-action ss-gridfield-nextpage',
'disable-next' => ($this->getCurrentPage() == $arguments['total-pages'])
),
'last' => array(
'title' => 'Last',
'args' => array('first-shown' => ($this->getTotalPages() - 1) * $this->getItemsPerPage() + 1),
'extra-class' => 'btn btn-secondary btn--hide-text btn-sm font-icon-angle-double-right '
. 'ss-gridfield-pagination-action ss-gridfield-lastpage',
'disable-next' => ($this->getCurrentPage() == $arguments['total-pages'])
),
'pagesize' => array(
'title' => 'Page Size',
'args' => array('first-shown' => $arguments['first-shown']),
'extra-class' => 'ss-gridfield-pagination-action ss-gridfield-pagesize-submit'
),
);
if ($controls['prev']['args']['first-shown'] < 1) {
$controls['prev']['args']['first-shown'] = 1;
}
$actions = $this->getPagerActions($controls, $gridField);
// Render in template
return ArrayData::create(array(
'OnlyOnePage' => ($arguments['total-pages'] == 1),
'FirstPage' => $actions['first'],
'PreviousPage' => $actions['prev'],
'NextPage' => $actions['next'],
'LastPage' => $actions['last'],
'PageSizesSubmit' => $actions['pagesize'],
'CurrentPageNum' => $this->getCurrentPage(),
'NumPages' => $arguments['total-pages'],
'FirstShownRecord' => $arguments['first-shown'],
'LastShownRecord' => $arguments['last-shown'],
'NumRecords' => $arguments['total-rows'],
'PageSizes' => $this->getPageSizesAsList(),
'PageSizesName' => $gridField->getName() . '[page-sizes]',
));
}
public function getHTMLFragments($gridField)
{
GridFieldExtensions::include_requirements();
$gridField->addExtraClass('ss-gridfield-configurable-paginator');
$forTemplate = $this->getTemplateParameters($gridField);
if ($forTemplate) {
return array(
'footer' => $forTemplate->renderWith(
__CLASS__,
array('Colspan' => count($gridField->getColumns() ?? []))
)
);
}
}
/**
* Returns an array containing the arguments for the pagination: total rows, pages, first record etc
*
* @return array
*/
protected function getPagerArguments()
{
return array(
'total-rows' => $this->getTotalRecords(),
'total-pages' => $this->getTotalPages(),
'items-per-page' => $this->getItemsPerPage(),
'first-shown' => $this->getFirstShown(),
'last-shown' => $this->getLastShown(),
);
}
/**
* Returns FormActions for each of the pagination actions, in an array
*
* @param array $controls
* @param GridField $gridField
* @return GridField_FormAction[]
*/
public function getPagerActions(array $controls, GridField $gridField)
{
$actions = array();
foreach ($controls as $key => $arguments) {
$action = GridField_FormAction::create(
$gridField,
'pagination_' . $key,
$arguments['title'],
'paginate',
$arguments['args']
);
if (isset($arguments['extra-class'])) {
$action->addExtraClass($arguments['extra-class']);
}
if (isset($arguments['disable-previous']) && $arguments['disable-previous']) {
$action = $action->performDisabledTransformation();
} elseif (isset($arguments['disable-next']) && $arguments['disable-next']) {
$action = $action->performDisabledTransformation();
}
$actions[$key] = $action;
}
return $actions;
}
public function getActions($gridField)
{
return array('paginate');
}
/**
* Gets the state from the current request's GridField and sets some default values on it
*
* @param GridField $gridField Not used, but present for parent method compatibility
* @return GridState_Data
*/
protected function getGridPagerState(GridField $gridField = null)
{
if (!$this->gridFieldState) {
$state = $this->getGridField()->State->GridFieldConfigurablePaginator;
// SS 3.1 compatibility (missing __call)
if (is_object($state->firstShown)) {
$state->firstShown = 1;
}
if (is_object($state->pageSize)) {
$state->pageSize = $this->getItemsPerPage();
}
// Handle free input in the page number field
$parentState = $this->getGridField()->State->GridFieldPaginator;
if (is_object($parentState->currentPage)) {
$parentState->currentPage = 0;
}
if ($parentState->currentPage >= 1) {
$state->firstShown = ($parentState->currentPage - 1) * $this->getItemsPerPage() + 1;
$parentState->currentPage = null;
}
$this->gridFieldState = $state;
}
return $this->gridFieldState;
}
}