You are here

function nodeorder_select_nodes in Node Order 5

Same name and namespace in other branches
  1. 6 nodeorder.module \nodeorder_select_nodes()
  2. 7 nodeorder.module \nodeorder_select_nodes()

NOTE: This is nearly a direct copy of taxonomy_select_nodes() -- see http://drupal.org/node/25801 if you find this sort of copy and paste upsetting...

Finds all nodes that match selected taxonomy conditions.

Parameters

$tids: An array of term IDs to match.

$operator: How to interpret multiple IDs in the array. Can be "or" or "and".

$depth: How many levels deep to traverse the taxonomy tree. Can be a nonnegative integer or "all".

$pager: Whether the nodes are to be used with a pager (the case on most Drupal pages) or not (in an XML feed, for example).

$order: The order clause for the query that retrieve the nodes.

$count: If $pager is TRUE, the number of nodes per page, or -1 to use the backward-compatible 'default_nodes_main' variable setting. If $pager is FALSE, the total number of nodes to select; or -1 to use the backward-compatible 'feed_default_items' variable setting; or 0 to select all nodes.

Return value

A resource identifier pointing to the query results.

1 call to nodeorder_select_nodes()
nodeorder_order_nodes in ./nodeorder.module

File

./nodeorder.module, line 240

Code

function nodeorder_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC', $count = -1) {
  if (count($tids) > 0) {

    // For each term ID, generate an array of descendant term IDs to the right depth.
    $descendant_tids = array();
    if ($depth === 'all') {
      $depth = NULL;
    }
    foreach ($tids as $index => $tid) {
      $term = taxonomy_get_term($tid);
      $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
      $descendant_tids[] = array_merge(array(
        $tid,
      ), array_map('_taxonomy_get_tid_from_term', $tree));
    }
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (' . $str_tids . ') AND n.status = 1 ORDER BY ' . $order;
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (' . $str_tids . ') AND n.status = 1';
    }
    else {
      $joins = '';
      $wheres = '';
      foreach ($descendant_tids as $index => $tids) {
        $joins .= ' INNER JOIN {term_node} tn' . $index . ' ON n.nid = tn' . $index . '.nid';
        $wheres .= ' AND tn' . $index . '.tid IN (' . implode(',', $tids) . ')';
      }
      $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n ' . $joins . ' WHERE n.status = 1 ' . $wheres . ' ORDER BY ' . $order;
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n ' . $joins . ' WHERE n.status = 1 ' . $wheres;
    }
    $sql = db_rewrite_sql($sql);
    $sql_count = db_rewrite_sql($sql_count);
    if ($pager) {
      if ($count == -1) {
        $count = variable_get('default_nodes_main', 10);
      }
      $result = pager_query($sql, $count, 0, $sql_count);
    }
    else {
      if ($count == -1) {
        $count = variable_get('feed_default_items', 10);
      }
      if ($count == 0) {
        $result = db_query($sql);
      }
      else {
        $result = db_query_range($sql, 0, $count);
      }
    }
  }
  return $result;
}