/[drupal]/drupal/includes/pager.inc
ViewVC logotype

Contents of /drupal/includes/pager.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.75 - (show annotations) (download) (as text)
Sat Oct 31 18:12:26 2009 UTC (3 weeks, 6 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.74: +2 -2 lines
File MIME type: text/x-php
- Patch #321023 by Everett Zufelt, mgifford: improve accesibility in pager.inc.
1 <?php
2 // $Id: pager.inc,v 1.74 2009/10/09 00:59:54 dries Exp $
3
4 /**
5 * @file
6 * Functions to aid in presenting database results as a set of pages.
7 */
8
9
10 /**
11 * Query extender for pager queries.
12 *
13 * This is the "default" pager mechanism. It creates a paged query with a fixed
14 * number of entries per page.
15 */
16 class PagerDefault extends SelectQueryExtender {
17
18 /**
19 * The highest element we've autogenerated so far.
20 *
21 * @var int
22 */
23 static protected $maxElement = 0;
24
25 /**
26 * The number of elements per page to allow.
27 *
28 * @var int
29 */
30 protected $limit = 10;
31
32 /**
33 * The unique ID of this pager on this page.
34 *
35 * @var int
36 */
37 protected $element = NULL;
38
39 /**
40 * The count query that will be used for this pager.
41 *
42 * @var SelectQueryInterface
43 */
44 protected $customCountQuery = FALSE;
45
46 public function __construct(SelectQueryInterface $query, DatabaseConnection $connection) {
47 parent::__construct($query, $connection);
48
49 // Add pager tag. Do this here to ensure that it is always added before
50 // preExecute() is called.
51 $this->addTag('pager');
52 }
53
54 /**
55 * Override the execute method.
56 *
57 * Before we run the query, we need to add pager-based range() instructions
58 * to it.
59 */
60 public function execute() {
61 global $pager_page_array, $pager_total, $pager_total_items, $pager_limits;
62
63 // Add convenience tag to mark that this is an extended query. We have to
64 // do this in the constructor to ensure that it is set before preExecute()
65 // gets called.
66 if (!$this->preExecute($this)) {
67 return NULL;
68 }
69
70 // A NULL limit is the "kill switch" for pager queries.
71 if (empty($this->limit)) {
72 return;
73 }
74 $this->ensureElement();
75
76 $page = isset($_GET['page']) ? $_GET['page'] : '';
77
78 // Convert comma-separated $page to an array, used by other functions.
79 $pager_page_array = explode(',', $page);
80
81 if (!isset($pager_page_array[$this->element])) {
82 $pager_page_array[$this->element] = 0;
83 }
84
85 // We calculate the total of pages as ceil(items / limit).
86 $pager_total_items[$this->element] = $this->getCountQuery()->execute()->fetchField();
87 $pager_total[$this->element] = ceil($pager_total_items[$this->element] / $this->limit);
88 $pager_page_array[$this->element] = max(0, min((int)$pager_page_array[$this->element], ((int)$pager_total[$this->element]) - 1));
89 $pager_limits[$this->element] = $this->limit;
90 $this->range($pager_page_array[$this->element] * $this->limit, $this->limit);
91
92 // Now that we've added our pager-based range instructions, run the query normally.
93 return $this->query->execute();
94 }
95
96 /**
97 * Ensure that there is an element associated with this query.
98 *
99 * After running this query, access $this->element to get the element for this
100 * query.
101 */
102 protected function ensureElement() {
103 if (!empty($this->element)) {
104 return;
105 }
106
107 $this->element = self::$maxElement++;
108 }
109
110 /**
111 * Specify the count query object to use for this pager.
112 *
113 * You will rarely need to specify a count query directly. If not specified,
114 * one is generated off of the pager query itself.
115 *
116 * @param SelectQueryInterface $query
117 * The count query object. It must return a single row with a single column,
118 * which is the total number of records.
119 */
120 public function setCountQuery(SelectQueryInterface $query) {
121 $this->customCountQuery = $query;
122 }
123
124 /**
125 * Retrieve the count query for this pager.
126 *
127 * The count query may be specified manually or, by default, taken from the
128 * query we are extending.
129 *
130 * @return
131 * A count SelectQueryInterface object.
132 */
133 protected function getCountQuery() {
134 if ($this->customCountQuery) {
135 return $this->customCountQuery;
136 }
137 else {
138 return $this->query->countQuery();
139 }
140 }
141
142 /**
143 * Specify the maximum number of elements per page for this query.
144 *
145 * The default if not specified is 10 items per page.
146 *
147 * @param $limit
148 * An integer specifying the number of elements per page. If passed a false
149 * value (FALSE, 0, NULL), the pager is disabled.
150 */
151 public function limit($limit = 10) {
152 $this->limit = $limit;
153 return $this;
154 }
155
156 /**
157 * Specify the element ID for this pager query.
158 *
159 * The element is used to differentiate different pager queries on the same
160 * page so that they may be operated independently. If you do not specify an
161 * element, every pager query on the page will get a unique element. If for
162 * whatever reason you want to explicitly define an element for a given query,
163 * you may do so here.
164 *
165 * @param $element
166 */
167 public function element($element) {
168 $this->element = $element;
169 return $this;
170 }
171 }
172
173 /**
174 * Compose a URL query parameter array for pager links.
175 *
176 * @return
177 * A URL query parameter array that consists of all components of the current
178 * page request except for those pertaining to paging.
179 */
180 function pager_get_query_parameters() {
181 $query = &drupal_static(__FUNCTION__);
182 if (!isset($query)) {
183 $query = drupal_get_query_parameters($_REQUEST, array_merge(array('q', 'page'), array_keys($_COOKIE)));
184 }
185 return $query;
186 }
187
188 /**
189 * Format a query pager.
190 *
191 * Menu callbacks that display paged query results should call theme('pager') to
192 * retrieve a pager control so that users can view other results.
193 * Format a list of nearby pages with additional query results.
194 *
195 * @param $variables
196 * An associative array containing:
197 * - tags: An array of labels for the controls in the pager.
198 * - element: An optional integer to distinguish between multiple pagers on
199 * one page.
200 * - parameters: An associative array of query string parameters to append to
201 * the pager links.
202 * - quantity: The number of pages in the list.
203 *
204 * @return
205 * An HTML string that generates the query pager.
206 *
207 * @ingroup themeable
208 */
209 function theme_pager($variables) {
210 $tags = $variables['tags'];
211 $element = $variables['element'];
212 $parameters = $variables['parameters'];
213 $quantity = $variables['quantity'];
214 global $pager_page_array, $pager_total;
215
216 // Calculate various markers within this pager piece:
217 // Middle is used to "center" pages around the current page.
218 $pager_middle = ceil($quantity / 2);
219 // current is the page we are currently paged to
220 $pager_current = $pager_page_array[$element] + 1;
221 // first is the first page listed by this pager piece (re quantity)
222 $pager_first = $pager_current - $pager_middle + 1;
223 // last is the last page listed by this pager piece (re quantity)
224 $pager_last = $pager_current + $quantity - $pager_middle;
225 // max is the maximum page number
226 $pager_max = $pager_total[$element];
227 // End of marker calculations.
228
229 // Prepare for generation loop.
230 $i = $pager_first;
231 if ($pager_last > $pager_max) {
232 // Adjust "center" if at end of query.
233 $i = $i + ($pager_max - $pager_last);
234 $pager_last = $pager_max;
235 }
236 if ($i <= 0) {
237 // Adjust "center" if at start of query.
238 $pager_last = $pager_last + (1 - $i);
239 $i = 1;
240 }
241 // End of generation loop preparation.
242
243 $li_first = theme('pager_first', array('text' => (isset($tags[0]) ? $tags[0] : t('« first')), 'element' => $element, 'parameters' => $parameters));
244 $li_previous = theme('pager_previous', array('text' => (isset($tags[1]) ? $tags[1] : t('‹ previous')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
245 $li_next = theme('pager_next', array('text' => (isset($tags[3]) ? $tags[3] : t('next ›')), 'element' => $element, 'interval' => 1, 'parameters' => $parameters));
246 $li_last = theme('pager_last', array('text' => (isset($tags[4]) ? $tags[4] : t('last »')), 'element' => $element, 'parameters' => $parameters));
247
248 if ($pager_total[$element] > 1) {
249 if ($li_first) {
250 $items[] = array(
251 'class' => array('pager-first'),
252 'data' => $li_first,
253 );
254 }
255 if ($li_previous) {
256 $items[] = array(
257 'class' => array('pager-previous'),
258 'data' => $li_previous,
259 );
260 }
261
262 // When there is more than one page, create the pager list.
263 if ($i != $pager_max) {
264 if ($i > 1) {
265 $items[] = array(
266 'class' => array('pager-ellipsis'),
267 'data' => '…',
268 );
269 }
270 // Now generate the actual pager piece.
271 for (; $i <= $pager_last && $i <= $pager_max; $i++) {
272 if ($i < $pager_current) {
273 $items[] = array(
274 'class' => array('pager-item'),
275 'data' => theme('pager_previous', array('text' => $i, 'element' => $element, 'interval' => ($pager_current - $i), 'parameters' => $parameters)),
276 );
277 }
278 if ($i == $pager_current) {
279 $items[] = array(
280 'class' => array('pager-current'),
281 'data' => $i,
282 );
283 }
284 if ($i > $pager_current) {
285 $items[] = array(
286 'class' => array('pager-item'),
287 'data' => theme('pager_next', array('text' => $i, 'element' => $element, 'interval' => ($i - $pager_current), 'parameters' => $parameters)),
288 );
289 }
290 }
291 if ($i < $pager_max) {
292 $items[] = array(
293 'class' => array('pager-ellipsis'),
294 'data' => '…',
295 );
296 }
297 }
298 // End generation.
299 if ($li_next) {
300 $items[] = array(
301 'class' => array('pager-next'),
302 'data' => $li_next,
303 );
304 }
305 if ($li_last) {
306 $items[] = array(
307 'class' => array('pager-last'),
308 'data' => $li_last,
309 );
310 }
311 return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array('items' => $items, 'title' => NULL, 'type' => 'ul', 'attributes' => array('class' => array('pager'))));
312 }
313 }
314
315
316 /**
317 * @name Pager pieces
318 * @{
319 * Use these pieces to construct your own custom pagers in your theme. Note that
320 * you should NOT modify this file to customize your pager.
321 */
322
323 /**
324 * Format a "first page" link.
325 *
326 * @param $variables
327 * An associative array containing:
328 * - text: The name (or image) of the link.
329 * - element: An optional integer to distinguish between multiple pagers on
330 * one page.
331 * - parameters: An associative array of query string parameters to append to
332 * the pager links.
333 *
334 * @return
335 * An HTML string that generates this piece of the query pager.
336 *
337 * @ingroup themeable
338 */
339 function theme_pager_first($variables) {
340 $text = $variables['text'];
341 $element = $variables['element'];
342 $parameters = $variables['parameters'];
343 global $pager_page_array;
344 $output = '';
345
346 // If we are anywhere but the first page
347 if ($pager_page_array[$element] > 0) {
348 $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array(0, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
349 }
350
351 return $output;
352 }
353
354 /**
355 * Format a "previous page" link.
356 *
357 * @param $variables
358 * An associative array containing:
359 * - text: The name (or image) of the link.
360 * - element: An optional integer to distinguish between multiple pagers on
361 * one page.
362 * - interval: The number of pages to move backward when the link is clicked.
363 * - parameters: An associative array of query string parameters to append to
364 * the pager links.
365 *
366 * @return
367 * An HTML string that generates this piece of the query pager.
368 *
369 * @ingroup themeable
370 */
371 function theme_pager_previous($variables) {
372 $text = $variables['text'];
373 $element = $variables['element'];
374 $interval = $variables['interval'];
375 $parameters = $variables['parameters'];
376 global $pager_page_array;
377 $output = '';
378
379 // If we are anywhere but the first page
380 if ($pager_page_array[$element] > 0) {
381 $page_new = pager_load_array($pager_page_array[$element] - $interval, $element, $pager_page_array);
382
383 // If the previous page is the first page, mark the link as such.
384 if ($page_new[$element] == 0) {
385 $output = theme('pager_first', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
386 }
387 // The previous page is not the first page.
388 else {
389 $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
390 }
391 }
392
393 return $output;
394 }
395
396 /**
397 * Format a "next page" link.
398 *
399 * @param $variables
400 * An associative array containing:
401 * - text: The name (or image) of the link.
402 * - element: An optional integer to distinguish between multiple pagers on
403 * one page.
404 * - interval: The number of pages to move forward when the link is clicked.
405 * - parameters: An associative array of query string parameters to append to
406 * the pager links.
407 *
408 * @return
409 * An HTML string that generates this piece of the query pager.
410 *
411 * @ingroup themeable
412 */
413 function theme_pager_next($variables) {
414 $text = $variables['text'];
415 $element = $variables['element'];
416 $interval = $variables['interval'];
417 $parameters = $variables['parameters'];
418 global $pager_page_array, $pager_total;
419 $output = '';
420
421 // If we are anywhere but the last page
422 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
423 $page_new = pager_load_array($pager_page_array[$element] + $interval, $element, $pager_page_array);
424 // If the next page is the last page, mark the link as such.
425 if ($page_new[$element] == ($pager_total[$element] - 1)) {
426 $output = theme('pager_last', array('text' => $text, 'element' => $element, 'parameters' => $parameters));
427 }
428 // The next page is not the last page.
429 else {
430 $output = theme('pager_link', array('text' => $text, 'page_new' => $page_new, 'element' => $element, 'parameters' => $parameters));
431 }
432 }
433
434 return $output;
435 }
436
437 /**
438 * Format a "last page" link.
439 *
440 * @param $variables
441 * An associative array containing:
442 * - text: The name (or image) of the link.
443 * - element: An optional integer to distinguish between multiple pagers on
444 * one page.
445 * - parameters: An associative array of query string parameters to append to
446 * the pager links.
447 *
448 * @return
449 * An HTML string that generates this piece of the query pager.
450 *
451 * @ingroup themeable
452 */
453 function theme_pager_last($variables) {
454 $text = $variables['text'];
455 $element = $variables['element'];
456 $parameters = $variables['parameters'];
457 global $pager_page_array, $pager_total;
458 $output = '';
459
460 // If we are anywhere but the last page
461 if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
462 $output = theme('pager_link', array('text' => $text, 'page_new' => pager_load_array($pager_total[$element] - 1, $element, $pager_page_array), 'element' => $element, 'parameters' => $parameters));
463 }
464
465 return $output;
466 }
467
468
469 /**
470 * Format a link to a specific query result page.
471 *
472 * @param $variables
473 * An associative array containing:
474 * - page_new: The first result to display on the linked page.
475 * - element: An optional integer to distinguish between multiple pagers on
476 * one page.
477 * - parameters: An associative array of query string parameters to append to
478 * the pager link.
479 * - attributes: An associative array of HTML attributes to apply to a pager
480 * anchor tag.
481 *
482 * @return
483 * An HTML string that generates the link.
484 *
485 * @ingroup themeable
486 */
487 function theme_pager_link($variables) {
488 $text = $variables['text'];
489 $page_new = $variables['page_new'];
490 $element = $variables['element'];
491 $parameters = $variables['parameters'];
492 $attributes = $variables['attributes'];
493
494 $page = isset($_GET['page']) ? $_GET['page'] : '';
495 if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
496 $parameters['page'] = $new_page;
497 }
498
499 $query = array();
500 if (count($parameters)) {
501 $query = drupal_get_query_parameters($parameters, array());
502 }
503 if ($query_pager = pager_get_query_parameters()) {
504 $query = array_merge($query, $query_pager);
505 }
506
507 // Set each pager link title
508 if (!isset($attributes['title'])) {
509 static $titles = NULL;
510 if (!isset($titles)) {
511 $titles = array(
512 t('« first') => t('Go to first page'),
513 t('‹ previous') => t('Go to previous page'),
514 t('next ›') => t('Go to next page'),
515 t('last »') => t('Go to last page'),
516 );
517 }
518 if (isset($titles[$text])) {
519 $attributes['title'] = $titles[$text];
520 }
521 elseif (is_numeric($text)) {
522 $attributes['title'] = t('Go to page @number', array('@number' => $text));
523 }
524 }
525
526 return l($text, $_GET['q'], array('attributes' => $attributes, 'query' => $query));
527 }
528
529 /**
530 * @} End of "Pager pieces".
531 */
532
533 /**
534 * Helper function
535 *
536 * Copies $old_array to $new_array and sets $new_array[$element] = $value
537 * Fills in $new_array[0 .. $element - 1] = 0
538 */
539 function pager_load_array($value, $element, $old_array) {
540 $new_array = $old_array;
541 // Look for empty elements.
542 for ($i = 0; $i < $element; $i++) {
543 if (!$new_array[$i]) {
544 // Load found empty element with 0.
545 $new_array[$i] = 0;
546 }
547 }
548 // Update the changed element.
549 $new_array[$element] = (int)$value;
550 return $new_array;
551 }

  ViewVC Help
Powered by ViewVC 1.1.2