You are here

function taxonomy_edge_get_tree_optimized in Taxonomy Edge 7

Same name and namespace in other branches
  1. 8 taxonomy_edge.core.inc \taxonomy_edge_get_tree_optimized()
  2. 6 taxonomy_edge.core.inc \taxonomy_edge_get_tree_optimized()
  3. 7.2 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().

File

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

Code

function taxonomy_edge_get_tree_optimized($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
  $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, $load_entities);
  }
  $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, $load_entities);
  }

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

  // Return cache if present.
  if ($static_cache) {

    // Cache per vocabulary.
    if (!isset($cache[$vid])) {
      $cache[$vid] = array();
    }
    $cache =& $cache[$vid];
    if (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.
  $query = db_select('taxonomy_term_edge', 'e');
  $query
    ->join('taxonomy_term_data', 'd', 'd.tid = e.tid');
  $query
    ->addTag('translatable')
    ->addTag('term_access')
    ->fields('e', array(
    'parent',
    'distance',
  ))
    ->fields('d')
    ->condition('e.parent', $parent)
    ->condition('e.distance', 0, '>')
    ->condition('e.vid', $vid);
  if ($max_depth) {
    $query
      ->condition('e.distance', $max_depth, '<=');
  }
  if ($valid_order) {
    $query
      ->join('taxonomy_term_edge_order', 'o', 'o.eid = e.eid AND o.parent = e.parent AND o.vid = ' . intval($vid));
    $query
      ->orderBy('o.oid');
  }
  else {
    $query
      ->addExpression(_taxonomy_edge_generate_term_path_query('e.tid'), 'sort_path');
    $query
      ->orderBy('sort_path');
  }
  $result = $query
    ->execute();
  $tree = array();
  if ($static_cache) {
    $cache[$parent]['offset'] = 0;
    $cache[$parent]['count'] = 0;
    $cache[$parent]['depth'] = 0;
  }
  foreach ($result as $term) {
    $term->depth = $term->distance - 1;
    $term->parents = array(
      $term->parent,
    );
    unset($term->distance);
    unset($term->parent);
    unset($term->sort_path);
    $tree[$term->tid] = $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;
    }
  }

  // Load full entities, if necessary. The entity controller statically
  // caches the results.
  if ($load_entities) {
    $term_entities = taxonomy_term_load_multiple(array_keys($tree));
  }

  // 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 &$term) {
    if ($load_entities) {

      // Store values depth and parents, because the entity which overwrites
      // does not carry these.
      $t_depth = $term->depth;
      $t_parents = $term->parents;
      $term = $term_entities[$term->tid];

      // Re-add depth and parents to the term (entity).
      $term->depth = $t_depth;
      $term->parents = $t_parents;
    }
    if (!$max_depth || $term->depth < $max_depth) {
      $clean_tree[] = $term;
    }
  }
  unset($tree);
  return $clean_tree;
}