Elgg  Version master
list.php
Go to the documentation of this file.
1 <?php
20 
21 if (!$items) {
22  echo elgg_view('page/components/no_results', $vars);
23  echo elgg_view('page/components/list/out_of_bounds', $vars);
24  return;
25 }
26 
27 if (!is_array($items) || count($items) == 0) {
28  return;
29 }
30 
31 $position = elgg_extract('position', $vars, 'after');
32 $pagination_behaviour = elgg_extract('pagination_behaviour', $vars, elgg_get_config('pagination_behaviour'));
33 $pagination = (bool) elgg_extract('pagination', $vars, true);
34 if (elgg_in_context('widget')) {
35  // widgets do not show pagination
36  $pagination = false;
37 }
38 
39 $pagination_before_options = (array) elgg_extract('pagination_before_options', $vars, []);
40 unset($vars['pagination_before_options']);
41 $pagination_after_options = (array) elgg_extract('pagination_after_options', $vars, []);
42 unset($vars['pagination_after_options']);
43 
44 if (in_array($pagination_behaviour, ['ajax-append', 'ajax-append-auto'])) {
45  $position = 'both';
46  $vars['position'] = $position;
47 
48  // set before options
49  $pagination_before_options['pagination_show_numbers'] = false;
50  $pagination_before_options['pagination_show_next'] = false;
51  $pagination_before_options['pagination_show_previous'] = true;
52  $pagination_before_options['pagination_previous_text'] = elgg_echo('ajax:pagination:load_more');
53 
54  // set after options
55  $pagination_after_options['pagination_show_numbers'] = false;
56  $pagination_after_options['pagination_show_next'] = true;
57  $pagination_after_options['pagination_show_previous'] = false;
58  $pagination_after_options['pagination_next_text'] = elgg_echo('ajax:pagination:load_more');
59 }
60 
61 $list_classes = elgg_extract_class($vars, 'elgg-list', 'list_class');
62 
63 $list_item_view = elgg_extract('list_item_view', $vars);
65  $list_item_view = 'page/components/list/item';
66 }
67 
68 $index = 0;
70 foreach ($items as $item) {
71  $item_view_vars = $vars;
72  $item_view_vars['list_item_index'] = $index;
73  $item_view = elgg_view_list_item($item, $item_view_vars);
74  if (empty($item_view)) {
75  continue;
76  }
77 
78  $list_item_vars = $vars;
79  $list_item_vars['content'] = $item_view;
80  $list_item_vars['item'] = $item;
81 
82  $list_items .= elgg_view($list_item_view, $list_item_vars);
83 
84  $index++;
85 }
86 
87 $result = '';
88 if ($pagination && ($position == 'before' || $position == 'both')) {
89  $pagination_options = array_merge($vars, $pagination_before_options);
90  $pagination_options['position'] = 'before';
91 
92  $result .= elgg_view('navigation/pagination', $pagination_options);
93 }
94 
95 if (empty($list_items)) {
96  // there are scenarios where item views do not output html. In those cases show the no results info
97  $result .= elgg_view('page/components/no_results', $vars);
98 } else {
99  $result .= elgg_format_element('ul', ['class' => $list_classes], $list_items);
100 }
101 
102 if ($pagination && ($position == 'after' || $position == 'both')) {
103  $pagination_options = array_merge($vars, $pagination_after_options);
104  $pagination_options['position'] = 'after';
105 
106  $result .= elgg_view('navigation/pagination', $pagination_options);
107 }
108 
110 if (!$pagination && $limit !== false && !empty($items) && count($items) >= $limit) {
111  $result .= elgg_view('page/components/list/widget_more', $vars);
112 }
113 
114 $base_url = elgg_extract('base_url', $vars);
115 if (!empty($base_url)) {
116  // strip offset and limit from base url as they always change when navigating and therefore should not be part of base url
117  $base_url = elgg_http_add_url_query_elements($base_url, ['offset' => null, 'limit' => null]);
118 }
119 
120 // make a unique ID for AJAX pagination
121 $id = null;
122 if ($pagination) {
123  $id = 'list-container-' . elgg_build_hmac([
126  elgg_extract('item_view', $vars),
127  elgg_extract('type', $vars),
128  elgg_extract('subtype', $vars),
129  elgg_extract('offset_key', $vars),
130  elgg_extract('pagination_class', $vars),
131  $base_url,
132  ])->getToken();
133 }
134 
135 $container_classes = ['elgg-list-container'];
136 if ($pagination && ($pagination_behaviour !== 'navigate')) {
137  $container_classes[] = "elgg-list-container-{$pagination_behaviour}";
138  if ($pagination_behaviour === 'ajax-append-auto') {
139  // make sure default logic for ajax append works
140  $container_classes[] = 'elgg-list-container-ajax-append';
141  }
142 
143  if (elgg_view_exists("page/components/list/{$pagination_behaviour}.mjs")) {
144  elgg_import_esm("page/components/list/{$pagination_behaviour}");
145  }
146 }
147 
148 echo elgg_format_element('div', ['class' => $container_classes, 'id' => $id], $result);
if(!$items) $item
Definition: delete.php:13
$list_items
Definition: list.php:69
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
elgg_in_context(string $context)
Check if this context exists anywhere in the stack.
Definition: context.php:78
if(elgg_in_context('widget')) $pagination_before_options
Definition: list.php:39
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if($pagination &&($position== 'after'||$position== 'both')) $limit
Definition: list.php:109
$pagination_behaviour
Definition: list.php:32
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:254
if(!$pagination &&$limit!==false &&!empty($items)&&count($items) >=$limit) $base_url
Definition: list.php:114
elgg_view(string $view, array $vars=[], string $viewtype= '')
Return a parsed view.
Definition: views.php:156
$item_view
elgg_view_list_item($item, array $vars=[])
View an item in a list.
Definition: views.php:1232
$list_item_view
Definition: list.php:63
elgg_import_esm(string $name)
Helper functions for external files like css/js.
$items
View a list of items.
Definition: list.php:19
if(!$items) if(!is_array($items)||count($items)==0) $position
Definition: list.php:31
elgg_http_add_url_query_elements(string $url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:181
$pagination
Definition: list.php:33
if($pagination) $container_classes
Definition: list.php:135
$vars
Definition: theme.php:5
if(in_array($pagination_behaviour, ['ajax-append', 'ajax-append-auto'])) $list_classes
Definition: list.php:61
foreach($items as $item) $result
Definition: list.php:87
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:277
if(empty($list_item_view)||!elgg_view_exists($list_item_view)) $index
Definition: list.php:68
elgg_build_hmac($data)
Get an HMAC token builder/validator object.
Definition: actions.php:56
elgg_view_exists(string $view, string $viewtype= '', bool $recurse=true)
Returns whether the specified view exists.
Definition: views.php:131
$pagination_after_options
Definition: list.php:41
if(!empty($base_url)) $id
Definition: list.php:121