]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Pagination.php
4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Core
;
24 use Slim\Routing\RouteParser
;
27 use Laminas\Db\Sql\Select
;
31 * Pagination and ordering facilities
33 * @author Johan Cwiklinski <johan@x-tnd.be>
35 * @property integer $current_page
36 * @property string $orderby
37 * @property string $ordered
38 * @property integer $show
39 * @property integer $pages
40 * @property integer $counter
43 abstract class Pagination
45 private int $current_page;
46 private int|
string $orderby;
47 private string $ordered;
49 private int $pages = 1;
50 private ?
int $counter = null;
51 protected ?Twig
$view;
52 protected ?RouteParser
$routeparser;
54 public const ORDER_ASC
= 'ASC';
55 public const ORDER_DESC
= 'DESC';
57 /** @var array<string> */
58 protected array $pagination_fields = array(
70 public function __construct()
76 * Returns the field we want to default set order to
80 abstract protected function getDefaultOrder(): int|
string;
83 * Return the default direction for ordering
85 * @return string ASC or DESC
87 protected function getDefaultDirection(): string
89 return self
::ORDER_ASC
;
93 * Reinit default parameters
97 public function reinit(): void
101 $this->current_page
= 1;
102 $this->orderby
= $this->getDefaultOrder();
103 $this->ordered
= $this->getDefaultDirection();
104 $this->show
= $preferences->pref_numrows
;
112 public function invertorder(): void
114 $actual = $this->ordered
;
115 if ($actual == self
::ORDER_ASC
) {
116 $this->ordered
= self
::ORDER_DESC
;
118 if ($actual == self
::ORDER_DESC
) {
119 $this->ordered
= self
::ORDER_ASC
;
124 * Get current sort direction
126 * @return self::ORDER_ASC|self::ORDER_DESC
128 public function getDirection(): string
130 return $this->ordered
;
136 * @param string $direction self::ORDER_ASC|self::ORDER_DESC
140 public function setDirection(string $direction): void
142 if ($direction == self
::ORDER_ASC ||
$direction == self
::ORDER_DESC
) {
143 $this->ordered
= $direction;
146 'Trying to set a sort direction that is not know (`' .
147 $direction . '`). Reverting to default value.',
150 $this->ordered
== self
::ORDER_ASC
;
155 * Add limits so we retrieve only relavant rows
157 * @param Select $select Original select
161 public function setLimits(Select
$select): void
163 if ($this->show
!== 0) {
164 $select->limit($this->show
);
166 ($this->current_page
- 1) * $this->show
174 * @param int $c Count
178 public function setCounter(int $c): void
185 * Update or set pages count
189 protected function countPages(): void
191 if ($this->show
!== 0) {
192 if ($this->counter %
$this->show
== 0) {
193 $this->pages
= (int)($this->counter
/ $this->show
);
195 $this->pages
= (int)($this->counter
/ $this->show
) +
1;
200 if ($this->pages
=== 0) {
203 if ($this->current_page
> $this->pages
) {
204 $this->current_page
= $this->pages
;
209 * Creates pagination links and assign some useful variables to the template
211 * @param RouteParser $routeparser Application instance
212 * @param Twig $view View instance
213 * @param boolean $restricted Do not permit to display all
217 public function setViewPagination(RouteParser
$routeparser, Twig
$view, bool $restricted = true): void
219 $is_paginated = true;
222 $this->routeparser
= $routeparser;
224 //Create pagination links
225 if ($this->current_page
< 11) {
228 $idepart = $this->current_page
- 10;
230 if ($this->current_page +
10 < $this->pages
) {
231 $ifin = $this->current_page +
10;
233 $ifin = $this->pages
;
236 $next = $this->current_page +
1;
237 $previous = $this->current_page
- 1;
239 if ($this->current_page
!= 1) {
240 $paginate .= $this->getLink(
241 '<i class="fast backward small icon" aria-hidden="true"></i>',
243 preg_replace("(%i)", (string)$next, _T("First page"))
246 $paginate .= $this->getLink(
247 '<i class="step backward small icon" aria-hidden="true"></i>',
248 $this->getHref($previous),
249 preg_replace("(%i)", (string)$previous, _T("Previous page (%i)"))
253 for ($i = $idepart; $i <= $ifin; $i++
) {
254 if ($i == $this->current_page
) {
255 $paginate .= $this->getLink(
257 $this->getHref($this->current_page
),
260 (string)$this->current_page
,
261 _T("Current page (%i)")
266 $paginate .= $this->getLink(
269 preg_replace("(%i)", (string)$i, _T("Page %i"))
273 if ($this->current_page
!= $this->pages
) {
274 $paginate .= $this->getLink(
275 '<i class="step forward small icon" aria-hidden="true"></i>',
276 $this->getHref($next),
277 preg_replace("(%i)", (string)$next, _T("Next page (%i)"))
280 $paginate .= $this->getLink(
281 '<i class="fast forward small icon" aria-hidden="true"></i>',
282 $this->getHref($this->pages
),
283 preg_replace("(%i)", (string)$this->pages
, _T("Last page (%i)"))
286 if ($this->current_page
== 1 && $this->current_page
== $this->pages
) {
287 $is_paginated = false;
297 if ($restricted === false) {
298 $options[0] = _T("All");
301 //Now, we assign common variables to template
302 $view->getEnvironment()->addGlobal('nb_pages', $this->pages
);
303 $view->getEnvironment()->addGlobal('page', $this->current_page
);
304 $view->getEnvironment()->addGlobal('numrows', $this->show
);
305 $view->getEnvironment()->addGlobal('is_paginated', $is_paginated);
306 $view->getEnvironment()->addGlobal('pagination', $paginate);
307 $view->getEnvironment()->addGlobal('nbshow_options', $options);
309 //resetting prevents following error:
310 //PHP Fatal error: Uncaught Exception: Serialization of '[...]' is not allowed in [no active file]:0
312 $this->routeparser
= null;
316 * Get a pagination link
318 * @param string $content Links content
319 * @param string $url URL the link to point on
320 * @param string $title Link's title
321 * @param bool $current Is current page
325 private function getLink(string $content, string $url, string $title, bool $current = false): string
327 if ($current === true) {
332 $link = "<a href=\"" . $url . "\" " .
333 "title=\"" . $title . "\" class=\"" . $active . "item\">" . $content . "</a>\n";
340 * @param int $page Page
344 protected function getHref(int $page): string
351 if ($this->view
->getEnvironment()->getGlobals()['cur_subroute']) {
352 $args['type'] = $this->view
->getEnvironment()->getGlobals()['cur_subroute'];
355 $href = $this->routeparser
->urlFor(
356 $this->view
->getEnvironment()->getGlobals()['cur_route'],
363 * Global getter method
365 * @param string $name name of the property we want to retrieve
367 * @return mixed the called property
369 public function __get(string $name)
371 if (in_array($name, $this->pagination_fields
)) {
375 '[' . get_class($this) .
376 '|Pagination] Unable to get property `' . $name . '`',
383 * Global isset method
384 * Required for twig to access properties via __get
386 * @param string $name name of the property we want to retrieve
390 public function __isset(string $name): bool
392 if (in_array($name, $this->pagination_fields
)) {
395 return property_exists($this, $name);
399 * Global setter method
401 * @param string $name name of the property we want to assign a value to
402 * @param mixed $value a relevant value for the property
406 public function __set(string $name, $value): void
410 if ($value == self
::ORDER_ASC ||
$value == self
::ORDER_DESC
) {
411 $this->$name = $value;
414 '[' . get_class($this) .
415 '|Pagination] Possibles values for field `' .
416 $name . '` are: `' . self
::ORDER_ASC
. '` or `' .
417 self
::ORDER_DESC
. '` - `' . $value . '` given',
423 if ($this->$name == $value) {
424 $this->invertorder();
426 $this->$name = $value;
427 $this->setDirection(self
::ORDER_ASC
);
433 if (is_int($value) && $value > 0) {
434 $this->$name = $value;
437 '[' . get_class($this) .
438 '|Pagination] Value for field `' .
439 $name . '` should be a positive integer - (' .
440 gettype($value) . ')' . $value . ' given',
448 ||
preg_match('/[[:digit:]]/', $value)
451 $this->$name = (int)$value;
454 '[' . get_class($this) . '|Pagination] Value for `' .
455 $name . '` should be a positive integer or \'all\' - (' .
456 gettype($value) . ')' . $value . ' given',
463 '[' . get_class($this) .
464 '|Pagination] Unable to set property `' . $name . '`',