You are here

function taxonomy_csv_find_term in Taxonomy CSV import/export 5

Find an existing term by name in the given vocabulary and given parent.

1 call to taxonomy_csv_find_term()
taxonomy_csv_import_term in ./taxonomy_csv.module
Get or create a term with the given name in the given vocabulary and given parent.

File

./taxonomy_csv.module, line 294
Quick import of lists of terms from a csv file.

Code

function taxonomy_csv_find_term($name, $vid, $parent = NULL) {
  static $cache = array();
  $name = drupal_strtolower(trim($name));
  $key = $name . '_' . $vid;
  if (!is_null($parent)) {
    $key .= '_' . $parent;
  }
  if (isset($cache[$key])) {
    return $cache[$key];
  }
  else {
    $sql = "SELECT t.tid, t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE '%s' LIKE LOWER(t.name) AND t.vid = %d ";
    $args = array(
      $name,
      $vid,
    );
    if (!is_null($parent)) {
      $sql .= "AND h.parent = %d ";
      $args[] = $parent;
    }
    $sql .= "LIMIT 1 ";
    $result = db_query($sql, $args);
    $term = db_fetch_array($result);
    if ($term) {
      $cache[$key] = $term;
      if (is_null($parent)) {
        $cache[$key . '_' . $term['parent']] = $term;
      }
    }
  }
  return $term;
}