-
Notifications
You must be signed in to change notification settings - Fork 1
/
views_secondary_row.module
executable file
·416 lines (371 loc) · 13.6 KB
/
views_secondary_row.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/**
* Implements hook_views_api().
*/
function views_secondary_row_views_api() {
return array(
'api' => 3,
);
}
/**
* Implements hook_views_plugins().
*/
function views_secondary_row_views_plugins() {
$module_path = backdrop_get_path('module', 'views_secondary_row');
$plugins = array(
'style' => array(
'break_row' => array(
'title' => t('Table with secondary rows'),
'help' => t('Allows table rows to be breaken up into two.'),
'handler' => 'views_secondary_row_plugin_style_table',
'theme' => 'views_secondary_row_view_table',
'theme path' => $module_path,
'theme file' => 'views_secondary_row.module',
'uses row plugin' => FALSE,
'uses row class' => TRUE,
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'normal',
),
),
);
return $plugins;
}
/**
* Implements hook_theme().
*/
function views_secondary_row_theme() {
$theme_info = array(
'views_secondary_row_style_plugin_table' => array(
'render element' => 'form',
),
);
return $theme_info;
}
/**
* Implements hook_autoload_info().
*/
function views_secondary_row_autoload_info() {
return array(
'views_secondary_row_plugin_style_table' => 'views_secondary_row_plugin_style_table.inc',
);
}
/**
* Theme the admin form for the table style plugin
*
* Copied from views\includes\admin.inc, and 2 columns inserted.
* Version history: Aligned with Views 7.x-3.6, not changed in 3.13
*/
function theme_views_secondary_row_style_plugin_table($variables) {
$form = $variables['form'];
$output = backdrop_render($form['description_markup']);
$header = array(
t('Field'),
t('Column'),
t('Align'),
t('Separator'),
array(
'data' => t('Secondary row'),
'align' => 'center',
),
array(
'data' => t('2nd row Separator'),
'align' => 'center',
),
array(
'data' => t('2nd row Colspan'),
'align' => 'center',
),
array(
'data' => t('Sortable'),
'align' => 'center',
),
array(
'data' => t('Default order'),
'align' => 'center',
),
array(
'data' => t('Default sort'),
'align' => 'center',
),
array(
'data' => t('Hide empty column'),
'align' => 'center',
),
);
$rows = array();
foreach (element_children($form['columns']) as $id) {
$row = array();
$row[] = check_plain(backdrop_render($form['info'][$id]['name']));
$row[] = backdrop_render($form['columns'][$id]);
$row[] = backdrop_render($form['info'][$id]['align']);
$row[] = backdrop_render($form['info'][$id]['separator']);
$row[] = backdrop_render($form['info'][$id]['break']);
$row[] = backdrop_render($form['info'][$id]['separator2']);
$row[] = backdrop_render($form['info'][$id]['colspan']);
if (!empty($form['info'][$id]['sortable'])) {
$row[] = array(
'data' => backdrop_render($form['info'][$id]['sortable']),
'align' => 'center',
);
$row[] = array(
'data' => backdrop_render($form['info'][$id]['default_sort_order']),
'align' => 'center',
);
$row[] = array(
'data' => backdrop_render($form['default'][$id]),
'align' => 'center',
);
}
else {
$row[] = '';
$row[] = '';
$row[] = '';
}
$row[] = array(
'data' => backdrop_render($form['info'][$id]['empty_column']),
'align' => 'center',
);
$rows[] = $row;
}
// Add the special 'None' row.
$rows[] = array(t('None'), '', '', '', '', '', '', '', '', array(
'align' => 'center',
'data' => backdrop_render($form['default'][-1]),
), '');
$output .= theme('table', array('header' => $header, 'rows' => $rows));
$output .= backdrop_render_children($form);
return $output;
}
/**
* Display a view as a table style.
* Copied from views\theme\theme.inc.
* Version history: Aligned with Views 7.x-3.6, not changed in 3.13
*/
function template_preprocess_views_secondary_row_view_table(&$vars) {
$view = $vars['view'];
// Check if secondary rows are specified. If not, just call the default 'table' preprocessing.
$options = $view->style_plugin->options;
$has_secondary_row = FALSE;
foreach ($options['info'] as $field => $field_options) {
if (!empty($field_options['break'])) {
$has_secondary_row = TRUE;
}
}
if (!$has_secondary_row) {
template_preprocess_views_view_table($vars);
return;
}
// We need the raw data for this grouping, which is passed in as $vars['rows'].
// However, the template also needs to use for the rendered fields. We
// therefore swap the raw data out to a new variable and reset $vars['rows']
// so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $vars['result'] = $vars['rows'];
$vars['rows'] = array();
$vars['field_classes'] = array();
$vars['header'] = array();
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
$row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
$fields = &$view->field;
$columns = $handler->sanitize_columns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
$query = tablesort_get_query_parameters();
if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input;
}
// Fields must be rendered in order as of Views 2.3, so we will pre-render
// everything.
$renders = $handler->render_fields($result);
foreach ($columns as $field => $column) {
// Create a second variable so we can easily find what fields we have and what the
// CSS classes should be.
$vars['fields'][$field] = backdrop_clean_css_identifier($field);
if ($active == $field) {
$vars['fields'][$field] .= ' active';
}
// Render the header labels. Skip the ones that are broken into the secondary row.
if ($field == $column && empty($fields[$field]->options['exclude']) && empty($options['info'][$field]['break'])) {
$label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
$vars['header'][$field] = $label;
}
else {
$initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
if ($active == $field) {
$initial = ($order == 'asc') ? 'desc' : 'asc';
}
$title = t('sort by @s', array('@s' => $label));
if ($active == $field) {
$label .= theme('tablesort_indicator', array('style' => $initial));
}
$query['order'] = $field;
$query['sort'] = $initial;
$link_options = array(
'html' => TRUE,
'attributes' => array('title' => $title),
'query' => $query,
);
$vars['header'][$field] = l($label, $_GET['q'], $link_options);
}
$vars['header_classes'][$field] = '';
// Set up the header label class.
if ($fields[$field]->options['element_default_classes']) {
$vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
}
$class = $fields[$field]->element_label_classes(0);
if ($class) {
if ($vars['header_classes'][$field]) {
$vars['header_classes'][$field] .= ' ';
}
$vars['header_classes'][$field] .= $class;
}
// Add a CSS align class to each field if one was set
if (!empty($options['info'][$field]['align'])) {
$vars['header_classes'][$field] .= ' ' . backdrop_clean_css_identifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if ($vars['header'][$field]) {
$element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
if ($element_label_type) {
$vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
}
}
}
// Add a CSS align class to each field if one was set
if (!empty($options['info'][$field]['align'])) {
$vars['fields'][$field] .= ' ' . backdrop_clean_css_identifier($options['info'][$field]['align']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Make all rows even rows, so that there's an empty odd/secondary row between them,
// where the "secondary row" fields can be placed.
$primary_row_num = $num * 2;
$secondary_row_num = $primary_row_num + 1;
$old_num = $num;
if (!empty($options['info'][$field]['break'])) {
$num = $secondary_row_num;
$target_column = $options['info'][$field]['break'];
}
else {
$num = $primary_row_num;
$target_column = $column;
}
// Add field classes, respecting primary/secondary rows.
$vars['field_classes'][$target_column][$num] = '';
if ($fields[$field]->options['element_default_classes']) {
$vars['field_classes'][$target_column][$num] = "views-field views-field-" . $vars['fields'][$field];
}
if ($classes = $fields[$field]->element_classes($num)) {
if ($vars['field_classes'][$target_column][$num]) {
$vars['field_classes'][$target_column][$num] .= ' ';
}
$vars['field_classes'][$target_column][$num] .= $classes;
}
$vars['field_attributes'][$target_column][$num] = array();
if (!empty($options['info'][$field]['colspan'])) {
$vars['field_attributes'][$target_column][$num]['colspan'] = $options['info'][$field]['colspan'];
}
if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
$field_output = $renders[$old_num][$field];
$element_type = $fields[$field]->element_type(TRUE, TRUE);
if ($element_type) {
$field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
}
// Don't bother with separators and stuff if the field does not show up.
if (empty($field_output) && !empty($vars['rows'][$num][$target_column])) {
continue;
}
// Place the field into the column, along with an optional separator.
if (!empty($vars['rows'][$num][$target_column])) {
if ($num == $secondary_row_num) {
if (!empty($options['info'][$target_column]['separator2'])) {
$vars['rows'][$num][$target_column] .= filter_xss_admin($options['info'][$target_column]['separator2']);
}
}
else if (!empty($options['info'][$target_column]['separator'])) {
$vars['rows'][$num][$target_column] .= filter_xss_admin($options['info'][$target_column]['separator']);
}
}
else {
$vars['rows'][$num][$target_column] = '';
}
$vars['rows'][$num][$target_column] .= $field_output;
}
}
// Remove columns if the option is hide empty column is checked and the field is not empty.
if (!empty($options['info'][$field]['empty_column'])) {
$empty = TRUE;
foreach ($vars['rows'] as $num => $columns) {
$empty &= empty($columns[$column]);
}
if ($empty) {
foreach ($vars['rows'] as $num => &$column_items) {
unset($column_items[$column]);
unset($vars['header'][$column]);
}
}
}
}
// Reorder rows, since we appended the odd/secondary rows at the end of the list.
ksort($vars['rows']);
// Reorder columns, since fields may be in different order as target columns.
foreach ($vars['rows'] as $num => $row) {
if ($num % 2 == 0) {
continue;
}
$ordered = array();
foreach ($columns as $field => $column) {
if (isset($row[$column])) {
$ordered[$column] = $row[$column];
}
}
$vars['rows'][$num] = $ordered;
}
// Hide table header if all labels are empty.
if (!array_filter($vars['header'])) {
$vars['header'] = array();
}
$count = 0;
foreach ($vars['rows'] as $num => $row) {
$vars['row_classes'][$num] = array();
if ($row_class_special) {
// Views secondary row: give both lines the same class.
//$vars['row_classes'][$num][] = ($count++ % 2 == 0) ? 'odd' : 'even';
$vars['row_classes'][$num][] = (($count++ / 2) % 2 == 0) ? 'odd' : 'even';
}
if ($row_class = $handler->get_row_class($num)) {
$vars['row_classes'][$num][] = $row_class;
}
}
if ($row_class_special) {
$vars['row_classes'][0][] = 'views-row-first';
$vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
}
$vars['classes_array'] = array('views-table');
if (empty($vars['rows']) && !empty($options['empty_table'])) {
$vars['rows'][0][0] = $view->display_handler->render_area('empty');
// Calculate the amounts of rows with output.
$vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
$vars['field_classes'][0][0] = 'views-empty';
}
if (!empty($options['sticky'])) {
backdrop_add_js('misc/tableheader.js');
$vars['classes_array'][] = "sticky-enabled";
}
$vars['classes_array'][] = 'cols-' . count($vars['header']);
// Add the summary to the list if set.
if (!empty($handler->options['summary'])) {
$vars['attributes_array'] = array('summary' => filter_xss_admin($handler->options['summary']));
}
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$vars['caption'] = filter_xss_admin($handler->options['caption']);
}
else {
$vars['caption'] = '';
}
}