Index: views_calc_table.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views_calc/views_calc_table.inc,v
retrieving revision 1.17
diff -u -p -r1.17 views_calc_table.inc
--- views_calc_table.inc	13 Jun 2009 17:05:38 -0000	1.17
+++ views_calc_table.inc	10 Jun 2010 04:57:15 -0000
@@ -104,32 +104,13 @@ class views_calc_table extends views_plu
       // 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->execute_summary_view($calc, $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->execute_summary_view($calc);
     }
   }
 
@@ -250,4 +231,42 @@ class views_calc_table extends views_plu
     }
     return $calc_fields;
   }
+
+  function execute_summary_view($calc, $nids = array()) {
+    // Load the view and check access to the given display.
+    if (!($summary_view = views_get_view($this->view->name)) || !$this->view->access($this->view->current_display)) {
+      return NULL;
+    }
+
+    // Make sure the view is completely valid.
+    $errors = $summary_view->validate();
+    if (is_array($errors)) {
+      foreach ($errors as $error) {
+        drupal_set_message($error, 'error');
+      }
+      return NULL;
+    }
+
+    // intialize summary view
+    $is_subtotal = !empty($nids);
+    $summary_view->preview = TRUE;
+    $summary_view->is_cacheable = FALSE;
+    $summary_view->views_calc_calculation = $calc;
+    $summary_view->views_calc_nids = $nids;
+    $summary_view->views_calc_sub_total = $is_subtotal;
+
+    // if not subtotal, get all results
+    if (!$is_subtotal) {
+      $summary_view->pager['items_per_page'] = 0;
+    }
+
+    // Execute and render the view in preview mode. Note that we only store
+    // the result if the executed view query returns any result.
+    $summary_view->pre_execute($this->view->args);
+    $summary_view->execute($this->view->current_display);
+    $summary_view->post_execute();
+    if (!empty($summary_view->result)) {
+      $this->view->totals[] = array_shift($summary_view->result);
+    }
+  }
 }
\ No newline at end of file
