You are here

function taxonomy_csv_vocabulary_import in Taxonomy CSV import/export 7.5

Same name and namespace in other branches
  1. 6.5 import/taxonomy_csv.import.api.inc \taxonomy_csv_vocabulary_import()
  2. 6.2 import/taxonomy_csv.import.api.inc \taxonomy_csv_vocabulary_import()
  3. 6.3 import/taxonomy_csv.import.api.inc \taxonomy_csv_vocabulary_import()
  4. 6.4 import/taxonomy_csv.import.api.inc \taxonomy_csv_vocabulary_import()
  5. 7.4 import/taxonomy_csv.import.api.inc \taxonomy_csv_vocabulary_import()

Prepare the batch to import the vocabulary.

@note If not used in a form, don't forget to use batch_process().

Parameters

$options: Array. Same as taxonomy_csv_import. See above.

Return value

Array of errors or nothing (batch process to execute).

2 calls to taxonomy_csv_vocabulary_import()
taxonomy_csv_import in import/taxonomy_csv.import.api.inc
Process the import of an input.
taxonomy_csv_import_form_submit in import/taxonomy_csv.import.admin.inc
Handles CSV import form submission and launch batch set.

File

import/taxonomy_csv.import.api.inc, line 276
Validate import options and manage import process.

Code

function taxonomy_csv_vocabulary_import($options) {

  // Check options and return array of messages in case of errors.
  if ($options['check_options']) {
    $module_dir = drupal_get_path('module', 'taxonomy_csv');
    require_once $module_dir . '/import/taxonomy_csv.import.admin.inc';
    $result = _taxonomy_csv_import_check_options($options);
    if (count($result)) {
      return $result;
    }
  }

  // Complete $options.
  // Switch soft tab delimiter with a true one if needed.
  if (drupal_strlen($options['delimiter']) > 1) {
    $result = _taxonomy_csv_import_soft_tab($options['file'], $options['delimiter']);
    $options['delimiter'] = "\t";
  }

  // Calculates number of lines to be imported. File is already checked.
  $options['total_lines'] = count(file($options['file']->filepath));

  // Prepare vocabularies. Options are passed by-reference and can be updated.
  $options['vocabulary'] = _taxonomy_csv_import_vocabulary_prepare($options);

  // Get infos about fields of vocabulary.
  if ($options['import_format'] == TAXONOMY_CSV_FORMAT_FIELDS) {
    $options['instances'] = field_info_instances('taxonomy_term', $options['vocabulary']->machine_name);
    $options['fields'] = array();
    if (is_array($options['instances'])) {
      foreach ($options['instances'] as $key => $value) {
        $options['fields'][$key] = field_info_field($key);
      }
    }
  }

  // Set locale if needed.
  // See http://drupal.org/node/872366
  $options['locale_previous'] = setlocale(LC_CTYPE, 0);
  if ($options['locale_custom']) {
    setlocale(LC_CTYPE, $options['locale_custom']);
  }

  // Prepare import batch.
  // Use a one step batch in order to avoid memory crash in case of big import.
  $batch = array(
    'title' => $options['source_choice'] == 'text' ? t('Importing !total_lines lines from text...', array(
      '!total_lines' => $options['total_lines'],
    )) : t('Importing !total_lines lines from CSV file "%filename"...', array(
      '%filename' => $options['vocabulary']->name,
      '!total_lines' => $options['total_lines'],
    )),
    'init_message' => t('Starting uploading of datas...') . '<br />' . t('Wait some seconds for pre-processing...'),
    'progress_message' => '',
    'error_message' => t('An error occurred during the import.'),
    'finished' => '_taxonomy_csv_vocabulary_import_finished',
    'file' => drupal_get_path('module', 'taxonomy_csv') . '/import/taxonomy_csv.import.api.inc',
    'progressive' => TRUE,
    'operations' => array(
      0 => array(
        '_taxonomy_csv_vocabulary_import_process',
        array(
          $options,
        ),
      ),
    ),
  );
  batch_set($batch);
}