|
4 | 4 |
|
5 | 5 | use Neos\Flow\Annotations as Flow; |
6 | 6 | use Neos\Fusion\FusionObjects\AbstractFusionObject; |
| 7 | +use Neos\Fusion\Core\Runtime; |
7 | 8 |
|
8 | 9 | class PaginationArrayImplementation extends AbstractFusionObject |
9 | 10 | { |
10 | 11 | protected $currentPage; |
11 | 12 | protected $itemsPerPage; |
12 | 13 | protected $maximumNumberOfLinks; |
13 | 14 | protected $totalCount; |
| 15 | + protected $numberOfPages; |
| 16 | + protected $firstPage; |
| 17 | + protected $lastPage; |
14 | 18 | /** |
15 | 19 | * @inheritDoc |
16 | 20 | */ |
17 | 21 | public function __construct(Runtime $runtime, $path, $fusionObjectName) |
18 | 22 | { |
19 | 23 | parent::__construct($runtime, $path, $fusionObjectName); |
| 24 | + |
20 | 25 | $this->currentPage = \intval($this->fusionValue('currentPage')); |
21 | 26 | $this->itemsPerPage = \intval($this->fusionValue('itemsPerPage')); |
22 | | - $this->maximumNumberOfLinks = \intval($this->fusionValue('maximumNumberOfLinks')) - 2; |
| 27 | + $this->maximumNumberOfLinks = \intval($this->fusionValue('maximumNumberOfLinks')); |
23 | 28 | $this->totalCount = \intval($this->fusionValue('totalCount')); |
| 29 | + $this->paginationConfig = $this->fusionValue('paginationConfig'); |
| 30 | + $this->numberOfPages = $this->getNumberOfPages(); |
| 31 | + |
| 32 | + $this->calculateFirstAndLastPage(); |
| 33 | + } |
| 34 | + /** |
| 35 | + * calculates the first and last page to show |
| 36 | + * |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + protected function calculateFirstAndLastPage() |
| 40 | + { |
| 41 | + $delta = \floor($this->maximumNumberOfLinks / 2); |
| 42 | + $firstPage = $this->currentPage - $delta; |
| 43 | + $lastPage = $this->currentPage + $delta + ($this->maximumNumberOfLinks % 2 === 0 ? 1 : 0); |
| 44 | + |
| 45 | + if ($firstPage < 1) { |
| 46 | + $lastPage -= $firstPage - 1; |
| 47 | + } |
| 48 | + if ($lastPage > $this->numberOfPages) { |
| 49 | + $firstPage -= ($lastPage - $this->numberOfPages); |
| 50 | + } |
| 51 | + |
| 52 | + $this->firstPage = \max($firstPage, 1); |
| 53 | + $this->lastPage = \min($lastPage, $this->numberOfPages); |
24 | 54 | } |
25 | 55 | /** |
26 | 56 | * calculates the number of pages |
| 57 | + * |
27 | 58 | * @return integer |
28 | 59 | */ |
29 | 60 | protected function getNumberOfPages() |
30 | 61 | { |
31 | 62 | $numberOfPages = \ceil($this->totalCount / $this->itemsPerPage); |
| 63 | + |
32 | 64 | if ($this->maximumNumberOfLinks > $numberOfPages) { |
33 | 65 | return $numberOfPages; |
34 | 66 | } |
35 | 67 | return $numberOfPages; |
36 | 68 | } |
37 | 69 | /** |
38 | 70 | * get an array of pages to display |
| 71 | + * |
39 | 72 | * @return array |
40 | 73 | */ |
41 | | - protected function getPageRangeArray($numberOfPages) |
| 74 | + protected function getPageArray() |
42 | 75 | { |
43 | | - $delta = \floor($this->maximumNumberOfLinks / 2); |
44 | | - $rangeStart = $this->currentPage - $delta; |
45 | | - $rangeEnd = $this->currentPage + $delta + ($this->maximumNumberOfLinks % 2 === 0 ? 1 : 0); |
46 | | - if ($rangeStart < 1) { |
47 | | - $rangeEnd -= $rangeStart - 1; |
| 76 | + $range = \range($this->firstPage, $this->lastPage); |
| 77 | + $pageArray = []; |
| 78 | + |
| 79 | + foreach ($range as $page) { |
| 80 | + $pageArray[] = [ |
| 81 | + 'page' => $page, |
| 82 | + 'label' => $page, |
| 83 | + 'type' => 'page' |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + return $pageArray; |
| 88 | + } |
| 89 | + /** |
| 90 | + * add a item to the start of the page array |
| 91 | + * |
| 92 | + * @param array $pageArray |
| 93 | + * @param integer $page |
| 94 | + * @param string $type |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + protected function addItemToTheStartOfPageArray($pageArray, $page, $type) |
| 98 | + { |
| 99 | + if ($this->firstPage > 1) { |
| 100 | + array_unshift($pageArray, [ |
| 101 | + 'page' => $page, |
| 102 | + 'label' => $this->paginationConfig['labels'][$type], |
| 103 | + 'type' => $type |
| 104 | + ]); |
48 | 105 | } |
49 | | - if ($rangeEnd > $numberOfPages) { |
50 | | - $rangeStart -= ($rangeEnd - $numberOfPages); |
| 106 | + |
| 107 | + return $pageArray; |
| 108 | + } |
| 109 | + /** |
| 110 | + * add a item to the end of the page array |
| 111 | + * |
| 112 | + * @param array $pageArray |
| 113 | + * @param integer $page |
| 114 | + * @param string $type |
| 115 | + * @return array |
| 116 | + */ |
| 117 | + protected function addItemToTheEndOfPageArray($pageArray, $page, $type) |
| 118 | + { |
| 119 | + if ($this->lastPage < $this->numberOfPages) { |
| 120 | + $pageArray[] = [ |
| 121 | + 'page' => $page, |
| 122 | + 'label' => $this->paginationConfig['labels'][$type], |
| 123 | + 'type' => $type |
| 124 | + ]; |
51 | 125 | } |
52 | | - $rangeStart = \max($rangeStart, 1); |
53 | | - $rangeEnd = \min($rangeEnd, $numberOfPages); |
54 | | - return \range($rangeStart, $rangeEnd); |
| 126 | + |
| 127 | + return $pageArray; |
55 | 128 | } |
56 | 129 | /** |
57 | | - * @return Array |
| 130 | + * @return array |
58 | 131 | */ |
59 | 132 | public function evaluate() |
60 | 133 | { |
61 | | - if ($this->totalCount > 0 !== true) { |
| 134 | + if ($this->totalCount > 0 !== true || $this->numberOfPages === 1) { |
62 | 135 | return []; |
63 | 136 | } |
64 | | - $numberOfPages = $this->getNumberOfPages(); |
65 | | - $pageArray = $this->getPageRangeArray($numberOfPages); |
66 | | - if ($pageArray[0] > 2) { |
67 | | - array_unshift($pageArray, "..."); |
68 | | - array_unshift($pageArray, 1); |
| 137 | + |
| 138 | + $pageArray = $this->getPageArray(); |
| 139 | + |
| 140 | + if ($this->paginationConfig['showSeperators']) { |
| 141 | + $pageArray = $this->addItemToTheStartOfPageArray($pageArray, false, 'seperator'); |
| 142 | + $pageArray = $this->addItemToTheEndOfPageArray($pageArray, false, 'seperator'); |
| 143 | + } |
| 144 | + if ($this->paginationConfig['showNextAndPrevious']) { |
| 145 | + $pageArray = $this->addItemToTheStartOfPageArray($pageArray, $this->currentPage - 1, 'previous'); |
| 146 | + $pageArray = $this->addItemToTheEndOfPageArray($pageArray, $this->currentPage + 1, 'next'); |
69 | 147 | } |
70 | | - if (end($pagesArray) + 1 < $numberOfPages) { |
71 | | - $links[] = "..."; |
72 | | - $links[] = $numberOfPages; |
| 148 | + if ($this->paginationConfig['showFirstAndLast']) { |
| 149 | + $pageArray = $this->addItemToTheStartOfPageArray($pageArray, 1, 'first'); |
| 150 | + $pageArray = $this->addItemToTheEndOfPageArray($pageArray, $this->numberOfPages, 'last'); |
73 | 151 | } |
74 | | - return $links; |
| 152 | + return $pageArray; |
75 | 153 | } |
76 | 154 | } |
0 commit comments