You are here

views_bonus_export.module in Views Bonus Pack 5

File

views_bonus_export.module
View source
<?php

/**
 *  Implementaion of hook_perm
 */
function views_bonus_export_perm() {
  return array(
    'export views',
  );
}

/**
 *  Implementation of hook_views_style_plugins
 */
function views_bonus_export_views_style_plugins() {
  return array(
    'views_csv' => array(
      'name' => t('Views Bonus: CSV file'),
      'theme' => 'views_bonus_export_csv',
      'needs_table_header' => TRUE,
      'needs_fields' => TRUE,
      'even_empty' => TRUE,
    ),
    'views_doc' => array(
      'name' => t('Views Bonus: DOC file'),
      'theme' => 'views_bonus_export_doc',
      'needs_table_header' => TRUE,
      'needs_fields' => TRUE,
      'even_empty' => TRUE,
    ),
    'views_txt' => array(
      'name' => t('Views Bonus: TXT file'),
      'theme' => 'views_bonus_export_txt',
      'needs_table_header' => TRUE,
      'needs_fields' => TRUE,
      'even_empty' => TRUE,
    ),
    'views_xml' => array(
      'name' => t('Views Bonus: XML file'),
      'theme' => 'views_bonus_export_xml',
      'needs_table_header' => TRUE,
      'needs_fields' => TRUE,
      'even_empty' => TRUE,
    ),
  );
}

/*
 * Implementation of hook_views_arguments to add the CSV
 * and DOC argument selectors.
 */
function views_bonus_export_views_arguments() {
  $arguments = array(
    'csv' => array(
      'name' => t('CSV: CSV File Selector'),
      'handler' => 'views_bonus_export_views_handler',
      'option' => 'string',
      'help' => t('This argument specifies a specific CSV file selector; it will only select CSV files.'),
    ),
    'doc' => array(
      'name' => t('DOC: DOC File Selector'),
      'handler' => 'views_bonus_export_views_handler',
      'option' => 'string',
      'help' => t('This argument specifies a specific DOC file selector; it will only select CSV files.'),
    ),
    'txt' => array(
      'name' => t('TXT: TXT File Selector'),
      'handler' => 'views_bonus_export_views_handler',
      'option' => 'string',
      'help' => t('This argument specifies a specific TXT file selector; it will only select TXT files.'),
    ),
  );
  return $arguments;
}

/**
 * handler for our own CSV or DOC argument handler
 */
function views_bonus_export_views_handler($op, &$query, $argtype, $arg = '') {
  if ($op == 'filter') {
    views_bonus_export_views_file_argument('argument', $GLOBALS['current_view'], $arg);
  }
}

/**
 * argument hook that will display the file or display export icons.
 */
function views_bonus_export_views_file_argument($op, &$view, $arg) {
  if ($op == 'argument' && ($arg == 'csv' || $arg == 'doc' || $arg == 'txt')) {
    $view->page_type = 'views_' . $arg;
    $view->use_pager = 0;
    $view->pager_limit = 0;
  }
  else {
    if ($op == 'post_view' && $view->build_type != 'block') {
      $args = views_post_view_make_args($view, $arg, $arg);
      $url = views_get_url($view, $args);
      $title = views_get_title($view, 'page', $args);
      $links = array();

      // Add the exposed filters to the URL.
      if (!empty($view->used_filters)) {
        $url_filter = drupal_query_string_encode($view->used_filters);
      }
      else {
        $url_filter = NULL;
      }
      if (user_access('export views')) {
        if ($arg == 'csv') {
          if ($image = theme('image', drupal_get_path('module', 'views_bonus_export') . '/csv.png', t('CSV export'), t('Export @title to an Spreadsheet-readable CSV file', array(
            '@title' => $title,
          )))) {
            $links[] = l($image, $url, array(
              'class' => 'xml-icon',
            ), $url_filter, NULL, FALSE, TRUE);
          }
        }
        else {
          if ($arg == 'doc') {
            if ($image = theme('image', drupal_get_path('module', 'views_bonus_export') . '/doc.png', t('DOC export'), t('Export @title to an Wordprocessor-readable DOC file', array(
              '@title' => $title,
            )))) {
              $links[] = l($image, $url, array(
                'class' => 'xml-icon',
              ), $url_filter, NULL, FALSE, TRUE);
            }
          }
          else {
            if ($arg == 'txt') {
              if ($image = theme('image', drupal_get_path('module', 'views_bonus_export') . '/txt.png', t('TXT export'), t('Export @title to a TXT file', array(
                '@title' => $title,
              )))) {
                $links[] = l($image, $url, array(
                  'class' => 'xml-icon',
                ), $url_filter, NULL, FALSE, TRUE);
              }
            }
          }
        }
      }
      return implode('&nbsp;&nbsp;', $links);
    }
  }
}

/*
 * describes how to theme a doc view
 */
function theme_views_doc($view, $nodes, $type) {
  views_bonus_export('doc', $view->vid);
}

/*
 * describes how to theme a csv view
 */
function theme_views_csv($view, $nodes, $type) {
  views_bonus_export('csv', $view->vid);
}

/*
 * describes how to theme a txt view
 */
function theme_views_txt($view, $nodes, $type) {
  views_bonus_export('txt', $view->vid);
}

/**
 * post view to display the export icons
 */
function views_bonus_export_views_post_view($view, $items, $output) {
  $links = '';
  foreach ($view->argument as $id => $argument) {
    if ($argument['type'] == 'csv' || $argument['type'] == 'doc' || $argument['type'] == 'txt') {
      $links .= views_bonus_export_views_file_argument('post_view', $view, $argument['type']);
    }
  }
  return $links;
}

/**
 *  Menu callback to make the CSV/DOC
 */
function views_bonus_export($type, $vid) {
  if (!is_numeric($vid)) {
    drupal_not_found();
    return;
  }
  $view = views_load_view($vid);
  $result = views_build_view('items', $view);
  if ($type == 'csv' || $type == 'doc' || $type == 'txt') {
    if (user_access('export views')) {
      drupal_set_header("Cache-Control: no-store, no-cache, must-revalidate");
      theme('views_bonus_export_' . $type, $view, $result['items']);
    }
    else {
      drupal_access_denied();
    }
  }
}

/**
 *  Main Function to export a view as CSV
 */
function theme_views_bonus_export_csv($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  $fields = _views_get_fields();
  drupal_set_header('Content-Type: text/csv');
  drupal_set_header('Content-Disposition: attachment; filename="view-' . $view->name . '.csv"');

  // headings row
  $headings = array();
  foreach ($view->field as $field) {
    if ($fields[$field['id']]['visible'] !== false) {
      $headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
    }
  }
  $comma = t(',');
  print implode($comma, $headings) . "\r\n";

  // one row for each node
  foreach ($nodes as $node) {
    $values = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== false) {
        $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
        $values[] = '"' . str_replace('"', '""', decode_entities(strip_tags($value))) . '"';
      }
    }
    print implode($comma, $values) . "\r\n";
  }
  module_invoke_all('exit');
  exit;
}

/**
 *  Main Function to export a view as DOC
 */
function theme_views_bonus_export_doc($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  drupal_set_header('Content-Type: application/msword; charset=utf-8');
  drupal_set_header('Content-Disposition: attachment; filename="view-' . $view->name . '.doc"');
  print '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
  $table = theme('views_view_table', $view, $nodes, null);
  $table = preg_replace('/<\\/?(a|span) ?.*?>/', '', $table);

  // strip 'a' and 'span' tags
  print $table;
  print "</body></html>";
  module_invoke_all('exit');
  exit;
}

/**
 * Main Function to export a view as TXT
 */
function theme_views_bonus_export_txt($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  $separator = theme('views_bonus_export_txt_separator');
  drupal_set_header('Content-Type: text/plain');
  drupal_set_header('Content-Disposition: attachment; filename="view-' . $view->name . '.txt"');
  $fields = _views_get_fields();
  $output = '';
  foreach ($nodes as $node) {
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        print "[" . $field['label'] . "]\n\n";
        print filter_xss(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view), array()) . "\n\n";
      }
    }
    print $separator;
  }
  module_invoke_all('exit');
  exit;
}

/**
 * Main Function to export a view as XML
 */
function theme_views_bonus_export_xml($view, $nodes) {
  if (!user_access('export views')) {
    return;
  }
  drupal_set_header('Content-Type: text/xml');
  drupal_set_header('Content-Disposition: attachment; filename="view-' . $view->name . '.xml"');
  $fields = _views_get_fields();
  print '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
  print "<nodes>\n";
  foreach ($nodes as $node) {
    print "\t<node>\n";
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $label = $field['label'];
        print "\t\t<{$label}><![CDATA[";
        print filter_xss(views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view), array());
        print "]]></{$label}>\n";
      }
    }
    print "\t</node>\n";
  }
  print "</nodes>\n";
  module_invoke_all('exit');
  exit;
}
function theme_views_bonus_export_txt_separator() {
  return "----------------------------------------\n\n";
}

Functions

Namesort descending Description
theme_views_bonus_export_csv Main Function to export a view as CSV
theme_views_bonus_export_doc Main Function to export a view as DOC
theme_views_bonus_export_txt Main Function to export a view as TXT
theme_views_bonus_export_txt_separator
theme_views_bonus_export_xml Main Function to export a view as XML
theme_views_csv
theme_views_doc
theme_views_txt
views_bonus_export Menu callback to make the CSV/DOC
views_bonus_export_perm Implementaion of hook_perm
views_bonus_export_views_arguments
views_bonus_export_views_file_argument argument hook that will display the file or display export icons.
views_bonus_export_views_handler handler for our own CSV or DOC argument handler
views_bonus_export_views_post_view post view to display the export icons
views_bonus_export_views_style_plugins Implementation of hook_views_style_plugins