View source
<?php
function views_bonus_export_perm() {
return array(
'export views',
);
}
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,
),
);
}
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;
}
function views_bonus_export_views_handler($op, &$query, $argtype, $arg = '') {
if ($op == 'filter') {
views_bonus_export_views_file_argument('argument', $GLOBALS['current_view'], $arg);
}
}
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();
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(' ', $links);
}
}
}
function theme_views_doc($view, $nodes, $type) {
views_bonus_export('doc', $view->vid);
}
function theme_views_csv($view, $nodes, $type) {
views_bonus_export('csv', $view->vid);
}
function theme_views_txt($view, $nodes, $type) {
views_bonus_export('txt', $view->vid);
}
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;
}
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();
}
}
}
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 = 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";
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;
}
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);
print $table;
print "</body></html>";
module_invoke_all('exit');
exit;
}
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;
}
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";
}