Elgg  Version 6.1
pagination.php
Go to the documentation of this file.
1 <?php
21 $count = (int) elgg_extract('count', $vars, 0);
22 if (empty($count)) {
23  return;
24 }
25 
26 $offset = abs((int) elgg_extract('offset', $vars, 0));
27 // because you can say $vars['limit'] = 0
28 $limit = (int) elgg_extract('limit', $vars, elgg_get_config('default_limit'), false);
29 
30 $offset_key = elgg_extract('offset_key', $vars, 'offset');
31 $url_fragment = elgg_extract('url_fragment', $vars, '');
32 $use_referer = (bool) elgg_extract('use_referer', $vars, get_input('use_referer', true));
33 
34 // some views pass an empty string for base_url
35 if (isset($vars['base_url']) && $vars['base_url']) {
36  $base_url = elgg_extract('base_url', $vars);
37 } elseif ($use_referer && elgg_is_xhr() && !empty($_SERVER['HTTP_REFERER'])) {
38  $base_url = $_SERVER['HTTP_REFERER'];
39 } else {
41 }
42 
44 
48  $link .= "#$url_fragment";
49  }
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 = [];
63 
64 // determine starting page
65 $start_page = max(min([$current_page - 2, $total_pages - 4]), 1);
66 
67 // add previous
68 if ((bool) elgg_extract('pagination_show_previous', $vars, true) && ($current_page !== 1)) {
69  $prev_offset = $offset - $limit;
70  if ($prev_offset < 1) {
71  // don't include offset=0
72  $prev_offset = null;
73  }
74 
75  $pages['prev'] = [
76  'text' => elgg_extract('pagination_previous_text', $vars, elgg_echo('previous')),
77  'href' => $get_href($prev_offset),
78  'class' => 'elgg-pagination-previous',
79  ];
80 }
81 
82 // show in between page numbers
83 if ((bool) elgg_extract('pagination_show_numbers', $vars, true)) {
84  // add first page to be listed
85  if ($start_page > 1) {
86  $pages[1] = [];
87  }
88 
89  // added dotted spacer
90  if (($start_page - 2) > 1) {
91  $pages[] = ['text' => '...', 'disabled' => true];
92  } elseif ($start_page == 3) {
93  $pages[2] = [];
94  }
95 
96  $max = 1;
97  for ($page = $start_page; $page <= $total_pages; $page++) {
98  if ($max > 5) {
99  break;
100  }
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 
150  $href = $get_href($page_offset);
151  }
152 
153  if ($href && !$disabled) {
155  } else {
156  $link = elgg_format_element('span', [], $page['text']);
157  }
158 
159  $element_options = [
160  'class' => elgg_extract_class($page),
161  ];
162  if ($disabled) {
163  $element_options['class'][] = 'elgg-state-disabled';
164  }
165 
166  $list .= elgg_format_element('li', $element_options, $link);
167  }
168 }
169 
170 if (empty($list)) {
171  return;
172 }
173 
174 $pagination_class = elgg_extract_class($vars, ['elgg-pagination'], 'pagination_class');
175 $position = elgg_extract('position', $vars);
176 if (!empty($position)) {
177  $pagination_class[] = "elgg-pagination-{$position}";
178 }
179 
180 echo elgg_format_element('ul', ['class' => $pagination_class], $list);
$url_fragment
Definition: pagination.php:31
$position
Definition: pagination.php:175
$use_referer
Definition: pagination.php:32
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$current_page
Definition: pagination.php:60
if(empty($count)) $offset
Definition: pagination.php:26
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!$item instanceof ElggEntity) $link
Definition: container.php:16
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition: item.php:48
get_input(string $variable, $default=null, bool $filter_result=true)
Parameter input functions.
Definition: input.php:20
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
$page
Definition: admin.php:23
if(!$pagination &&$limit!==false &&!empty($items)&&count($items) >=$limit) $base_url
Definition: list.php:114
$count
Elgg pagination.
Definition: pagination.php:21
$limit
Definition: pagination.php:28
$start_page
Definition: pagination.php:65
elgg_get_current_url()
Returns the current page&#39;s complete URL.
$offset_key
Definition: pagination.php:30
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:183
$pages
Definition: pagination.php:62
$vars
Definition: theme.php:5
if($count<=$limit &&$offset==0) $total_pages
Definition: pagination.php:59
elgg_view_url(string $href, string $text=null, array $options=[])
Helper function for outputting urls.
Definition: views.php:1427
$get_href
Definition: pagination.php:45
$base_url_has_fragment
Definition: pagination.php:43
elgg_format_element(string $tag_name, array $attributes=[], string $text= '', array $options=[])
Format an HTML element.
Definition: output.php:145
elgg_extract_class(array $array, array|string $existing=[], string $extract_key= 'class')
Extract class names from an array, optionally merging into a preexisting set.
Definition: elgglib.php:279
$max
Definition: time.php:37
$text
Definition: button.php:33
elgg_is_xhr()
Checks whether the request was requested via ajax.
Definition: actions.php:78
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:174