Elgg  Version 1.11
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;
18 }
19 
20 $count = (int) elgg_extract('count', $vars, 0);
21 if (!$count) {
22  return;
23 }
24 
25 $offset = abs((int) elgg_extract('offset', $vars, 0));
26 // because you can say $vars['limit'] = 0
27 if (!$limit = (int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) {
28  $limit = 10;
29 }
30 
31 $offset_key = elgg_extract('offset_key', $vars, 'offset');
32 // some views pass an empty string for base_url
33 if (isset($vars['base_url']) && $vars['base_url']) {
34  $base_url = $vars['base_url'];
35 } else if (isset($vars['baseurl']) && $vars['baseurl']) {
36  elgg_deprecated_notice("Use 'base_url' instead of 'baseurl' for the navigation/pagination view", 1.8);
37  $base_url = $vars['baseurl'];
38 } elseif (elgg_is_xhr() && !empty($_SERVER['HTTP_REFERER'])) {
39  $base_url = $_SERVER['HTTP_REFERER'];
40 } else {
41  $base_url = current_page_url();
42 }
43 
44 if ($count <= $limit && $offset == 0) {
45  // no need for pagination
46  return;
47 }
48 
49 $total_pages = (int) ceil($count / $limit);
50 $current_page = (int) ceil($offset / $limit) + 1;
51 
52 $pages = array();
53 
54 // determine starting page
55 $start_page = max(min([$current_page - 2, $total_pages - 4]), 1);
56 
57 // add previous
59 if ($prev_offset < 1) {
60  // don't include offset=0
61  $prev_offset = null;
62 }
63 
64 $pages['prev'] = [
65  'text' => elgg_echo('previous'),
66  'href' => elgg_http_add_url_query_elements($base_url, array($offset_key => $prev_offset))
67 ];
68 
69 if ($current_page == 1) {
70  $pages['prev']['disabled'] = true;
71 }
72 
73 // add first page to be listed
74 if (1 < $start_page) {
75  $pages[1] = [];
76 }
77 
78 // added dotted spacer
79 if (1 < ($start_page - 2)) {
80  $pages[] = ['text' => '...', 'disabled' => true];
81 } elseif ($start_page == 3) {
82  $pages[2] = [];
83 }
84 
85 $max = 1;
86 for ($page = $start_page; $page <= $total_pages; $page++) {
87  if ($max > 5) {
88  break;
89  }
90  $pages[$page] = [];
91  $max++;
92 }
93 
94 // added dotted spacer
95 if ($total_pages > ($start_page + 6)) {
96  $pages[] = ['text' => '...', 'disabled' => true];
97 } elseif (($start_page + 5) == ($total_pages - 1)) {
98  $pages[$total_pages - 1] = [];
99 }
100 
101 // add last page to be listed
102 if ($total_pages >= ($start_page + 5)) {
103  $pages[$total_pages] = [];
104 }
105 
106 // add next
108 if ($next_offset >= $count) {
109  $next_offset--;
110 }
111 
112 $pages['next'] = [
113  'text' => elgg_echo('next'),
114  'href' => elgg_http_add_url_query_elements($base_url, array($offset_key => $next_offset))
115 ];
116 
117 if ($current_page == $total_pages) {
118  $pages['next']['disabled'] = true;
119 }
120 
121 $list ="";
122 foreach ($pages as $page_num => $page) {
123  if ($page_num == $current_page) {
124  $list .= elgg_format_element('li', ['class' => 'elgg-state-selected'], "<span>$page_num</span>");
125  } else {
126  $href = elgg_extract('href', $page);
127  $text = elgg_extract('text', $page, $page_num);
128  $disabled = elgg_extract('disabled', $page, false);
129 
130  if (!$href && !$disabled) {
131  $page_offset = (($page_num - $current_page) * $limit) + $offset;
132  if ($page_offset <= 0) {
133  // don't include offset=0
134  $page_offset = null;
135  }
136  $href = elgg_http_add_url_query_elements($base_url, array($offset_key => $page_offset));
137  }
138 
139  if ($href && !$disabled) {
140  $link = elgg_view('output/url', array(
141  'href' => $href,
142  'text' => $text,
143  'is_trusted' => true,
144  ));
145  } else {
146  $link = elgg_format_element('span', [], $page['text']);
147  }
148 
149  $element_options = array();
150  if ($disabled) {
151  $element_options['class'] = 'elgg-state-disabled';
152  }
153 
154  $list .= elgg_format_element('li', $element_options, $link);
155  }
156 }
157 
158 echo elgg_format_element('ul', ['class' => 'elgg-pagination'], $list);
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1107
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:227
$current_page
Definition: pagination.php:50
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
if(!$count) $offset
Definition: pagination.php:25
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:1246
elgg_format_element($tag_name, array $attributes=array(), $text= '', array $options=array())
Format an HTML element.
Definition: output.php:215
$text
Definition: default.php:25
for($page=$start_page;$page<=$total_pages;$page++) if($total_pages >($start_page+6)) elseif(($start_page+5)==($total_pages-1)) if($total_pages >=($start_page+5)) $next_offset
Definition: pagination.php:107
if($prev_offset< 1) if($current_page==1) if(1< $start_page) if(1< ($start_page-2)) elseif($start_page==3) $max
Definition: pagination.php:85
$limit
Definition: userpicker.php:31
if($next_offset >=$count) if($current_page==$total_pages) $list
Definition: pagination.php:121
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$start_page
Definition: pagination.php:55
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:250
elgg echo
Translates a string.
Definition: languages.js:43
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
elgg_view($view, $vars=array(), $bypass=false, $ignored=false, $viewtype= '')
Return a parsed view.
Definition: views.php:354
$pages
Definition: pagination.php:52
if($count<=$limit &&$offset==0) $total_pages
Definition: pagination.php:49
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:31
$prev_offset
Definition: pagination.php:58
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
if(file_exists($welcome)) $vars
Definition: upgrade.php:93