You are here

function drush_taxonomy_csv_export in Taxonomy CSV import/export 7.5

Same name and namespace in other branches
  1. 6.5 taxonomy_csv.drush.inc \drush_taxonomy_csv_export()

Process the export of a vocabulary.

See also

drush_invoke()

drush.api.php

1 string reference to 'drush_taxonomy_csv_export'
taxonomy_csv_drush_command in ./taxonomy_csv.drush.inc
Implements hook_drush_command().

File

./taxonomy_csv.drush.inc, line 204
Drush commands for taxonomy CSV import/export.

Code

function drush_taxonomy_csv_export($vocabulary_id, $export_format = 'flat', $file_path = '') {

  // Start process.
  drush_print('Checking options...');
  if ($vocabulary_id === NULL) {
    drush_log('You need to set a vocabulary id (0 means all).', 'error');
    return;
  }

  // Set the destination file path.
  if ($file_path == '') {
    $file_path = getcwd() . '/taxocsv.csv';
  }
  require_once drupal_get_path('module', 'taxonomy_csv') . '/export/taxonomy_csv.export.api.inc';

  // Set arguments.
  $options = array();
  $options['vocabulary_id'] = $vocabulary_id;
  $options['export_format'] = $export_format;

  // Get the defaults options and update them with user ones.
  $options += _taxonomy_csv_values('export_default_api');

  // Set simple options.
  foreach (array(
    'delimiter',
    'enclosure',
    'line_ending',
    'order',
  ) as $value) {
    $option = drush_get_option($value);
    if ($option !== NULL) {
      $options[$value] = $option;
    }
  }

  // Set boolean options.
  foreach (array(
    'result_duplicates',
  ) as $value) {
    $options[$value] = drush_get_option($value) === NULL ? FALSE : TRUE;
  }

  // Set array options.
  foreach (array() as $value) {
    $option = drush_get_option_list($value);
    if ($option !== array()) {
      $options[$value] = $option;
    }
  }

  // Set general options.
  $options['check_options'] = TRUE;
  $options['result_display'] = TRUE;

  // Prepare export.
  $messages = taxonomy_csv_export($options);
  $file = '';
  if (is_object($messages)) {
    $file = $messages;
    $messages = array();
  }
  if (count($messages)) {
    foreach ($messages as $message) {
      $msg = str_replace('<br />', " \n", $message);
      drush_log('- ' . $msg, 'error');
    }
    return;
  }

  // Process the batch.
  drush_log('Options are good.', 'status');
  drush_print('Launch export process. Please wait...');
  $batch =& batch_get();
  $batch['progressive'] = FALSE;
  drush_backend_batch_process();

  // Move file to chosen directory if needed.
  if (file_exists($file->filepath)) {
    rename($file->filepath, $file_path);
    drush_log("The vocabulary has been exported to {$file_path}.", 'status');
  }

  // End of drush process.
  drush_print('End of drush export batch.');
}