|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace Breadlesscode\Listable\Fusion; |
3 | 4 |
|
4 | 5 | use Neos\Flow\Annotations as Flow; |
5 | 6 | use Neos\Fusion\FusionObjects\AbstractFusionObject; |
6 | 7 |
|
7 | 8 | class PaginationArrayImplementation extends AbstractFusionObject |
8 | 9 | { |
9 | | - /** |
10 | | - * @return Array |
11 | | - */ |
12 | | - public function evaluate() |
13 | | - { |
14 | | - $maximumNumberOfLinks = $this->tsValue('maximumNumberOfLinks') - 2; |
15 | | - $itemsPerPage = $this->tsValue('itemsPerPage'); |
16 | | - $totalCount = $this->tsValue('totalCount'); |
17 | | - $currentPage = $this->tsValue('currentPage'); |
18 | | - if ($totalCount > 0 !== true) { |
19 | | - return []; |
20 | | - } |
21 | | - $numberOfPages = ceil($totalCount / $itemsPerPage); |
22 | | - if ($maximumNumberOfLinks > $numberOfPages) { |
23 | | - $maximumNumberOfLinks = $numberOfPages; |
24 | | - } |
25 | | - $delta = floor($maximumNumberOfLinks / 2); |
26 | | - $displayRangeStart = $currentPage - $delta; |
27 | | - $displayRangeEnd = $currentPage + $delta + ($maximumNumberOfLinks % 2 === 0 ? 1 : 0); |
28 | | - if ($displayRangeStart < 1) { |
29 | | - $displayRangeEnd -= $displayRangeStart - 1; |
30 | | - } |
31 | | - if ($displayRangeEnd > $numberOfPages) { |
32 | | - $displayRangeStart -= ($displayRangeEnd - $numberOfPages); |
33 | | - } |
34 | | - $displayRangeStart = (integer)max($displayRangeStart, 1); |
35 | | - $displayRangeEnd = (integer)min($displayRangeEnd, $numberOfPages); |
36 | | - $links = \range($displayRangeStart, $displayRangeEnd); |
37 | | - if ($displayRangeStart > 2) { |
38 | | - array_unshift($links, "..."); |
39 | | - array_unshift($links, 1); |
40 | | - } |
41 | | - if ($displayRangeEnd + 1 < $numberOfPages) { |
42 | | - $links[] = "..."; |
43 | | - $links[] = $numberOfPages; |
44 | | - } |
45 | | - return $links; |
46 | | - } |
| 10 | + protected $currentPage; |
| 11 | + protected $itemsPerPage; |
| 12 | + protected $maximumNumberOfLinks; |
| 13 | + protected $totalCount; |
| 14 | + /** |
| 15 | + * @inheritDoc |
| 16 | + */ |
| 17 | + public function __construct(Runtime $runtime, $path, $fusionObjectName) |
| 18 | + { |
| 19 | + parent::__construct($runtime, $path, $fusionObjectName); |
| 20 | + $this->currentPage = \intval($this->fusionValue('currentPage')) |
| 21 | + $this->itemsPerPage = \intval($this->fusionValue('itemsPerPage')); |
| 22 | + $this->maximumNumberOfLinks = \intval($this->fusionValue('maximumNumberOfLinks')) - 2; |
| 23 | + $this->totalCount = \intval($this->fusionValue('totalCount')); |
| 24 | + } |
| 25 | + /** |
| 26 | + * calculates the number of pages |
| 27 | + * @return integer |
| 28 | + */ |
| 29 | + protected function getNumberOfPages() |
| 30 | + { |
| 31 | + $numberOfPages = \ceil($this->totalCount / $this->itemsPerPage); |
| 32 | + if ($this->maximumNumberOfLinks > $numberOfPages) { |
| 33 | + return $numberOfPages; |
| 34 | + } |
| 35 | + return $numberOfPages; |
| 36 | + } |
| 37 | + /** |
| 38 | + * get an array of pages to display |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + protected function getPageRangeArray($numberOfPages) |
| 42 | + { |
| 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; |
| 48 | + } |
| 49 | + if ($rangeEnd > $numberOfPages) { |
| 50 | + $rangeStart -= ($rangeEnd - $numberOfPages); |
| 51 | + } |
| 52 | + $rangeStart = \max($rangeStart, 1); |
| 53 | + $rangeEnd = \min($rangeEnd, $numberOfPages); |
| 54 | + return \range($rangeStart, $rangeEnd); |
| 55 | + } |
| 56 | + /** |
| 57 | + * @return Array |
| 58 | + */ |
| 59 | + public function evaluate() |
| 60 | + { |
| 61 | + if ($this->totalCount > 0 !== true) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + $numberOfPages = $this->getNumberOfPages(); |
| 65 | + $pageArray = $this->getPageRangeArray($numberOfPages) |
| 66 | + if ($pageArray[0] > 2) { |
| 67 | + array_unshift($pageArray, "..."); |
| 68 | + array_unshift($pageArray, 1); |
| 69 | + } |
| 70 | + if (end($pagesArray) + 1 < $numberOfPages) { |
| 71 | + $links[] = "..."; |
| 72 | + $links[] = $numberOfPages; |
| 73 | + } |
| 74 | + return $links; |
| 75 | + } |
47 | 76 | } |
0 commit comments