You are here

function vbo_export_action_base in VBO export 7

Action base function.

2 calls to vbo_export_action_base()
vbo_export_csv_action in ./vbo_export.module
The csv export action.
vbo_export_xlsx_action in ./vbo_export.module
The xlsx export action.

File

./vbo_export.module, line 102
Provides VBO action to create a csv based on returned results.

Code

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');
    }
  }
}