changeset 3:5635080385bd tip

Merge patch with 1.x-dev
author Franck Deroche <franck@defr.org>
date Fri, 07 Aug 2009 15:20:12 +0200
parents cedf71edacf5 (diff) b0a976e17cc7 (current diff)
children
files views_calc_table.inc
diffstat 2 files changed, 49 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/theme.inc	Fri Aug 07 15:17:08 2009 +0200
+++ b/theme.inc	Fri Aug 07 15:20:12 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	Fri Aug 07 15:17:08 2009 +0200
+++ b/views_calc_table.inc	Fri Aug 07 15:20:12 2009 +0200
@@ -101,38 +101,34 @@
         $nids[] = $value->nid;
       }
       // Add sub_total rows to the results.
-      // TODO Looks like we have problems unless we
-      // force a non-page display, need to keep an eye on this.
       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->preview();
-          $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->preview();
-        $this->view->totals[] = array_shift($summary_view->result);
-      }
+      $this->view->totals[] = $this->do_calculation($calc, FALSE);
     }
   }
 
+  function do_calculation($calc, $sub_total, $nids = array()) {
+    // TODO Looks like we have problems unless we
+    // force a non-page display, need to keep an eye on this.
+    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->preview();
+      return array_shift($summary_view->result);
+    }
+    return '';
+  }
+
   function query() {
     parent::query();
     
@@ -165,6 +161,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;
@@ -250,4 +251,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;
+  }
+}