vbo_export.module in VBO export 7
Same filename and directory in other branches
Provides VBO action to create a csv based on returned results.
File
vbo_export.moduleView source
<?php
/**
* @file
* Provides VBO action to create a csv based on returned results.
*/
/**
* Implements hook_action_info().
*/
function vbo_export_action_info() {
return array(
'vbo_export_csv_action' => array(
'type' => 'entity',
'label' => t('Export as csv'),
'configurable' => FALSE,
'vbo_configurable' => FALSE,
'triggers' => array(
'any',
),
'pass rows' => TRUE,
'behavior' => array(
'views_property',
),
),
'vbo_export_xlsx_action' => array(
'type' => 'entity',
'label' => t('Export as xlsx'),
'configurable' => FALSE,
'vbo_configurable' => FALSE,
'triggers' => array(
'any',
),
'pass rows' => TRUE,
'behavior' => array(
'views_property',
),
),
);
}
/**
* Implements hook_theme().
*/
function vbo_export_theme() {
return array(
'vbo_export_csv' => array(
'variables' => array(
'header' => array(),
'rows' => array(),
'separator' => ';',
),
'file' => 'vbo_export.theme.inc',
),
'vbo_export_xlsx' => array(
'variables' => array(
'header' => array(),
'rows' => array(),
),
'file' => 'vbo_export.theme.inc',
),
);
}
/**
* Config for csv VBO action.
*/
function vbo_export_csv_action_views_bulk_operations_form($options) {
return vbo_export_action_views_bulk_operations_form($options);
}
/**
* Config for xlsx VBO action.
*/
function vbo_export_xlsx_action_views_bulk_operations_form($options) {
return vbo_export_action_views_bulk_operations_form($options);
}
/**
* Action configuration form.
*/
function vbo_export_action_views_bulk_operations_form($options) {
$form = array();
$form['strip_tags'] = array(
'#type' => 'checkbox',
'#title' => t('Strip HTML tags'),
'#default_value' => !empty($options['strip_tags']) ? $options['strip_tags'] : 0,
);
return $form;
}
/**
* The csv export action.
*/
function vbo_export_csv_action($node, $context) {
vbo_export_action_base($node, $context, 'vbo_export_csv', 'csv');
}
/**
* The xlsx export action.
*/
function vbo_export_xlsx_action($node, $context) {
vbo_export_action_base($node, $context, 'vbo_export_xlsx', 'xlsx');
}
/**
* Action base function.
*/
function vbo_export_action_base($node, $context, $theme, $file_extension) {
// Get view identifier.
$view_identifier = $context['view_info']['name'] . '@' . $context['view_info']['display'];
if (strlen($view_identifier) > 128) {
$view_identifier = substr($view_identifier, 0, 128);
}
$view = views_get_view($context['view_info']['name']);
$view->vbo_export = TRUE;
$view
->build($context['view_info']['display']);
$view
->init_handlers();
$view
->init_style();
// Get action data from current batch.
$batch =& batch_get();
// If there is no batch process, we assume that batch processing is off
// and we can use a different data storage valid for a single request.
if (!isset($batch['current_set'])) {
if (!isset($GLOBALS['vbo_export_storage'])) {
$GLOBALS['vbo_export_storage'] = array(
'current_set' => 'vbo_export',
);
}
$batch =& $GLOBALS['vbo_export_storage'];
}
if (!isset($batch['sets'][$batch['current_set']]['sandbox']['vbo_csv_action_data'])) {
$action_data = array(
'header' => array(),
'rows' => array(),
);
// Generate header row.
foreach ($view->field as $field_id => $field) {
if ($field->options['exclude'] || $field_id == 'views_bulk_operations') {
continue;
}
$action_data['header'][$field_id] = $field->options['label'];
}
$batch['sets'][$batch['current_set']]['sandbox']['vbo_csv_action_data'] = $action_data;
}
$action_data =& $batch['sets'][$batch['current_set']]['sandbox']['vbo_csv_action_data'];
// Render fields.
unset($view->style_plugin->rendered_fields);
$view->result = array(
reset($context['rows']),
);
// Give field handlers the opportunity to perform additional queries
// using the entire resultset prior to rendering, as in view::render().
if ($view->style_plugin
->uses_fields()) {
foreach ($view->field as $id => $handler) {
if (!empty($view->field[$id])) {
$view->field[$id]
->pre_render($view->result);
}
}
}
$rendered_fields = $view->style_plugin
->render_fields($view->result);
$entity_fields = reset($rendered_fields);
$index = count($action_data['rows']);
foreach ($action_data['header'] as $field_id => $title) {
$action_data['rows'][$index][$field_id] = $entity_fields[$field_id];
if (!empty($context['settings']['strip_tags'])) {
$action_data['rows'][$index][$field_id] = html_entity_decode(strip_tags($action_data['rows'][$index][$field_id]));
}
}
if ($context['progress']['current'] >= $context['progress']['total']) {
// Output export data.
try {
$output = theme($theme, $action_data);
} catch (Exception $e) {
drupal_set_message(t('An error occurred.'), 'error');
watchdog_exception('vbo_export', $e, 'VBO Export output data rendering error.');
}
if (!empty($output)) {
$rand = substr(hash('ripemd160', uniqid()), 0, 8);
$filename = $context['view_info']['name'] . '-' . date('Y_m_d_H_i', REQUEST_TIME) . '-' . $rand . '.' . $file_extension;
$file = new stdClass();
$file->fid = NULL;
$dir = file_default_scheme() . '://vbo_export';
$filepath = $dir . '/' . $filename;
if ($file->uri = file_unmanaged_save_data($output, $filepath, FILE_EXISTS_RENAME)) {
if (file_exists($file->uri)) {
$file->filename = drupal_basename($file->uri);
$file->filemime = file_get_mimetype($file->uri);
$file->uid = $GLOBALS['user']->uid;
$file = file_save($file);
drupal_set_message(t('Export file generated. !link to download, link will expire in 3 hours.', array(
'!link' => l(t('Click here'), file_create_url($file->uri)),
)));
}
else {
drupal_set_message(t("Output couldn't be saved to a file."), 'error');
}
}
}
if (empty($file->uri)) {
drupal_set_message(t("Operation didn't produce any output."), 'error');
}
}
}
/**
* Implements hook_libraries_info().
*/
function vbo_export_libraries_info() {
$libraries['PHPExcel'] = array(
'name' => 'PHPExcel',
'vendor url' => 'https://github.com/PHPOffice/PHPExcel',
'download url' => 'https://github.com/PHPOffice/PHPExcel/releases',
'path' => 'Classes',
'version arguments' => array(
'file' => 'changelog.txt',
// 1.8.x: Version 1.8.0.
'pattern' => '/\\d{4}-\\d{2}-\\d{2} \\(v(\\d+\\.\\d+\\.\\d+)\\):/',
'lines' => 30,
),
'files' => array(
'php' => array(
'PHPExcel.php',
),
),
);
return $libraries;
}
/**
* Implements hook_help().
*/
function vbo_export_help($path, $arg) {
switch ($path) {
case 'admin/help#vbo_export':
$filepath = dirname(__FILE__) . '/README.md';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
if (module_exists('markdown')) {
$filters = module_invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
return $output;
}
}
$output = '<pre>' . $readme . '</pre>';
return $output;
}
}
}
Functions
Name | Description |
---|---|
vbo_export_action_base | Action base function. |
vbo_export_action_info | Implements hook_action_info(). |
vbo_export_action_views_bulk_operations_form | Action configuration form. |
vbo_export_csv_action | The csv export action. |
vbo_export_csv_action_views_bulk_operations_form | Config for csv VBO action. |
vbo_export_help | Implements hook_help(). |
vbo_export_libraries_info | Implements hook_libraries_info(). |
vbo_export_theme | Implements hook_theme(). |
vbo_export_xlsx_action | The xlsx export action. |
vbo_export_xlsx_action_views_bulk_operations_form | Config for xlsx VBO action. |