<?php
// $Id: views_bonus_export.module,v 1.17 2010/03/04 19:42:27 neclimdul Exp $
/**
 * @file
 * Provides the ability to export to specific
 */

/**
 * Implementation of hook_views_api().
 */
function views_bonus_export_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implementation of hook_theme().
 */
function views_bonus_export_theme() {
  return array(
    'views_bonus_export_feed_icon' => array(
      'pattern' => 'views_bonus_export_feed_icon__',
      'arguments' => array('image_path', 'url', 'query', 'text'),
    ),
  );
}

/**
 * Theme a feed link.
 *
 * This theme function uses the theme pattern system to allow it to be
 * overidden in a more specific manner. The options for overiding this include
 * providing per display id; per type; per display id and per type.
 *
 * e.g.
 * For the view "export_test" with the display "page_1" and the type "csv" you
 * would have the following options.
 *   views_bonus_export_feed_icon__export_test__page_1__csv
 *   views_bonus_export_feed_icon__export_test__page_1
 *   views_bonus_export_feed_icon__export_test__csv
 *   views_bonus_export_feed_icon__page_1__csv
 *   views_bonus_export_feed_icon__page_1
 *   views_bonus_export_feed_icon__csv
 *   views_bonus_export_feed_icon
 *
 * @ingroup themeable
 */
function theme_views_bonus_export_feed_icon($image_path, $url, $query = '', $text = '') {
  $url_options = array('html' => true);
  if ($query) {
    $url_options['query'] = $query;
  }
  $image = theme('image', $image_path, $text, $text);
  return l($image, $url, $url_options);
}

/**
 * Shared helper function for export preprocess functions.
 */
function _views_bonus_export_shared_preprocess(&$vars) {
  $view     = $vars['view'];
  $fields   = &$view->field;

  // Add headers here as well as post rendering so older versions of views are
  // actually supported.
  _views_bonus_export_add_headers($view);

  $rows = $vars['rows'];

  $vars['header'] = array();
  foreach ($fields as $key => $field) {
    if (empty($field->options['exclude'])) {
      $vars['header'][$key] = check_plain($field->label());
    }
  }

  $vars['themed_rows'] = array();
  $keys = array_keys($fields);
  foreach ($rows as $num => $row) {
    $vars['themed_rows'][$num] = array();

    foreach ($keys as $id) {
      if (empty($fields[$id]->options['exclude'])) {
        $vars['themed_rows'][$num][$id] = $fields[$id]->theme($row);
      }
    }
  }
}

/**
 * Implementation of hook_views_post_render().
 */
function views_bonus_export_views_post_render(&$view, &$output, &$cache) {
  if (is_a($view->style_plugin, 'views_bonus_plugin_style_export')) {
    _views_bonus_export_add_headers($view);
  }
}

/**
 * Private helper function for adding headers to exported views.
 *
 * @param $view
 * A views object who's current style plugin is a views export style.
 * @return
 * No return. Headers are added to page.
 */
function _views_bonus_export_add_headers(&$view) {
  $style = $view->style_plugin;

  drupal_set_header('Cache-Control: max-age=60, must-revalidate');

  if (!empty($style->definition['export headers'])) {
    foreach ($style->definition['export headers'] as $header) {
      drupal_set_header($header);
    }
  }

  if (isset($style->options['filename']) && !empty($style->options['provide_file'])) {
    $filename = strtr(
      $style->options['filename'],
      array('%view' => check_plain($view->name))
    );

    if ($filename) {
      drupal_set_header('Content-Disposition: attachment; filename="'. $filename .'"');
    }
  }
}
