]>
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2010-03-03
38 namespace Galette\Core
;
44 * Pagination and ordering facilities
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2010-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
56 abstract class Pagination
58 private $current_page;
63 private $counter = null;
67 const ORDER_ASC
= 'ASC';
68 const ORDER_DESC
= 'DESC';
70 protected $pagination_fields = array(
82 public function __construct()
88 * Returns the field we want to default set order to
90 * @return string field name
92 abstract protected function getDefaultOrder();
95 * Return the default direction for ordering
97 * @return string ASC or DESC
99 protected function getDefaultDirection()
101 return self
::ORDER_ASC
;
105 * Reinit default parameters
109 public function reinit()
113 $this->current_page
= 1;
114 $this->orderby
= $this->getDefaultOrder();
115 $this->ordered
= $this->getDefaultDirection();
116 $this->show
= (int)$preferences->pref_numrows
;
124 public function invertorder()
126 $actual = $this->ordered
;
127 if ($actual == self
::ORDER_ASC
) {
128 $this->ordered
= self
::ORDER_DESC
;
130 if ($actual == self
::ORDER_DESC
) {
131 $this->ordered
= self
::ORDER_ASC
;
136 * Get current sort direction
138 * @return self::ORDER_ASC|self::ORDER_DESC
140 public function getDirection()
142 return $this->ordered
;
148 * @param string $direction self::ORDER_ASC|self::ORDER_DESC
152 public function setDirection($direction)
154 if ($direction == self
::ORDER_ASC ||
$direction == self
::ORDER_DESC
) {
155 $this->ordered
= $direction;
158 'Trying to set a sort direction that is not know (`' .
159 $direction . '`). Reverting to default value.',
162 $this->ordered
== self
::ORDER_ASC
;
167 * Add limits so we retrieve only relavant rows
169 * @param Select $select Original select
173 public function setLimits($select)
175 if ($this->show
!== 0) {
176 $select->limit($this->show
);
178 ($this->current_page
- 1)*$this->show
186 * @param int $c Count
190 public function setCounter($c)
192 $this->counter
= (int)$c;
197 * Update or set pages count
201 protected function countPages()
203 if ($this->show
!== 0) {
204 if ($this->counter%
$this->show
== 0) {
205 $this->pages
= intval($this->counter
/$this->show
);
207 $this->pages
= intval($this->counter
/$this->show
) +
1;
212 if ($this->pages
== 0) {
215 if ($this->current_page
> $this->pages
) {
216 $this->current_page
= $this->pages
;
221 * Creates pagination links and assign some usefull variables to the
224 * @param Router $router Application instance
225 * @param Smarty $view View instance
226 * @param boolean $restricted Do not permit to display all
230 public function setSmartyPagination(\Slim\Router
$router, \Smarty
$view, $restricted = true)
234 $this->router
= $router;
236 //Create pagination links
237 if ($this->current_page
< 11) {
240 $idepart = $this->current_page
- 10;
242 if ($this->current_page +
10 < $this->pages
) {
243 $ifin = $this->current_page +
10;
245 $ifin = $this->pages
;
248 $next = $this->current_page +
1;
249 $previous = $this->current_page
- 1;
251 if ($this->current_page
!= 1) {
252 $paginate .= $this->getLink(
255 preg_replace("(%i)", $next, _T("First page"))
258 $paginate .= $this->getLink(
260 $this->getHref($previous),
261 preg_replace("(%i)", $previous, _T("Previous page (%i)"))
265 for ($i = $idepart; $i <= $ifin; $i++
) {
266 if ($i == $this->current_page
) {
267 $paginate .= $this->getLink(
269 $this->getHref($this->current_page
),
273 _T("Current page (%i)")
278 $paginate .= $this->getLink(
281 preg_replace("(%i)", $i, _T("Page %i"))
285 if ($this->current_page
!= $this->pages
) {
286 $paginate .= $this->getLink(
288 $this->getHref($next),
289 preg_replace("(%i)", $next, _T("Next page (%i)"))
292 $paginate .= $this->getLink(
294 $this->getHref($this->pages
),
295 preg_replace("(%i)", $this->pages
, _T("Last page (%i)"))
306 if ($restricted === false) {
307 $options[0] = _T("All");
310 //Now, we assign common variables to Smarty template
311 $view->assign('nb_pages', $this->pages
);
312 $view->assign('page', $this->current_page
);
313 $view->assign('numrows', $this->show
);
314 $view->assign('pagination', $paginate);
315 $view->assign('nbshow_options', $options);
318 $this->router
= null;
322 * Get a pagination link
324 * @param string $content Links content
325 * @param string $url URL the link to point on
326 * @param string $title Link's title
327 * @param bool $current Is current page
331 private function getLink($content, $url, $title, $current = false)
333 $tabs = "\t\t\t\t\t\t";
334 $link = $tabs . "<li";
335 if ($current === true) {
336 $link .= " class=\"current\" ";
338 $link .= "><a href=\"" . $url . "\" " .
339 "title=\"" . $title . "\">" . $content . "</a></li>\n";
346 * @param int $page Page
350 protected function getHref($page)
357 if ($this->view
->getTemplateVars('cur_subroute')) {
358 $args['type'] = $this->view
->getTemplateVars('cur_subroute');
361 $href = $this->router
->pathFor(
362 $this->view
->getTemplateVars('cur_route'),
369 * Global getter method
371 * @param string $name name of the property we want to retrive
373 * @return object the called property
375 public function __get($name)
379 '[' . get_class($this) .
380 '|Pagination] Getting property `' . $name . '`',
384 if (in_array($name, $this->pagination_fields
)) {
388 '[' . get_class($this) .
389 '|Pagination] Unable to get proprety `' . $name . '`',
396 * Global setter method
398 * @param string $name name of the property we want to assign a value to
399 * @param object $value a relevant value for the property
403 public function __set($name, $value)
407 '[' . get_class($this) . '|Pagination] Setting property `' .
414 if ($value == self
::ORDER_ASC ||
$value == self
::ORDER_DESC
) {
415 $this->$name = $value;
418 '[' . get_class($this) .
419 '|Pagination] Possibles values for field `' .
420 $name . '` are: `' . self
::ORDER_ASC
. '` or `' .
421 self
::ORDER_DESC
. '` - `' . $value . '` given',
427 if ($this->$name == $value) {
428 $this->invertorder();
430 $this->$name = $value;
431 $this->setDirection(self
::ORDER_ASC
);
437 if (is_int($value) && $value > 0) {
438 $this->$name = $value;
441 '[' . get_class($this) .
442 '|Pagination] Value for field `' .
443 $name . '` should be a positive integer - (' .
444 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 . '`',