Mercurial > defr > drupal > views_calc
comparison theme.inc @ 0:0651c02e6ed7
views_calc 1.3
author | Franck Deroche <franck@defr.org> |
---|---|
date | Wed, 05 Aug 2009 18:20:29 +0200 |
parents | |
children | cedf71edacf5 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0651c02e6ed7 |
---|---|
1 <?php | |
2 // $Id: theme.inc,v 1.9 2009/04/23 18:32:48 karens Exp $ | |
3 | |
4 /** | |
5 * @file theme.inc | |
6 * | |
7 * An array of preprocessors to fill variables for templates and helper | |
8 * functions to make theming easier. | |
9 */ | |
10 /** | |
11 * Theme the form for the table style plugin | |
12 */ | |
13 function theme_views_calc_ui_table($form) { | |
14 $output = drupal_render($form['description_markup']); | |
15 | |
16 $header = array( | |
17 t('Field'), | |
18 t('Justification'), | |
19 t('Column calculations'), | |
20 t('Column'), | |
21 t('Separator'), | |
22 array( | |
23 'data' => t('Sortable'), | |
24 'align' => 'center', | |
25 ), | |
26 array( | |
27 'data' => t('Default sort'), | |
28 'align' => 'center', | |
29 ), | |
30 ); | |
31 $rows = array(); | |
32 foreach (element_children($form['columns']) as $id) { | |
33 $row = array(); | |
34 $row[] = drupal_render($form['info'][$id]['name']); | |
35 $row[] = drupal_render($form['info'][$id]['justification']); | |
36 $row[] = drupal_render($form['info'][$id]['has_calc']) . drupal_render($form['info'][$id]['calc']); | |
37 $row[] = drupal_render($form['columns'][$id]); | |
38 $row[] = drupal_render($form['info'][$id]['separator']); | |
39 if (!empty($form['info'][$id]['sortable'])) { | |
40 $row[] = array( | |
41 'data' => drupal_render($form['info'][$id]['sortable']), | |
42 'align' => 'center', | |
43 ); | |
44 $row[] = array( | |
45 'data' => drupal_render($form['default'][$id]), | |
46 'align' => 'center', | |
47 ); | |
48 } | |
49 else { | |
50 $row[] = ''; | |
51 $row[] = ''; | |
52 } | |
53 $rows[] = $row; | |
54 } | |
55 | |
56 // Add the special 'None' row. | |
57 $rows[] = array(t('None'), '', '', '', '', array('align' => 'center', 'data' => drupal_render($form['default'][-1]))); | |
58 | |
59 $output .= theme('table', $header, $rows); | |
60 $output .= drupal_render($form); | |
61 return $output; | |
62 } | |
63 | |
64 | |
65 /** | |
66 * Display a view as a table style. | |
67 */ | |
68 function template_preprocess_views_calc_table(&$vars) { | |
69 $view = $vars['view']; | |
70 if (!empty($view->views_calc_calculation)) { | |
71 $vars['rows'] = array(); | |
72 return; | |
73 } | |
74 | |
75 drupal_add_css(drupal_get_path('module', 'views_calc') .'/views_calc.css'); | |
76 | |
77 // We need the raw data for this grouping, which is passed in as $vars['rows']. | |
78 // However, the template also needs to use for the rendered fields. We | |
79 // therefore swap the raw data out to a new variable and reset $vars['rows'] | |
80 // so that it can get rebuilt. | |
81 $result = $vars['rows']; | |
82 $vars['rows'] = array(); | |
83 $totals = $view->totals; | |
84 $sub_totals = $view->sub_totals; | |
85 | |
86 $options = $view->style_plugin->options; | |
87 $handler = $view->style_plugin; | |
88 $vars['options'] = $options; | |
89 $hide_details = $options['detailed_values']; | |
90 | |
91 $fields = &$view->field; | |
92 $columns = $handler->sanitize_columns($options['columns'], $fields); | |
93 | |
94 $active = !empty($handler->active) ? $handler->active : ''; | |
95 $order = !empty($handler->order) ? $handler->order : 'asc'; | |
96 | |
97 $query = tablesort_get_querystring(); | |
98 if ($query) { | |
99 $query = '&' . $query; | |
100 } | |
101 | |
102 foreach ($columns as $field => $column) { | |
103 // render the header labels | |
104 if ($field == $column && empty($fields[$field]->options['exclude'])) { | |
105 $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : ''); | |
106 if (empty($options['info'][$field]['sortable'])) { | |
107 $vars['header'][$field] = $label; | |
108 } | |
109 else { | |
110 // @todo -- make this a setting | |
111 $initial = 'asc'; | |
112 | |
113 if ($active == $field && $order == 'asc') { | |
114 $initial = 'desc'; | |
115 } | |
116 | |
117 $image = theme('tablesort_indicator', $initial); | |
118 $title = t('sort by @s', array('@s' => $label)); | |
119 $link_options = array( | |
120 'html' => true, | |
121 'attributes' => array('title' => $title), | |
122 'query' => 'order=' . urlencode($field) . '&sort=' . $initial . $query, | |
123 ); | |
124 $vars['header'][$field] = l($label . $image, $_GET['q'], $link_options); | |
125 } | |
126 } | |
127 | |
128 // Create a second variable so we can easily find what fields we have and what the | |
129 // CSS classes should be. | |
130 $vars['fields'][$field] = views_css_safe($field); | |
131 if ($active == $field) { | |
132 $vars['fields'][$field] .= ' active'; | |
133 } | |
134 | |
135 // Render each field into its appropriate column. | |
136 foreach ($result as $num => $row) { | |
137 if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) { | |
138 $field_output = $fields[$field]->theme($row); | |
139 | |
140 // Don't bother with separators and stuff if the field does not show up. | |
141 if (!isset($field_output) && isset($vars['rows'][$num][$column])) { | |
142 continue; | |
143 } | |
144 | |
145 if ($hide_details) { | |
146 continue; | |
147 } | |
148 | |
149 // Place the field into the column, along with an optional separator. | |
150 if (isset($vars['rows'][$num][$column])) { | |
151 if (!empty($options['info'][$column]['separator'])) { | |
152 $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']); | |
153 } | |
154 } | |
155 else { | |
156 $vars['rows'][$num][$column] = ''; | |
157 } | |
158 | |
159 $vars['rows'][$num][$column] .= $field_output; | |
160 } | |
161 } | |
162 } | |
163 | |
164 // Add totals. | |
165 if($view->total_rows > $view->pager['items_per_page']) { | |
166 $process_available = array('sub_totals', 'totals'); | |
167 } | |
168 else{ | |
169 $process_available = array('totals'); | |
170 $vars['sub_totals'] = array(); //if don't set, error in template | |
171 } | |
172 foreach ($process_available as $process) { | |
173 $vars[$process] = array(); | |
174 $added_label = array(); | |
175 foreach ($columns as $field => $column) { | |
176 $field_alias = $fields[$field]->field_alias; | |
177 if ($field == $column && empty($fields[$field]->options['exclude'])) { | |
178 foreach ($$process as $num => $row) { | |
179 $type = ''; | |
180 foreach ($row as $item) { | |
181 if (in_array($item, array_keys(_views_calc_calc_options()))) { | |
182 $type = $item; | |
183 break; | |
184 } | |
185 } | |
186 if (!empty($row->$field_alias) || (isset($row->$field_alias) && $row->$field_alias === 0)) { | |
187 | |
188 // COUNT is always a numeric value, no matter what kind of field it is. | |
189 if ($type == 'COUNT') { | |
190 $vars[$process][$type][$column] = $row->$field_alias; | |
191 } | |
192 // Calculations other than COUNT should run the value through the field's theme. | |
193 // This will allow dates and numeric values to apply the right formatting to the result. | |
194 else { | |
195 $vars[$process][$type][$column] = $fields[$field]->theme($row); | |
196 } | |
197 } | |
198 elseif (!empty($type)) { | |
199 // Add the calc type label into the first empty column. | |
200 // Identify which is the sub total and which the grand total | |
201 // when both are provided. | |
202 if (empty($added_label[$type])) { | |
203 if ($process == 'sub_totals') { | |
204 $label = t("Page !Calculation", array("!Calculation" => $type)); | |
205 } | |
206 else { | |
207 $label = t("Total !Calculation", array("!Calculation" => $type)); | |
208 } | |
209 $vars[$process][$type][$column] = $label; | |
210 $added_label[$type] = TRUE; | |
211 } | |
212 else { | |
213 $vars[$process][$type][$column] = ''; | |
214 } | |
215 } | |
216 } | |
217 } | |
218 } | |
219 } | |
220 | |
221 $vars['class'] = 'views-table'; | |
222 if (!empty($options['sticky'])) { | |
223 drupal_add_js('misc/tableheader.js'); | |
224 $vars['class'] .= " sticky-enabled"; | |
225 } | |
226 | |
227 return; | |
228 | |
229 } |