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