--- views_calc_table.inc.orig 2009-04-22 04:21:41.000000000 +0200 +++ views_calc_table.inc 2010-11-12 12:11:57.000000000 +0100 @@ -99,7 +99,8 @@ class views_calc_table extends views_plu && !empty($this->view->pager['items_per_page'])) { $nids = array(); foreach ($this->view->result as $delta => $value) { - $nids[] = $value->nid; + //$nids[] = $value->nid; + $nids[] = $value->{$this->view->base_field}; } // Add sub_total rows to the results. foreach ($calc_fields as $calc => $field) { @@ -116,6 +117,35 @@ class views_calc_table extends views_plu } } + // When grouping the views_calc table is sent to the theme layer multiple times + // On each table output we want to overwrite the grand totals with the grouped totals + // this snippet prepares the necessary array with extra fieldreferences + $grouping = $this->view->style_options['grouping']; + if($grouping) { + $field_alias = $this->view->field[$grouping]->field_alias; + $nids = array(); + foreach ($this->view->result as $delta => $value) { + $nids[$value->{$field_alias}][] = $value->{$this->view->base_field}; + } + + // Add group_total rows to the results. + foreach ($calc_fields as $calc => $field) { + foreach($nids as $group_field_alias => $group_nids) { + if ($summary_view = views_get_view($this->view->name, TRUE)) { + //$summary_view->set_display($this->view->current_display); + $summary_view->set_arguments($this->view->args); + $summary_view->views_calc_calculation = $calc; + $summary_view->views_calc_nids = $group_nids; + $summary_view->views_calc_group_total = TRUE; + $summary_view->is_cacheable = FALSE; + $summary_view->preview(); + $this->view->group_totals[$group_field_alias][] = array_shift($summary_view->result); + $this->query(); + } + } + } + } + // Add grand totals to the results. foreach ($calc_fields as $calc => $field) { if ($summary_view = views_get_view($this->view->name)) { @@ -147,6 +177,9 @@ class views_calc_table extends views_plu if (!empty($this->view->views_calc_sub_total)) { $this->query_sub_total(); } + elseif (!empty($this->view->views_calc_group_total)) { + $this->query_sub_total(); + } else { $this->query_total(); } @@ -182,6 +215,7 @@ class views_calc_table extends views_plu // TODO This won't work right with relationships, need a fix here. if (!empty($this->view->views_calc_nids)) { - $this->view->query->add_where(NULL, "node.nid IN (%s)", implode(',', $this->view->views_calc_nids)); + $this->view->query->add_where(NULL, "%s.%s IN (%s)", $this->view->base_table, $this->view->base_field, implode(',', $this->view->views_calc_nids)); } } --- theme.inc.orig 2009-04-23 20:32:48.000000000 +0200 +++ theme.inc 2010-11-12 12:27:37.000000000 +0100 @@ -172,6 +172,18 @@ function template_preprocess_views_calc_ foreach ($process_available as $process) { $vars[$process] = array(); $added_label = array(); + // foreach ($columns as $field => $column) { + // grouping support + + $grouping = $view->style_options['grouping']; + $grouping_alias = $fields[$grouping]->field_alias; + + if($grouping && $process == 'totals') { + $first_result = array_shift($result); + $current_group = $first_result->{$grouping_alias}; + $totals = $view->group_totals[$current_group]; + } + foreach ($columns as $field => $column) { $field_alias = $fields[$field]->field_alias; if ($field == $column && empty($fields[$field]->options['exclude'])) {