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