You are here

function taxonomy_edge_select_nodes in Taxonomy Edge 7

Same name and namespace in other branches
  1. 8 taxonomy_edge.module \taxonomy_edge_select_nodes()
  2. 6 taxonomy_edge.module \taxonomy_edge_select_nodes()
  3. 7.2 taxonomy_edge.module \taxonomy_edge_select_nodes()

Reimplementation of taxonomy_select_nodes() re-allowing depth modifier.

3 calls to taxonomy_edge_select_nodes()
TaxonomyEdgeTreeTestCase::testNodes in tests/tree.test
taxonomy_edge_term_feed in ./taxonomy_edge.pages.inc
Generate the content feed for a taxonomy term.
taxonomy_edge_term_page in ./taxonomy_edge.pages.inc
Menu callback; displays all nodes associated with a term.

File

./taxonomy_edge.module, line 889
Optimization of taxonomy data model for SQL performance.

Code

function taxonomy_edge_select_nodes($tid, $pager = TRUE, $limit = FALSE, $depth = 0, $order = NULL) {
  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }

  // If no depth, just use core (faster query due to no joins).
  if ($depth == 0) {
    $args = isset($order) ? array(
      $tid,
      $pager,
      $limit,
      $order,
    ) : array(
      $tid,
      $pager,
      $limit,
    );
    return call_user_func_array('taxonomy_select_nodes', $args);
  }

  // Set default order if not specified.
  if (!isset($order)) {
    $order = array(
      'n.sticky' => 'DESC',
      'n.created' => 'DESC',
      'n.nid' => 'DESC',
    );
  }

  // Ensure that valid (but ridiculous?) usage doesn't break query.
  unset($order['t.tid']);

  // Lookup vid for better query on edge table.
  $vid = db_query("SELECT vid FROM {taxonomy_term_data} WHERE tid = :tid", array(
    ':tid' => $tid,
  ))
    ->fetchField();

  // Locate nodes to.
  $subquery = db_select('taxonomy_index', 'i');
  $subquery
    ->join('taxonomy_term_edge', 'e', 'e.tid = i.tid');
  $subquery
    ->condition('e.parent', $tid);
  $subquery
    ->condition('e.vid', $vid);
  $subquery
    ->condition('e.distance', $depth, '<=');
  $subquery
    ->fields('i', array(
    'nid',
  ));

  // Fetch nodes.
  $query = db_select('node', 'n');
  $query
    ->addTag('node_access');
  $query
    ->condition('n.nid', $subquery, 'IN');
  if ($pager) {
    $count_query = db_select('taxonomy_index', 't');
    $count_query
      ->join('taxonomy_term_edge', 'e', 'e.tid = t.tid');
    $count_query
      ->condition('e.parent', $tid);
    $count_query
      ->condition('e.vid', $vid);
    $count_query
      ->condition('e.distance', $depth, '<=');
    $count_query
      ->addExpression('COUNT(DISTINCT t.nid)');
    $query = $query
      ->extend('PagerDefault');
    if ($limit !== FALSE) {
      $query = $query
        ->limit($limit);
    }
    $query
      ->setCountQuery($count_query);
  }
  else {
    if ($limit !== FALSE) {
      $query
        ->range(0, $limit);
    }
  }
  $query
    ->addField('n', 'nid');
  foreach ($order as $field => $direction) {
    $query
      ->orderBy($field, $direction);

    // ORDER BY fields need to be loaded too, assume they are in the form
    // table_alias.name
    list($table_alias, $name) = explode('.', $field);
    $query
      ->addField($table_alias, $name);
  }
  return $query
    ->execute()
    ->fetchCol();
}