|
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\Pagination; |
|
42
|
|
|
|
|
43
|
|
|
use Jaxon\Request\Call\Call; |
|
44
|
|
|
|
|
45
|
|
|
use function array_walk; |
|
46
|
|
|
use function ceil; |
|
47
|
|
|
use function floor; |
|
48
|
|
|
use function max; |
|
49
|
|
|
|
|
50
|
|
|
class Paginator |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* @var integer |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $nTotalItems = 0; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var integer |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $nTotalPages = 0; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var integer |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $nItemsPerPage = 0; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var integer |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $nCurrentPage = 0; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var integer |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $nMaxPages = 10; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var string |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $sPreviousText = '«'; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var string |
|
84
|
|
|
*/ |
|
85
|
|
|
protected $sNextText = '»'; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $sEllipsysText = '...'; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @var PaginatorPlugin |
|
94
|
|
|
*/ |
|
95
|
|
|
private $xPlugin; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* The constructor. |
|
99
|
|
|
* |
|
100
|
|
|
* @param PaginatorPlugin $xPlugin |
|
|
|
|
|
|
101
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
|
|
102
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
|
|
103
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
|
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(PaginatorPlugin $xPlugin, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$this->xPlugin = $xPlugin; |
|
108
|
|
|
$this->setCurrentPage($nCurrentPage) |
|
109
|
|
|
->setItemsPerPage($nItemsPerPage) |
|
|
|
|
|
|
110
|
|
|
->setTotalItems($nTotalItems); |
|
|
|
|
|
|
111
|
|
|
} |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Render the paginator |
|
115
|
|
|
* |
|
116
|
|
|
* @param Call $xCall |
|
|
|
|
|
|
117
|
|
|
* @param string $sWrapperId |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function paginate(Call $xCall, string $sWrapperId) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->xPlugin->render($this, $xCall, $sWrapperId); |
|
124
|
|
|
} |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Set the text for the previous page link |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
|
|
130
|
|
|
* |
|
131
|
|
|
* @return Paginator |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setPreviousText(string $sText): Paginator |
|
134
|
|
|
{ |
|
135
|
|
|
$this->sPreviousText = $sText; |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the text for the next page link |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
|
|
143
|
|
|
* |
|
144
|
|
|
* @return Paginator |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setNextText(string $sText): Paginator |
|
147
|
|
|
{ |
|
148
|
|
|
$this->sNextText = $sText; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Update the number of pages |
|
154
|
|
|
* |
|
155
|
|
|
* @return Paginator |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function updateTotalPages(): Paginator |
|
158
|
|
|
{ |
|
159
|
|
|
$this->nTotalPages = ($this->nItemsPerPage === 0 ? 0 : |
|
|
|
|
|
|
160
|
|
|
(int)ceil($this->nTotalItems / $this->nItemsPerPage)); |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Set the max number of pages to show |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $nMaxPages The max number of pages to show |
|
|
|
|
|
|
168
|
|
|
* |
|
169
|
|
|
* @return Paginator |
|
170
|
|
|
*/ |
|
171
|
|
|
public function setMaxPages(int $nMaxPages): Paginator |
|
172
|
|
|
{ |
|
173
|
|
|
$this->nMaxPages = max($nMaxPages, 4); |
|
174
|
|
|
return $this; |
|
175
|
|
|
} |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set the current page number |
|
179
|
|
|
* |
|
180
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
|
|
181
|
|
|
* |
|
182
|
|
|
* @return Paginator |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function setCurrentPage(int $nCurrentPage): Paginator |
|
185
|
|
|
{ |
|
186
|
|
|
$this->nCurrentPage = $nCurrentPage; |
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Set the number of items per page |
|
192
|
|
|
* |
|
193
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
|
|
194
|
|
|
* |
|
195
|
|
|
* @return Paginator |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function setItemsPerPage(int $nItemsPerPage): Paginator |
|
198
|
|
|
{ |
|
199
|
|
|
$this->nItemsPerPage = $nItemsPerPage; |
|
200
|
|
|
return $this->updateTotalPages(); |
|
201
|
|
|
} |
|
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Set the total number of items |
|
205
|
|
|
* |
|
206
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
|
|
207
|
|
|
* |
|
208
|
|
|
* @return Paginator |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function setTotalItems(int $nTotalItems): Paginator |
|
211
|
|
|
{ |
|
212
|
|
|
$this->nTotalItems = $nTotalItems; |
|
213
|
|
|
return $this->updateTotalPages(); |
|
214
|
|
|
} |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get the previous page data. |
|
218
|
|
|
* |
|
219
|
|
|
* @return Page |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function getPrevPage(): Page |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->nCurrentPage <= 1 ? |
|
|
|
|
|
|
224
|
|
|
new Page('disabled', $this->sPreviousText, 0) : |
|
|
|
|
|
|
225
|
|
|
new Page('enabled', $this->sPreviousText, $this->nCurrentPage - 1); |
|
226
|
|
|
} |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the next page data. |
|
230
|
|
|
* |
|
231
|
|
|
* @return Page |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getNextPage(): Page |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->nCurrentPage >= $this->nTotalPages ? |
|
|
|
|
|
|
236
|
|
|
new Page('disabled', $this->sNextText, 0) : |
|
|
|
|
|
|
237
|
|
|
new Page('enabled', $this->sNextText, $this->nCurrentPage + 1); |
|
238
|
|
|
} |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get a page data. |
|
242
|
|
|
* |
|
243
|
|
|
* @param integer $nNumber The page number |
|
|
|
|
|
|
244
|
|
|
* |
|
245
|
|
|
* @return Page |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getPage(int $nNumber): Page |
|
248
|
|
|
{ |
|
249
|
|
|
if($nNumber < 1) |
|
250
|
|
|
{ |
|
251
|
|
|
return new Page('disabled', $this->sEllipsysText, 0); |
|
252
|
|
|
} |
|
253
|
|
|
$sType = ($nNumber === $this->nCurrentPage ? 'current' : 'enabled'); |
|
254
|
|
|
return new Page($sType, "$nNumber", $nNumber); |
|
255
|
|
|
} |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get the array of page numbers to be printed. |
|
259
|
|
|
* |
|
260
|
|
|
* Example: [1, 0, 4, 5, 6, 0, 10] |
|
261
|
|
|
* |
|
262
|
|
|
* @return array |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function getPageNumbers(): array |
|
265
|
|
|
{ |
|
266
|
|
|
$aPageNumbers = []; |
|
267
|
|
|
|
|
268
|
|
|
if($this->nTotalPages <= $this->nMaxPages) |
|
269
|
|
|
{ |
|
270
|
|
|
for($i = 0; $i < $this->nTotalPages; $i++) |
|
271
|
|
|
{ |
|
272
|
|
|
$aPageNumbers[] = $i + 1; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return $aPageNumbers; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
// Determine the sliding range, centered around the current page. |
|
279
|
|
|
$nNumAdjacents = (int)floor(($this->nMaxPages - 4) / 2); |
|
280
|
|
|
|
|
281
|
|
|
$nSlidingStart = 1; |
|
|
|
|
|
|
282
|
|
|
$nSlidingEndOffset = $nNumAdjacents + 3 - $this->nCurrentPage; |
|
283
|
|
|
if($nSlidingEndOffset < 0) |
|
284
|
|
|
{ |
|
285
|
|
|
$nSlidingStart = $this->nCurrentPage - $nNumAdjacents; |
|
|
|
|
|
|
286
|
|
|
$nSlidingEndOffset = 0; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
$nSlidingEnd = $this->nTotalPages; |
|
|
|
|
|
|
290
|
|
|
$nSlidingStartOffset = $this->nCurrentPage + $nNumAdjacents + 2 - $this->nTotalPages; |
|
291
|
|
|
if($nSlidingStartOffset < 0) |
|
292
|
|
|
{ |
|
293
|
|
|
$nSlidingEnd = $this->nCurrentPage + $nNumAdjacents; |
|
|
|
|
|
|
294
|
|
|
$nSlidingStartOffset = 0; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
// Build the list of page numbers. |
|
298
|
|
|
if($nSlidingStart > 1) |
|
299
|
|
|
{ |
|
300
|
|
|
$aPageNumbers[] = 1; |
|
301
|
|
|
$aPageNumbers[] = 0; // Ellipsys; |
|
302
|
|
|
} |
|
303
|
|
|
for($i = $nSlidingStart - $nSlidingStartOffset; $i <= $nSlidingEnd + $nSlidingEndOffset; $i++) |
|
304
|
|
|
{ |
|
305
|
|
|
$aPageNumbers[] = $i; |
|
306
|
|
|
} |
|
307
|
|
|
if($nSlidingEnd < $this->nTotalPages) |
|
308
|
|
|
{ |
|
309
|
|
|
$aPageNumbers[] = 0; // Ellipsys; |
|
310
|
|
|
$aPageNumbers[] = $this->nTotalPages; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $aPageNumbers; |
|
314
|
|
|
} |
|
|
|
|
|
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Get the links (pages raw data). |
|
318
|
|
|
* |
|
319
|
|
|
* @return array<Page> |
|
320
|
|
|
*/ |
|
321
|
|
|
public function pages(): array |
|
322
|
|
|
{ |
|
323
|
|
|
if($this->nTotalPages < 2) |
|
324
|
|
|
{ |
|
325
|
|
|
return []; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$aPageNumbers = $this->getPageNumbers(); |
|
329
|
|
|
$aPages = [$this->getPrevPage()]; |
|
|
|
|
|
|
330
|
|
|
array_walk($aPageNumbers, function($nNumber) use(&$aPages) { |
|
331
|
|
|
$aPages[] = $this->getPage($nNumber); |
|
332
|
|
|
}); |
|
333
|
|
|
$aPages[] = $this->getNextPage(); |
|
334
|
|
|
|
|
335
|
|
|
return $aPages; |
|
336
|
|
|
} |
|
|
|
|
|
|
337
|
|
|
} |
|
338
|
|
|
|