Mercurial > defr > drupal > views_calc
comparison views_calc_table.inc @ 2:b0a976e17cc7
Views Calc: Version de dev du 14/06/2009
author | Franck Deroche <franck@defr.org> |
---|---|
date | Fri, 07 Aug 2009 15:17:08 +0200 |
parents | 0651c02e6ed7 |
children | 5635080385bd |
comparison
equal
deleted
inserted
replaced
0:0651c02e6ed7 | 2:b0a976e17cc7 |
---|---|
1 <?php | 1 <?php |
2 // $Id: views_calc_table.inc,v 1.15 2009/04/22 02:21:41 karens Exp $ | 2 // $Id: views_calc_table.inc,v 1.17 2009/06/13 17:05:38 karens Exp $ |
3 /** | 3 /** |
4 * @file | 4 * @file |
5 * Copied from the table style plugin. | 5 * Copied from the table style plugin. |
6 */ | 6 */ |
7 | 7 |
76 * TODO | 76 * TODO |
77 * figure out what changes are needed so Views field groups will work. | 77 * figure out what changes are needed so Views field groups will work. |
78 */ | 78 */ |
79 function pre_render($results) { | 79 function pre_render($results) { |
80 parent::pre_render($results); | 80 parent::pre_render($results); |
81 | 81 |
82 // If there are no calc fields, do nothing. | 82 // If there are no calc fields, do nothing. |
83 if (!$calc_fields = $this->get_calc_fields()) { | 83 if (!$calc_fields = $this->get_calc_fields()) { |
84 return; | 84 return; |
85 } | 85 } |
86 // If we're not getting a summary row, do nothing. | 86 // If we're not getting a summary row, do nothing. |
87 if (!empty($this->view->views_calc_calculation)) { | 87 if (!empty($this->view->views_calc_calculation)) { |
88 return; | 88 return; |
89 } | 89 } |
90 | |
91 $this->view->totals = array(); | 90 $this->view->totals = array(); |
92 $this->view->sub_totals = array(); | 91 $this->view->sub_totals = array(); |
93 | 92 |
94 // Subtotals and pager totals require a list of the specific | 93 // Subtotals and pager totals require a list of the specific |
95 // values to include. | 94 // values to include. |
100 $nids = array(); | 99 $nids = array(); |
101 foreach ($this->view->result as $delta => $value) { | 100 foreach ($this->view->result as $delta => $value) { |
102 $nids[] = $value->nid; | 101 $nids[] = $value->nid; |
103 } | 102 } |
104 // Add sub_total rows to the results. | 103 // Add sub_total rows to the results. |
104 // TODO Looks like we have problems unless we | |
105 // force a non-page display, need to keep an eye on this. | |
105 foreach ($calc_fields as $calc => $field) { | 106 foreach ($calc_fields as $calc => $field) { |
106 if ($summary_view = views_get_view($this->view->name)) { | 107 if ($summary_view = views_get_view($this->view->name)) { |
107 $summary_view->set_display($this->view->current_display); | 108 //$summary_view->set_display($this->view->current_display); |
108 $summary_view->set_arguments($this->view->args); | 109 $summary_view->set_arguments($this->view->args); |
109 $summary_view->views_calc_calculation = $calc; | 110 $summary_view->views_calc_calculation = $calc; |
110 $summary_view->views_calc_nids = $nids; | 111 $summary_view->views_calc_nids = $nids; |
111 $summary_view->views_calc_sub_total = TRUE; | 112 $summary_view->views_calc_sub_total = TRUE; |
112 $summary_view->is_cacheable = FALSE; | 113 $summary_view->is_cacheable = FALSE; |
113 $summary_view->execute(); | 114 $summary_view->preview(); |
114 $this->view->sub_totals[] = array_shift($summary_view->result); | 115 $this->view->sub_totals[] = array_shift($summary_view->result); |
115 } | 116 } |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 // Add grand totals to the results. | 120 // Add grand totals to the results. |
120 foreach ($calc_fields as $calc => $field) { | 121 foreach ($calc_fields as $calc => $field) { |
121 if ($summary_view = views_get_view($this->view->name)) { | 122 if ($summary_view = views_get_view($this->view->name)) { |
122 $summary_view->set_display($this->view->current_display); | 123 //$summary_view->set_display($this->view->current_display); |
123 $summary_view->set_arguments($this->view->args); | 124 $summary_view->set_arguments($this->view->args); |
124 $summary_view->pager['items_per_page'] = 0; | 125 $summary_view->pager['items_per_page'] = 0; |
125 $summary_view->views_calc_calculation = $calc; | 126 $summary_view->views_calc_calculation = $calc; |
126 $summary_view->views_calc_nids = array(); | 127 $summary_view->views_calc_nids = array(); |
127 $summary_view->views_calc_sub_total = FALSE; | 128 $summary_view->views_calc_sub_total = FALSE; |
128 $summary_view->is_cacheable = FALSE; | 129 $summary_view->is_cacheable = FALSE; |
129 $summary_view->execute(); | 130 $summary_view->preview(); |
130 $this->view->totals[] = array_shift($summary_view->result); | 131 $this->view->totals[] = array_shift($summary_view->result); |
131 } | 132 } |
132 } | 133 } |
133 } | 134 } |
134 | 135 |
165 // we don't need them for the summary totals. | 166 // we don't need them for the summary totals. |
166 $this->view->query->fields = array(); | 167 $this->view->query->fields = array(); |
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; |
171 // Bail if we have a broken handler. | |
172 if ($query_alias == 'unknown') { | |
173 continue; | |
174 } | |
170 if (in_array($field->field, $fields)) { | 175 if (in_array($field->field, $fields)) { |
171 // Calculated fields. | 176 // Calculated fields. |
172 $this->view->query->add_field(NULL, "$calc($query_field)", $query_alias); | 177 $this->view->query->add_field(NULL, "$calc($query_field)", $query_alias); |
173 $this->view->query->add_table($field->table, NULL, NULL, $field->table); | 178 $this->view->query->add_table($field->table, NULL, NULL, $field->table); |
174 } | 179 } |
204 $this->view->query->groupby = array(); | 209 $this->view->query->groupby = array(); |
205 | 210 |
206 foreach ($this->view->field as $field) { | 211 foreach ($this->view->field as $field) { |
207 $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field; | 212 $query_field = substr($field->field, 0, 3) == 'cid' ? $field->definition['calc'] : $field->table .'.'. $field->field; |
208 $query_alias = $field->field_alias; | 213 $query_alias = $field->field_alias; |
214 // Bail if we have a broken handler. | |
215 if ($query_alias == 'unknown') { | |
216 continue; | |
217 } | |
209 $this->view->query->add_table($field->table, NULL, NULL, $field->table); | 218 $this->view->query->add_table($field->table, NULL, NULL, $field->table); |
210 if (!empty($fields) && in_array($field->field, $fields)) { | 219 if (!empty($fields) && in_array($field->field, $fields)) { |
211 // Calculated fields. | 220 // Calculated fields. |
212 $this->view->query->add_field(NULL, "$calc($query_field)", $query_alias); | 221 $this->view->query->add_field(NULL, "$calc($query_field)", $query_alias); |
213 } | 222 } |