function taxonomy_edge_select_nodes in Taxonomy Edge 6
Same name and namespace in other branches
- 8 taxonomy_edge.module \taxonomy_edge_select_nodes()
- 7.2 taxonomy_edge.module \taxonomy_edge_select_nodes()
- 7 taxonomy_edge.module \taxonomy_edge_select_nodes()
Reimplementation of taxonomy_select_nodes(). Uses the Taxonomy Edge table instead of cpu/mem (mostly mem) hungry taxonomy_get_tree(). @note This function also fixes the "broken" default order by from the taxonomy_select_nodes() function, by added "n.nid" to the ORDER BY clause. This is ensure consistent results on each query.
See also
2 calls to taxonomy_edge_select_nodes()
- taxonomy_edge_taxonomy_select_nodes in ./
taxonomy_edge.module - taxonomy_edge_term_page in ./
taxonomy_edge.pages.inc - Menu callback; displays all nodes associated with a term.
File
- ./
taxonomy_edge.module, line 858 - Selecting all children of a given taxonomy term can be a pain. This module makes it easier to do this, by maintaining a complete list of edges for each term using the adjecency matrix graph theory.
Code
function taxonomy_edge_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC, n.nid DESC') {
// Fix broken default order by the Taxonomy Module.
// Ordering without a unique key could result in incosistent result sets,
// depending on which index (if any) the database decides to use for the query.
switch ($order) {
case 'n.sticky DESC, n.created DESC':
$order .= ', n.nid DESC';
break;
case 'n.sticky ASC, n.created ASC':
$order .= ', n.nid ASC';
break;
}
// If possible, use optimize by using GROUP BY instead of DISTINCT.
// This is what the idx_taxonomy_edge index is used for.
$group_by = '';
$distinct = 'DISTINCT(n.nid)';
global $db_type;
if ($db_type == 'mysql' || $db_type == 'mysqli') {
switch ($order) {
case 'n.sticky DESC, n.created DESC, n.nid DESC':
case 'n.sticky ASC, n.created ASC, n.nid ASC':
$group_by = ' GROUP BY n.sticky, n.created, n.nid';
$distinct = 'n.nid';
break;
case 'n.created DESC, n.nid DESC':
case 'n.created ASC, n.nid ASC':
$group_by = ' GROUP BY n.created, n.nid';
$distinct = 'n.nid';
break;
}
}
if (count($tids) > 0) {
if ($operator == 'or') {
$args = $tids;
$placeholders = db_placeholders($args, 'int');
if ($depth === 'all') {
$sql = 'SELECT ' . $distinct . ', n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid JOIN {term_edge} te ON tn.tid = te.tid WHERE te.parent IN(' . $placeholders . ') AND n.status = 1 ' . $group_by . ' ORDER BY ' . $order;
$sql_count = 'SELECT COUNT(DISTINCT n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid JOIN {term_edge} te ON tn.tid = te.tid WHERE te.parent IN(' . $placeholders . ') AND n.status = 1';
}
else {
$sql = 'SELECT ' . $distinct . ', n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid JOIN {term_edge} te ON tn.tid = te.tid WHERE te.parent IN(' . $placeholders . ') AND te.distance <= %d AND n.status = 1 ' . $group_by . ' ORDER BY ' . $order;
$sql_count = 'SELECT COUNT(DISTINCT n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid JOIN {term_edge} te ON tn.tid = te.tid WHERE te.parent IN(' . $placeholders . ') AND te.distance <= %d AND n.status = 1';
$args[] = $depth;
}
}
else {
$join = '';
$args = array();
foreach ($tids as $index => $tid) {
$join .= ' INNER JOIN {term_node} tn' . $index . ' ON n.vid = tn' . $index . '.vid';
$join .= ' INNER JOIN {term_edge} te' . $index . ' ON tn' . $index . '.tid = te' . $index . '.tid AND te' . $index . '.parent = %d';
$args[] = $tid;
if ($depth !== 'all') {
$join .= ' AND te' . $index . '.distance <= %d';
$args[] = $depth;
}
}
$sql = 'SELECT ' . $distinct . ', n.sticky, n.title, n.created FROM {node} n ' . $join . ' WHERE n.status = 1 ' . $group_by . ' ORDER BY ' . $order;
$sql_count = 'SELECT COUNT(DISTINCT n.nid) FROM {node} n ' . $join . ' WHERE n.status = 1';
}
$sql = db_rewrite_sql($sql);
if ($pager) {
$sql_count = db_rewrite_sql($sql_count);
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
}
return $result;
}