Subs-List.php 11 KB

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