-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_summarize.module
More file actions
259 lines (225 loc) · 5.96 KB
/
views_summarize.module
File metadata and controls
259 lines (225 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Implementation of hook_views_api().
*/
function views_summarize_views_api() {
return array(
'api' => 2,
);
}
/*
* Implementation of hook_theme()
*/
function views_summarize_theme() {
return array(
'views_summarize_plugin_style_tablesummarized' => array(
'arguments' => array('form' => NULL),
),
'views_summarize_type_none' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_count' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_range' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_spread' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_spreadmulti' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_total' => array(
'arguments' => array('data' => array()),
),
'views_summarize_type_average' => array(
'arguments' => array('data' => array()),
),
);
}
/**
* List all of the handlers
*/
function views_summarize_get_handlers() {
return array(
'none' => t('None'),
'count' => t('Count'),
'range' => t('Range'),
'total' => t('Total'),
'average' => t('Average'),
'spread' => t('Spread'),
'spreadmulti' => t('Spread Multi'),
);
}
/**
* Display a view as a table summary.
*/
function template_preprocess_views_summarize_views_tablesummarized(&$vars) {
template_preprocess_views_view_table(&$vars);
if (!count($vars['rows'])) {
return;
}
$opts =& $vars['view']->style_plugin->options['info'];
$vars['summary_only'] = $vars['view']->style_plugin->options['summary_only'];
$data = array();
foreach ($vars['rows'] as $row) {
foreach ($row as $field => $value) {
$data[$field][] = $value;
}
}
$vars['summarized'] = array();
foreach ($opts as $field => $settings) {
$theme = 'views_summarize_type_' . $settings['summarize'];
$vars['summarized'][$field] = theme($theme, $data[$field]);
}
}
/**
* Theme the form for the table style plugin
*
* This is almost the same as theme_views_summarize_plugin_style_table().
*/
function theme_views_summarize_plugin_style_tablesummarized($form) {
$output = drupal_render($form['description_markup']);
$header = array(
t('Field'),
t('Column'),
t('Align'),
t('Separator'),
array(
'data' => t('Summarize'),
'align' => 'center',
),
array(
'data' => t('Sortable'),
'align' => 'center',
),
array(
'data' => t('Default order'),
'align' => 'center',
),
array(
'data' => t('Default sort'),
'align' => 'center',
),
);
$rows = array();
foreach (element_children($form['columns']) as $id) {
$row = array();
$row[] = drupal_render($form['info'][$id]['name']);
$row[] = drupal_render($form['columns'][$id]);
$row[] = drupal_render($form['info'][$id]['align']);
$row[] = drupal_render($form['info'][$id]['separator']);
$row[] = drupal_render($form['info'][$id]['summarize']);
if (!empty($form['info'][$id]['sortable'])) {
$row[] = array(
'data' => drupal_render($form['info'][$id]['sortable']),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['info'][$id]['default_sort_order']),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['default'][$id]),
'align' => 'center',
);
}
else {
$row[] = '';
$row[] = '';
$row[] = '';
}
$rows[] = $row;
}
// Add the special 'None' row.
$rows[] = array(t('None'), '', '', '', '', '', '', array('align' => 'center', 'data' => drupal_render($form['default'][-1])));
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
/**
* Theme: No summary for this column
*/
function theme_views_summarize_type_none($data) {
return '';
}
/**
* Theme: Total number of non-empty values in this column
*/
function theme_views_summarize_type_count($data) {
return '<div class="label">' . t('Count:') . '</div>' .
count(array_filter($data));
}
/**
* Theme: Max and minimum value in this column
*/
function theme_views_summarize_type_range($data) {
$rows = array(
array(t('Min'), min($data)),
array(t('Max'), max($data)),
);
return '<div class="label">' . t('Range:') . '</div>' .
theme('table', array(), $rows);
}
/**
* Theme: Total value for a numeric column
*/
function theme_views_summarize_type_total($data) {
$total = 0;
foreach ($data as $val) {
$total += $val;
}
return '<div class="label">' . t('Total:') . '</div>' .
$total;
}
/**
* Theme: Total value for an average column
*/
function theme_views_summarize_type_average($data) {
$total = 0;
foreach ($data as $val) {
$total += $val;
}
return '<div class="label">' .t('Average:') . '</div>' .
sprintf("%.2f", $total / count($data));
}
/**
* Theme: Spread table for a column
*/
function theme_views_summarize_type_spread($data) {
$hist = array();
foreach ($data as $val) {
if (!isset($hist[$val])) {
$hist[$val] = 0;
}
$hist[$val]++;
}
$rows = array();
foreach ($hist as $value => $count) {
$rows[] = array($value, $count);
}
return '<div class="label">' . t('Spread:') . '</div>' .
theme('table', array(), $rows);
}
/**
* Theme: Spread table for multi values
*/
function theme_views_summarize_type_spreadmulti($data) {
$data_concat=implode('',$data);
$dom = new DOMDocument();
$dom->loadHTML($data_concat);
$divs = $dom->getElementsByTagName('div');
foreach ($divs as $div) {$datums[]= $div->nodeValue;}
foreach ($datums as $val) {
if (!isset($options[$val])) {
$options[$val] = 0;
}
$options[$val]++;
}
$rows = array();
foreach ($options as $value => $count) {
$rows[] = array($value, $count);
}
return '<div class="label">' . t('Spread (Multivalue):') . '</div>' .theme('table', array(), $rows);
}