]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Pagination.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Core / Pagination.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Global pagination
7 *
8 * PHP version 5
9 *
10 * Copyright © 2010-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Core
28 * @package Galette
29 *
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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2010-03-03
36 */
37
38 namespace Galette\Core;
39
40 use Slim\Slim;
41 use Analog\Analog;
42
43 /**
44 * Pagination and ordering facilities
45 *
46 * @name Pagination
47 * @category Core
48 * @package Galette
49 *
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
54 */
55
56 abstract class Pagination
57 {
58 private $current_page;
59 private $orderby;
60 private $ordered;
61 private $show;
62 private $pages = 1;
63 private $counter = null;
64 protected $view;
65 protected $router;
66
67 const ORDER_ASC = 'ASC';
68 const ORDER_DESC = 'DESC';
69
70 protected $pagination_fields = array(
71 'current_page',
72 'orderby',
73 'ordered',
74 'show',
75 'pages',
76 'counter'
77 );
78
79 /**
80 * Default constructor
81 */
82 public function __construct()
83 {
84 $this->reinit();
85 }
86
87 /**
88 * Returns the field we want to default set order to
89 *
90 * @return string field name
91 */
92 abstract protected function getDefaultOrder();
93
94 /**
95 * Return the default direction for ordering
96 *
97 * @return string ASC or DESC
98 */
99 protected function getDefaultDirection()
100 {
101 return self::ORDER_ASC;
102 }
103
104 /**
105 * Reinit default parameters
106 *
107 * @return void
108 */
109 public function reinit()
110 {
111 global $preferences;
112
113 $this->current_page = 1;
114 $this->orderby = $this->getDefaultOrder();
115 $this->ordered = $this->getDefaultDirection();
116 $this->show = (int)$preferences->pref_numrows;
117 }
118
119 /**
120 * Invert sort order
121 *
122 * @return void
123 */
124 public function invertorder()
125 {
126 $actual = $this->ordered;
127 if ($actual == self::ORDER_ASC) {
128 $this->ordered = self::ORDER_DESC;
129 }
130 if ($actual == self::ORDER_DESC) {
131 $this->ordered = self::ORDER_ASC;
132 }
133 }
134
135 /**
136 * Get current sort direction
137 *
138 * @return self::ORDER_ASC|self::ORDER_DESC
139 */
140 public function getDirection()
141 {
142 return $this->ordered;
143 }
144
145 /**
146 * Set sort direction
147 *
148 * @param string $direction self::ORDER_ASC|self::ORDER_DESC
149 *
150 * @return void
151 */
152 public function setDirection($direction)
153 {
154 if ($direction == self::ORDER_ASC || $direction == self::ORDER_DESC) {
155 $this->ordered = $direction;
156 } else {
157 Analog::log(
158 'Trying to set a sort direction that is not know (`' .
159 $direction . '`). Reverting to default value.',
160 Analog::WARNING
161 );
162 $this->ordered == self::ORDER_ASC;
163 }
164 }
165
166 /**
167 * Add limits so we retrieve only relavant rows
168 *
169 * @param Select $select Original select
170 *
171 * @return void
172 */
173 public function setLimits($select)
174 {
175 if ($this->show !== 0) {
176 $select->limit($this->show);
177 $select->offset(
178 ($this->current_page - 1)*$this->show
179 );
180 }
181 }
182
183 /**
184 * Set counter
185 *
186 * @param int $c Count
187 *
188 * @return void
189 */
190 public function setCounter($c)
191 {
192 $this->counter = (int)$c;
193 $this->countPages();
194 }
195
196 /**
197 * Update or set pages count
198 *
199 * @return void
200 */
201 protected function countPages()
202 {
203 if ($this->show !== 0) {
204 if ($this->counter%$this->show == 0) {
205 $this->pages = intval($this->counter/$this->show);
206 } else {
207 $this->pages = intval($this->counter/$this->show) + 1;
208 }
209 } else {
210 $this->pages = 0;
211 }
212 if ($this->pages == 0) {
213 $this->pages = 1;
214 }
215 if ($this->current_page > $this->pages) {
216 $this->current_page = $this->pages;
217 }
218 }
219
220 /**
221 * Creates pagination links and assign some usefull variables to the
222 * Smarty template
223 *
224 * @param Router $router Application instance
225 * @param Smarty $view View instance
226 * @param boolean $restricted Do not permit to display all
227 *
228 * @return void
229 */
230 public function setSmartyPagination(\Slim\Router $router, \Smarty $view, $restricted = true)
231 {
232 $paginate = null;
233 $this->view = $view;
234 $this->router = $router;
235
236 //Create pagination links
237 if ($this->current_page < 11) {
238 $idepart = 1;
239 } else {
240 $idepart = $this->current_page - 10;
241 }
242 if ($this->current_page + 10 < $this->pages) {
243 $ifin = $this->current_page + 10;
244 } else {
245 $ifin = $this->pages;
246 }
247
248 $next = $this->current_page + 1;
249 $previous = $this->current_page - 1;
250
251 if ($this->current_page != 1) {
252 $paginate .= $this->getLink(
253 '&lt;&lt;',
254 $this->getHref(1),
255 preg_replace("(%i)", $next, _T("First page"))
256 );
257
258 $paginate .= $this->getLink(
259 '&lt;',
260 $this->getHref($previous),
261 preg_replace("(%i)", $previous, _T("Previous page (%i)"))
262 );
263 }
264
265 for ($i = $idepart; $i <= $ifin; $i++) {
266 if ($i == $this->current_page) {
267 $paginate .= $this->getLink(
268 "-&nbsp;$i&nbsp;-",
269 $this->getHref($this->current_page),
270 preg_replace(
271 "(%i)",
272 $this->current_page,
273 _T("Current page (%i)")
274 ),
275 true
276 );
277 } else {
278 $paginate .= $this->getLink(
279 $i,
280 $this->getHref($i),
281 preg_replace("(%i)", $i, _T("Page %i"))
282 );
283 }
284 }
285 if ($this->current_page != $this->pages) {
286 $paginate .= $this->getLink(
287 '&gt;',
288 $this->getHref($next),
289 preg_replace("(%i)", $next, _T("Next page (%i)"))
290 );
291
292 $paginate .= $this->getLink(
293 '&gt;&gt;',
294 $this->getHref($this->pages),
295 preg_replace("(%i)", $this->pages, _T("Last page (%i)"))
296 );
297 }
298
299 $options = array(
300 10 => "10",
301 20 => "20",
302 50 => "50",
303 100 => "100"
304 );
305
306 if ($restricted === false) {
307 $options[0] = _T("All");
308 }
309
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);
316
317 $this->view = null;
318 $this->router = null;
319 }
320
321 /**
322 * Get a pagination link
323 *
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
328 *
329 * @return string
330 */
331 private function getLink($content, $url, $title, $current = false)
332 {
333 $tabs = "\t\t\t\t\t\t";
334 $link = $tabs . "<li";
335 if ($current === true) {
336 $link .= " class=\"current\" ";
337 }
338 $link .= "><a href=\"" . $url . "\" " .
339 "title=\"" . $title . "\">" . $content . "</a></li>\n";
340 return $link;
341 }
342
343 /**
344 * Build href
345 *
346 * @param int $page Page
347 *
348 * @return string
349 */
350 protected function getHref($page)
351 {
352 $args = [
353 'option' => 'page',
354 'value' => $page
355 ];
356
357 if ($this->view->getTemplateVars('cur_subroute')) {
358 $args['type'] = $this->view->getTemplateVars('cur_subroute');
359 }
360
361 $href = $this->router->pathFor(
362 $this->view->getTemplateVars('cur_route'),
363 $args
364 );
365 return $href;
366 }
367
368 /**
369 * Global getter method
370 *
371 * @param string $name name of the property we want to retrive
372 *
373 * @return object the called property
374 */
375 public function __get($name)
376 {
377
378 Analog::log(
379 '[' . get_class($this) .
380 '|Pagination] Getting property `' . $name . '`',
381 Analog::DEBUG
382 );
383
384 if (in_array($name, $this->pagination_fields)) {
385 return $this->$name;
386 } else {
387 Analog::log(
388 '[' . get_class($this) .
389 '|Pagination] Unable to get proprety `' . $name . '`',
390 Analog::WARNING
391 );
392 }
393 }
394
395 /**
396 * Global setter method
397 *
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
400 *
401 * @return void
402 */
403 public function __set($name, $value)
404 {
405
406 Analog::log(
407 '[' . get_class($this) . '|Pagination] Setting property `' .
408 $name . '`',
409 Analog::DEBUG
410 );
411
412 switch ($name) {
413 case 'ordered':
414 if ($value == self::ORDER_ASC || $value == self::ORDER_DESC) {
415 $this->$name = $value;
416 } else {
417 Analog::log(
418 '[' . get_class($this) .
419 '|Pagination] Possibles values for field `' .
420 $name . '` are: `' . self::ORDER_ASC . '` or `' .
421 self::ORDER_DESC . '` - `' . $value . '` given',
422 Analog::WARNING
423 );
424 }
425 break;
426 case 'orderby':
427 if ($this->$name == $value) {
428 $this->invertorder();
429 } else {
430 $this->$name = $value;
431 $this->setDirection(self::ORDER_ASC);
432 }
433 break;
434 case 'current_page':
435 case 'counter':
436 case 'pages':
437 if (is_int($value) && $value > 0) {
438 $this->$name = $value;
439 } else {
440 Analog::log(
441 '[' . get_class($this) .
442 '|Pagination] Value for field `' .
443 $name . '` should be a positive integer - (' .
444 gettype($value) . ')' . $value . ' given',
445 Analog::WARNING
446 );
447 }
448 break;
449 case 'show':
450 if ($value == 'all'
451 || preg_match('/[[:digit:]]/', $value)
452 && $value >= 0
453 ) {
454 $this->$name = (int)$value;
455 } else {
456 Analog::log(
457 '[' . get_class($this) . '|Pagination] Value for `' .
458 $name . '` should be a positive integer or \'all\' - (' .
459 gettype($value) . ')' . $value . ' given',
460 Analog::WARNING
461 );
462 }
463 break;
464 default:
465 Analog::log(
466 '[' . get_class($this) .
467 '|Pagination] Unable to set proprety `' . $name . '`',
468 Analog::WARNING
469 );
470 break;
471 }
472 }
473 }