Subs-List.php 11 KB

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