Elgg  Version 1.9
pagination.php
Go to the documentation of this file.
1 <?php
15 if (elgg_in_context('widget')) {
16  // widgets do not show pagination
17  return true;
18 }
19 
20 $offset = abs((int) elgg_extract('offset', $vars, 0));
21 // because you can say $vars['limit'] = 0
22 if (!$limit = (int) elgg_extract('limit', $vars, 10)) {
23  $limit = 10;
24 }
25 
26 $count = (int) elgg_extract('count', $vars, 0);
27 $offset_key = elgg_extract('offset_key', $vars, 'offset');
28 // some views pass an empty string for base_url
29 if (isset($vars['base_url']) && $vars['base_url']) {
30  $base_url = $vars['base_url'];
31 } else if (isset($vars['baseurl']) && $vars['baseurl']) {
32  elgg_deprecated_notice("Use 'base_url' instead of 'baseurl' for the navigation/pagination view", 1.8);
33  $base_url = $vars['baseurl'];
34 } elseif (elgg_is_xhr() && !empty($_SERVER['HTTP_REFERER'])) {
35  $base_url = $_SERVER['HTTP_REFERER'];
36 } else {
38 }
39 
40 $num_pages = elgg_extract('num_pages', $vars, 10);
41 $delta = ceil($num_pages / 2);
42 
43 if ($count <= $limit && $offset == 0) {
44  // no need for pagination
45  return true;
46 }
47 
50 
51 $pages = new stdClass();
52 $pages->prev = array(
53  'text' => elgg_echo('previous'),
54  'href' => '',
55  'is_trusted' => true,
56 );
57 $pages->next = array(
58  'text' => elgg_echo('next'),
59  'href' => '',
60  'is_trusted' => true,
61 );
62 $pages->items = array();
63 
64 // Add pages before the current page
65 if ($current_page > 1) {
66  $prev_offset = $offset - $limit;
67  if ($prev_offset < 1) {
68  // don't include offset=0
69  $prev_offset = null;
70  }
71 
72  $pages->prev['href'] = elgg_http_add_url_query_elements($base_url, array($offset_key => $prev_offset));
73 
74  $first_page = $current_page - $delta;
75  if ($first_page < 1) {
76  $first_page = 1;
77  }
78 
79  $pages->items = range($first_page, $current_page - 1);
80 }
81 
82 
83 $pages->items[] = $current_page;
84 
85 
86 // add pages after the current one
88  $next_offset = $offset + $limit;
89  if ($next_offset >= $count) {
90  $next_offset--;
91  }
92 
93  $pages->next['href'] = elgg_http_add_url_query_elements($base_url, array($offset_key => $next_offset));
94 
95  $last_page = $current_page + $delta;
96  if ($last_page > $total_pages) {
97  $last_page = $total_pages;
98  }
99 
100  $pages->items = array_merge($pages->items, range($current_page + 1, $last_page));
101 }
102 
103 
104 echo '<ul class="elgg-pagination">';
105 
106 if ($pages->prev['href']) {
107  $link = elgg_view('output/url', $pages->prev);
108  echo "<li>$link</li>";
109 } else {
110  echo "<li class=\"elgg-state-disabled\"><span>{$pages->prev['text']}</span></li>";
111 }
112 
113 foreach ($pages->items as $page) {
114  if ($page == $current_page) {
115  echo "<li class=\"elgg-state-selected\"><span>$page</span></li>";
116  } else {
117  $page_offset = (($page - 1) * $limit);
118  if ($page_offset == 0) {
119  // don't include offset=0
120  $page_offset = null;
121  }
122  $url = elgg_http_add_url_query_elements($base_url, array($offset_key => $page_offset));
123  $link = elgg_view('output/url', array(
124  'href' => $url,
125  'text' => $page,
126  'is_trusted' => true,
127  ));
128  echo "<li>$link</li>";
129  }
130 }
131 
132 if ($pages->next['href']) {
133  $link = elgg_view('output/url', $pages->next);
134  echo "<li>$link</li>";
135 } else {
136  echo "<li class=\"elgg-state-disabled\"><span>{$pages->next['text']}</span></li>";
137 }
138 
139 echo '</ul>';
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1325
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:237
$current_page
Definition: pagination.php:49
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:106
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
if(!$limit=(int) elgg_extract('limit', $vars, 10)) $count
Definition: pagination.php:26
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1464
$url
Definition: exceptions.php:24
$num_pages
Definition: pagination.php:40
$delta
Definition: pagination.php:41
$limit
Definition: userpicker.php:33
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:273
elgg echo
Translates a string.
Definition: languages.js:43
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
$offset_key
Definition: pagination.php:27
elgg_view($view, $vars=array(), $bypass=false, $ignored=false, $viewtype= '')
Return a parsed view.
Definition: views.php:354
$pages
Definition: pagination.php:51
if($count<=$limit &&$offset==0) $total_pages
Definition: pagination.php:48
$base_url
Definition: list.php:24
if(file_exists($welcome)) $vars
Definition: upgrade.php:93