You are here

function taxonomy_csv_term_import in Taxonomy CSV import/export 6.5

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

Update or create a term with the given name in the given vocabulary and given parent.

Parameters

$term: A term object to import. Term contains:

  • 'name' => term name string,

and eventually, matching options:

  • 'tid' => term id,
  • 'vid' => the vocabulary id where to import,
  • 'description' => description string,
  • 'weight' => weight integer,
  • 'parents' => array of first level parent tids,

// Specific thesaurus fields.

  • 'synonyms' => array of synonyms terms names strings,
  • 'relations' => array of related tids,

$existing_items: (Optional) Type of import on existing terms. Default to ignore and create.

$all_vocabularies: (Optional) Boolean. Search in all vocabularies or only in $term->vid vocabulary (default), which need to be set. Used with relations import.

$parent_tid: (Optional) The direct parent term id where to restrict search. Used for structure import. Default to NULL (no parent restriction).

$import_format: (Optional) Name of import format. Needed only with formats with specific fields.

Return value

array 'name' => term name, 'tid' => term id, 'msg' => messages array.

3 calls to taxonomy_csv_term_import()
taxonomy_csv_line_import in import/taxonomy_csv.import.line.api.inc
Process the import of items.
taxonomy_csv_line_import_geotaxonomy in formats/geotaxonomy.format.inc
Import a line of items.
taxonomy_csv_line_import_taxonomy_manager in formats/taxonomy_manager.format.inc
Import a line of items.

File

import/taxonomy_csv.import.line.api.inc, line 604
Process import of a csv line, i.e. of a term or a list of terms.

Code

function taxonomy_csv_term_import($term, $existing_items = TAXONOMY_CSV_EXISTING_IGNORE, $all_vocabularies = FALSE, $parent_tid = NULL, $import_format = '') {
  $messages = array();

  // Basic check to avoid notices when "Check lines" option is disabled.
  if (!isset($term->name) && !isset($term->tid) || isset($term->name) && !$term->name || isset($term->tid) && !$term->tid) {
    $term->tid = 0;
    $messages[] = 432;

    // Warning line contains an empty term.
    return array(
      'name' => '',
      'tid' => 0,
      'msg' => $messages,
    );
  }
  switch ($existing_items) {
    case TAXONOMY_CSV_EXISTING_UPDATE:
      $existing_term = taxonomy_csv_term_find($term, $all_vocabularies, $parent_tid);
      if ($existing_term) {

        // Update only fields that are set. Other fields are not changed.
        foreach ((array) $term as $key => $value) {

          // Array fields : merge existing and new items.
          if (is_array($value)) {
            $term->{$key} = array_unique(array_merge($existing_term->{$key}, $term->{$key}));
          }

          // Else simply use new key: no merge is possible and useful for
          // string, numeric or boolean fields.
          // Description was an exception in previous version, but this
          // exception is removed for simplicity and real use of this module.
          // An option may be added if needed.
        }

        // Existing fields of existing term should be kept even if they have
        // not been set in new term.
        $term = (object) array_merge((array) $existing_term, (array) $term);
      }
      break;
    case TAXONOMY_CSV_EXISTING_IGNORE_PREVIOUS:

    // Doesn't ignore, but use previous parents.
    case TAXONOMY_CSV_EXISTING_UPDATE_REPLACE:
      $existing_term = taxonomy_csv_term_find($term, $all_vocabularies, $parent_tid);
      if ($existing_term) {

        // All fields are replaced by new ones. Other existing fields of
        // existing term should be kept even if they are not set in new term.
        $term = (object) array_merge((array) $existing_term, (array) $term);
      }
      break;
    case TAXONOMY_CSV_EXISTING_IGNORE:

      // Nothing to do: keep the term.
      break;
    case TAXONOMY_CSV_EXISTING_IGNORE_ALL:

      // Ignore even existing terms in additional columns.
      // @todo IGNORE_ALL
      break;
  }

  // Save regularly formatted term.
  // Return either SAVED_NEW, SAVED_UPDATED or FALSE (no change).
  $result = taxonomy_csv_term_save($term);

  // If there are specific fields, save them.
  $funcname = "taxonomy_csv_term_import_{$import_format}";
  if (taxonomy_csv_format_check($import_format, $funcname)) {
    $result_temp = $funcname($term);
  }
  $messages[] = $result == SAVED_NEW ? 691 : 692;

  // Saved or updated.
  return array(
    'name' => $term->name,
    'tid' => $term->tid,
    'msg' => $messages,
  );
}