View source
<?php
function views_secondary_row_views_api() {
return array(
'api' => 3,
);
}
function views_secondary_row_views_plugins() {
$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 file' => 'views_secondary_row.module',
'uses row plugin' => FALSE,
'uses row class' => TRUE,
'uses fields' => TRUE,
'uses options' => TRUE,
'type' => 'normal',
),
),
);
return $plugins;
}
function views_secondary_row_theme() {
$theme_info = array(
'views_secondary_row_style_plugin_table' => array(
'render element' => 'form',
),
);
return $theme_info;
}
function theme_views_secondary_row_style_plugin_table($variables) {
$form = $variables['form'];
$output = drupal_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(drupal_render($form['info'][$id]['name']));
$row[] = drupal_render($form['columns'][$id]);
$row[] = drupal_render($form['info'][$id]['align']);
$row[] = drupal_render($form['info'][$id]['separator']);
$row[] = drupal_render($form['info'][$id]['break']);
$row[] = drupal_render($form['info'][$id]['separator2']);
$row[] = drupal_render($form['info'][$id]['colspan']);
if (!empty($form['info'][$id]['sortable'])) {
$row[] = array(
'data' => drupal_render($form['info'][$id]['sortable']),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['info'][$id]['default_sort_order']),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['default'][$id]),
'align' => 'center',
);
}
else {
$row[] = '';
$row[] = '';
$row[] = '';
}
$row[] = array(
'data' => drupal_render($form['info'][$id]['empty_column']),
'align' => 'center',
);
$rows[] = $row;
}
$rows[] = array(
t('None'),
'',
'',
'',
'',
'',
'',
'',
'',
array(
'align' => 'center',
'data' => drupal_render($form['default'][-1]),
),
'',
);
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
));
$output .= drupal_render_children($form);
return $output;
}
function template_preprocess_views_secondary_row_view_table(&$vars) {
$view = $vars['view'];
$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;
}
$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;
}
$renders = $handler
->render_fields($result);
foreach ($columns as $field => $column) {
$vars['fields'][$field] = drupal_clean_css_identifier($field);
if ($active == $field) {
$vars['fields'][$field] .= ' active';
}
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] = '';
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;
}
if (!empty($options['info'][$field]['align'])) {
$vars['header_classes'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
}
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 . '>';
}
}
}
if (!empty($options['info'][$field]['align'])) {
$vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
}
foreach ($result as $num => $row) {
$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;
}
$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 . '>';
}
if (empty($field_output) && !empty($vars['rows'][$num][$target_column])) {
continue;
}
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;
}
}
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]);
}
}
}
}
ksort($vars['rows']);
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;
}
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) {
$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');
$vars['field_attributes'][0][0]['colspan'] = count($vars['header']);
$vars['field_classes'][0][0] = 'views-empty';
}
if (!empty($options['sticky'])) {
drupal_add_js('misc/tableheader.js');
$vars['classes_array'][] = "sticky-enabled";
}
$vars['classes_array'][] = 'cols-' . count($vars['header']);
if (!empty($handler->options['summary'])) {
$vars['attributes_array'] = array(
'summary' => filter_xss_admin($handler->options['summary']),
);
}
if (!empty($handler->options['caption'])) {
$vars['caption'] = filter_xss_admin($handler->options['caption']);
}
else {
$vars['caption'] = '';
}
}