You are here

function cvsancestry_import_row in Taxonomy import/export via XML 7

Same name and namespace in other branches
  1. 6.2 csvancestry_format.inc \cvsancestry_import_row()
1 string reference to 'cvsancestry_import_row'
taxonomy_xml_csvancestry_parse in formats/csvancestry_format.inc
Scan the input CSV file and create a taxonomy structure out of it.

File

formats/csvancestry_format.inc, line 96

Code

function cvsancestry_import_row($vid, $row_data, &$context) {

  // As we may be calling back via batch, ensure the functions are loaded
  module_load_include('inc', 'taxonomy_xml', 'taxonomy_xml.process');

  // $_taxonomy_xml_terms is semi-persistant.
  // We expect to be called atomically, but if the term has been cached recently,
  // keep a note of it in the meantime.
  global $_taxonomy_xml_terms;
  while (!empty($row_data) && empty($term_name)) {
    $term_name = array_pop($row_data);
  }
  if (empty($term_name)) {

    // blank line;
    return;
  }
  $term = isset($_taxonomy_xml_terms[$term_name]) ? $_taxonomy_xml_terms[$term_name] : NULL;
  if (!$term) {

    // Start by looking for it
    $term = _taxonomy_xml_get_term_placeholder($term_name, $vid);
    if (empty($term->tid)) {
      $context['results']['new_terms'][] = $term_name;
    }
    else {
      $context['results']['old_terms'][] = $term_name;
    }
    $_taxonomy_xml_terms[$term_name] = $term;
  }

  // Attach parent, if it exists
  $parent_name = array_pop($row_data);
  if (!empty($parent_name)) {
    csvancestry_set_parent($term, $parent_name);
  }

  // And save it.
  $term_data = (array) $term;
  taxonomy_term_save($term);

  // Re-retrieve it, so we know the tid.
  $retrieved_term = taxonomy_xml_get_term_by_name_from_vocab($term_name, $vid);
  $_taxonomy_xml_terms[$term_name] = $term;
}