22
33namespace Breadlesscode \Listable \Fusion ;
44
5- use Neos \Flow \Annotations as Flow ;
65use Neos \Fusion \FusionObjects \AbstractFusionObject ;
76use Neos \Fusion \Core \Runtime ;
87
98class 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}
0 commit comments