|
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\Ui\Pagination; |
|
42
|
|
|
|
|
43
|
|
|
use Jaxon\Request\Call\Call; |
|
44
|
|
|
use Jaxon\Ui\View\Store; |
|
45
|
|
|
|
|
46
|
|
|
use function array_map; |
|
47
|
|
|
use function ceil; |
|
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
|
|
|
* The pagination renderer |
|
68
|
|
|
* |
|
69
|
|
|
* @var PaginationRenderer |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $xRenderer = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* The constructor |
|
75
|
|
|
* |
|
76
|
|
|
* @param PaginationRenderer $xRenderer |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
public function __construct(PaginationRenderer $xRenderer) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$this->xRenderer = $xRenderer; |
|
81
|
|
|
} |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the text for the previous page link |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
|
|
87
|
|
|
* |
|
88
|
|
|
* @return Paginator |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setPreviousText(string $sText): Paginator |
|
91
|
|
|
{ |
|
92
|
|
|
$this->xRenderer->setPreviousText($sText); |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set the text for the next page link |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $sText The text for the previous page link |
|
|
|
|
|
|
100
|
|
|
* |
|
101
|
|
|
* @return Paginator |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setNextText(string $sText): Paginator |
|
104
|
|
|
{ |
|
105
|
|
|
$this->xRenderer->setNextText($sText); |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Update the number of pages |
|
111
|
|
|
* |
|
112
|
|
|
* @return Paginator |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function updateTotalPages(): Paginator |
|
115
|
|
|
{ |
|
116
|
|
|
$this->nTotalPages = ($this->nItemsPerPage === 0 ? 0 : (int)ceil($this->nTotalItems / $this->nItemsPerPage)); |
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set the max number of pages to show |
|
122
|
|
|
* |
|
123
|
|
|
* @param int $nMaxPagesToShow The max number of pages to show |
|
|
|
|
|
|
124
|
|
|
* |
|
125
|
|
|
* @return Paginator |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setMaxPagesToShow(int $nMaxPagesToShow): Paginator |
|
128
|
|
|
{ |
|
129
|
|
|
$this->xRenderer->setMaxPagesToShow($nMaxPagesToShow); |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Set the current page number |
|
135
|
|
|
* |
|
136
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
|
|
137
|
|
|
* |
|
138
|
|
|
* @return Paginator |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function setCurrentPage(int $nCurrentPage): Paginator |
|
141
|
|
|
{ |
|
142
|
|
|
$this->xRenderer->setCurrentPage($nCurrentPage); |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set the number of items per page |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
|
|
150
|
|
|
* |
|
151
|
|
|
* @return Paginator |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function setItemsPerPage(int $nItemsPerPage): Paginator |
|
154
|
|
|
{ |
|
155
|
|
|
$this->nItemsPerPage = $nItemsPerPage; |
|
156
|
|
|
return $this->updateTotalPages(); |
|
157
|
|
|
} |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Set the total number of items |
|
161
|
|
|
* |
|
162
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
|
|
163
|
|
|
* |
|
164
|
|
|
* @return Paginator |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function setTotalItems(int $nTotalItems): Paginator |
|
167
|
|
|
{ |
|
168
|
|
|
$this->nTotalItems = $nTotalItems; |
|
169
|
|
|
return $this->updateTotalPages(); |
|
170
|
|
|
} |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Setup the paginator |
|
174
|
|
|
* |
|
175
|
|
|
* @param Call $xCall The call to be paginated |
|
|
|
|
|
|
176
|
|
|
* @param int $nCurrentPage The current page number |
|
|
|
|
|
|
177
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
|
|
|
|
|
178
|
|
|
* @param int $nTotalItems The total number of items |
|
|
|
|
|
|
179
|
|
|
* |
|
180
|
|
|
* @return Paginator |
|
181
|
|
|
*/ |
|
182
|
|
|
public function setup(Call $xCall, int $nCurrentPage, int $nItemsPerPage, int $nTotalItems): Paginator |
|
183
|
|
|
{ |
|
184
|
|
|
$this->setCurrentPage($nCurrentPage)->setItemsPerPage($nItemsPerPage)->setTotalItems($nTotalItems); |
|
185
|
|
|
$this->xRenderer->setRequest($xCall); |
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get the pages. |
|
191
|
|
|
* |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getPages(): array |
|
195
|
|
|
{ |
|
196
|
|
|
return array_map(function($aPage) { |
|
197
|
|
|
return (object)['type' => $aPage[0], 'text' => $aPage[1], 'call' => $aPage[2]]; |
|
198
|
|
|
}, $this->xRenderer->getPages($this->nTotalPages)); |
|
199
|
|
|
} |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Render an HTML pagination control. |
|
203
|
|
|
* |
|
204
|
|
|
* @return null|Store |
|
205
|
|
|
*/ |
|
206
|
|
|
public function render(): ?Store |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->xRenderer->render($this->nTotalPages); |
|
209
|
|
|
} |
|
|
|
|
|
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Render an HTML pagination control. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
public function __toString() |
|
217
|
|
|
{ |
|
218
|
|
|
if($this->nTotalPages < 2) |
|
219
|
|
|
{ |
|
220
|
|
|
return ''; |
|
221
|
|
|
} |
|
222
|
|
|
return $this->render()->__toString(); |
|
223
|
|
|
} |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|