Mercurial > defr > drupal > views_calc
changeset 1:cedf71edacf5
Views Calc: Total du group dans le cas d'un group by
author | Franck Deroche <franck@defr.org> |
---|---|
date | Thu, 06 Aug 2009 19:09:42 +0200 |
parents | 0651c02e6ed7 |
children | 5635080385bd |
files | theme.inc views_calc_table.inc |
diffstat | 2 files changed, 47 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/theme.inc Wed Aug 05 18:20:29 2009 +0200 +++ b/theme.inc Thu Aug 06 19:09:42 2009 +0200 @@ -82,6 +82,9 @@ $vars['rows'] = array(); $totals = $view->totals; $sub_totals = $view->sub_totals; + if ($vars['title'] && $view->group_totals[$vars['title']]) { + $totals = $view->group_totals[$vars['title']]; + } $options = $view->style_plugin->options; $handler = $view->style_plugin; @@ -226,4 +229,4 @@ return; -} \ No newline at end of file +}
--- a/views_calc_table.inc Wed Aug 05 18:20:29 2009 +0200 +++ b/views_calc_table.inc Thu Aug 06 19:09:42 2009 +0200 @@ -103,35 +103,31 @@ } // Add sub_total rows to the results. foreach ($calc_fields as $calc => $field) { - if ($summary_view = views_get_view($this->view->name)) { - $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 = $nids; - $summary_view->views_calc_sub_total = TRUE; - $summary_view->is_cacheable = FALSE; - $summary_view->execute(); - $this->view->sub_totals[] = array_shift($summary_view->result); - } + $this->view->sub_totals[] = $this->do_calculation($calc, TRUE, $nids); } } // Add grand totals to the results. foreach ($calc_fields as $calc => $field) { - if ($summary_view = views_get_view($this->view->name)) { - $summary_view->set_display($this->view->current_display); - $summary_view->set_arguments($this->view->args); - $summary_view->pager['items_per_page'] = 0; - $summary_view->views_calc_calculation = $calc; - $summary_view->views_calc_nids = array(); - $summary_view->views_calc_sub_total = FALSE; - $summary_view->is_cacheable = FALSE; - $summary_view->execute(); - $this->view->totals[] = array_shift($summary_view->result); - } + $this->view->totals[] = $this->do_calculation($calc, FALSE); } } + function do_calculation($calc, $sub_total, $nids = array()) { + if ($summary_view = views_get_view($this->view->name)) { + $summary_view->set_display($this->view->current_display); + $summary_view->set_arguments($this->view->args); + $summary_view->pager['items_per_page'] = 0; + $summary_view->views_calc_calculation = $calc; + $summary_view->views_calc_nids = $nids; + $summary_view->views_calc_sub_total = $sub_total; + $summary_view->is_cacheable = FALSE; + $summary_view->execute(); + return array_shift($summary_view->result); + } + return ''; + } + function query() { parent::query(); @@ -164,6 +160,11 @@ // 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(); + 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; @@ -241,4 +242,24 @@ } return $calc_fields; } -} \ No newline at end of file + + function render_grouping($records, $grouping_field = '') { + $sets = parent::render_grouping($records, $grouping_field); + // If we have more than one set, we'll try recalculate results + // baded on the nodes in the group. + $calc_fields = $this->get_calc_fields(); + if (count($sets) > 1 && $calc_fields) { + $this->group_totals = array(); + foreach($sets as $value => $set) { + $nids = array(); + foreach($set as $k => $v) { + $nids[] = $v->nid; + } + foreach($calc_fields as $calc => $field) { + $this->view->group_totals[$value][] = $this->do_calculation($calc, TRUE, $nids); + } + } + } + return $sets; + } +}