function template_preprocess_views_view_pivot_table in Pivot Tables for Views 7
Callback to prepare row data for later rendering.
Parameters
$vars:
File
- ./
views_pivot.theme.inc, line 8
Code
function template_preprocess_views_view_pivot_table(&$vars) {
$view = $vars['view'];
$fields_info = field_info_fields();
// 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();
$vars['hide_subheader'] = TRUE;
$vars['aggregate_columns'] = array();
/* @var $options array */
/* @var $handler views_plugin_style_pivot */
$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;
$pivot_col = $options['pivot_col'];
$pivot_row = $options['pivot_row'];
$vertical = $options['pivot_mode_multi_values'] == VIEWS_PIVOT_MODE_MULTI_VALUE_VERTICAL;
$fields =& $view->field;
$columns = $handler
->sanitize_columns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
$aggregates = array();
$pivot_extra = array();
foreach ($columns as $field => $column) {
if (!empty($options['info'][$field]['pivot_row_extra'])) {
if ($column != $pivot_col && $column != $pivot_row) {
$pivot_extra[] = $column;
}
}
}
$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 ($result as $num => $row) {
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $num, VIEWS_PIVOT_HEADER_KEY, $pivot_col, $renders[$num][$pivot_col], $renders[$num][$pivot_col], FALSE);
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $num, $renders[$num][$pivot_row], $pivot_row, $columns[$pivot_row], $columns[$pivot_row], FALSE);
}
foreach ($columns as $field => $column) {
/* @var $field_handler views_handler_field_field */
$field_handler = $fields[$field];
// Create a second variable so we can easily find what fields we have and what the
// CSS classes should be.
$vars['fields'][$field] = drupal_clean_css_identifier($field);
if ($active == $field) {
$vars['fields'][$field] .= ' active';
}
// render the header labels
if ($field == $column && empty($field_handler->options['exclude'])) {
$label = check_plain(!empty($field_handler) ? $field_handler
->label() : '');
if ($field == $pivot_col) {
if (!empty($vars['pivotheader'])) {
foreach ($vars['pivotheader'] as $key => $value) {
$vars['header'][$key] = $value;
}
}
continue;
}
$header_area = 'subheader';
if ($field == $pivot_row) {
$header_area = 'header';
}
else {
if (in_array($column, $pivot_extra)) {
$header_area = 'header';
}
}
if (empty($options['info'][$field]['sortable']) || !$field_handler
->click_sortable()) {
$vars[$header_area][$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_area][$field] = l($label, $_GET['q'], $link_options);
}
$vars['header_classes'][$field] = '';
// Set up the header label class.
if ($field_handler->options['element_default_classes']) {
$vars['header_classes'][$field] .= "views-field views-field-" . $vars['fields'][$field];
}
$class = $field_handler
->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] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if (!empty($vars['header'][$field])) {
$element_label_type = $field_handler
->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] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
}
if ($field == $pivot_row) {
continue;
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
if ($column == $pivot_row || $column == $pivot_col) {
// OK, we will append to one of those columns.
$column_key = $column;
}
else {
if (in_array($column, $pivot_extra)) {
$column_key = $column;
}
else {
$column_key = $column . ':' . $renders[$num][$pivot_col];
}
}
$row_key = $renders[$num][$pivot_row];
// Handle the Pivot aggregates
$full_field_name = 'field_' . $field;
if (isset($fields_info[$field]['bundles']['field_collection_item'])) {
$full_field_name = $field;
}
$full_field_alias = isset($field_handler->field_alias) ? $field_handler->field_alias : FALSE;
if (isset($view->result[$num]->{$full_field_name})) {
if (is_array($view->result[$num]->{$full_field_name}) && isset($view->result[$num]->{$full_field_name}[0]['raw'])) {
$value = $view->result[$num]->{$full_field_name}[0]['raw']['value'];
}
else {
$value = $view->result[$num]->{$full_field_name};
}
}
else {
if (isset($view->result[$num]->{$field})) {
$value = $view->result[$num]->{$field};
}
else {
if (isset($view->result[$num]->{$full_field_alias})) {
if (is_array($view->result[$num]->{$full_field_alias}) && isset($view->result[$num]->{$full_field_alias}[0]['raw'])) {
$value = $view->result[$num]->{$full_field_alias}[0]['raw']['value'];
}
else {
$value = $view->result[$num]->{$full_field_alias};
}
}
else {
if (isset($view->result[$num]->{$full_field_alias})) {
$value = $view->result[$num]->{$full_field_alias};
}
else {
$value = 0;
}
}
}
}
if (!empty($options['info'][$field]['pivot_row_aggregate'])) {
$aggregates['row'][$row_key][$field][] = $value;
$aggregates['overall'][$field][] = $value;
}
if (!empty($options['info'][$field]['pivot_col_aggregate'])) {
$aggregates['col'][$column_key][] = $value;
}
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $num, $row_key, $field, $column_key, $column);
}
}
if (!empty($aggregates['row'])) {
foreach ($aggregates['row'] as $row_key => $agg_columns) {
foreach ($agg_columns as $column_key => $values) {
$agg_key = 'agg:' . $column_key;
$vars['rows'][VIEWS_PIVOT_HEADER_KEY][$agg_key] = $options['info'][$column_key]['pivot_row_aggregate_label'];
$vars['aggregate_columns'][$agg_key] = TRUE;
$renders[$agg_key][$column_key] = _views_pivot_format_aggregate_field($view, $fields, $column_key, $values, $options['info'][$column_key]['pivot_row_aggregate']);
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $agg_key, $row_key, $column_key, $agg_key, '');
}
}
}
if (!empty($aggregates['col'])) {
foreach ($aggregates['col'] as $column_key => $values) {
$parts = explode(':', $column_key);
$field = $parts[0];
$row_key = 'agg:' . $field;
$renders[$row_key][$field] = $options['info'][$field]['pivot_col_aggregate_label'];
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $row_key, $row_key, $field, $pivot_row, '');
$renders[$row_key][$field] = _views_pivot_format_aggregate_field($view, $fields, $field, $values, $options['info'][$field]['pivot_col_aggregate']);
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $row_key, $row_key, $field, $column_key, '');
}
}
if (!empty($aggregates['overall'])) {
foreach ($aggregates['overall'] as $field => $values) {
$agg_key = 'agg:' . $field;
$renders[$agg_key][$field] = _views_pivot_format_aggregate_field($view, $fields, $field, $values, $options['info'][$field]['pivot_col_aggregate']);
_views_pivot_add_field_to_row($vars, $options, $renders, $fields, $agg_key, $agg_key, $field, $agg_key, '');
}
}
if (!empty($vars['rows'][VIEWS_PIVOT_HEADER_KEY])) {
foreach ($vars['rows'][VIEWS_PIVOT_HEADER_KEY] as $key => $value) {
$vars['header'][$key] = $value;
}
unset($vars['rows'][VIEWS_PIVOT_HEADER_KEY]);
}
// Hide table header if all labels are empty.
if (!array_filter($vars['header'])) {
$vars['header'] = array();
}
// Show table subheader if labels are not empty.
if (array_filter($vars['subheader'])) {
$vars['hide_subheader'] = FALSE;
}
$count = 0;
foreach ($vars['rows'] as $num => $row) {
$vars['row_classes'][$num] = array();
if ($row_class_special) {
$vars['row_classes'][$num][] = $count++ % 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'])) {
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' => $handler->options['summary'],
);
}
$c = count($vars['subheader']);
$row_span_extra_header = $vars['hide_subheader'] ? 1 : ($vertical ? $c + 1 : 2);
array_unshift($pivot_extra, $pivot_row);
foreach ($pivot_extra as $field) {
$vars['header_attributes'][$field]['rowspan'] = $row_span_extra_header;
$vars['pivot_rows'][$field] = $vars['header'][$field];
unset($vars['header'][$field]);
}
// Do this for horizontal multi field setting only.
if ($c > 0) {
if ($vertical) {
foreach ($vars['rows'] as $key => $values) {
$vars['row_attributes'][$key]['rowspan'] = $c;
}
}
else {
foreach ($vars['header'] as $field) {
$vars['header_attributes'][$field]['colspan'] = $c;
}
}
}
$vars['vertical'] = $vertical;
}