You are here

function taxonomy_edge_get_tree_optimized in Taxonomy Edge 6

Same name and namespace in other branches
  1. 8 taxonomy_edge.core.inc \taxonomy_edge_get_tree_optimized()
  2. 7.2 taxonomy_edge.core.inc \taxonomy_edge_get_tree_optimized()
  3. 7 taxonomy_edge.core.inc \taxonomy_edge_get_tree_optimized()

Reimplementation of taxonomy_get_tree(). Limit db fetch to only specified parent AND use presorting.

See also

taxonomy_get_tree()

1 call to taxonomy_edge_get_tree_optimized()
taxonomy_edge_get_tree in ./taxonomy_edge.module
Reimplementation of taxonomy_get_tree(). Limit db fetch to only specified parent.

File

./taxonomy_edge.core.inc, line 116
This file contains the core override functions

Code

function taxonomy_edge_get_tree_optimized($vid, $parent = 0, $max_depth = NULL) {
  $vocabulary = taxonomy_vocabulary_load($vid);

  // Optimized version does not work with multiple parents
  if ($vocabulary->hierarchy == 2) {
    return taxonomy_edge_get_tree_generic($vid, $parent, $max_depth);
  }
  $valid_order = taxonomy_edge_is_order_valid($vid);

  // Use generic if entire tree is queried and we don't have a presorted tree.
  if (!$parent && !$valid_order) {
    return taxonomy_edge_get_tree_generic($vid, $parent, $max_depth);
  }

  // Setup static cache if applicable
  $static_cache = variable_get('taxonomy_edge_static_caching', TAXONOMY_EDGE_STATIC_CACHING);
  static $cache = array();

  // Return cache if present
  if ($static_cache && isset($cache[$parent])) {
    $tree = array();
    for ($i = $cache[$parent]['offset']; $i < $cache[$parent]['offset'] + $cache[$parent]['count']; $i++) {
      $clone = clone $cache[$parent]['data'][$i];
      $clone->depth -= $cache[$parent]['depth'];
      if ($clone->depth < 0) {
        break;
      }
      if (!$max_depth || $clone->depth < $max_depth) {
        $tree[] = $clone;
      }
    }
    return $tree;
  }

  // No cache, let's fetch from db
  $args[] = $parent;
  $args[] = $vid;
  $select = "SELECT d.*, h.parent, e.distance\n    FROM {term_edge} e\n    JOIN {term_data} d ON d.tid = e.tid\n    JOIN {term_hierarchy} h ON h.tid = d.tid\n  ";
  $where = "\n    WHERE e.parent = %d\n    AND e.vid = %d\n    AND e.distance > 0\n  ";
  if ($max_depth) {
    $where .= " AND e.distance <= %d ";
    $args[] = $max_depth;
  }
  if ($valid_order) {
    $select .= " JOIN {term_edge_order} o ON o.eid = e.eid AND o.parent = e.parent ";
    $where .= " AND o.vid = %d ";
    $args[] = $vid;
    $orderby = " ORDER BY o.oid ";
  }
  else {
    $orderby = " ORDER BY " . _taxonomy_edge_generate_term_path_query('e.tid');
  }
  $sql = $select . $where . $orderby;
  $result = db_query($sql, $args);
  $tree = array();
  if ($static_cache) {
    $cache[$parent]['offset'] = 0;
    $cache[$parent]['count'] = 0;
    $cache[$parent]['depth'] = 0;
  }
  while ($term = db_fetch_object($result)) {
    $term->depth = $term->distance - 1;
    $term->parents = array(
      $term->parent,
    );
    unset($term->distance);
    unset($term->parent);
    unset($term->sort_path);
    $tree[$term->tid] = clone $term;
    if ($static_cache) {
      $cache[$parent]['data'][] =& $tree[$term->tid];
      $cache[$term->tid]['data'] =& $cache[$parent]['data'];
      $cache[$term->tid]['count'] = $cache[$parent]['count'];
      $cache[$term->tid]['offset'] = ++$cache[$parent]['count'];
      $cache[$term->tid]['depth'] = $term->depth + 1;
    }
  }

  // Original taxonomy_get_tree() has an incremental index. Let's do this as well in a memory efficient way.
  $clean_tree = array();
  foreach ($tree as $idx => &$term) {
    if (!$max_depth || $term->depth < $max_depth) {
      $clean_tree[] = $term;
    }
  }
  unset($tree);
  return $clean_tree;
}