You are here

function views_calc_table::query_total in Views Calc 6

Same name and namespace in other branches
  1. 6.3 views_calc_table.inc \views_calc_table::query_total()

Query grand total

The grand total can be computed using GROUPBY without regard to pager values.

See also

query().

1 call to views_calc_table::query_total()
views_calc_table::query in ./views_calc_table.inc
Views Method query().

File

./views_calc_table.inc, line 205
Copied from the table style plugin.

Class

views_calc_table
Style plugin to render each item as a row in a table.

Code

function query_total() {

  // Create summary rows.
  // Empty out any fields that have been added to the query,
  // we don't need them for the summary totals.
  $this->view->query->fields = array();

  // Clear out any sorting and grouping, it can create unexpected results
  // when Views adds aggregation values for the sorts.
  $this->view->query->orderby = array();
  $this->view->query->groupby = array();
  $calc_fields = $this->view->views_calc_fields;
  foreach ($calc_fields as $calc => $fields) {
    foreach ($this->view->field as $field) {
      $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table . '.' . $field->field;
      $query_alias = $field->field_alias;

      // Bail if we have a broken handler.
      if ($query_alias == 'unknown') {
        continue;
      }
      $this->view->query
        ->add_table($field->table, NULL, NULL, $field->table);

      // add all fields
      $this->view->query
        ->add_field(NULL, "NULL", $query_alias);

      // aggregation functions
      $ext_alias = $calc . '__' . $query_alias;
      if (in_array($field->field, $fields)) {

        // Calculated fields.
        $this->view->query
          ->add_field(NULL, $calc . '(' . $query_field . ')', $ext_alias);
      }
    }
  }

  // TODO This won't work right with relationships, need a fix here.
  // Limit to specific primary ids. This is for subtotals.
  if (!empty($this->view->views_calc_ids)) {
    $this->view->query
      ->add_where(NULL, $this->view->base_table . "." . $this->view->base_field . " IN (%s)", implode(',', $this->view->views_calc_ids));
  }
}