comparison views_calc_table.inc @ 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
comparison
equal deleted inserted replaced
0:0651c02e6ed7 1:cedf71edacf5
101 foreach ($this->view->result as $delta => $value) { 101 foreach ($this->view->result as $delta => $value) {
102 $nids[] = $value->nid; 102 $nids[] = $value->nid;
103 } 103 }
104 // Add sub_total rows to the results. 104 // Add sub_total rows to the results.
105 foreach ($calc_fields as $calc => $field) { 105 foreach ($calc_fields as $calc => $field) {
106 if ($summary_view = views_get_view($this->view->name)) { 106 $this->view->sub_totals[] = $this->do_calculation($calc, TRUE, $nids);
107 $summary_view->set_display($this->view->current_display);
108 $summary_view->set_arguments($this->view->args);
109 $summary_view->views_calc_calculation = $calc;
110 $summary_view->views_calc_nids = $nids;
111 $summary_view->views_calc_sub_total = TRUE;
112 $summary_view->is_cacheable = FALSE;
113 $summary_view->execute();
114 $this->view->sub_totals[] = array_shift($summary_view->result);
115 }
116 } 107 }
117 } 108 }
118 109
119 // Add grand totals to the results. 110 // Add grand totals to the results.
120 foreach ($calc_fields as $calc => $field) { 111 foreach ($calc_fields as $calc => $field) {
121 if ($summary_view = views_get_view($this->view->name)) { 112 $this->view->totals[] = $this->do_calculation($calc, FALSE);
122 $summary_view->set_display($this->view->current_display); 113 }
123 $summary_view->set_arguments($this->view->args); 114 }
124 $summary_view->pager['items_per_page'] = 0; 115
125 $summary_view->views_calc_calculation = $calc; 116 function do_calculation($calc, $sub_total, $nids = array()) {
126 $summary_view->views_calc_nids = array(); 117 if ($summary_view = views_get_view($this->view->name)) {
127 $summary_view->views_calc_sub_total = FALSE; 118 $summary_view->set_display($this->view->current_display);
128 $summary_view->is_cacheable = FALSE; 119 $summary_view->set_arguments($this->view->args);
129 $summary_view->execute(); 120 $summary_view->pager['items_per_page'] = 0;
130 $this->view->totals[] = array_shift($summary_view->result); 121 $summary_view->views_calc_calculation = $calc;
131 } 122 $summary_view->views_calc_nids = $nids;
132 } 123 $summary_view->views_calc_sub_total = $sub_total;
124 $summary_view->is_cacheable = FALSE;
125 $summary_view->execute();
126 return array_shift($summary_view->result);
127 }
128 return '';
133 } 129 }
134 130
135 function query() { 131 function query() {
136 parent::query(); 132 parent::query();
137 133
162 $fields = $calc_fields[$calc]; 158 $fields = $calc_fields[$calc];
163 159
164 // Empty out any fields that have been added to the query, 160 // Empty out any fields that have been added to the query,
165 // we don't need them for the summary totals. 161 // we don't need them for the summary totals.
166 $this->view->query->fields = array(); 162 $this->view->query->fields = array();
163 // Clear out any sorting and grouping, it can create unexpected results
164 // when Views adds aggregation values for the sorts.
165 $this->view->query->orderby = array();
166 $this->view->query->groupby = array();
167
167 foreach ($this->view->field as $field) { 168 foreach ($this->view->field as $field) {
168 $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field; 169 $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field;
169 $query_alias = $field->field_alias; 170 $query_alias = $field->field_alias;
170 if (in_array($field->field, $fields)) { 171 if (in_array($field->field, $fields)) {
171 // Calculated fields. 172 // Calculated fields.
239 } 240 }
240 } 241 }
241 } 242 }
242 return $calc_fields; 243 return $calc_fields;
243 } 244 }
245
246 function render_grouping($records, $grouping_field = '') {
247 $sets = parent::render_grouping($records, $grouping_field);
248 // If we have more than one set, we'll try recalculate results
249 // baded on the nodes in the group.
250 $calc_fields = $this->get_calc_fields();
251 if (count($sets) > 1 && $calc_fields) {
252 $this->group_totals = array();
253 foreach($sets as $value => $set) {
254 $nids = array();
255 foreach($set as $k => $v) {
256 $nids[] = $v->nid;
257 }
258 foreach($calc_fields as $calc => $field) {
259 $this->view->group_totals[$value][] = $this->do_calculation($calc, TRUE, $nids);
260 }
261 }
262 }
263 return $sets;
264 }
244 } 265 }