You are here

function taxonomy_manager_mass_add_terms in Taxonomy Manager 7

Helper function for mass adding of terms.

Parameters

$input: The textual input with terms. Each line contains a single term. Child term can be prefixed with a dash '-' (one dash for each level). Term names starting with a dash and should not become a child term need to be wrapped in quotes.

$vid: The vocabulary id.

int $parents: An array of parent term ids for the new inserted terms. Can be 0.

$lang: The i18n language, if i18n exists.

$term_names_too_long: Return value that is used to indicate that some term names were too long and truncated to 255 characters.

Return value

An array of the newly inserted term objects

1 call to taxonomy_manager_mass_add_terms()
taxonomy_manager_form_add_submit in ./taxonomy_manager.admin.inc
Submit handler for adding terms

File

./taxonomy_manager.admin.inc, line 1353

Code

function taxonomy_manager_mass_add_terms($input, $vid, $parents, $lang = "", &$term_names_too_long = array()) {
  $new_terms = array();
  $terms = explode("\n", str_replace("\r", '', $input));
  $parents = count($parents) ? $parents : 0;

  // Stores the current lineage of newly inserted terms.
  $last_parents = array();
  foreach ($terms as $name) {
    if (empty($name)) {
      continue;
    }
    $matches = array();

    // Child term prefixed with one or more dashes
    if (preg_match('/^(-){1,}/', $name, $matches)) {
      $depth = strlen($matches[0]);
      $name = substr($name, $depth);
      $current_parents = isset($last_parents[$depth - 1]) ? $last_parents[$depth - 1]->tid : 0;
    }
    elseif (preg_match('/^\\"(-){1,}.*\\"/', $name, $matches)) {
      $name = substr($name, 1, -1);
      $depth = 0;
      $current_parents = $parents;
    }
    else {
      $depth = 0;
      $current_parents = $parents;
    }

    // Truncate long string names that will cause database exceptions.
    if (strlen($name) > 255) {
      $term_names_too_long[] = $name;
      $name = substr($name, 0, 255);
    }
    $term = new stdClass();
    $term->name = $name;
    $term->vid = $vid;
    $term->parent = $current_parents;
    if (module_exists('i18n_taxonomy') && !empty($lang) && i18n_taxonomy_vocabulary_mode($vid, I18N_MODE_TRANSLATE)) {
      $term->language = $lang;
    }
    taxonomy_term_save($term);
    $new_terms[] = $term;
    $last_parents[$depth] = $term;
  }
  return $new_terms;
}