'views_bonus_export',
'callback' => 'views_bonus_export',
'type' => MENU_CALLBACK,
'access' => user_access('export views as CSV and DOC'),
);
}
return $items;
}
/**
* 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 Export: Doc file'),
'theme' => 'views_bonus_export_doc',
'needs_table_header' => true,
'needs_fields' => true,
'even_empty' => true,
),
);
}
/**
* 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);
switch ($type) {
case 'csv':
theme('views_bonus_export_csv', $view, $result['items']);
break;
case 'doc':
theme('views_bonus_export_doc', $view, $result['items']);
break;
}
}
/**
* Main Function to export a view as CSV
*/
function theme_views_bonus_export_csv($view, $nodes, $type) {
if (!user_access('export views as CSV and DOC')) {
return;
}
$fields = _views_get_fields();
// headings row
$headings = array();
foreach ($view->field as $field) {
if ($fields[$field['id']]['visible'] !== false) {
$headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name'];
}
}
$output .= implode(',', $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 = $field;
$value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$value = preg_replace('/<.*?>/', '', $value); // strip html tags
$value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas
$value = str_replace('"', '""', $value); // escape " characters
$value = decode_entities($value);
$values[] = '"' . $value . '"';
}
}
$output .= implode(',', $values) . "\r\n";
}
drupal_set_header('Content-Type: text/x-comma-separated-values');
drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"');
print $output;
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 as CSV and DOC')) {
return;
}
drupal_set_header('Content-Type: application/msword');
drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.doc"');
$table = theme('views_view_table', $view, $nodes, null);
$table = preg_replace('/<\/?(a|span) ?.*?>/', '', $table); // strip 'a' and 'span' tags
print $table;
module_invoke_all('exit');
exit;
}
/**
* Implementation of hook_views_post_view
*/
function views_bonus_export_views_post_view($view, $items, $output) {
if (!user_access('export views as CSV and DOC') || $view->page_type == 'teaser' || $view->block == true || !$view->vid) {
return;
}
if ($image = theme('image', drupal_get_path('module', 'views_bonus_export').'/csv.png', t('CSV export'), t('Export this table to an Spreadsheet-readable CSV file'))) {
$url = url('views_bonus_export/csv/'. $view->vid);
$output .= ''. $image. '';
}
if ($image = theme('image', drupal_get_path('module', 'views_bonus_export').'/doc.png', t('DOC export'), t('Export this table to an Wordprocessor-readable DOC file'))) {
$url = url('views_bonus_export/doc/'. $view->vid);
$output .= ''. $image. '';
}
return $output;
}