Skip to content

Commit 690e628

Browse files
committed
added show numeric last & frist pagination link
1 parent 499092f commit 690e628

File tree

6 files changed

+123
-88
lines changed

6 files changed

+123
-88
lines changed

Classes/Fusion/PaginationArrayImplementation.php

Lines changed: 100 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,67 @@
22

33
namespace Breadlesscode\Listable\Fusion;
44

5-
use Neos\Flow\Annotations as Flow;
65
use Neos\Fusion\FusionObjects\AbstractFusionObject;
76
use Neos\Fusion\Core\Runtime;
87

98
class PaginationArrayImplementation extends AbstractFusionObject
109
{
10+
/**
11+
* @var int
12+
*/
1113
protected $currentPage;
14+
/**
15+
* @var int
16+
*/
1217
protected $itemsPerPage;
18+
/**
19+
* @var int
20+
*/
1321
protected $maximumNumberOfLinks;
14-
protected $totalCount;
22+
/**
23+
* @var int
24+
*/
25+
protected $itemCount;
26+
/**
27+
* @var array
28+
*/
29+
protected $paginationConfig;
30+
/**
31+
* @var int
32+
*/
1533
protected $numberOfPages;
16-
protected $firstPage;
17-
protected $lastPage;
34+
/**
35+
* @var int
36+
*/
37+
protected $firstPageShown;
38+
/**
39+
* @var int
40+
*/
41+
protected $lastPageShown;
42+
/**
43+
* @var array
44+
*/
45+
protected $pages = [];
46+
1847
/**
1948
* @inheritDoc
2049
*/
2150
public function __construct(Runtime $runtime, $path, $fusionObjectName)
2251
{
2352
parent::__construct($runtime, $path, $fusionObjectName);
2453

25-
$this->currentPage = \intval($this->fusionValue('currentPage'));
26-
$this->itemsPerPage = \intval($this->fusionValue('itemsPerPage'));
27-
$this->maximumNumberOfLinks = \intval($this->fusionValue('maximumNumberOfLinks'));
28-
$this->totalCount = \intval($this->fusionValue('totalCount'));
54+
$this->currentPage = (int) $this->fusionValue('currentPage');
55+
$this->itemsPerPage = (int) $this->fusionValue('itemsPerPage');
56+
$this->itemCount = (int) $this->fusionValue('totalCount');
57+
$this->maximumNumberOfLinks = (int) $this->fusionValue('maximumNumberOfLinks');
2958
$this->paginationConfig = $this->fusionValue('paginationConfig');
30-
$this->numberOfPages = $this->getNumberOfPages();
59+
60+
$this->numberOfPages = \ceil($this->itemCount / $this->itemsPerPage);
3161

3262
$this->calculateFirstAndLastPage();
63+
$this->createPageArray();
3364
}
65+
3466
/**
3567
* calculates the first and last page to show
3668
*
@@ -39,129 +71,124 @@ public function __construct(Runtime $runtime, $path, $fusionObjectName)
3971
protected function calculateFirstAndLastPage()
4072
{
4173
$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;
74+
$firstPageShown = $this->currentPage - $delta;
75+
$lastPageShown = $this->currentPage + $delta;
76+
// check for the correct number of shown links for the left side
77+
// if you are on the first page the number of missing links should be added to the right links
78+
if ($firstPageShown < 1) {
79+
$lastPageShown -= $firstPageShown - 1;
4780
}
48-
if ($lastPage > $this->numberOfPages) {
49-
$firstPage -= ($lastPage - $this->numberOfPages);
81+
// check for the correct number of shown links for the right side
82+
// if you are on the last page the number of missing links should be added to the left links
83+
if ($lastPageShown > $this->numberOfPages) {
84+
$firstPageShown -= ($lastPageShown - $this->numberOfPages);
5085
}
51-
52-
$this->firstPage = \max($firstPage, 1);
53-
$this->lastPage = \min($lastPage, $this->numberOfPages);
86+
// make sure the calculdated numbers are not out of range
87+
$this->firstPageShown = \max($firstPageShown, 1);
88+
$this->lastPageShown = \min($lastPageShown, $this->numberOfPages);
5489
}
55-
/**
56-
* calculates the number of pages
57-
*
58-
* @return integer
59-
*/
60-
protected function getNumberOfPages()
61-
{
62-
$numberOfPages = \ceil($this->totalCount / $this->itemsPerPage);
6390

64-
if ($this->maximumNumberOfLinks > $numberOfPages) {
65-
return $numberOfPages;
66-
}
67-
return $numberOfPages;
68-
}
6991
/**
7092
* get an array of pages to display
71-
*
72-
* @return array
7393
*/
74-
protected function getPageArray()
94+
protected function createPageArray()
7595
{
76-
$range = \range($this->firstPage, $this->lastPage);
77-
$pageArray = [];
96+
$range = \range($this->firstPageShown, $this->lastPageShown);
7897

7998
foreach ($range as $page) {
80-
$pageArray[] = [
99+
$this->pages[] = [
81100
'page' => $page,
82101
'label' => $page,
83102
'type' => 'page'
84103
];
85104
}
86-
87-
return $pageArray;
88105
}
106+
89107
/**
90108
* add a item to the start of the page array
91109
*
92-
* @param array $pageArray
93110
* @param integer $page
111+
* @param $label
94112
* @param string $type
95-
* @return array
96113
*/
97-
protected function addItemToTheStartOfPageArray($pageArray, $page, $type)
114+
protected function addItemToTheStartOfPageArray($page, $label, $type)
98115
{
99-
array_unshift($pageArray, [
116+
array_unshift($this->pages, [
100117
'page' => $page,
101-
'label' => $this->paginationConfig['labels'][$type],
118+
'label' => $label,
102119
'type' => $type
103120
]);
104-
105-
return $pageArray;
106121
}
122+
107123
/**
108124
* add a item to the end of the page array
109125
*
110-
* @param array $pageArray
111-
* @param integer $page
112-
* @param string $type
113-
* @return array
126+
* @param $page
127+
* @param $label
128+
* @param $type
114129
*/
115-
protected function addItemToTheEndOfPageArray($pageArray, $page, $type)
130+
protected function addItemToTheEndOfPageArray($page, $label, $type)
116131
{
117-
$pageArray[] = [
132+
$this->pages[] = [
118133
'page' => $page,
119-
'label' => $this->paginationConfig['labels'][$type],
134+
'label' => $label,
120135
'type' => $type
121136
];
122-
123-
return $pageArray;
124137
}
138+
125139
/**
126140
* @return array
127141
*/
128142
public function evaluate()
129143
{
130-
if ($this->totalCount > 0 !== true || $this->numberOfPages === 1) {
144+
if ($this->itemCount > 0 !== true || $this->numberOfPages === 1) {
131145
return [];
132146
}
133-
134-
$pageArray = $this->getPageArray();
135-
// add seperators
147+
// add configured seperators
136148
if ($this->paginationConfig['showSeperators']) {
137-
if ($this->firstPage > 1) {
138-
$pageArray = $this->addItemToTheStartOfPageArray($pageArray, false, 'seperator');
149+
if ($this->firstPageShown > 1) {
150+
$this->addItemToTheStartOfPageArray(false, $this->paginationConfig['labels']['seperator'], 'seperator');
139151
}
140-
if ($this->lastPage < $this->numberOfPages) {
141-
$pageArray = $this->addItemToTheEndOfPageArray($pageArray, false, 'seperator');
152+
if ($this->lastPageShown < $this->numberOfPages) {
153+
$this->addItemToTheEndOfPageArray(false, $this->paginationConfig['labels']['seperator'], 'seperator');
154+
}
155+
}
156+
// add numeric first & last links
157+
if ($this->paginationConfig['showFirstAndLastNumeric']) {
158+
if ($this->firstPageShown > 1 || $this->paginationConfig['alwaysShowFirstAndLast']) {
159+
$this->addItemToTheStartOfPageArray(1, 1, 'first');
160+
}
161+
if ($this->lastPageShown < $this->numberOfPages || $this->paginationConfig['alwaysShowFirstAndLast']) {
162+
$this->addItemToTheEndOfPageArray($this->numberOfPages, $this->numberOfPages, 'last');
142163
}
143164
}
144165
// add previous and next
145166
if ($this->paginationConfig['showNextAndPrevious']) {
146167
if ($this->currentPage > 1 || $this->paginationConfig['alwaysShowNextAndPrevious']) {
147-
$pageArray = $this->addItemToTheStartOfPageArray($pageArray, $this->currentPage - 1, 'previous');
168+
if ($this->currentPage > 1) {
169+
$this->addItemToTheStartOfPageArray($this->currentPage - 1, $this->paginationConfig['labels']['previous'],'previous');
170+
} else {
171+
$this->addItemToTheStartOfPageArray(false, $this->paginationConfig['labels']['previous'],'previous');
172+
}
148173
}
149-
if ($this->currentPage < $this->lastPage || $this->paginationConfig['alwaysShowNextAndPrevious']) {
174+
if ($this->currentPage < $this->lastPageShown || $this->paginationConfig['alwaysShowNextAndPrevious']) {
150175
if ($this->currentPage < $this->numberOfPages) {
151-
$pageArray = $this->addItemToTheEndOfPageArray($pageArray, $this->currentPage + 1, 'next');
176+
$this->addItemToTheEndOfPageArray($this->currentPage + 1, $this->paginationConfig['labels']['next'], 'next');
152177
} else {
153-
$pageArray = $this->addItemToTheEndOfPageArray($pageArray, $this->currentPage, 'next');
178+
$this->addItemToTheEndOfPageArray(false, $this->paginationConfig['labels']['next'], 'next');
154179
}
155180
}
156181
}
182+
// add first & last link with configured label
157183
if ($this->paginationConfig['showFirstAndLast']) {
158-
if ($this->firstPage > 1 || $this->paginationConfig['alwaysShowFirstAndLast']) {
159-
$pageArray = $this->addItemToTheStartOfPageArray($pageArray, 1, 'first');
184+
if ($this->firstPageShown > 1 || $this->paginationConfig['alwaysShowFirstAndLast']) {
185+
$this->addItemToTheStartOfPageArray(1, $this->paginationConfig['labels']['first'], 'first');
160186
}
161-
if ($this->lastPage < $this->numberOfPages || $this->paginationConfig['alwaysShowFirstAndLast']) {
162-
$pageArray = $this->addItemToTheEndOfPageArray($pageArray, $this->numberOfPages, 'last');
187+
if ($this->lastPageShown < $this->numberOfPages || $this->paginationConfig['alwaysShowFirstAndLast']) {
188+
$this->addItemToTheEndOfPageArray($this->numberOfPages, $this->paginationConfig['labels']['last'], 'last');
163189
}
164190
}
165-
return $pageArray;
191+
192+
return $this->pages;
166193
}
167194
}

Configuration/Settings.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Breadlesscode:
2323
showNextAndPrevious: true
2424
alwaysShowNextAndPrevious: false
2525
showFirstAndLast: true
26+
showFirstAndLastNumeric: false
2627
alwaysShowFirstAndLast: false
28+
numberOfLinks: 3
2729
labels:
2830
seperator: '&hellip;'
2931
previous: '&lang;'

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ prototype(Vendor.Xy:MyPersonalListItem) < prototype(Neos.Fusion:Tag) {
4444
4545
```
4646
## Configuration
47-
You have two possibilities to configure the pagination of this package. You can set the configurations global via `Settings.yaml`:
47+
You have to possibilities to configure the pagination of this package. You can set the configurations global via `Settings.yaml`:
4848

4949
```yaml
5050
Breadlesscode:
@@ -55,11 +55,13 @@ Breadlesscode:
5555
alwaysShowNextAndPrevious: true
5656
showFirstAndLast: true
5757
alwaysShowFirstAndLast: true
58+
alwaysShowFirstAndLastNumeric: false
59+
numberOfLinks: 3
5860
labels:
5961
seperator: '&hellip;'
6062
previous: '&lang;'
6163
next: '&rang;'
62-
first: '&lang;'
64+
first: '&laquo;'
6365
last: '&raquo;'
6466
```
6567
@@ -69,18 +71,20 @@ And you can overwrite this configuration in Fusion for a specific list:
6971
prototype(Vendor.Xy:MyPersonalList) < prototype(Breadlesscode.Listable:List) {
7072
# ...
7173
paginationConfig {
72-
showSeperators = ${ true }
73-
showNextAndPrevious = ${ true }
74-
alwaysShowNextAndPrevious = ${ true }
75-
showFirstAndLast = ${ true }
76-
alwaysShowFirstAndLast = ${ true }
74+
showSeperators = true
75+
showNextAndPrevious = true
76+
alwaysShowNextAndPrevious = true
77+
showFirstAndLast = true
78+
alwaysShowFirstAndLast = true
79+
alwaysShowFirstAndLastNumeric = false
80+
numberOfLinks = 3
7781

7882
labels {
79-
seperator = ${ '&hellip' }
80-
previous = ${ '&lang;' }
81-
next = ${ '&rang;' }
82-
first = ${ '&lang;' }
83-
last = ${ '&rang;' }
83+
seperator = '&hellip'
84+
previous = '&lang;'
85+
next = '&rang;'
86+
first = '&laquo;'
87+
last = '&raquo;'
8488
}
8589
}
8690
}

Resources/Private/Fusion/Component/List.fusion

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
prototype(Breadlesscode.Listable:List) < prototype(Neos.Fusion:Component) {
22
collection = ${ [] }
33
// itemsPerPage is deprecated, please use limit instead
4-
itemsPerPage = 10
4+
itemsPerPage = 10
55
limit = ${ this.itemsPerPage }
66
itemRenderer = 'Breadlesscode.Listable:ListItem'
77
itemName = ${ 'item' }
@@ -29,7 +29,7 @@ prototype(Breadlesscode.Listable:List) < prototype(Neos.Fusion:Component) {
2929
}
3030
pagination = Breadlesscode.Listable:Pagination {
3131
currentPage = ${ currentPage }
32-
maximumNumberOfLinks = ${ 3 }
32+
maximumNumberOfLinks = ${ props.paginationConfig.numberOfLinks }
3333
totalCount = ${ collection.totalCount }
3434
itemsPerPage = ${ props.limit }
3535
paginationConfig = ${ props.paginationConfig }

Resources/Private/Fusion/Component/Pagination.fusion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ prototype(Breadlesscode.Listable:Pagination) < prototype(Neos.Fusion:Component)
2626
additionalParams = Neos.Fusion:DataStructure {
2727
currentPage = ${ item.page }
2828
}
29-
@if.noSperator = ${ item.page }
29+
@if.noSeparator = ${ item.page }
3030
}
3131
label = ${ item.label }
3232
typeClass = ${ item.type }

Resources/Private/Fusion/Component/PaginationConfig.fusion

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ prototype(Breadlesscode.Listable:PaginationConfig) < prototype(Neos.Fusion:DataS
33
showNextAndPrevious = ${ Configuration.setting('Breadlesscode.Listable.pagination.showNextAndPrevious') }
44
alwaysShowNextAndPrevious = ${ Configuration.setting('Breadlesscode.Listable.pagination.alwaysShowNextAndPrevious') }
55
showFirstAndLast = ${ Configuration.setting('Breadlesscode.Listable.pagination.showFirstAndLast') }
6+
showFirstAndLastNumeric = ${ Configuration.setting('Breadlesscode.Listable.pagination.showFirstAndLastNumeric') }
67
alwaysShowFirstAndLast = ${ Configuration.setting('Breadlesscode.Listable.pagination.alwaysShowFirstAndLast') }
8+
numberOfLinks = ${ Configuration.setting('Breadlesscode.Listable.pagination.numberOfLinks') }
79

810
labels = Neos.Fusion:DataStructure {
911
seperator = ${ Configuration.setting('Breadlesscode.Listable.pagination.labels.seperator') }

0 commit comments

Comments
 (0)