|
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 an 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\Utils\Pagination; |
|
42
|
|
|
|
|
43
|
|
|
use Jaxon\Request\Factory\Request; |
|
44
|
|
|
use Jaxon\Request\Factory\Parameter; |
|
45
|
|
|
|
|
46
|
|
|
class Paginator |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
/** |
|
49
|
|
|
* @var integer |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $totalItems = 0; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var integer |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $totalPages = 0; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var integer |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $itemsPerPage = 0; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var integer |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $currentPage = 0; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var integer |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $maxPagesToShow = 10; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The pagination renderer |
|
75
|
|
|
* |
|
76
|
|
|
* @var Renderer |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $xRenderer = null; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The constructor |
|
82
|
|
|
* |
|
83
|
|
|
* @param Renderer $xRenderer |
|
|
|
|
|
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct(Renderer $xRenderer) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->xRenderer = $xRenderer; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the text for the previous page link |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $text The text for the previous page link |
|
94
|
|
|
* |
|
95
|
|
|
* @return Paginator |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setPreviousText($text) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->xRenderer->setPreviousText($text); |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Set the text for the next page link |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $text The text for the previous page link |
|
107
|
|
|
* |
|
108
|
|
|
* @return Paginator |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setNextText($text) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->xRenderer->setNextText($text); |
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Update the number of pages |
|
118
|
|
|
* |
|
119
|
|
|
* @return Paginator |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function updateTotalPages() |
|
122
|
|
|
{ |
|
123
|
|
|
$this->totalPages = ($this->itemsPerPage == 0 ? 0 : (int)ceil($this->totalItems / $this->itemsPerPage)); |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Set the max number of pages to show |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $maxPagesToShow The max number of pages to show |
|
131
|
|
|
* |
|
132
|
|
|
* @return Paginator |
|
133
|
|
|
* @throws \InvalidArgumentException if $maxPagesToShow is less than 3. |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setMaxPagesToShow($maxPagesToShow) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->maxPagesToShow = intval($maxPagesToShow); |
|
138
|
|
|
if($this->maxPagesToShow < 4) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->maxPagesToShow = 10; |
|
141
|
|
|
throw new \InvalidArgumentException('maxPagesToShow cannot be less than 4.'); |
|
142
|
|
|
} |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the current page number |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $currentPage The current page number |
|
150
|
|
|
* |
|
151
|
|
|
* @return Paginator |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setCurrentPage($currentPage) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->currentPage = intval($currentPage); |
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set the number of items per page |
|
161
|
|
|
* |
|
162
|
|
|
* @param int $itemsPerPage The number of items per page |
|
163
|
|
|
* |
|
164
|
|
|
* @return Paginator |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setItemsPerPage($itemsPerPage) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->itemsPerPage = intval($itemsPerPage); |
|
169
|
|
|
return $this->updateTotalPages(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Set the total number of items |
|
174
|
|
|
* |
|
175
|
|
|
* @param int $totalItems The total number of items |
|
176
|
|
|
* |
|
177
|
|
|
* @return Paginator |
|
178
|
|
|
*/ |
|
179
|
|
|
public function setTotalItems($totalItems) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->totalItems = intval($totalItems); |
|
182
|
|
|
return $this->updateTotalPages(); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Setup the paginator |
|
187
|
|
|
* |
|
188
|
|
|
* @param int $totalItems The total number of items |
|
|
|
|
|
|
189
|
|
|
* @param int $itemsPerPage The number of items per page |
|
|
|
|
|
|
190
|
|
|
* @param int $currentPage The current page number |
|
|
|
|
|
|
191
|
|
|
* @param Request $xRequest The request to be paginated |
|
|
|
|
|
|
192
|
|
|
* |
|
193
|
|
|
* @return Paginator |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setup($totalItems, $itemsPerPage, $currentPage, $xRequest) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->setTotalItems($totalItems) |
|
198
|
|
|
->setItemsPerPage($itemsPerPage) |
|
|
|
|
|
|
199
|
|
|
->setCurrentPage($currentPage); |
|
|
|
|
|
|
200
|
|
|
$this->xRenderer->setRequest($xRequest); |
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Get an array of paginated page data. |
|
206
|
|
|
* |
|
207
|
|
|
* Example: [1, 0, 4, 5, 6, 0, 10] |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getPageNumbers() |
|
212
|
|
|
{ |
|
213
|
|
|
$pageNumbers = []; |
|
214
|
|
|
|
|
215
|
|
|
if($this->totalPages <= $this->maxPagesToShow) |
|
216
|
|
|
{ |
|
217
|
|
|
for($i = 0; $i < $this->totalPages; $i++) |
|
218
|
|
|
{ |
|
219
|
|
|
$pageNumbers[] = $i + 1; |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
else |
|
223
|
|
|
{ |
|
224
|
|
|
// Determine the sliding range, centered around the current page. |
|
225
|
|
|
$numAdjacents = (int)floor(($this->maxPagesToShow - 4) / 2); |
|
226
|
|
|
|
|
227
|
|
|
$slidingStart = 1; |
|
|
|
|
|
|
228
|
|
|
$slidingEndOffset = $numAdjacents + 3 - $this->currentPage; |
|
229
|
|
|
if($slidingEndOffset < 0) |
|
230
|
|
|
{ |
|
231
|
|
|
$slidingStart = $this->currentPage - $numAdjacents; |
|
|
|
|
|
|
232
|
|
|
$slidingEndOffset = 0; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$slidingEnd = $this->totalPages; |
|
|
|
|
|
|
236
|
|
|
$slidingStartOffset = $this->currentPage + $numAdjacents + 2 - $this->totalPages; |
|
237
|
|
|
if($slidingStartOffset < 0) |
|
238
|
|
|
{ |
|
239
|
|
|
$slidingEnd = $this->currentPage + $numAdjacents; |
|
|
|
|
|
|
240
|
|
|
$slidingStartOffset = 0; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
// Build the list of page numbers. |
|
244
|
|
|
if($slidingStart > 1) |
|
245
|
|
|
{ |
|
246
|
|
|
$pageNumbers[] = 1; |
|
247
|
|
|
$pageNumbers[] = 0; // Ellipsys; |
|
248
|
|
|
} |
|
249
|
|
|
for($i = $slidingStart - $slidingStartOffset; $i <= $slidingEnd + $slidingEndOffset; $i++) |
|
250
|
|
|
{ |
|
251
|
|
|
$pageNumbers[] = $i; |
|
252
|
|
|
} |
|
253
|
|
|
if($slidingEnd < $this->totalPages) |
|
254
|
|
|
{ |
|
255
|
|
|
$pageNumbers[] = 0; // Ellipsys; |
|
256
|
|
|
$pageNumbers[] = $this->totalPages; |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
return $pageNumbers; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Render an HTML pagination control. |
|
265
|
|
|
* |
|
266
|
|
|
* @return string |
|
267
|
|
|
*/ |
|
268
|
|
|
public function render() |
|
269
|
|
|
{ |
|
270
|
|
|
if($this->totalPages > 1) |
|
271
|
|
|
{ |
|
272
|
|
|
return $this->xRenderer->render($this->getPageNumbers(), $this->currentPage, $this->totalPages); |
|
273
|
|
|
} |
|
274
|
|
|
return ''; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Render an HTML pagination control. |
|
279
|
|
|
* |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
public function __toString() |
|
283
|
|
|
{ |
|
284
|
|
|
return $this->render(); |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|