]>
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-2014 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-2014 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
;
43 * Pagination and ordering facilities
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2010-2014 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
55 abstract class Pagination
57 private $current_page;
62 private $counter = null;
66 const ORDER_ASC
= 'ASC';
67 const ORDER_DESC
= 'DESC';
69 protected $pagination_fields = array(
81 public function __construct()
87 * Returns the field we want to default set order to
89 * @return string field name
91 abstract protected function getDefaultOrder();
94 * Return the default direction for ordering
96 * @return string ASC or DESC
98 protected function getDefaultDirection()
100 return self
::ORDER_ASC
;
104 * Reinit default parameters
108 public function reinit()
112 $this->current_page
= 1;
113 $this->orderby
= $this->getDefaultOrder();
114 $this->ordered
= $this->getDefaultDirection();
115 $this->show
= (int)$preferences->pref_numrows
;
123 public function invertorder()
125 $actual = $this->ordered
;
126 if ($actual == self
::ORDER_ASC
) {
127 $this->ordered
= self
::ORDER_DESC
;
129 if ($actual == self
::ORDER_DESC
) {
130 $this->ordered
= self
::ORDER_ASC
;
135 * Get current sort direction
137 * @return self::ORDER_ASC|self::ORDER_DESC
139 public function getDirection()
141 return $this->ordered
;
147 * @param string $direction self::ORDER_ASC|self::ORDER_DESC
151 public function setDirection($direction)
153 if ($direction == self
::ORDER_ASC ||
$direction == self
::ORDER_DESC
) {
154 $this->ordered
= $direction;
157 'Trying to set a sort direction that is not know (`' .
158 $direction . '`). Reverting to default value.',
161 $this->ordered
== self
::ORDER_ASC
;
166 * Add limits so we retrieve only relavant rows
168 * @param Select $select Original select
172 public function setLimits($select)
174 if ($this->show
!== 0) {
175 $select->limit($this->show
);
177 ($this->current_page
- 1) * $this->show
185 * @param int $c Count
189 public function setCounter($c)
191 $this->counter
= (int)$c;
196 * Update or set pages count
200 protected function countPages()
202 if ($this->show
!== 0) {
203 if ($this->counter %
$this->show
== 0) {
204 $this->pages
= intval($this->counter
/ $this->show
);
206 $this->pages
= intval($this->counter
/ $this->show
) +
1;
211 if ($this->pages
== 0) {
214 if ($this->current_page
> $this->pages
) {
215 $this->current_page
= $this->pages
;
220 * Creates pagination links and assign some usefull variables to the
223 * @param Router $router Application instance
224 * @param Smarty $view View instance
225 * @param boolean $restricted Do not permit to display all
229 public function setSmartyPagination(\Slim\Router
$router, \Smarty
$view, $restricted = true)
233 $this->router
= $router;
235 //Create pagination links
236 if ($this->current_page
< 11) {
239 $idepart = $this->current_page
- 10;
241 if ($this->current_page +
10 < $this->pages
) {
242 $ifin = $this->current_page +
10;
244 $ifin = $this->pages
;
247 $next = $this->current_page +
1;
248 $previous = $this->current_page
- 1;
250 if ($this->current_page
!= 1) {
251 $paginate .= $this->getLink(
254 preg_replace("(%i)", $next, _T("First page"))
257 $paginate .= $this->getLink(
259 $this->getHref($previous),
260 preg_replace("(%i)", $previous, _T("Previous page (%i)"))
264 for ($i = $idepart; $i <= $ifin; $i++
) {
265 if ($i == $this->current_page
) {
266 $paginate .= $this->getLink(
268 $this->getHref($this->current_page
),
272 _T("Current page (%i)")
277 $paginate .= $this->getLink(
280 preg_replace("(%i)", $i, _T("Page %i"))
284 if ($this->current_page
!= $this->pages
) {
285 $paginate .= $this->getLink(
287 $this->getHref($next),
288 preg_replace("(%i)", $next, _T("Next page (%i)"))
291 $paginate .= $this->getLink(
293 $this->getHref($this->pages
),
294 preg_replace("(%i)", $this->pages
, _T("Last page (%i)"))
305 if ($restricted === false) {
306 $options[0] = _T("All");
309 //Now, we assign common variables to Smarty template
310 $view->assign('nb_pages', $this->pages
);
311 $view->assign('page', $this->current_page
);
312 $view->assign('numrows', $this->show
);
313 $view->assign('pagination', $paginate);
314 $view->assign('nbshow_options', $options);
317 $this->router
= null;
321 * Get a pagination link
323 * @param string $content Links content
324 * @param string $url URL the link to point on
325 * @param string $title Link's title
326 * @param bool $current Is current page
330 private function getLink($content, $url, $title, $current = false)
332 $tabs = "\t\t\t\t\t\t";
333 $link = $tabs . "<li";
334 if ($current === true) {
335 $link .= " class=\"current\" ";
337 $link .= "><a href=\"" . $url . "\" " .
338 "title=\"" . $title . "\">" . $content . "</a></li>\n";
345 * @param int $page Page
349 protected function getHref($page)
356 if ($this->view
->getTemplateVars('cur_subroute')) {
357 $args['type'] = $this->view
->getTemplateVars('cur_subroute');
360 $href = $this->router
->pathFor(
361 $this->view
->getTemplateVars('cur_route'),
368 * Global getter method
370 * @param string $name name of the property we want to retrive
372 * @return object the called property
374 public function __get($name)
378 '[' . get_class($this) .
379 '|Pagination] Getting property `' . $name . '`',
383 if (in_array($name, $this->pagination_fields
)) {
387 '[' . get_class($this) .
388 '|Pagination] Unable to get proprety `' . $name . '`',
395 * Global setter method
397 * @param string $name name of the property we want to assign a value to
398 * @param object $value a relevant value for the property
402 public function __set($name, $value)
406 '[' . get_class($this) . '|Pagination] Setting property `' .
413 if ($value == self
::ORDER_ASC ||
$value == self
::ORDER_DESC
) {
414 $this->$name = $value;
417 '[' . get_class($this) .
418 '|Pagination] Possibles values for field `' .
419 $name . '` are: `' . self
::ORDER_ASC
. '` or `' .
420 self
::ORDER_DESC
. '` - `' . $value . '` given',
426 if ($this->$name == $value) {
427 $this->invertorder();
429 $this->$name = $value;
430 $this->setDirection(self
::ORDER_ASC
);
436 if (is_int($value) && $value > 0) {
437 $this->$name = $value;
440 '[' . get_class($this) .
441 '|Pagination] Value for field `' .
442 $name . '` should be a positive integer - (' .
443 gettype($value) . ')' . $value . ' given',
451 ||
preg_match('/[[:digit:]]/', $value)
454 $this->$name = (int)$value;
457 '[' . get_class($this) . '|Pagination] Value for `' .
458 $name . '` should be a positive integer or \'all\' - (' .
459 gettype($value) . ')' . $value . ' given',
466 '[' . get_class($this) .
467 '|Pagination] Unable to set proprety `' . $name . '`',