You are here

function template_preprocess_views_calc_table in Views Calc 7

Same name and namespace in other branches
  1. 6.3 theme.inc \template_preprocess_views_calc_table()
  2. 6 theme.inc \template_preprocess_views_calc_table()

Display a view as a table style.

File

./theme.inc, line 88
theme.inc

Code

function template_preprocess_views_calc_table(&$vars) {

  // Some plugins assume a row_index is set.
  if (!isset($vars['view']->row_index)) {
    $vars['view']->row_index = '';
  }
  $view = $vars['view'];
  if (!empty($view->views_calc_calculation)) {
    $vars['rows'] = array();
    return;
  }
  drupal_add_css(drupal_get_path('module', 'views_calc') . '/views_calc.css');

  // 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.
  $result = $vars['rows'];
  $vars['rows'] = array();
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
  $vars['options'] = $options;
  $hide_details = $options['detailed_values'];
  $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;
  }

  // Render the header labels.
  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] = drupal_clean_css_identifier($field);
    if ($active == $field) {
      $vars['fields'][$field] .= ' active';
    }

    // render the header labels
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $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';
        }
        $image = theme('tablesort_indicator', array(
          'style' => $initial,
        ));
        $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 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] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
    }
  }

  // Render each field into its appropriate column. Preserve rows.
  foreach ($result as $num => $row) {
    foreach ($columns as $field => $column) {
      $field_output = $fields[$field]
        ->theme($row);
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {

        // Don't bother with separators and stuff if the field does not show up.
        if (!isset($field_output) && isset($vars['rows'][$num][$column])) {
          continue;
        }
        if ($hide_details) {
          continue;
        }

        // Place the field into the column, along with an optional separator.
        if (isset($vars['rows'][$num][$column])) {
          if (!empty($options['info'][$column]['separator'])) {
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
          }
        }
        else {
          $vars['rows'][$num][$column] = '';
        }
        $vars['rows'][$num][$column] .= $field_output;
      }
    }
  }

  // Add totals.
  $vars['totals'] = array();
  $vars['sub_totals'] = array();
  if ($view->query->pager
    ->get_total_items() > $view
    ->get_items_per_page() && isset($view->sub_totals)) {
    views_calc_table_total($vars, 'sub_totals', $view->sub_totals);
  }
  if (isset($view->totals)) {
    views_calc_table_total($vars, 'totals', $view->totals);
  }

  // Add classes.
  $vars['class'] = 'views-table';
  if (!empty($options['sticky'])) {
    drupal_add_js('misc/tableheader.js');
    $vars['class'] .= " sticky-enabled";
  }
}