Debian Codegraph @

vendor/ipl/web/src/Control/PaginationControl.php

raw
1 <?php
2
3 namespace ipl\Web\Control;
4
5 use ipl\Html\BaseHtmlElement;
6 use ipl\Html\Html;
7 use ipl\Html\TemplateString;
8 use ipl\Html\ValidHtml;
9 use ipl\I18n\Translation;
10 use ipl\Stdlib\Contract\Paginatable;
11 use ipl\Web\Compat\CompatForm;
12 use ipl\Web\Url;
13 use ipl\Web\Widget\ButtonLink;
14 use ipl\Web\Widget\Icon;
15
16 /**
17 * The pagination control displays a list of links that point to different pages of the current view
18 *
19 * The default HTML markup (tag and attributes) for the paginator look like the following:
20 * <div class="pagination-control" role="navigation">...</div>
21 */
22 class PaginationControl extends BaseHtmlElement
23 {
24 use Translation;
25
26 /** @var string Default page parameter */
27 public const DEFAULT_PAGE_PARAM = 'page';
28
29 /** @var int Default maximum number of items which should be shown per page */
30 protected $defaultPageSize;
31
32 /** @var string Name of the URL parameter which stores the current page number */
33 protected $pageParam = 'page';
34
35 /** @var string Name of the URL parameter which holds the page size. If given, overrides {@link $defaultPageSize} */
36 protected $pageSizeParam = 'limit';
37
38 /** @var Paginatable The pagination adapter which handles the underlying data source */
39 protected $paginatable;
40
41 /** @var Url The URL to base off pagination URLs */
42 protected $url;
43
44 /** @var int Cache for the total number of items */
45 private $totalCount;
46
47 protected $tag = 'div';
48
49 protected $defaultAttributes = [
50 'class' => 'pagination-control',
51 'role' => 'navigation'
52 ];
53
54 /**
55 * Create a pagination control
56 *
57 * @param Paginatable $paginatable The paginatable
58 * @param Url $url The URL to base off paging URLs
59 */
60 public function __construct(Paginatable $paginatable, Url $url)
61 {
62 $this->paginatable = $paginatable;
63 $this->url = $url;
64 }
65
66 /**
67 * Set the URL to base off paging URLs
68 *
69 * @param Url $url
70 *
71 * @return $this
72 */
73 public function setUrl(Url $url)
74 {
75 $this->url = $url;
76
77 return $this;
78 }
79
80 /**
81 * Get the default page size
82 *
83 * @return int
84 */
85 public function getDefaultPageSize()
86 {
87 return $this->defaultPageSize ?: LimitControl::DEFAULT_LIMIT;
88 }
89
90 /**
91 * Set the default page size
92 *
93 * @param int $defaultPageSize
94 *
95 * @return $this
96 */
97 public function setDefaultPageSize($defaultPageSize)
98 {
99 $this->defaultPageSize = $defaultPageSize;
100
101 return $this;
102 }
103
104 /**
105 * Get the name of the URL parameter which stores the current page number
106 *
107 * @return string
108 */
109 public function getPageParam()
110 {
111 return $this->pageParam;
112 }
113
114 /**
115 * Set the name of the URL parameter which stores the current page number
116 *
117 * @param string $pageParam
118 *
119 * @return $this
120 */
121 public function setPageParam($pageParam)
122 {
123 $this->pageParam = $pageParam;
124
125 return $this;
126 }
127
128 /**
129 * Get message content to show when the list is empty
130 *
131 * @return ?ValidHtml Content is returned when given page is out of range, null otherwise
132 */
133 public function getEmptyStateMessage(): ?ValidHtml
134 {
135 $currentPage = $this->getCurrentPageNumber();
136 $pageCount = $this->getPageCount();
137 if ($pageCount && $pageCount < $currentPage) {
138 return TemplateString::create(
139 $this->translate('Page %d is out of range. {{#button}}Navigate to first page{{/button}}'),
140 $currentPage,
141 ['button' => new ButtonLink(null, $this->url->without('page'))]
142 );
143 }
144
145 return null;
146 }
147
148 /**
149 * Get the name of the URL parameter which stores the page size
150 *
151 * @return string
152 */
153 public function getPageSizeParam()
154 {
155 return $this->pageSizeParam;
156 }
157 /**
158 * Set the name of the URL parameter which stores the page size
159 *
160 * @param string $pageSizeParam
161 *
162 * @return $this
163 */
164 public function setPageSizeParam($pageSizeParam)
165 {
166 $this->pageSizeParam = $pageSizeParam;
167
168 return $this;
169 }
170
171 /**
172 * Get the total number of items
173 *
174 * @return int
175 */
176 public function getTotalCount()
177 {
178 if ($this->totalCount === null) {
179 $this->totalCount = $this->paginatable->count();
180 }
181
182 return $this->totalCount;
183 }
184
185 /**
186 * Get the current page number
187 *
188 * @return int
189 */
190 public function getCurrentPageNumber()
191 {
192 return (int) $this->url->getParam($this->pageParam, 1);
193 }
194
195 /**
196 * Get the configured page size
197 *
198 * @return int
199 */
200 public function getPageSize()
201 {
202 return (int) $this->url->getParam($this->pageSizeParam, $this->getDefaultPageSize());
203 }
204
205 /**
206 * Get the total page count
207 *
208 * @return int
209 */
210 public function getPageCount()
211 {
212 $pageSize = $this->getPageSize();
213
214 if ($pageSize === 0) {
215 return 0;
216 }
217
218 if ($pageSize < 0) {
219 return 1;
220 }
221
222 return (int) ceil($this->getTotalCount() / $pageSize);
223 }
224
225 /**
226 * Get the limit
227 *
228 * Use this method to set the LIMIT part of a query for fetching the current page.
229 *
230 * @return int If the page size is infinite, -1 will be returned
231 */
232 public function getLimit()
233 {
234 $pageSize = $this->getPageSize();
235
236 return $pageSize < 0 ? -1 : $pageSize;
237 }
238
239 /**
240 * Get the offset
241 *
242 * Use this method to set the OFFSET part of a query for fetching the current page.
243 *
244 * @return int
245 */
246 public function getOffset()
247 {
248 $currentPageNumber = $this->getCurrentPageNumber();
249 $pageSize = $this->getPageSize();
250
251 return $currentPageNumber <= 1 ? 0 : ($currentPageNumber - 1) * $pageSize;
252 }
253
254 /**
255 * Apply limit and offset on the paginatable
256 *
257 * @return $this
258 */
259 public function apply()
260 {
261 $this->paginatable->limit($this->getLimit());
262 $this->paginatable->offset($this->getOffset());
263
264 return $this;
265 }
266
267 /**
268 * Create a URL for paging from the given page number
269 *
270 * @param int $pageNumber The page number
271 * @param int $pageSize The number of items per page. If you want to stick to the defaults,
272 * don't set this parameter
273 *
274 * @return Url
275 */
276 public function createUrl($pageNumber, $pageSize = null)
277 {
278 $params = [$this->getPageParam() => $pageNumber];
279
280 if ($pageSize !== null) {
281 $params[$this->getPageSizeParam()] = $pageSize;
282 }
283
284 return $this->url->with($params);
285 }
286
287 /**
288 * Get the first item number of the given page
289 *
290 * @param int $pageNumber
291 *
292 * @return int
293 */
294 protected function getFirstItemNumberOfPage($pageNumber)
295 {
296 return ($pageNumber - 1) * $this->getPageSize() + 1;
297 }
298
299 /**
300 * Get the last item number of the given page
301 *
302 * @param int $pageNumber
303 *
304 * @return int
305 */
306 protected function getLastItemNumberOfPage($pageNumber)
307 {
308 return min($pageNumber * $this->getPageSize(), $this->getTotalCount());
309 }
310
311 /**
312 * Create the label for the given page number
313 *
314 * @param int $pageNumber
315 *
316 * @return string
317 */
318 protected function createLabel($pageNumber)
319 {
320 return sprintf(
321 $this->translate('Show items %u to %u of %u'),
322 $this->getFirstItemNumberOfPage($pageNumber),
323 $this->getLastItemNumberOfPage($pageNumber),
324 $this->getTotalCount()
325 );
326 }
327
328 /**
329 * Create and return the previous page item
330 *
331 * @return BaseHtmlElement
332 */
333 protected function createPreviousPageItem()
334 {
335 $prevIcon = new Icon('angle-left');
336
337 $currentPageNumber = $this->getCurrentPageNumber();
338
339 if ($currentPageNumber > 1) {
340 $prevItem = Html::tag('li', ['class' => 'nav-item']);
341
342 $prevItem->add(Html::tag(
343 'a',
344 [
345 'class' => 'previous-page',
346 'href' => $this->createUrl($currentPageNumber - 1),
347 'title' => $this->createLabel($currentPageNumber - 1)
348 ],
349 $prevIcon
350 ));
351 } else {
352 $prevItem = Html::tag(
353 'li',
354 [
355 'aria-hidden' => true,
356 'class' => 'nav-item disabled'
357 ]
358 );
359
360 $prevItem->add(Html::tag('span', ['class' => 'previous-page'], $prevIcon));
361 }
362
363 return $prevItem;
364 }
365
366 /**
367 * Create and return the next page item
368 *
369 * @return BaseHtmlElement
370 */
371 protected function createNextPageItem()
372 {
373 $nextIcon = new Icon('angle-right');
374
375 $currentPageNumber = $this->getCurrentPageNumber();
376
377 if ($currentPageNumber < $this->getPageCount()) {
378 $nextItem = Html::tag('li', ['class' => 'nav-item']);
379
380 $nextItem->add(Html::tag(
381 'a',
382 [
383 'class' => 'next-page',
384 'href' => $this->createUrl($currentPageNumber + 1),
385 'title' => $this->createLabel($currentPageNumber + 1)
386 ],
387 $nextIcon
388 ));
389 } else {
390 $nextItem = Html::tag(
391 'li',
392 [
393 'aria-hidden' => true,
394 'class' => 'nav-item disabled'
395 ]
396 );
397
398 $nextItem->add(Html::tag('span', ['class' => 'next-page'], $nextIcon));
399 }
400
401 return $nextItem;
402 }
403
404 /**
405 * Create and return the first page item
406 *
407 * @return BaseHtmlElement
408 */
409 protected function createFirstPageItem()
410 {
411 $currentPageNumber = $this->getCurrentPageNumber();
412
413 $url = clone $this->url;
414
415 $firstItem = Html::tag('li', ['class' => 'nav-item']);
416
417 if ($currentPageNumber === 1) {
418 $firstItem->addAttributes(['class' => 'disabled']);
419 $firstItem->add(Html::tag(
420 'span',
421 ['class' => 'first-page'],
422 $this->getFirstItemNumberOfPage(1)
423 ));
424 } else {
425 $firstItem->add(Html::tag(
426 'a',
427 [
428 'class' => 'first-page',
429 'href' => $url->remove(['page'])->getAbsoluteUrl(),
430 'title' => $this->createLabel(1)
431 ],
432 $this->getFirstItemNumberOfPage(1)
433 ));
434 }
435
436 return $firstItem;
437 }
438
439 /**
440 * Create and return the last page item
441 *
442 * @return BaseHtmlElement
443 */
444 protected function createLastPageItem()
445 {
446 $currentPageNumber = $this->getCurrentPageNumber();
447 $lastItem = Html::tag('li', ['class' => 'nav-item']);
448
449 if ($currentPageNumber === $this->getPageCount()) {
450 $lastItem->addAttributes(['class' => 'disabled']);
451 $lastItem->add(Html::tag(
452 'span',
453 ['class' => 'last-page'],
454 $this->getPageCount()
455 ));
456 } else {
457 $lastItem->add(Html::tag(
458 'a',
459 [
460 'class' => 'last-page',
461 'href' => $this->url->setParam('page', $this->getPageCount()),
462 'title' => $this->createLabel($this->getPageCount())
463 ],
464 $this->getPageCount()
465 ));
466 }
467
468 return $lastItem;
469 }
470
471 /**
472 * Create and return the page selector item
473 *
474 * @return BaseHtmlElement
475 */
476 protected function createPageSelectorItem()
477 {
478 $currentPageNumber = $this->getCurrentPageNumber();
479
480 $form = new CompatForm();
481 $form->addAttributes(['class' => 'inline']);
482 $form->setMethod('GET');
483
484 $select = Html::tag('select', [
485 'name' => $this->getPageParam(),
486 'class' => 'autosubmit',
487 'title' => $this->translate('Go to page …')
488 ]);
489
490 $pageCount = $this->getPageCount();
491
492 if ($currentPageNumber === 1 || $currentPageNumber === $pageCount) {
493 $select->add(Html::tag('option', ['disabled' => '', 'selected' => ''], '…'));
494 }
495
496 if (2 <= $pageCount - 1) {
497 foreach (range(2, $pageCount - 1) as $page) {
498 $option = Html::tag('option', [
499 'value' => $page
500 ], $page);
501
502 if ($page == $currentPageNumber) {
503 $option->addAttributes(['selected' => '']);
504 }
505
506 $select->add($option);
507 }
508 }
509
510 if ($currentPageNumber > $pageCount) {
511 $select->add(Html::tag('option', [
512 'value' => $currentPageNumber,
513 'selected' => '',
514 'disabled' => ''
515 ], $currentPageNumber));
516 }
517
518 $form->add($select);
519
520 $pageSelectorItem = Html::tag('li', $form);
521
522 return $pageSelectorItem;
523 }
524
525 protected function assemble()
526 {
527 if ($this->getPageCount() < 2) {
528 return;
529 }
530
531 // Accessibility info
532 $this->add(Html::tag(
533 'h2',
534 [
535 'class' => 'sr-only',
536 'tabindex' => '-1'
537 ],
538 $this->translate('Pagination')
539 ));
540
541 $paginator = Html::tag('ul', ['class' => 'tab-nav nav']);
542
543 $paginator->add([
544 $this->createFirstPageItem(),
545 $this->createPreviousPageItem(),
546 $this->createPageSelectorItem(),
547 $this->createNextPageItem(),
548 $this->createLastPageItem()
549 ]);
550
551 $this->add($paginator);
552 }
553 }