Elgg  Version 2.3
pagination.php
Go to the documentation of this file.
1 <?php
16 if (elgg_in_context('widget')) {
17  // widgets do not show pagination
18  return;
19 }
20 
21 $count = (int) elgg_extract('count', $vars, 0);
22 if (!$count) {
23  return;
24 }
25 
26 $offset = abs((int) elgg_extract('offset', $vars, 0));
27 // because you can say $vars['limit'] = 0
28 if (!$limit = (int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) {
29  $limit = 10;
30 }
31 
32 $offset_key = elgg_extract('offset_key', $vars, 'offset');
33 $url_fragment = elgg_extract('url_fragment', $vars, '');
34 
35 // some views pass an empty string for base_url
36 if (isset($vars['base_url']) && $vars['base_url']) {
37  $base_url = $vars['base_url'];
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 $base_url_has_fragment = preg_match('~#.~', $base_url);
45 
49  $link .= "#$url_fragment";
50  }
51  return $link;
52 };
53 
54 if ($count <= $limit && $offset == 0) {
55  // no need for pagination
56  return;
57 }
58 
59 $total_pages = (int) ceil($count / $limit);
60 $current_page = (int) ceil($offset / $limit) + 1;
61 
62 $pages = array();
63 
64 // determine starting page
65 $start_page = max(min([$current_page - 2, $total_pages - 4]), 1);
66 
67 // add previous
69 if ($prev_offset < 1) {
70  // don't include offset=0
71  $prev_offset = null;
72 }
73 
74 $pages['prev'] = [
75  'text' => elgg_echo('previous'),
76  'href' => $get_href($prev_offset),
77 ];
78 
79 if ($current_page == 1) {
80  $pages['prev']['disabled'] = true;
81 }
82 
83 // add first page to be listed
84 if (1 < $start_page) {
85  $pages[1] = [];
86 }
87 
88 // added dotted spacer
89 if (1 < ($start_page - 2)) {
90  $pages[] = ['text' => '...', 'disabled' => true];
91 } elseif ($start_page == 3) {
92  $pages[2] = [];
93 }
94 
95 $max = 1;
96 for ($page = $start_page; $page <= $total_pages; $page++) {
97  if ($max > 5) {
98  break;
99  }
100  $pages[$page] = [];
101  $max++;
102 }
103 
104 // added dotted spacer
105 if ($total_pages > ($start_page + 6)) {
106  $pages[] = ['text' => '...', 'disabled' => true];
107 } elseif (($start_page + 5) == ($total_pages - 1)) {
108  $pages[$total_pages - 1] = [];
109 }
110 
111 // add last page to be listed
112 if ($total_pages >= ($start_page + 5)) {
113  $pages[$total_pages] = [];
114 }
115 
116 // add next
118 if ($next_offset >= $count) {
119  $next_offset--;
120 }
121 
122 $pages['next'] = [
123  'text' => elgg_echo('next'),
124  'href' => $get_href($next_offset),
125 ];
126 
127 if ($current_page == $total_pages) {
128  $pages['next']['disabled'] = true;
129 }
130 
131 $list ="";
132 foreach ($pages as $page_num => $page) {
133  if ($page_num == $current_page) {
134  $list .= elgg_format_element('li', ['class' => 'elgg-state-selected'], "<span>$page_num</span>");
135  } else {
136  $href = elgg_extract('href', $page);
137  $text = elgg_extract('text', $page, $page_num);
138  $disabled = elgg_extract('disabled', $page, false);
139 
140  if (!$href && !$disabled) {
141  $page_offset = (($page_num - $current_page) * $limit) + $offset;
142  if ($page_offset <= 0) {
143  // don't include offset=0
144  $page_offset = null;
145  }
146  $href = $get_href($page_offset);
147  }
148 
149  if ($href && !$disabled) {
150  $link = elgg_view('output/url', array(
151  'href' => $href,
152  'text' => $text,
153  'is_trusted' => true,
154  ));
155  } else {
156  $link = elgg_format_element('span', [], $page['text']);
157  }
158 
159  $element_options = array();
160  if ($disabled) {
161  $element_options['class'] = 'elgg-state-disabled';
162  }
163 
164  $list .= elgg_format_element('li', $element_options, $link);
165  }
166 }
167 
168 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:1199
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$url_fragment
Definition: pagination.php:33
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:237
$current_page
Definition: pagination.php:60
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
if(!$count) $offset
Definition: pagination.php:26
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:65
$link
Definition: container.php:14
$vars['entity']
elgg_format_element($tag_name, array $attributes=array(), $text= '', array $options=array())
Format an HTML element.
Definition: output.php:208
$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:117
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:95
$limit
Definition: userpicker.php:38
if($next_offset >=$count) if($current_page==$total_pages) $list
Definition: pagination.php:131
$start_page
Definition: pagination.php:65
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
elgg echo
Translates a string.
Definition: languages.js:48
elgg_view($view, $vars=array(), $ignore1=false, $ignore2=false, $viewtype= '')
Return a parsed view.
Definition: views.php:336
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1375
$pages
Definition: pagination.php:62
if($count<=$limit &&$offset==0) $total_pages
Definition: pagination.php:59
$get_href
Definition: pagination.php:46
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:32
$prev_offset
Definition: pagination.php:68
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
$base_url_has_fragment
Definition: pagination.php:44
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5