]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Pagination.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2010-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2010-2023 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2010-03-03
37 namespace Galette\Core
;
39 use Slim\Routing\RouteParser
;
42 use Laminas\Db\Sql\Select
;
45 * Pagination and ordering facilities
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2010-2023 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
56 * @property integer $current_page
57 * @property string $orderby
58 * @property string $ordered
59 * @property integer $show
60 * @property integer $pages
61 * @property integer $counter
64 abstract class Pagination
66 private $current_page;
71 private $counter = null;
73 protected $routeparser;
75 public const ORDER_ASC
= 'ASC';
76 public const ORDER_DESC
= 'DESC';
78 protected $pagination_fields = array(
90 public function __construct()
96 * Returns the field we want to default set order to
100 abstract protected function getDefaultOrder();
103 * Return the default direction for ordering
105 * @return string ASC or DESC
107 protected function getDefaultDirection()
109 return self
::ORDER_ASC
;
113 * Reinit default parameters
117 public function reinit()
121 $this->current_page
= 1;
122 $this->orderby
= $this->getDefaultOrder();
123 $this->ordered
= $this->getDefaultDirection();
124 $this->show
= (int)$preferences->pref_numrows
;
132 public function invertorder()
134 $actual = $this->ordered
;
135 if ($actual == self
::ORDER_ASC
) {
136 $this->ordered
= self
::ORDER_DESC
;
138 if ($actual == self
::ORDER_DESC
) {
139 $this->ordered
= self
::ORDER_ASC
;
144 * Get current sort direction
146 * @return self::ORDER_ASC|self::ORDER_DESC
148 public function getDirection()
150 return $this->ordered
;
156 * @param string $direction self::ORDER_ASC|self::ORDER_DESC
160 public function setDirection($direction)
162 if ($direction == self
::ORDER_ASC ||
$direction == self
::ORDER_DESC
) {
163 $this->ordered
= $direction;
166 'Trying to set a sort direction that is not know (`' .
167 $direction . '`). Reverting to default value.',
170 $this->ordered
== self
::ORDER_ASC
;
175 * Add limits so we retrieve only relavant rows
177 * @param Select $select Original select
181 public function setLimits(Select
$select)
183 if ($this->show
!== 0) {
184 $select->limit($this->show
);
186 ($this->current_page
- 1) * $this->show
194 * @param int $c Count
198 public function setCounter($c)
200 $this->counter
= (int)$c;
205 * Update or set pages count
209 protected function countPages()
211 if ($this->show
!== 0) {
212 if ($this->counter %
$this->show
== 0) {
213 $this->pages
= (int)($this->counter
/ $this->show
);
215 $this->pages
= (int)($this->counter
/ $this->show
) +
1;
220 if ($this->pages
=== 0) {
223 if ($this->current_page
> $this->pages
) {
224 $this->current_page
= $this->pages
;
229 * Creates pagination links and assign some useful variables to the template
231 * @param RouteParser $routeparser Application instance
232 * @param mixed $view View instance
233 * @param boolean $restricted Do not permit to display all
237 * @deprecated 1.0.0 use setViewPagination
239 public function setSmartyPagination(RouteParser
$routeparser, $view, $restricted = true)
241 $this->setViewPagination($routeparser, $view, $restricted);
245 * Creates pagination links and assign some useful variables to the template
247 * @param RouteParser $routeparser Application instance
248 * @param mixed $view View instance
249 * @param boolean $restricted Do not permit to display all
253 public function setViewPagination(RouteParser
$routeparser, $view, $restricted = true)
255 $is_paginated = true;
258 $this->routeparser
= $routeparser;
260 //Create pagination links
261 if ($this->current_page
< 11) {
264 $idepart = $this->current_page
- 10;
266 if ($this->current_page +
10 < $this->pages
) {
267 $ifin = $this->current_page +
10;
269 $ifin = $this->pages
;
272 $next = $this->current_page +
1;
273 $previous = $this->current_page
- 1;
275 if ($this->current_page
!= 1) {
276 $paginate .= $this->getLink(
279 preg_replace("(%i)", $next, _T("First page"))
282 $paginate .= $this->getLink(
284 $this->getHref($previous),
285 preg_replace("(%i)", $previous, _T("Previous page (%i)"))
289 for ($i = $idepart; $i <= $ifin; $i++
) {
290 if ($i == $this->current_page
) {
291 $paginate .= $this->getLink(
293 $this->getHref($this->current_page
),
297 _T("Current page (%i)")
302 $paginate .= $this->getLink(
305 preg_replace("(%i)", $i, _T("Page %i"))
309 if ($this->current_page
!= $this->pages
) {
310 $paginate .= $this->getLink(
312 $this->getHref($next),
313 preg_replace("(%i)", $next, _T("Next page (%i)"))
316 $paginate .= $this->getLink(
318 $this->getHref($this->pages
),
319 preg_replace("(%i)", $this->pages
, _T("Last page (%i)"))
322 if ($this->current_page
== 1 && $this->current_page
== $this->pages
) {
323 $is_paginated = false;
333 if ($restricted === false) {
334 $options[0] = _T("All");
337 //Now, we assign common variables to template
338 $view->getEnvironment()->addGlobal('nb_pages', $this->pages
);
339 $view->getEnvironment()->addGlobal('page', $this->current_page
);
340 $view->getEnvironment()->addGlobal('numrows', $this->show
);
341 $view->getEnvironment()->addGlobal('is_paginated', $is_paginated);
342 $view->getEnvironment()->addGlobal('pagination', $paginate);
343 $view->getEnvironment()->addGlobal('nbshow_options', $options);
346 $this->routeparser
= null;
350 * Get a pagination link
352 * @param string $content Links content
353 * @param string $url URL the link to point on
354 * @param string $title Link's title
355 * @param bool $current Is current page
359 private function getLink($content, $url, $title, $current = false)
361 if ($current === true) {
366 $link = "<a href=\"" . $url . "\" " .
367 "title=\"" . $title . "\" class=\"" . $active . "item\">" . $content . "</a>\n";
374 * @param int $page Page
378 protected function getHref($page)
385 if ($this->view
->getEnvironment()->getGlobals()['cur_subroute']) {
386 $args['type'] = $this->view
->getEnvironment()->getGlobals()['cur_subroute'];
389 $href = $this->routeparser
->urlFor(
390 $this->view
->getEnvironment()->getGlobals()['cur_route'],
397 * Global getter method
399 * @param string $name name of the property we want to retrive
401 * @return mixed the called property
403 public function __get($name)
405 if (in_array($name, $this->pagination_fields
)) {
409 '[' . get_class($this) .
410 '|Pagination] Unable to get proprety `' . $name . '`',
417 * Global isset method
418 * Required for twig to access properties via __get
420 * @param string $name name of the property we want to retrive
424 public function __isset($name)
426 if (in_array($name, $this->pagination_fields
)) {
429 return property_exists($this, $name);
433 * Global setter method
435 * @param string $name name of the property we want to assign a value to
436 * @param mixed $value a relevant value for the property
440 public function __set($name, $value)
444 if ($value == self
::ORDER_ASC ||
$value == self
::ORDER_DESC
) {
445 $this->$name = $value;
448 '[' . get_class($this) .
449 '|Pagination] Possibles values for field `' .
450 $name . '` are: `' . self
::ORDER_ASC
. '` or `' .
451 self
::ORDER_DESC
. '` - `' . $value . '` given',
457 if ($this->$name == $value) {
458 $this->invertorder();
460 $this->$name = $value;
461 $this->setDirection(self
::ORDER_ASC
);
467 if (is_int($value) && $value > 0) {
468 $this->$name = $value;
471 '[' . get_class($this) .
472 '|Pagination] Value for field `' .
473 $name . '` should be a positive integer - (' .
474 gettype($value) . ')' . $value . ' given',
482 ||
preg_match('/[[:digit:]]/', $value)
485 $this->$name = (int)$value;
488 '[' . get_class($this) . '|Pagination] Value for `' .
489 $name . '` should be a positive integer or \'all\' - (' .
490 gettype($value) . ')' . $value . ' given',
497 '[' . get_class($this) .
498 '|Pagination] Unable to set proprety `' . $name . '`',