table.func.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * @file
  4. * Stub file for bootstrap_table().
  5. */
  6. /**
  7. * Returns HTML for a table.
  8. *
  9. * @param array $variables
  10. * An associative array containing:
  11. * - header: An array containing the table headers. Each element of the array
  12. * can be either a localized string or an associative array with the
  13. * following keys:
  14. * - "data": The localized title of the table column.
  15. * - "field": The database field represented in the table column (required
  16. * if user is to be able to sort on this column).
  17. * - "sort": A default sort order for this column ("asc" or "desc"). Only
  18. * one column should be given a default sort order because table sorting
  19. * only applies to one column at a time.
  20. * - Any HTML attributes, such as "colspan", to apply to the column header
  21. * cell.
  22. * - rows: An array of table rows. Every row is an array of cells, or an
  23. * associative array with the following keys:
  24. * - "data": an array of cells
  25. * - Any HTML attributes, such as "class", to apply to the table row.
  26. * - "no_striping": a boolean indicating that the row should receive no
  27. * 'even / odd' styling. Defaults to FALSE.
  28. * Each cell can be either a string or an associative array with the
  29. * following keys:
  30. * - "data": The string to display in the table cell.
  31. * - "header": Indicates this cell is a header.
  32. * - Any HTML attributes, such as "colspan", to apply to the table cell.
  33. * Here's an example for $rows:
  34. * @code
  35. * $rows = array(
  36. * // Simple row
  37. * array(
  38. * 'Cell 1', 'Cell 2', 'Cell 3'
  39. * ),
  40. * // Row with attributes on the row and some of its cells.
  41. * array(
  42. * 'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => array('funky')
  43. * )
  44. * );
  45. * @endcode
  46. * - footer: An array containing the table footer. Each element of the array
  47. * can be either a localized string or an associative array with the
  48. * following keys:
  49. * - "data": The localized title of the table column.
  50. * - "field": The database field represented in the table column (required
  51. * if user is to be able to sort on this column).
  52. * - "sort": A default sort order for this column ("asc" or "desc"). Only
  53. * one column should be given a default sort order because table sorting
  54. * only applies to one column at a time.
  55. * - Any HTML attributes, such as "colspan", to apply to the column footer
  56. * cell.
  57. * - attributes: An array of HTML attributes to apply to the table tag.
  58. * - caption: A localized string to use for the <caption> tag.
  59. * - colgroups: An array of column groups. Each element of the array can be
  60. * either:
  61. * - An array of columns, each of which is an associative array of HTML
  62. * attributes applied to the COL element.
  63. * - An array of attributes applied to the COLGROUP element, which must
  64. * include a "data" attribute. To add attributes to COL elements, set the
  65. * "data" attribute with an array of columns, each of which is an
  66. * associative array of HTML attributes.
  67. * Here's an example for $colgroup:
  68. * @code
  69. * $colgroup = array(
  70. * // COLGROUP with one COL element.
  71. * array(
  72. * array(
  73. * 'class' => array('funky'), // Attribute for the COL element.
  74. * ),
  75. * ),
  76. * // Colgroup with attributes and inner COL elements.
  77. * array(
  78. * 'data' => array(
  79. * array(
  80. * 'class' => array('funky'), // Attribute for the COL element.
  81. * ),
  82. * ),
  83. * 'class' => array('jazzy'), // Attribute for the COLGROUP element.
  84. * ),
  85. * );
  86. * @endcode
  87. * These optional tags are used to group and set properties on columns
  88. * within a table. For example, one may easily group three columns and
  89. * apply same background style to all.
  90. * - sticky: Use a "sticky" table header.
  91. * - empty: The message to display in an extra row if table does not have any
  92. * rows.
  93. *
  94. * @return string
  95. * The constructed HTML.
  96. *
  97. * @see theme_table()
  98. *
  99. * @ingroup theme_functions
  100. */
  101. function bootstrap_table($variables) {
  102. $header = $variables['header'];
  103. $rows = $variables['rows'];
  104. $footer = $variables['footer'];
  105. $attributes = $variables['attributes'];
  106. $caption = $variables['caption'];
  107. $colgroups = $variables['colgroups'];
  108. $sticky = $variables['sticky'];
  109. $empty = $variables['empty'];
  110. $responsive = $variables['responsive'];
  111. // Add sticky headers, if applicable.
  112. if (count($header) && $sticky) {
  113. drupal_add_js('misc/tableheader.js');
  114. // Add 'sticky-enabled' class to the table to identify it for JS.
  115. // This is needed to target tables constructed by this function.
  116. $attributes['class'][] = 'sticky-enabled';
  117. }
  118. $output = '';
  119. if ($responsive) {
  120. $output .= "<div class=\"table-responsive\">\n";
  121. }
  122. $output .= '<table' . drupal_attributes($attributes) . ">\n";
  123. if (isset($caption)) {
  124. $output .= '<caption>' . $caption . "</caption>\n";
  125. }
  126. // Format the table columns:
  127. if (count($colgroups)) {
  128. foreach ($colgroups as $number => $colgroup) {
  129. $attributes = array();
  130. // Check if we're dealing with a simple or complex column.
  131. if (isset($colgroup['data'])) {
  132. foreach ($colgroup as $key => $value) {
  133. if ($key == 'data') {
  134. $cols = $value;
  135. }
  136. else {
  137. $attributes[$key] = $value;
  138. }
  139. }
  140. }
  141. else {
  142. $cols = $colgroup;
  143. }
  144. // Build colgroup.
  145. if (is_array($cols) && count($cols)) {
  146. $output .= ' <colgroup' . drupal_attributes($attributes) . '>';
  147. $i = 0;
  148. foreach ($cols as $col) {
  149. $output .= ' <col' . drupal_attributes($col) . ' />';
  150. }
  151. $output .= " </colgroup>\n";
  152. }
  153. else {
  154. $output .= ' <colgroup' . drupal_attributes($attributes) . " />\n";
  155. }
  156. }
  157. }
  158. // Add the 'empty' row message if available.
  159. if (!count($rows) && $empty) {
  160. $header_count = 0;
  161. foreach ($header as $header_cell) {
  162. if (is_array($header_cell)) {
  163. $header_count += isset($header_cell['colspan']) ? $header_cell['colspan'] : 1;
  164. }
  165. else {
  166. $header_count++;
  167. }
  168. }
  169. $rows[] = array(
  170. array(
  171. 'data' => $empty,
  172. 'colspan' => $header_count,
  173. 'class' => array('empty', 'message'),
  174. ),
  175. );
  176. }
  177. // Format the table header:
  178. if (count($header)) {
  179. $ts = tablesort_init($header);
  180. // HTML requires that the thead tag has tr tags in it followed by tbody
  181. // tags. Using ternary operator to check and see if we have any rows.
  182. $output .= (count($rows) ? ' <thead><tr>' : ' <tr>');
  183. foreach ($header as $cell) {
  184. $cell = tablesort_header($cell, $header, $ts);
  185. $output .= _theme_table_cell($cell, TRUE);
  186. }
  187. // Using ternary operator to close the tags based on whether or not there
  188. // are rows.
  189. $output .= (count($rows) ? " </tr></thead>\n" : "</tr>\n");
  190. }
  191. else {
  192. $ts = array();
  193. }
  194. // Format the table rows:
  195. if (count($rows)) {
  196. $output .= "<tbody>\n";
  197. foreach ($rows as $row) {
  198. // Check if we're dealing with a simple or complex row.
  199. if (isset($row['data'])) {
  200. $cells = $row['data'];
  201. // Set the attributes array and exclude 'data' and 'no_striping'.
  202. $attributes = $row;
  203. unset($attributes['data']);
  204. unset($attributes['no_striping']);
  205. }
  206. else {
  207. $cells = $row;
  208. $attributes = array();
  209. }
  210. if (count($cells)) {
  211. // Build row.
  212. $output .= ' <tr' . drupal_attributes($attributes) . '>';
  213. $i = 0;
  214. foreach ($cells as $cell) {
  215. $cell = tablesort_cell($cell, $header, $ts, $i++);
  216. $output .= _theme_table_cell($cell);
  217. }
  218. $output .= " </tr>\n";
  219. }
  220. }
  221. $output .= "</tbody>\n";
  222. }
  223. // Format the table footer:
  224. if (count($footer)) {
  225. $output .= "<tfoot>\n";
  226. foreach ($footer as $row) {
  227. // Check if we're dealing with a simple or complex row.
  228. if (isset($row['data'])) {
  229. $cells = $row['data'];
  230. // Set the attributes array and exclude 'data'.
  231. $attributes = $row;
  232. unset($attributes['data']);
  233. }
  234. else {
  235. $cells = $row;
  236. $attributes = array();
  237. }
  238. if (count($cells)) {
  239. // Build row.
  240. $output .= ' <tr' . drupal_attributes($attributes) . '>';
  241. $i = 0;
  242. foreach ($cells as $cell) {
  243. $cell = tablesort_cell($cell, $header, $ts, $i++);
  244. $output .= _theme_table_cell($cell);
  245. }
  246. $output .= " </tr>\n";
  247. }
  248. }
  249. // Using ternary operator to close the tags based on whether or not there
  250. // are rows.
  251. $output .= "</tfoot>\n";
  252. }
  253. $output .= "</table>\n";
  254. if ($responsive) {
  255. $output .= "</div>\n";
  256. }
  257. return $output;
  258. }