You are here

function drush_views_data_export in Views data export 6.3

Same name and namespace in other branches
  1. 6 views_data_export.drush.inc \drush_views_data_export()
  2. 6.2 views_data_export.drush.inc \drush_views_data_export()
  3. 7.4 views_data_export.drush.inc \drush_views_data_export()
  4. 7 views_data_export.drush.inc \drush_views_data_export()
  5. 7.3 views_data_export.drush.inc \drush_views_data_export()

Drush command callback to export a views data to a file.

See also

drush_views_data_export_validate().

views_data_export_views_data_export_batch_alter().

File

./views_data_export.drush.inc, line 91

Code

function drush_views_data_export($view_name, $display_id, $output_file) {
  $view = views_get_view($view_name);

  // If the given display_id is not views_data_alter then
  // we programatically clone it to a views_data_alter display
  // and then execute that one instead
  if ($view->display[$display_id]->display_plugin != 'views_data_export') {

    //drush_log("Display '$display_id' is not views_data_export. Making one that is and executing that instead =).", 'success');
    $format = drush_get_option('format');
    $settings = array();
    switch ($format) {
      case 'doc':
      case 'xls':
      case 'xml':
      case 'txt':
        $settings['display_options']['style_plugin'] = 'views_data_export_' . $format;
        break;
      case 'csv':
      default:
        $settings['display_options']['style_plugin'] = 'views_data_export_csv';
        if ($separator = drush_get_option('separator')) {
          $settings['display_options']['style_options']['separator'] = $separator;
        }
        if (!($trim = drush_get_option('trim-whitespace'))) {
          $settings['display_options']['style_options']['trim'] = 0;
        }
        if (!($header = drush_get_option('header-row'))) {
          $settings['display_options']['style_options']['header'] = 0;
        }
        if (!($quote = drush_get_option('quote-values'))) {
          $settings['display_options']['style_options']['quote'] = 0;
        }
    }
    $display_id = _drush_views_data_export_clone_display($view, $display_id, $settings);
  }
  $view
    ->set_display($display_id);

  // We execute the view normally, and take advantage
  // of an alter function to interject later and batch it ourselves
  $options = array(
    'output_file' => realpath(drush_get_context('DRUSH_OLDCWD', getcwd())) . '/' . $output_file,
  );
  _drush_views_data_export_override_batch($view_name, $display_id, $options);
  $view
    ->execute_display($display_id);
}