1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file contains a standard way of displaying lists for SMF. |
||
5 | * Simple Machines Forum (SMF) |
||
6 | * |
||
7 | * @package SMF |
||
8 | * @author Simple Machines https://www.simplemachines.org |
||
9 | * @copyright 2022 Simple Machines and individual contributors |
||
10 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||
11 | * |
||
12 | * @version 2.1.0 |
||
13 | */ |
||
14 | |||
15 | if (!defined('SMF')) |
||
16 | die('No direct access...'); |
||
17 | |||
18 | /** |
||
19 | * Create a new list |
||
20 | * |
||
21 | * @param array $listOptions An array of options for the list - 'id', 'columns', 'items_per_page', 'get_count', etc. |
||
22 | */ |
||
23 | function createList($listOptions) |
||
24 | { |
||
25 | global $context, $smcFunc; |
||
26 | |||
27 | assert(isset($listOptions['id'])); |
||
28 | assert(isset($listOptions['columns'])); |
||
29 | assert(is_array($listOptions['columns'])); |
||
30 | |||
31 | if (!isset($listOptions['get_count']['value'])) |
||
32 | assert((empty($listOptions['items_per_page']) || |
||
33 | (isset($listOptions['get_count']['function'], $listOptions['base_href']) && |
||
34 | is_numeric($listOptions['items_per_page'] |
||
35 | )))); |
||
36 | |||
37 | assert((empty($listOptions['default_sort_col']) || isset($listOptions['columns'][$listOptions['default_sort_col']]))); |
||
38 | assert((!isset($listOptions['form']) || isset($listOptions['form']['href']))); |
||
39 | |||
40 | call_integration_hook('integrate_' . $listOptions['id'], array(&$listOptions)); |
||
41 | |||
42 | // All the context data will be easily accessible by using a reference. |
||
43 | $context[$listOptions['id']] = array(); |
||
44 | $list_context = &$context[$listOptions['id']]; |
||
45 | |||
46 | // Figure out the sort. |
||
47 | if (empty($listOptions['default_sort_col'])) |
||
48 | { |
||
49 | $list_context['sort'] = array(); |
||
50 | $sort = '1=1'; |
||
51 | } |
||
52 | else |
||
53 | { |
||
54 | $request_var_sort = isset($listOptions['request_vars']['sort']) ? $listOptions['request_vars']['sort'] : 'sort'; |
||
55 | $request_var_desc = isset($listOptions['request_vars']['desc']) ? $listOptions['request_vars']['desc'] : 'desc'; |
||
56 | if (isset($_REQUEST[$request_var_sort], $listOptions['columns'][$_REQUEST[$request_var_sort]], $listOptions['columns'][$_REQUEST[$request_var_sort]]['sort'])) |
||
57 | $list_context['sort'] = array( |
||
58 | 'id' => $_REQUEST[$request_var_sort], |
||
59 | 'desc' => isset($_REQUEST[$request_var_desc]) && isset($listOptions['columns'][$_REQUEST[$request_var_sort]]['sort']['reverse']), |
||
60 | ); |
||
61 | else |
||
62 | $list_context['sort'] = array( |
||
63 | 'id' => $listOptions['default_sort_col'], |
||
64 | 'desc' => (!empty($listOptions['default_sort_dir']) && $listOptions['default_sort_dir'] == 'desc') || (!empty($listOptions['columns'][$listOptions['default_sort_col']]['sort']['default']) && substr($listOptions['columns'][$listOptions['default_sort_col']]['sort']['default'], -4, 4) == 'desc') ? true : false, |
||
65 | ); |
||
66 | |||
67 | // Set the database column sort. |
||
68 | $sort = $listOptions['columns'][$list_context['sort']['id']]['sort'][$list_context['sort']['desc'] ? 'reverse' : 'default']; |
||
69 | } |
||
70 | |||
71 | $list_context['start_var_name'] = isset($listOptions['start_var_name']) ? $listOptions['start_var_name'] : 'start'; |
||
72 | // In some cases the full list must be shown, regardless of the amount of items. |
||
73 | if (empty($listOptions['items_per_page'])) |
||
74 | { |
||
75 | $list_context['start'] = 0; |
||
76 | $list_context['items_per_page'] = 0; |
||
77 | } |
||
78 | // With items per page set, calculate total number of items and page index. |
||
79 | else |
||
80 | { |
||
81 | // First get an impression of how many items to expect. |
||
82 | if (isset($listOptions['get_count']['value']) && (is_int($listOptions['get_count']['value']) || ctype_digit($listOptions['get_count']['value']))) |
||
83 | $list_context['total_num_items'] = $listOptions['get_count']['value']; |
||
84 | |||
85 | else |
||
86 | { |
||
87 | if (isset($listOptions['get_count']['file'])) |
||
88 | require_once($listOptions['get_count']['file']); |
||
89 | |||
90 | $call = call_helper($listOptions['get_count']['function'], true); |
||
91 | $list_context['total_num_items'] = call_user_func_array($call, empty($listOptions['get_count']['params']) ? |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
92 | array() : array_values($listOptions['get_count']['params'])); |
||
93 | } |
||
94 | |||
95 | // Default the start to the beginning...sounds logical. |
||
96 | $list_context['start'] = isset($_REQUEST[$list_context['start_var_name']]) ? (int) $_REQUEST[$list_context['start_var_name']] : 0; |
||
97 | $list_context['items_per_page'] = $listOptions['items_per_page']; |
||
98 | |||
99 | // Then create a page index. |
||
100 | if ($list_context['total_num_items'] > $list_context['items_per_page']) |
||
101 | $list_context['page_index'] = constructPageIndex($listOptions['base_href'] . (empty($list_context['sort']) ? '' : ';' . $request_var_sort . '=' . $list_context['sort']['id'] . ($list_context['sort']['desc'] ? ';' . $request_var_desc : '')) . ($list_context['start_var_name'] != 'start' ? ';' . $list_context['start_var_name'] . '=%1$d' : ''), $list_context['start'], $list_context['total_num_items'], $list_context['items_per_page'], $list_context['start_var_name'] != 'start'); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
102 | } |
||
103 | |||
104 | // Prepare the headers of the table. |
||
105 | $list_context['headers'] = array(); |
||
106 | foreach ($listOptions['columns'] as $column_id => $column) |
||
107 | $list_context['headers'][] = array( |
||
108 | 'id' => $column_id, |
||
109 | 'label' => isset($column['header']['eval']) ? eval($column['header']['eval']) : (isset($column['header']['value']) ? $column['header']['value'] : ''), |
||
0 ignored issues
–
show
|
|||
110 | 'href' => empty($listOptions['default_sort_col']) || empty($column['sort']) ? '' : $listOptions['base_href'] . ';' . $request_var_sort . '=' . $column_id . ($column_id === $list_context['sort']['id'] && !$list_context['sort']['desc'] && isset($column['sort']['reverse']) ? ';' . $request_var_desc : '') . (empty($list_context['start']) ? '' : ';' . $list_context['start_var_name'] . '=' . $list_context['start']), |
||
111 | 'sort_image' => empty($listOptions['default_sort_col']) || empty($column['sort']) || $column_id !== $list_context['sort']['id'] ? null : ($list_context['sort']['desc'] ? 'down' : 'up'), |
||
112 | 'class' => isset($column['header']['class']) ? $column['header']['class'] : '', |
||
113 | 'style' => isset($column['header']['style']) ? $column['header']['style'] : '', |
||
114 | 'colspan' => isset($column['header']['colspan']) ? $column['header']['colspan'] : '', |
||
115 | ); |
||
116 | |||
117 | // We know the amount of columns, might be useful for the template. |
||
118 | $list_context['num_columns'] = count($listOptions['columns']); |
||
119 | $list_context['width'] = isset($listOptions['width']) ? $listOptions['width'] : '0'; |
||
120 | |||
121 | // Call the function and include which items we want and in what order. |
||
122 | if (!empty($listOptions['get_items']['value']) && is_array($listOptions['get_items']['value'])) |
||
123 | $list_items = $listOptions['get_items']['value']; |
||
124 | |||
125 | else |
||
126 | { |
||
127 | // Get the file with the function for the item list. |
||
128 | if (isset($listOptions['get_items']['file'])) |
||
129 | require_once($listOptions['get_items']['file']); |
||
130 | |||
131 | $call = call_helper($listOptions['get_items']['function'], true); |
||
132 | $list_items = call_user_func_array($call, array_merge(array($list_context['start'], $list_context['items_per_page'], $sort), empty($listOptions['get_items']['params']) ? array() : $listOptions['get_items']['params'])); |
||
133 | $list_items = empty($list_items) ? array() : $list_items; |
||
134 | } |
||
135 | |||
136 | // Loop through the list items to be shown and construct the data values. |
||
137 | $list_context['rows'] = array(); |
||
138 | foreach ($list_items as $item_id => $list_item) |
||
139 | { |
||
140 | $cur_row = array(); |
||
141 | foreach ($listOptions['columns'] as $column_id => $column) |
||
142 | { |
||
143 | $cur_data = array(); |
||
144 | |||
145 | // A value straight from the database? |
||
146 | if (isset($column['data']['db'])) |
||
147 | $cur_data['value'] = $list_item[$column['data']['db']]; |
||
148 | |||
149 | // Take the value from the database and make it HTML safe. |
||
150 | elseif (isset($column['data']['db_htmlsafe'])) |
||
151 | $cur_data['value'] = $smcFunc['htmlspecialchars']($list_item[$column['data']['db_htmlsafe']]); |
||
152 | |||
153 | // Using sprintf is probably the most readable way of injecting data. |
||
154 | elseif (isset($column['data']['sprintf'])) |
||
155 | { |
||
156 | $params = array(); |
||
157 | foreach ($column['data']['sprintf']['params'] as $sprintf_param => $htmlsafe) |
||
158 | $params[] = $htmlsafe ? $smcFunc['htmlspecialchars']($list_item[$sprintf_param]) : $list_item[$sprintf_param]; |
||
159 | $cur_data['value'] = vsprintf($column['data']['sprintf']['format'], $params); |
||
160 | } |
||
161 | |||
162 | // The most flexible way probably is applying a custom function. |
||
163 | elseif (isset($column['data']['function'])) |
||
164 | $cur_data['value'] = call_user_func_array($column['data']['function'], array($list_item)); |
||
165 | |||
166 | // A modified value (inject the database values). |
||
167 | elseif (isset($column['data']['eval'])) |
||
168 | $cur_data['value'] = eval(preg_replace('~%([a-zA-Z0-9\-_]+)%~', '$list_item[\'$1\']', $column['data']['eval'])); |
||
0 ignored issues
–
show
|
|||
169 | |||
170 | // A literal value. |
||
171 | elseif (isset($column['data']['value'])) |
||
172 | $cur_data['value'] = $column['data']['value']; |
||
173 | |||
174 | // Empty value. |
||
175 | else |
||
176 | $cur_data['value'] = ''; |
||
177 | |||
178 | // Allow for basic formatting. |
||
179 | if (!empty($column['data']['comma_format'])) |
||
180 | $cur_data['value'] = comma_format($cur_data['value']); |
||
181 | elseif (!empty($column['data']['timeformat'])) |
||
182 | $cur_data['value'] = timeformat($cur_data['value']); |
||
183 | |||
184 | // Set a style class for this column? |
||
185 | if (isset($column['data']['class'])) |
||
186 | $cur_data['class'] = $column['data']['class']; |
||
187 | |||
188 | // Fully customized styling for the cells in this column only. |
||
189 | if (isset($column['data']['style'])) |
||
190 | $cur_data['style'] = $column['data']['style']; |
||
191 | |||
192 | // Add the data cell properties to the current row. |
||
193 | $cur_row[$column_id] = $cur_data; |
||
194 | } |
||
195 | |||
196 | // Maybe we wat set a custom class for the row based on the data in the row itself |
||
197 | if (isset($listOptions['data_check'])) |
||
198 | { |
||
199 | if (isset($listOptions['data_check']['class'])) |
||
200 | $list_context['rows'][$item_id]['class'] = $listOptions['data_check']['class']($list_item); |
||
201 | if (isset($listOptions['data_check']['style'])) |
||
202 | $list_context['rows'][$item_id]['style'] = $listOptions['data_check']['style']($list_item); |
||
203 | } |
||
204 | |||
205 | // Insert the row into the list. |
||
206 | $list_context['rows'][$item_id]['data'] = $cur_row; |
||
207 | } |
||
208 | |||
209 | // The title is currently optional. |
||
210 | if (isset($listOptions['title'])) |
||
211 | $list_context['title'] = $listOptions['title']; |
||
212 | |||
213 | // In case there's a form, share it with the template context. |
||
214 | if (isset($listOptions['form'])) |
||
215 | { |
||
216 | $list_context['form'] = $listOptions['form']; |
||
217 | |||
218 | if (!isset($list_context['form']['hidden_fields'])) |
||
219 | $list_context['form']['hidden_fields'] = array(); |
||
220 | |||
221 | // Always add a session check field. |
||
222 | $list_context['form']['hidden_fields'][$context['session_var']] = $context['session_id']; |
||
223 | |||
224 | // Will this do a token check? |
||
225 | if (isset($listOptions['form']['token'])) |
||
226 | $list_context['form']['hidden_fields'][$context[$listOptions['form']['token'] . '_token_var']] = $context[$listOptions['form']['token'] . '_token']; |
||
227 | |||
228 | // Include the starting page as hidden field? |
||
229 | if (!empty($list_context['form']['include_start']) && !empty($list_context['start'])) |
||
230 | $list_context['form']['hidden_fields'][$list_context['start_var_name']] = $list_context['start']; |
||
231 | |||
232 | // If sorting needs to be the same after submitting, add the parameter. |
||
233 | if (!empty($list_context['form']['include_sort']) && !empty($list_context['sort'])) |
||
234 | { |
||
235 | $list_context['form']['hidden_fields']['sort'] = $list_context['sort']['id']; |
||
236 | if ($list_context['sort']['desc']) |
||
237 | $list_context['form']['hidden_fields']['desc'] = 1; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | // Wanna say something nice in case there are no items? |
||
242 | if (isset($listOptions['no_items_label'])) |
||
243 | { |
||
244 | $list_context['no_items_label'] = $listOptions['no_items_label']; |
||
245 | $list_context['no_items_align'] = isset($listOptions['no_items_align']) ? $listOptions['no_items_align'] : ''; |
||
246 | } |
||
247 | |||
248 | // A list can sometimes need a few extra rows above and below. |
||
249 | if (isset($listOptions['additional_rows'])) |
||
250 | { |
||
251 | $list_context['additional_rows'] = array(); |
||
252 | foreach ($listOptions['additional_rows'] as $row) |
||
253 | { |
||
254 | if (empty($row)) |
||
255 | continue; |
||
256 | |||
257 | // Supported row positions: top_of_list, after_title, |
||
258 | // above_column_headers, below_table_data, bottom_of_list. |
||
259 | if (!isset($list_context['additional_rows'][$row['position']])) |
||
260 | $list_context['additional_rows'][$row['position']] = array(); |
||
261 | $list_context['additional_rows'][$row['position']][] = $row; |
||
262 | } |
||
263 | } |
||
264 | |||
265 | // Add an option for inline JavaScript. |
||
266 | if (isset($listOptions['javascript'])) |
||
267 | $list_context['javascript'] = $listOptions['javascript']; |
||
268 | |||
269 | // Make sure the template is loaded. |
||
270 | loadTemplate('GenericList'); |
||
271 | } |
||
272 | |||
273 | ?> |