Elgg  Version 4.3
pagination.php
Go to the documentation of this file.
1 <?php
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 $use_referer = (bool) elgg_extract('use_referer', $vars, get_input('use_referer', true));
35 
36 // some views pass an empty string for base_url
37 if (isset($vars['base_url']) && $vars['base_url']) {
38  $base_url = elgg_extract('base_url', $vars);
39 } elseif ($use_referer && elgg_is_xhr() && !empty($_SERVER['HTTP_REFERER'])) {
40  $base_url = $_SERVER['HTTP_REFERER'];
41 } else {
43 }
44 
46 
50  $link .= "#$url_fragment";
51  }
52  return $link;
53 };
54 
55 if ($count <= $limit && $offset == 0) {
56  // no need for pagination
57  return;
58 }
59 
60 $total_pages = (int) ceil($count / $limit);
61 $current_page = (int) ceil($offset / $limit) + 1;
62 
63 $pages = [];
64 
65 // determine starting page
66 $start_page = max(min([$current_page - 2, $total_pages - 4]), 1);
67 
68 // add previous
69 if ((bool) elgg_extract('pagination_show_previous', $vars, true) && ($current_page !== 1)) {
70  $prev_offset = $offset - $limit;
71  if ($prev_offset < 1) {
72  // don't include offset=0
73  $prev_offset = null;
74  }
75 
76  $pages['prev'] = [
77  'text' => elgg_extract('pagination_previous_text', $vars, elgg_echo('previous')),
78  'href' => $get_href($prev_offset),
79  'class' => 'elgg-pagination-previous',
80  ];
81 }
82 
83 // show in between page numbers
84 if ((bool) elgg_extract('pagination_show_numbers', $vars, true)) {
85  // add first page to be listed
86  if (1 < $start_page) {
87  $pages[1] = [];
88  }
89 
90  // added dotted spacer
91  if (1 < ($start_page - 2)) {
92  $pages[] = ['text' => '...', 'disabled' => true];
93  } elseif ($start_page == 3) {
94  $pages[2] = [];
95  }
96 
97  $max = 1;
98  for ($page = $start_page; $page <= $total_pages; $page++) {
99  if ($max > 5) {
100  break;
101  }
102  $pages[$page] = [];
103  $max++;
104  }
105 
106  // added dotted spacer
107  if ($total_pages > ($start_page + 6)) {
108  $pages[] = ['text' => '...', 'disabled' => true];
109  } elseif (($start_page + 5) == ($total_pages - 1)) {
110  $pages[$total_pages - 1] = [];
111  }
112 
113  // add last page to be listed
114  if ($total_pages >= ($start_page + 5)) {
115  $pages[$total_pages] = [];
116  }
117 }
118 
119 // add next
120 if ((bool) elgg_extract('pagination_show_next', $vars, true) && ($current_page !== $total_pages)) {
121  // add next
122  $next_offset = $offset + $limit;
123  if ($next_offset >= $count) {
124  $next_offset--;
125  }
126 
127  $pages['next'] = [
128  'text' => elgg_extract('pagination_next_text', $vars, elgg_echo('next')),
129  'href' => $get_href($next_offset),
130  'class' => 'elgg-pagination-next',
131  ];
132 }
133 
134 $list = '';
135 foreach ($pages as $page_num => $page) {
136  if ($page_num == $current_page) {
137  $list .= elgg_format_element('li', ['class' => 'elgg-state-selected'], "<span>{$page_num}</span>");
138  } else {
139  $href = elgg_extract('href', $page);
140  $text = elgg_extract('text', $page, $page_num);
141  $disabled = elgg_extract('disabled', $page, false);
142 
143  if (!$href && !$disabled) {
144  $page_offset = (($page_num - $current_page) * $limit) + $offset;
145  if ($page_offset <= 0) {
146  // don't include offset=0
147  $page_offset = null;
148  }
149  $href = $get_href($page_offset);
150  }
151 
152  if ($href && !$disabled) {
154  } else {
155  $link = elgg_format_element('span', [], $page['text']);
156  }
157 
158  $element_options = [
159  'class' => elgg_extract_class($page),
160  ];
161  if ($disabled) {
162  $element_options['class'][] = 'elgg-state-disabled';
163  }
164 
165  $list .= elgg_format_element('li', $element_options, $link);
166  }
167 }
168 
169 if (empty($list)) {
170  return;
171 }
172 
173 $pagination_class = elgg_extract_class($vars, ['elgg-pagination'], 'pagination_class');
174 $position = elgg_extract('position', $vars);
175 if (!empty($position)) {
176  $pagination_class[] = "elgg-pagination-{$position}";
177 }
178 
179 echo elgg_format_element('ul', ['class' => $pagination_class], $list);
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:481
$url_fragment
Definition: pagination.php:33
$position
Definition: pagination.php:174
$use_referer
Definition: pagination.php:34
$current_page
Definition: pagination.php:61
if(!$count) $offset
Definition: pagination.php:26
if($pagination &&($position== 'after'||$position== 'both')) $limit
Definition: list.php:108
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
elgg_extract_class(array $array, $existing=[], $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:569
if(!$item instanceof ElggEntity) $link
Definition: container.php:16
$page
Definition: admin.php:23
if(!$pagination &&$limit!==false &&!empty($items)&&count($items) >=$limit) $base_url
Definition: list.php:113
$count
Elgg pagination.
Definition: pagination.php:21
get_input($variable, $default=null, $filter_result=true)
Parameter input functions.
Definition: input.php:20
$start_page
Definition: pagination.php:66
elgg_format_element($tag_name, array $attributes=[], $text= '', array $options=[])
Format an HTML element.
Definition: output.php:135
elgg_get_current_url()
Returns the current page&#39;s complete URL.
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:547
$pages
Definition: pagination.php:63
if($count<=$limit &&$offset==0) $total_pages
Definition: pagination.php:60
elgg_view_url(string $href, string $text=null, array $options=[])
Helper function for outputting urls.
Definition: views.php:1519
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
$get_href
Definition: pagination.php:47
if(!$limit=(int) elgg_extract('limit', $vars, elgg_get_config('default_limit'))) $offset_key
Definition: pagination.php:32
elgg echo
Translates a string.
Definition: deprecated.js:530
$vars['head']
Definition: html.php:24
$base_url_has_fragment
Definition: pagination.php:45
$max
Definition: time.php:38
$text
Definition: button.php:32
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:76
elgg_get_config($name, $default=null)
Get an Elgg configuration value.
if((bool) elgg_extract('pagination_show_previous', $vars, true)&&($current_page!==1)) if((bool) elgg_extract('pagination_show_numbers', $vars, true)) if((bool) elgg_extract('pagination_show_next', $vars, true)&&($current_page!==$total_pages)) $list
Definition: pagination.php:134
foreach($pages as $page_num=> $page) if(empty($list)) $pagination_class
Definition: pagination.php:173