1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
/* |
|
|
|
|
4
|
|
|
The MIT License (MIT) |
|
|
|
|
5
|
|
|
|
6
|
|
|
Copyright (c) 2014 Jason Grimes |
|
|
|
|
7
|
|
|
|
8
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
|
|
|
9
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
|
|
|
10
|
|
|
in the Software without restriction, including without limitation the rights |
|
|
|
|
11
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
|
|
|
12
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
|
|
|
13
|
|
|
furnished to do so, subject to the following conditions: |
|
|
|
|
14
|
|
|
|
15
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
|
|
|
16
|
|
|
copies or substantial portions of the Software. |
|
|
|
|
17
|
|
|
|
18
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
|
|
|
19
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
|
|
|
20
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
|
|
|
21
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
|
|
|
22
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
|
|
|
23
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
|
|
|
24
|
|
|
SOFTWARE. |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Paginator.php - Jaxon Paginator |
29
|
|
|
* |
30
|
|
|
* Create pagination links from a Jaxon request and a data array. |
31
|
|
|
* |
32
|
|
|
* @package jaxon-core |
33
|
|
|
* @author Jason Grimes |
34
|
|
|
* @author Thierry Feuzeu |
35
|
|
|
* @copyright 2014 Jason Grimes |
36
|
|
|
* @copyright 2016 Thierry Feuzeu |
37
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
38
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
namespace Jaxon\Plugin\Response\Paginator; |
42
|
|
|
|
43
|
|
|
use function array_walk; |
44
|
|
|
use function ceil; |
45
|
|
|
use function floor; |
46
|
|
|
use function max; |
47
|
|
|
use function trim; |
48
|
|
|
|
49
|
|
|
class Paginator |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var integer |
53
|
|
|
*/ |
54
|
|
|
protected $nTotalItems = 0; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var integer |
58
|
|
|
*/ |
59
|
|
|
protected $nTotalPages = 0; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
protected $nItemsPerPage = 0; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var integer |
68
|
|
|
*/ |
69
|
|
|
protected $nCurrentPage = 0; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var integer |
73
|
|
|
*/ |
74
|
|
|
protected $nMaxPages = 10; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string |
78
|
|
|
*/ |
79
|
|
|
protected $sPreviousText = '«'; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $sNextText = '»'; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
protected $sEllipsysText = '...'; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
protected $sWrapperId = ''; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The constructor. |
98
|
|
|
* |
99
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
100
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
101
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public function __construct(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->setup($nCurrentPage, $nItemsPerPage, $nTotalItems); |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Setup the paginator |
110
|
|
|
* |
111
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
112
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
113
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
114
|
|
|
* |
115
|
|
|
* @return Paginator |
116
|
|
|
*/ |
117
|
|
|
public function setup(int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator |
118
|
|
|
{ |
119
|
|
|
return $this->setCurrentPage($nCurrentPage) |
120
|
|
|
->setItemsPerPage($nItemsPerPage) |
|
|
|
|
121
|
|
|
->setTotalItems($nTotalItems); |
|
|
|
|
122
|
|
|
} |
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the text for the previous page link |
126
|
|
|
* |
127
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return Paginator |
130
|
|
|
*/ |
131
|
|
|
public function setPreviousText(string $sText): Paginator |
132
|
|
|
{ |
133
|
|
|
$this->sPreviousText = $sText; |
134
|
|
|
return $this; |
135
|
|
|
} |
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the text for the next page link |
139
|
|
|
* |
140
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
141
|
|
|
* |
142
|
|
|
* @return Paginator |
143
|
|
|
*/ |
144
|
|
|
public function setNextText(string $sText): Paginator |
145
|
|
|
{ |
146
|
|
|
$this->sNextText = $sText; |
147
|
|
|
return $this; |
148
|
|
|
} |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Update the number of pages |
152
|
|
|
* |
153
|
|
|
* @return Paginator |
154
|
|
|
*/ |
155
|
|
|
protected function updateTotalPages(): Paginator |
156
|
|
|
{ |
157
|
|
|
$this->nTotalPages = ($this->nItemsPerPage === 0 ? 0 : |
|
|
|
|
158
|
|
|
(int)ceil($this->nTotalItems / $this->nItemsPerPage)); |
159
|
|
|
return $this; |
160
|
|
|
} |
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the max number of pages to show |
164
|
|
|
* |
165
|
|
|
* @param int $nMaxPages The max number of pages to show |
|
|
|
|
166
|
|
|
* |
167
|
|
|
* @return Paginator |
168
|
|
|
*/ |
169
|
|
|
public function setMaxPages(int $nMaxPages): Paginator |
170
|
|
|
{ |
171
|
|
|
$this->nMaxPages = max($nMaxPages, 4); |
172
|
|
|
return $this; |
173
|
|
|
} |
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the current page number |
177
|
|
|
* |
178
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
179
|
|
|
* |
180
|
|
|
* @return Paginator |
181
|
|
|
*/ |
182
|
|
|
protected function setCurrentPage(int $nCurrentPage): Paginator |
183
|
|
|
{ |
184
|
|
|
$this->nCurrentPage = $nCurrentPage; |
185
|
|
|
return $this; |
186
|
|
|
} |
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set the number of items per page |
190
|
|
|
* |
191
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return Paginator |
194
|
|
|
*/ |
195
|
|
|
protected function setItemsPerPage(int $nItemsPerPage): Paginator |
196
|
|
|
{ |
197
|
|
|
$this->nItemsPerPage = $nItemsPerPage; |
198
|
|
|
return $this->updateTotalPages(); |
199
|
|
|
} |
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the total number of items |
203
|
|
|
* |
204
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
205
|
|
|
* |
206
|
|
|
* @return Paginator |
207
|
|
|
*/ |
208
|
|
|
protected function setTotalItems(int $nTotalItems): Paginator |
209
|
|
|
{ |
210
|
|
|
$this->nTotalItems = $nTotalItems; |
211
|
|
|
return $this->updateTotalPages(); |
212
|
|
|
} |
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get the previous page data. |
216
|
|
|
* |
217
|
|
|
* @return Page |
218
|
|
|
*/ |
219
|
|
|
protected function getPrevPage(): Page |
220
|
|
|
{ |
221
|
|
|
return $this->nCurrentPage <= 1 ? |
|
|
|
|
222
|
|
|
new Page('disabled', $this->sPreviousText, 0) : |
|
|
|
|
223
|
|
|
new Page('enabled', $this->sPreviousText, $this->nCurrentPage - 1); |
224
|
|
|
} |
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the next page data. |
228
|
|
|
* |
229
|
|
|
* @return Page |
230
|
|
|
*/ |
231
|
|
|
protected function getNextPage(): Page |
232
|
|
|
{ |
233
|
|
|
return $this->nCurrentPage >= $this->nTotalPages ? |
|
|
|
|
234
|
|
|
new Page('disabled', $this->sNextText, 0) : |
|
|
|
|
235
|
|
|
new Page('enabled', $this->sNextText, $this->nCurrentPage + 1); |
236
|
|
|
} |
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get a page data. |
240
|
|
|
* |
241
|
|
|
* @param integer $nNumber The page number |
|
|
|
|
242
|
|
|
* |
243
|
|
|
* @return Page |
244
|
|
|
*/ |
245
|
|
|
protected function getPage(int $nNumber): Page |
246
|
|
|
{ |
247
|
|
|
if($nNumber < 1) |
248
|
|
|
{ |
249
|
|
|
return new Page('disabled', $this->sEllipsysText, 0); |
250
|
|
|
} |
251
|
|
|
$sType = ($nNumber === $this->nCurrentPage ? 'current' : 'enabled'); |
252
|
|
|
return new Page($sType, "$nNumber", $nNumber); |
253
|
|
|
} |
|
|
|
|
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the array of page numbers to be printed. |
257
|
|
|
* |
258
|
|
|
* Example: [1, 0, 4, 5, 6, 0, 10] |
259
|
|
|
* |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
protected function getPageNumbers(): array |
263
|
|
|
{ |
264
|
|
|
$aPageNumbers = []; |
265
|
|
|
|
266
|
|
|
if($this->nTotalPages <= $this->nMaxPages) |
267
|
|
|
{ |
268
|
|
|
for($i = 0; $i < $this->nTotalPages; $i++) |
269
|
|
|
{ |
270
|
|
|
$aPageNumbers[] = $i + 1; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $aPageNumbers; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
// Determine the sliding range, centered around the current page. |
277
|
|
|
$nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2); |
278
|
|
|
|
279
|
|
|
$nSlidingStart = 1; |
|
|
|
|
280
|
|
|
$nSlidingEndOffset = $nNumAdjacents + 3 - $this->nCurrentPage; |
281
|
|
|
if($nSlidingEndOffset < 0) |
282
|
|
|
{ |
283
|
|
|
$nSlidingStart = $this->nCurrentPage - $nNumAdjacents; |
|
|
|
|
284
|
|
|
$nSlidingEndOffset = 0; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$nSlidingEnd = $this->nTotalPages; |
|
|
|
|
288
|
|
|
$nSlidingStartOffset = $this->nCurrentPage + $nNumAdjacents + 2 - $this->nTotalPages; |
289
|
|
|
if($nSlidingStartOffset < 0) |
290
|
|
|
{ |
291
|
|
|
$nSlidingEnd = $this->nCurrentPage + $nNumAdjacents; |
|
|
|
|
292
|
|
|
$nSlidingStartOffset = 0; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// Build the list of page numbers. |
296
|
|
|
if($nSlidingStart > 1) |
297
|
|
|
{ |
298
|
|
|
$aPageNumbers[] = 1; |
299
|
|
|
$aPageNumbers[] = 0; // Ellipsys; |
300
|
|
|
} |
301
|
|
|
for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
302
|
|
|
{ |
303
|
|
|
$aPageNumbers[] = $i; |
304
|
|
|
} |
305
|
|
|
if($nSlidingEnd < $this->nTotalPages) |
306
|
|
|
{ |
307
|
|
|
$aPageNumbers[] = 0; // Ellipsys; |
308
|
|
|
$aPageNumbers[] = $this->nTotalPages; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return $aPageNumbers; |
312
|
|
|
} |
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Get the links (pages raw data). |
316
|
|
|
* |
317
|
|
|
* @return array<Page> |
318
|
|
|
*/ |
319
|
|
|
public function pages(): array |
320
|
|
|
{ |
321
|
|
|
if($this->nTotalPages < 2) |
322
|
|
|
{ |
323
|
|
|
return []; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
$aPageNumbers = $this->getPageNumbers(); |
327
|
|
|
$aPages = [$this->getPrevPage()]; |
|
|
|
|
328
|
|
|
array_walk($aPageNumbers, function($nNumber) use(&$aPages) { |
329
|
|
|
$aPages[] = $this->getPage($nNumber); |
330
|
|
|
}); |
331
|
|
|
$aPages[] = $this->getNextPage(); |
332
|
|
|
|
333
|
|
|
return $aPages; |
334
|
|
|
} |
|
|
|
|
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Get the pagination wrapper HTML |
338
|
|
|
* |
339
|
|
|
* @param string $sWrapperId The pagination wrapper id |
|
|
|
|
340
|
|
|
* |
341
|
|
|
* @return string |
342
|
|
|
*/ |
343
|
|
|
public function wrapper(string $sWrapperId): string |
344
|
|
|
{ |
345
|
|
|
$this->sWrapperId = trim($sWrapperId); |
346
|
|
|
|
347
|
|
|
return '<nav id="' . $this->sWrapperId . '"></nav>'; |
348
|
|
|
} |
|
|
|
|
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get the pagination wrapper id |
352
|
|
|
* |
353
|
|
|
* @return string |
354
|
|
|
*/ |
355
|
|
|
public function wrapperId(): string |
356
|
|
|
{ |
357
|
|
|
return $this->sWrapperId; |
358
|
|
|
} |
|
|
|
|
359
|
|
|
} |
360
|
|
|
|