1 | <?php |
||
20 | class Paginator |
||
21 | { |
||
22 | /** |
||
23 | * Container instance. |
||
24 | * |
||
25 | * @var \Pimple\Container |
||
26 | */ |
||
27 | private $container; |
||
28 | |||
29 | /** |
||
30 | * Total number of items. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | private $total = 0; |
||
35 | |||
36 | /** |
||
37 | * Page number. |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $page = 1; |
||
42 | |||
43 | /** |
||
44 | * Offset. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | private $offset = 0; |
||
49 | |||
50 | /** |
||
51 | * Limit. |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | private $limit = 0; |
||
56 | |||
57 | /** |
||
58 | * Sort by this column. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | private $order = ''; |
||
63 | |||
64 | /** |
||
65 | * Sorting direction. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $direction = 'ASC'; |
||
70 | |||
71 | /** |
||
72 | * Slice of items. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | private $items = []; |
||
77 | |||
78 | /** |
||
79 | * PicoDb Table instance. |
||
80 | * |
||
81 | * @var \Picodb\Table |
||
82 | */ |
||
83 | private $query = null; |
||
84 | |||
85 | /** |
||
86 | * Controller name. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $controller = ''; |
||
91 | |||
92 | /** |
||
93 | * Action name. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $action = ''; |
||
98 | |||
99 | /** |
||
100 | * Url params. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | private $params = []; |
||
105 | |||
106 | /** |
||
107 | * Constructor. |
||
108 | * |
||
109 | * @param \Pimple\Container $container |
||
110 | */ |
||
111 | public function __construct(Container $container) |
||
115 | |||
116 | /** |
||
117 | * Set a PicoDb query. |
||
118 | * |
||
119 | * @param \PicoDb\Table |
||
120 | * |
||
121 | * @return Paginator |
||
122 | */ |
||
123 | public function setQuery(Table $query) |
||
130 | |||
131 | /** |
||
132 | * Execute a PicoDb query. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function executeQuery() |
||
148 | |||
149 | /** |
||
150 | * Set url parameters. |
||
151 | * |
||
152 | * @param string $controller |
||
153 | * @param string $action |
||
154 | * @param array $params |
||
155 | * |
||
156 | * @return Paginator |
||
157 | */ |
||
158 | public function setUrl($controller, $action, array $params = []) |
||
166 | |||
167 | /** |
||
168 | * Add manually items. |
||
169 | * |
||
170 | * @param array $items |
||
171 | * |
||
172 | * @return Paginator |
||
173 | */ |
||
174 | public function setCollection(array $items) |
||
180 | |||
181 | /** |
||
182 | * Return the items. |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | public function getCollection() |
||
190 | |||
191 | /** |
||
192 | * Set the total number of items. |
||
193 | * |
||
194 | * @param int $total |
||
195 | * |
||
196 | * @return Paginator |
||
197 | */ |
||
198 | public function setTotal($total) |
||
204 | |||
205 | /** |
||
206 | * Get the total number of items. |
||
207 | * |
||
208 | * @return int |
||
209 | */ |
||
210 | public function getTotal() |
||
214 | |||
215 | /** |
||
216 | * Set the default page number. |
||
217 | * |
||
218 | * @param int $page |
||
219 | * |
||
220 | * @return Paginator |
||
221 | */ |
||
222 | public function setPage($page) |
||
228 | |||
229 | /** |
||
230 | * Get the number of current page. |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | public function getPage() |
||
238 | |||
239 | /** |
||
240 | * Get the total number of pages. |
||
241 | * |
||
242 | * @return int |
||
243 | */ |
||
244 | public function getPageTotal() |
||
248 | |||
249 | /** |
||
250 | * Set the default column order. |
||
251 | * |
||
252 | * @param string $order |
||
253 | * |
||
254 | * @return Paginator |
||
255 | */ |
||
256 | public function setOrder($order) |
||
262 | |||
263 | /** |
||
264 | * Set the default sorting direction. |
||
265 | * |
||
266 | * @param string $direction |
||
267 | * |
||
268 | * @return Paginator |
||
269 | */ |
||
270 | public function setDirection($direction) |
||
276 | |||
277 | /** |
||
278 | * Set the maximum number of items per page. |
||
279 | * |
||
280 | * @param int $limit |
||
281 | * |
||
282 | * @return Paginator |
||
283 | */ |
||
284 | public function setMax($limit) |
||
290 | |||
291 | /** |
||
292 | * Get the maximum number of items per page. |
||
293 | * |
||
294 | * @return int |
||
295 | */ |
||
296 | public function getMax() |
||
300 | |||
301 | /** |
||
302 | * Return true if the collection is empty. |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function isEmpty() |
||
310 | |||
311 | /** |
||
312 | * Execute the offset calculation only if the $condition is true. |
||
313 | * |
||
314 | * @param bool $condition |
||
315 | * |
||
316 | * @return Paginator |
||
317 | */ |
||
318 | public function calculateOnlyIf($condition) |
||
326 | |||
327 | /** |
||
328 | * Calculate the offset value accoring to url params and the page number. |
||
329 | * |
||
330 | * @return Paginator |
||
331 | */ |
||
332 | public function calculate() |
||
346 | |||
347 | /** |
||
348 | * Generation pagination links. |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | public function toHtml() |
||
366 | |||
367 | /** |
||
368 | * Magic method to output pagination links. |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | public function __toString() |
||
376 | |||
377 | /** |
||
378 | * Column sorting. |
||
379 | * |
||
380 | * @param string $label Column title |
||
381 | * @param string $column SQL column name |
||
382 | * |
||
383 | * @return string |
||
384 | */ |
||
385 | public function order($label, $column) |
||
402 | |||
403 | /** |
||
404 | * Get url params for link generation. |
||
405 | * |
||
406 | * @param int $page |
||
407 | * @param string $order |
||
408 | * @param string $direction |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | protected function getUrlParams($page, $order, $direction) |
||
422 | |||
423 | /** |
||
424 | * Generate the previous link. |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | protected function generatePreviousLink() |
||
449 | |||
450 | /** |
||
451 | * Generate the next link. |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | protected function generateNextLink() |
||
476 | |||
477 | /** |
||
478 | * Generate the page showing. |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | protected function generatPageShowing() |
||
486 | |||
487 | /** |
||
488 | * Return true if there is no pagination to show. |
||
489 | * |
||
490 | * @return bool |
||
491 | */ |
||
492 | protected function hasNothingtoShow() |
||
496 | } |
||
497 |