You are here

function drush_taxonomy_csv_import in Taxonomy CSV import/export 7.5

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

Process the import of an input.

See also

drush_invoke()

drush.api.php

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

File

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

Code

function drush_taxonomy_csv_import($file_path, $import_format = 'flat') {

  // Start process.
  drush_print('Checking options...');
  if (!$file_path || !file_exists($file_path)) {
    drush_log('You need to set the correct path or url of the file to import.', 'error');
    return;
  }
  require_once drupal_get_path('module', 'taxonomy_csv') . '/import/taxonomy_csv.import.api.inc';

  // Set arguments.
  $options = array();
  $options['url'] = $file_path;
  $options['import_format'] = $import_format;

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

  // Set simple options.
  foreach (array(
    'delimiter',
    'enclosure',
    'locale_custom',
    'vocabulary_target',
    'vocabulary_id',
    'set_hierarchy',
    'update_or_ignore',
    // Level of result process infos.
    'result_level',
    'result_type',
  ) as $value) {
    $option = drush_get_option($value);
    if ($option !== NULL) {
      $options[$value] = $option;
    }
  }

  // If user don't use 'vocabulary_target' but only 'vocabulary_id', this
  // vocabulary will be used. See https://drupal.org/node/1475952.
  if (drush_get_option('vocabulary_target') === NULL && drush_get_option('vocabulary_id') !== NULL) {
    $options['vocabulary_target'] = 'existing';
  }

  // Set boolean options.
  foreach (array(
    'keep_order',
    'check_line',
    'check_utf8',
    'delete_terms',
    'check_hierarchy',
    'set_hierarchy',
    // Level of result process infos.
    'result_stats',
    'result_terms',
  ) as $value) {
    $options[$value] = drush_get_option($value) === NULL ? FALSE : TRUE;
  }

  // Set array options.
  foreach (array(
    'translate_languages',
    'fields_format',
    'fields_custom',
  ) 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 import.
  $messages = taxonomy_csv_import($options);
  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 import process. Please wait...');
  $batch =& batch_get();
  $batch['progressive'] = FALSE;
  drush_backend_batch_process();

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