You are here

function taxonomy_csv_term_import in Taxonomy CSV import/export 7.4

Same name and namespace in other branches
  1. 6.5 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  2. 6.2 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  3. 6.3 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  4. 6.4 import/taxonomy_csv.import.line.api.inc \taxonomy_csv_term_import()
  5. 7.5 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).

Return value

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

1 call to taxonomy_csv_term_import()
taxonomy_csv_line_import in import/taxonomy_csv.import.line.api.inc
Process the import of items.

File

import/taxonomy_csv.import.line.api.inc, line 568
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_CREATE, $all_vocabularies = FALSE, $parent_tid = NULL) {
  $messages = array();
  if ($term) {
    switch ($existing_items) {
      case TAXONOMY_CSV_EXISTING_UPDATE:

      // When no difference between update/merge and update/replace.
      case TAXONOMY_CSV_EXISTING_UPDATE_MERGE:
        $existing_term = taxonomy_csv_term_find($term, $all_vocabularies, $parent_tid);
        if ($existing_term) {

          // Get name is not case sensitive and a name can be updated.
          if (isset($term->name)) {
            $existing_term->name = $term->name;
          }
          if (isset($term->vid)) {
            $existing_term->vid = $term->vid;
          }
          if (isset($term->parents)) {
            $existing_term->parents = array_unique(array_merge($existing_term->parents, $term->parents));
          }
          if (isset($term->description)) {
            $i = trim($existing_term->description);
            $existing_term->description = $i == '' || $i == $term->description ? $term->description : $i . "\n" . $term->description;
          }

          // Weight is always replaced as it is a simple number.
          if (isset($term->weight)) {
            $existing_term->weight = $term->weight;
          }
          if (isset($term->synonyms)) {
            $existing_term->synonyms = array_unique(array_merge($existing_term->synonyms, $term->synonyms));
          }
          if (isset($term->relations)) {
            $existing_term->relations = array_unique(array_merge($existing_term->relations, $term->relations));
          }
          $term = $existing_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) {
          foreach (array(
            'name',
            'vid',
            'description',
            'weight',
            'parents',
            'synonyms',
            'relations',
          ) as $key) {
            if (array_key_exists($key, $term)) {
              $existing_term->{$key} = $term->{$key};
            }
          }
          $term = $existing_term;
        }
        break;
      case TAXONOMY_CSV_EXISTING_IGNORE:

      // When no difference between ignore/create and ignore/all.
      case TAXONOMY_CSV_EXISTING_IGNORE_CREATE:
        break;
      case TAXONOMY_CSV_EXISTING_IGNORE_ALL:

        // Ignore even existing terms in additional columns.
        // @todo IGNORE_ALL
        break;
      case TAXONOMY_CSV_EXISTING_IGNORE_QUICK:

        // Don't check existing terms. That's very similar to ignore all.
        break;
    }

    // Save regularly formatted term.
    // Return either SAVED_NEW, SAVED_UPDATED or FALSE (no change).
    $result = _taxonomy_csv_term_save($term);
    $messages[] = $result == SAVED_NEW ? 691 : 692;

    // Saved or updated.
  }
  else {
    $messages[] = 391;

    // Error: not a term.
  }
  return array(
    'name' => $term->name,
    'tid' => $term->tid,
    'msg' => $messages,
  );
}