Subs-List.php 11 KB

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