View source
<?php
function views_summarize_views_api() {
return array(
'api' => 2,
);
}
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_total' => array(
'arguments' => array(
'data' => array(),
),
),
'views_summarize_type_average' => array(
'arguments' => array(
'data' => array(),
),
),
);
}
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'),
);
}
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'];
$summarized = 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]);
}
}
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;
}
$rows[] = array(
t('None'),
'',
'',
'',
'',
'',
'',
array(
'align' => 'center',
'data' => drupal_render($form['default'][-1]),
),
);
$output .= theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
}
function theme_views_summarize_type_none($data) {
return '';
}
function theme_views_summarize_type_count($data) {
return '<div class="label">' . t('Count:') . '</div>' . count(array_filter($data));
}
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);
}
function theme_views_summarize_type_total($data) {
$total = 0;
foreach ($data as $val) {
$total += $val;
}
return '<div class="label">' . t('Total:') . '</div>' . $total;
}
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));
}
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);
}