You are here

views_summarize.module in Views Summarize 6

File

views_summarize.module
View source
<?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_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'),
  );
}

/**
 * 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'];
  $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]);
  }
}

/**
 * Theme the form for the table style plugin
 */
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);
}

Functions

Namesort descending Description
template_preprocess_views_summarize_views_tablesummarized Display a view as a table summary.
theme_views_summarize_plugin_style_tablesummarized Theme the form for the table style plugin
theme_views_summarize_type_average Theme: Total value for an average column
theme_views_summarize_type_count Theme: Total number of non-empty values in this column
theme_views_summarize_type_none Theme: No summary for this column
theme_views_summarize_type_range Theme: Max and minimum value in this column
theme_views_summarize_type_spread Theme: Spread table for a column
theme_views_summarize_type_total Theme: Total value for a numeric column
views_summarize_get_handlers List all of the handlers
views_summarize_theme
views_summarize_views_api Implementation of hook_views_api().