You are here

function taxonomy_csv_term_find in Taxonomy CSV import/export 6.3

Same name and namespace in other branches
  1. 6.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  2. 6.2 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  3. 6.4 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  4. 7.5 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()
  5. 7.4 taxonomy_csv.term.api.inc \taxonomy_csv_term_find()

Find and load a term.

Parameters

$term: The term object to find. It's not necessarily a standard term object. It's an object which needs only a name and eventually a vid or a parent id. Of course, if tid is set, the found term is the existing one.

$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

Formatted found term object, or FALSE if not found or error.

2 calls to taxonomy_csv_term_find()
taxonomy_csv_line_import in import/taxonomy_csv.import.line.api.inc
Process the import of items.
taxonomy_csv_term_import in import/taxonomy_csv.import.line.api.inc
Update or create a term with the given name in the given vocabulary and given parent.

File

./taxonomy_csv.term.api.inc, line 127
Find, get and set full or detail term items.

Code

function taxonomy_csv_term_find($term, $all_vocabularies = FALSE, $parent_tid = NULL) {
  if (isset($term->tid) && $term->tid) {
    return taxonomy_csv_term_load($term->tid);
  }
  elseif (isset($term->name)) {
    $name = drupal_strtolower(trim($term->name));
    if (drupal_strlen($name)) {
      $sql = "\n        SELECT t.*, h.parent\n        FROM {term_data} t\n        INNER JOIN {term_hierarchy} h ON t.tid = h.tid\n        WHERE '%s' LIKE LOWER(t.name)\n      ";
      $args = array();
      $args[] = $name;
      if (isset($term->vid) && $term->vid && !$all_vocabularies) {
        $sql .= ' AND t.vid = %d';
        $args[] = $term->vid;
      }
      if ($parent_tid) {
        $sql .= ' AND h.parent = %d';
        $args[] = $parent_tid;
      }
      $sql .= ' ORDER BY t.tid ASC LIMIT 1';
      $term = db_fetch_object(db_query($sql, $args));
      if ($term) {
        return taxonomy_csv_term_get_full($term);
      }
    }
  }

  // Not found, or error (neither tid nor name).
  return FALSE;
}