diff --git views-calc-table.tpl.php views-calc-table.tpl.php index 78284ee..e048f85 100644 --- views-calc-table.tpl.php +++ views-calc-table.tpl.php @@ -7,9 +7,12 @@ * - $header: An array of header labels keyed by field id. * - $fields: An array of CSS IDs to use for each field id. * - $class: A class or classes to apply to the table, based on settings. - * - $rows: An array of row items. Each row is an array of content - * - $totals: An array of calculated totals. Each row contains the total for one calculation. - * keyed by field ID. + * - $rows: An array of row items. Each row is an array of content keyed by field + * ID. + * - $totals: An array of calculated totals. Each row contains the total for one + * calculation. + * - $sub_totals, $group_totals: Like $totals, but for group totals and page + * sub-totals. * @ingroup views_templates */ if (empty($rows) && empty($totals)) { @@ -41,6 +44,15 @@ + $row): ?> + + $content): ?> + + + + + + $row): ?> $content): ?> diff --git views_calc.module views_calc.module index 728c35e..127c3cb 100644 --- views_calc.module +++ views_calc.module @@ -25,6 +25,13 @@ 'arguments' => array('form' => NULL), 'file' => 'theme.inc', ), + 'views_calc_summary_label' => array( + 'arguments' => array( + 'total_type' => '', + 'calc_type' => '', + ), + 'file' => 'theme.inc', + ), ); } diff --git views_calc_table.inc views_calc_table.inc index dde915b..1f093e2 100644 --- views_calc_table.inc +++ views_calc_table.inc @@ -10,7 +10,7 @@ * @ingroup views_style_plugins */ class views_calc_table extends views_plugin_style_table { - + /** * Option definition. */ @@ -20,7 +20,7 @@ $options['detailed_values'] = array('default' => 0); return $options; } - + /** * Render the given style. */ @@ -35,10 +35,10 @@ '#default_value' => $this->options['detailed_values'], '#description' => t("Select 'Yes' to show detailed values followed by column calculations, 'No' to surpress details and show only calculated column totals."), ); - + $handlers = $this->display->handler->get_handlers('field'); $columns = $this->sanitize_columns($this->options['columns']); - + foreach ($columns as $field => $column) { $safe = str_replace(array('][', '_', ' '), '-', $field); $id = 'edit-style-options-columns-' . $safe; @@ -46,9 +46,9 @@ '#type' => 'select', '#default_value' => isset($this->options['info'][$field]['justification']) ? $this->options['info'][$field]['justification'] : 'views_calc_justify_none', '#options' => array( - 'views_calc_justify_none' => t('None'), - 'views_calc_justify_left' => t('Left'), - 'views_calc_justify_right' => t('Right'), + 'views_calc_justify_none' => t('None'), + 'views_calc_justify_left' => t('Left'), + 'views_calc_justify_right' => t('Right'), 'views_calc_justify_center' => t('Center'), ), '#process' => array('views_process_dependency'), @@ -61,7 +61,7 @@ '#process' => array('views_process_dependency'), '#dependency' => array($id => array($field)), ); - + $options = _views_calc_calc_options(); $form['info'][$field]['calc'] = array( '#type' => 'select', @@ -85,7 +85,7 @@ */ function pre_render($results) { parent::pre_render($results); - + // If there are no calc fields, do nothing. if (!$calc_fields = $this->get_calc_fields()) { return; @@ -98,12 +98,12 @@ $this->view->sub_totals = array(); $this->view->views_calc_fields = $calc_fields; $this->view->views_calc_calculation = FALSE; - + // Subtotals and pager totals require a list of the specific // values to include. $paged = FALSE; - if (!empty($this->view->pager) - && !empty($this->view->pager['use_pager']) + if (!empty($this->view->pager) + && !empty($this->view->pager['use_pager']) && !empty($this->view->pager['items_per_page']) && $this->view->total_rows > $this->view->pager['items_per_page']) { $ids = array(); @@ -116,12 +116,12 @@ // force a non-page display, need to keep an eye on this. $this->execute_summary_view($ids); } - + // Add grand totals to the results. $this->execute_summary_view(); } - function execute_summary_view($ids = array()) { + function execute_summary_view($ids = array(), $is_group = FALSE) { // Clone view for local subquery. $summary_view = $this->view->clone_view(); @@ -167,7 +167,10 @@ $summary_view->execute(); $summary_view->post_execute(); if (!empty($summary_view->result)) { - if ($is_subtotal) { + if ($is_group) { + $this->view->group_totals[] = array_shift($summary_view->result); + } + else if ($is_subtotal) { $this->view->sub_totals = array_shift($summary_view->result); } else { @@ -194,7 +197,7 @@ // Rebuild the total query. $this->query_total(); } - + /** * Query grand total * @@ -204,7 +207,7 @@ */ 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(); @@ -250,7 +253,7 @@ $fields = $this->view->field; $columns = $handler->sanitize_columns($options['columns'], $fields); $calcs = array_keys(_views_calc_calc_options()); - + $calc_fields = array(); foreach ($columns as $field => $column) { if ($field == $column && empty($fields[$field]->options['exclude'])) { @@ -264,5 +267,71 @@ } } return $calc_fields; + } + + + /** + * Override of the default style render, to include group totals. + */ + function render() { + if ($this->uses_row_plugin() && empty($this->row_plugin)) { + vpr('views_plugin_style_default: Missing row plugin'); + return; + } + + // Group the rows according to the grouping field, if specified. + $sets = $this->render_grouping($this->view->result, $this->options['grouping']); + + if (count($sets) > 1 ) { + $this->calc_group_totals($sets); + } + + // Render each group separately and concatenate. + $output = ''; + $group_index = 0; + $last_group_index = count($sets) - 1; + foreach ($sets as $title => $records) { + if ($this->uses_row_plugin()) { + $rows = array(); + foreach ($records as $row_index => $row) { + $this->view->row_index = $row_index; + $rows[] = $this->row_plugin->render($row); + } + } + else { + $rows = $records; + } + + $group_total = NULL; + if (isset($this->view->group_totals[$group_index])) { + $group_total = $this->view->group_totals[$group_index]; + } + $this->view->group_total = $group_total; + $this->view->last_group = ($group_index >= $last_group_index); + + $output .= theme($this->theme_functions(), $this->view, $this->options, $rows, $title); + $group_index++; + } + unset($this->view->row_index); + return $output; + } + + /** + * Calculates group totals. + * + * Should only be called if $sets has at least two sets in it. + */ + function calc_group_totals($sets) { + $field = $this->view->base_field; + $this->view->group_totals = array(); + foreach ($sets as $set) { + $ids = array(); + foreach ($set as $value) { + $ids[] = $value->$field; + } + $this->execute_summary_view($ids, TRUE); + } } + + }