You are here

function i18ntaxonomy_get_tree in Internationalization 6

Get taxonomy tree for a given language

Parameters

$vid: Vocabulary id

$lang: Language code

$parent: Parent term id for the tree

1 call to i18ntaxonomy_get_tree()
i18ntaxonomy_translation_term_form in i18ntaxonomy/i18ntaxonomy.admin.inc
Produces a vocabulary translation form.

File

i18ntaxonomy/i18ntaxonomy.module, line 923
i18n taxonomy module

Code

function i18ntaxonomy_get_tree($vid, $lang, $parent = 0, $depth = -1, $max_depth = NULL) {
  static $children, $parents, $terms;
  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$vid][$lang])) {
    $children[$vid][$lang] = array();
    $result = db_query(db_rewrite_sql("SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d AND t.language = '%s' ORDER BY weight, name", 't', 'tid'), $vid, $lang);
    while ($term = db_fetch_object($result)) {
      $children[$vid][$lang][$term->parent][] = $term->tid;
      $parents[$vid][$lang][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
    }
  }
  $max_depth = is_null($max_depth) ? count($children[$vid][$lang]) : $max_depth;
  $tree = array();
  if (!empty($children[$vid][$lang][$parent])) {
    foreach ($children[$vid][$lang][$parent] as $child) {
      if ($max_depth > $depth) {
        $term = drupal_clone($terms[$vid][$child]);
        $term->depth = $depth;

        // The "parent" attribute is not useful, as it would show one parent only.
        unset($term->parent);
        $term->parents = $parents[$vid][$lang][$child];
        $tree[] = $term;
        if (!empty($children[$vid][$lang][$child])) {
          $tree = array_merge($tree, i18ntaxonomy_get_tree($vid, $lang, $child, $depth, $max_depth));
        }
      }
    }
  }
  return $tree;
}