| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/* own version of taxonomy_select_nodes to use no limit query - @param $count */
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Finds all nodes that match selected taxonomy conditions.
|
| 8 |
*
|
| 9 |
* @param $tids
|
| 10 |
* An array of term IDs to match.
|
| 11 |
* @param $operator
|
| 12 |
* How to interpret multiple IDs in the array. Can be "or" or "and".
|
| 13 |
* @param $depth
|
| 14 |
* How many levels deep to traverse the taxonomy tree. Can be a nonnegative
|
| 15 |
* integer or "all".
|
| 16 |
* @param $pager
|
| 17 |
* Whether the nodes are to be used with a pager (the case on most Drupal
|
| 18 |
* pages) or not (in an XML feed, for example).
|
| 19 |
* @param $order
|
| 20 |
* The order clause for the query that retrieve the nodes.
|
| 21 |
* @param $count
|
| 22 |
* If $pager is TRUE, the number of nodes per page, or -1 to use the
|
| 23 |
* backward-compatible 'default_nodes_main' variable setting. If $pager
|
| 24 |
* is FALSE, the total number of nodes to select; or -1 to use the
|
| 25 |
* backward-compatible 'feed_default_items' variable setting; or 0 to
|
| 26 |
* select all nodes.
|
| 27 |
* @return
|
| 28 |
* A resource identifier pointing to the query results.
|
| 29 |
*/
|
| 30 |
function taxonomy_vtn_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC', $count = -1) {
|
| 31 |
if (count($tids) > 0) {
|
| 32 |
// For each term ID, generate an array of descendant term IDs to the right depth.
|
| 33 |
$descendant_tids = array();
|
| 34 |
if ($depth === 'all') {
|
| 35 |
$depth = NULL;
|
| 36 |
}
|
| 37 |
foreach ($tids as $index => $tid) {
|
| 38 |
$term = taxonomy_get_term($tid);
|
| 39 |
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
|
| 40 |
$descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
|
| 41 |
}
|
| 42 |
|
| 43 |
if ($operator == 'or') {
|
| 44 |
$args = call_user_func_array('array_merge', $descendant_tids);
|
| 45 |
$placeholders = db_placeholders($args, 'int');
|
| 46 |
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1 ORDER BY '. $order;
|
| 47 |
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1';
|
| 48 |
}
|
| 49 |
else {
|
| 50 |
$joins = '';
|
| 51 |
$wheres = '';
|
| 52 |
$args = array();
|
| 53 |
foreach ($descendant_tids as $index => $tids) {
|
| 54 |
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.vid = tn'. $index .'.vid';
|
| 55 |
$wheres .= ' AND tn'. $index .'.tid IN ('. db_placeholders($tids, 'int') .')';
|
| 56 |
$args = array_merge($args, $tids);
|
| 57 |
}
|
| 58 |
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY '. $order;
|
| 59 |
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres;
|
| 60 |
}
|
| 61 |
$sql = db_rewrite_sql($sql);
|
| 62 |
$sql_count = db_rewrite_sql($sql_count);
|
| 63 |
if ($pager) {
|
| 64 |
if ($count == -1) {
|
| 65 |
$count = variable_get('default_nodes_main', 10);
|
| 66 |
}
|
| 67 |
$result = pager_query($sql, $count, 0, $sql_count, $args);
|
| 68 |
}
|
| 69 |
else {
|
| 70 |
if ($count == -1) {
|
| 71 |
$count = variable_get('feed_default_items', 10);
|
| 72 |
}
|
| 73 |
|
| 74 |
if ($count == 0) {
|
| 75 |
$result = db_query($sql, $args);
|
| 76 |
}
|
| 77 |
else {
|
| 78 |
$result = db_query_range($sql, $args, 0, $count);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
}
|
| 82 |
return $result;
|
| 83 |
}
|