You are here

taxonomy_edge.core.inc in Taxonomy Edge 7.2

This file contains the core override functions

File

taxonomy_edge.core.inc
View source
<?php

/**
 * @file
 *
 * This file contains the core override functions
 */

/**
 * Reimplementation of taxonomy_get_tree().
 * Limit db fetch to only specified parent.
 * @see taxonomy_get_tree()
 */
function taxonomy_edge_get_tree_generic($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
  if (variable_get('taxonomy_edge_static_caching', TAXONOMY_EDGE_STATIC_CACHING)) {
    $children =& drupal_static('taxonomy_get_tree', array());
    $parents =& drupal_static('taxonomy_get_tree' . ':parents', array());
    $terms =& drupal_static('taxonomy_get_tree' . ':terms', array());
  }
  else {
    $children = $parents = $terms = array();
  }

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$vid][$parent])) {
    $children[$vid][$parent] = array();
    $query = db_select('taxonomy_term_data', 't');
    $query
      ->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
    $query
      ->addTag('translatable')
      ->addTag('term_access')
      ->fields('t')
      ->fields('h', array(
      'parent',
    ))
      ->condition('t.vid', $vid)
      ->orderBy('t.weight')
      ->orderBy('t.name');
    if ($parent) {
      $query
        ->join('taxonomy_term_edge_path', 'p', 'p.tid = t.tid');
      $query
        ->join('taxonomy_term_edge', 'e', 'e.pid = p.pid');
      $query
        ->join('taxonomy_term_edge_path', 'pp', 'pp.pid = e.parent');
      $query
        ->condition('pp.tid', $parent);
    }
    $result = $query
      ->execute();
    foreach ($result as $term) {

      // @todo Optimize this. Using tids as index protects us from multiple
      //       calls at the expense of performance on large trees.
      $children[$vid][$term->parent][$term->tid] = $term->tid;
      $parents[$vid][$term->tid][$term->parent] = $term->parent;
      $terms[$vid][$term->tid] = $term;
    }
  }

  // Load full entities, if necessary. The entity controller statically
  // caches the results.
  if ($load_entities) {
    $term_entities = taxonomy_term_load_multiple(array_keys($terms[$vid]));
  }
  $max_depth = !isset($max_depth) ? count($children[$vid]) : $max_depth;
  $tree = array();

  // Keeps track of the parents we have to process, the last entry is used
  // for the next processing step.
  $process_parents = array();
  $process_parents[] = $parent;

  // Loops over the parent terms and adds its children to the tree array.
  // Uses a loop instead of a recursion, because it's more efficient.
  while (count($process_parents)) {
    $parent = array_pop($process_parents);

    // The number of parents determines the current depth.
    $depth = count($process_parents);
    if ($max_depth > $depth && !empty($children[$vid][$parent])) {
      $has_children = FALSE;
      $child = current($children[$vid][$parent]);
      do {
        if (empty($child)) {
          break;
        }
        $term = $load_entities ? $term_entities[$child] : $terms[$vid][$child];
        if (count($parents[$vid][$term->tid]) > 1) {

          // We have a term with multi parents here. Clone the term,
          // so that the depth attribute remains correct.
          $term = clone $term;
        }
        $term->depth = $depth;
        unset($term->parent);
        $term->parents = array_values($parents[$vid][$term->tid]);
        $tree[] = $term;
        if (!empty($children[$vid][$term->tid])) {
          $has_children = TRUE;

          // We have to continue with this parent later.
          $process_parents[] = $parent;

          // Use the current term as parent for the next iteration.
          $process_parents[] = $term->tid;

          // Reset pointers for child lists because we step in there more often
          // with multi parents.
          reset($children[$vid][$term->tid]);

          // Move pointer so that we get the correct term the next time.
          next($children[$vid][$parent]);
          break;
        }
      } while ($child = next($children[$vid][$parent]));
      if (!$has_children) {

        // We processed all terms in this hierarchy-level, reset pointer
        // so that this function works the next time it gets called.
        reset($children[$vid][$parent]);
      }
    }
  }
  return $tree;
}

/**
 * Reimplementation of taxonomy_get_tree().
 * Limit db fetch to only specified parent AND use presorting.
 * @see taxonomy_get_tree()
 */
function taxonomy_edge_get_tree_optimized($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) {
  static $cache = array();
  static $pids = array();
  static $parents = array();
  static $offsets = array();
  $locate = array();
  if (variable_get('taxonomy_edge_static_caching', TAXONOMY_EDGE_STATIC_CACHING)) {
    if (!isset($cache[$vid])) {
      $cache[$vid] = array();
    }
    if (!isset($parents[$vid][$parent])) {
      $parents[$vid][$parent] = db_query("\n      SELECT e.parent, e.distance, c.tid\n      FROM taxonomy_term_edge_path p \n      INNER JOIN taxonomy_term_edge e ON e.pid = p.pid\n      INNER JOIN taxonomy_term_edge_path c ON c.pid = e.parent\n      WHERE p.tid = :tid\n      ", array(
        ':tid' => $parent,
      ))
        ->fetchAllAssoc('parent', PDO::FETCH_ASSOC);
    }
    $locate = array_intersect(array_keys($parents[$vid][$parent]), array_keys($cache[$vid]));
  }

  // No duplicates in the root level of query (i.e if the root level of query ($parent) has multiple parents)
  if (!isset($pids[$vid][$parent])) {
    $pids[$vid][$parent] = db_select('taxonomy_term_edge_path', 'p')
      ->fields('p', array(
      'pid',
    ))
      ->condition('p.tid', $parent)
      ->execute()
      ->fetchAll(PDO::FETCH_NUM);
    $pids[$vid][$parent] = _taxonomy_edge_unify_parents($pids[$vid][$parent]);
  }
  $pid = reset($pids[$vid][$parent]);
  if ($pid == '') {
    return array();
  }

  // If we are extracting a subtree from a previous cache, use that cache
  if ($locate) {
    $cache_pid = reset($locate);

    // If full cache, just return it
    if ($parents[$vid][$parent][$cache_pid]['distance'] == 0) {
      return $cache[$vid][$cache_pid];
    }

    // Locate subtree
    $tree = array();
    $idx = $offsets[$vid][$cache_pid][$pid];
    $sub_cache = $cache[$vid][$cache_pid];
    $idx_end = count($cache[$vid][$cache_pid]);
    $depth = $sub_cache[$idx]->depth + 1;
    for ($i = $idx + 1; $i < $idx_end; $i++) {
      $term = $sub_cache[$i];
      if ($depth > $term->depth) {
        break;
      }
      else {
        $term->depth -= $depth;
        $tree[] = $term;
      }
    }
    return $tree;
  }
  else {
    $valid_order = taxonomy_edge_is_order_valid($vid);

    // No cache, let's fetch from db
    $query = db_select('taxonomy_term_edge_path', 'p');
    $query
      ->join('taxonomy_term_data', 'd', 'd.tid = p.tid');
    $query
      ->join('taxonomy_term_edge', 'e', 'e.pid = p.pid');
    $query
      ->join('taxonomy_term_hierarchy', 'h', 'h.tid = d.tid');
    $query
      ->addTag('translatable')
      ->addTag('term_access')
      ->fields('p', array(
      'pid',
    ))
      ->fields('d')
      ->fields('e', array(
      'distance',
    ))
      ->fields('h', array(
      'parent',
    ))
      ->condition('e.parent', $pid)
      ->condition('e.distance', 0, '>')
      ->condition('e.vid', $vid);
    if ($valid_order) {
      $query
        ->join('taxonomy_term_edge_order', 'o', 'o.pid = p.pid');
      $query
        ->condition('o.vid', $vid);
      $query
        ->orderBy('o.oid');
    }
    else {
      $query
        ->addExpression(_taxonomy_edge_generate_term_path_query('p.pid'), 'sort_path');
      $query
        ->orderBy('sort_path');
    }
    $result = $query
      ->execute();
    $tree = array();
    $tids = array();
    $idx = 0;
    foreach ($result as $term) {
      if (!isset($tree[$term->pid])) {
        $term->depth = $term->distance - 1;
        $term->parents = array(
          $term->parent,
        );
        unset($term->distance);
        unset($term->parent);
        unset($term->sort_path);
        $tree[$term->pid] = $term;
        $offsets[$vid][$pid][$term->pid] = $idx++;
        unset($term->pid);
        $tids[] = $term->tid;
      }
      else {
        $tree[$term->pid]->parents[] = $term->parent;
      }
    }
  }

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

  // 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) {
    $clean_tree[] = $load_entities ? $term_entities[$term->tid] : $term;
    unset($tree[$idx]);
  }
  if (variable_get('taxonomy_edge_static_caching', TAXONOMY_EDGE_STATIC_CACHING)) {

    // Store result in static cache
    $cache[$vid][$pid] = $clean_tree;
    foreach ($pids[$vid][$parent] as $p) {
      if ($p != $pid) {
        $cache[$vid][$p] =& $cache[$vid][$pid];
        $offsets[$vid][$p] =& $offsets[$vid][$pid];
      }
    }
  }
  return $clean_tree;
}

Functions

Namesort descending Description
taxonomy_edge_get_tree_generic Reimplementation of taxonomy_get_tree(). Limit db fetch to only specified parent.
taxonomy_edge_get_tree_optimized Reimplementation of taxonomy_get_tree(). Limit db fetch to only specified parent AND use presorting.