summaryrefslogtreecommitdiff
path: root/modules/taxonomy/taxonomy.module
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2009-01-28 01:14:39 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2009-01-28 01:14:39 +0000
commit8fa274af765bb74a520fdb4211321ee489d9fec1 (patch)
tree8e80f027534ede61d08f2400070a6669d3b48410 /modules/taxonomy/taxonomy.module
parent585aa50e6a2d7d8f67f8116c207a342c47728dfb (diff)
downloadbrdo-8fa274af765bb74a520fdb4211321ee489d9fec1.tar.gz
brdo-8fa274af765bb74a520fdb4211321ee489d9fec1.tar.bz2
#144969 by beginner, Wim Leers, and catch: Fix count returned by taxonomy_term_count_nodes() with multi-select vocabularies (with tests).
Diffstat (limited to 'modules/taxonomy/taxonomy.module')
-rw-r--r--modules/taxonomy/taxonomy.module88
1 files changed, 39 insertions, 49 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index b28e0a306..cb9db61c3 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -798,14 +798,14 @@ function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
*
* @param $vid
* Which vocabulary to generate the tree for.
- *
* @param $parent
* The term ID under which to generate the tree. If 0, generate the tree
* for the entire vocabulary.
- *
* @param $max_depth
* The number of levels of the tree to return. Leave NULL to return all levels.
- *
+ * @param $reset
+ * Whether to reset the static cache, you should only use this if
+ * updating and loading term hierarchies during a page request.
* @param $depth
* Internal use only.
*
@@ -814,9 +814,13 @@ function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
* to have "depth" and "parents" attributes in addition to its normal ones.
* Results are statically cached.
*/
-function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) {
+function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $reset = FALSE, $depth = -1) {
static $children, $parents, $terms;
+ if ($reset) {
+ $children = $parents = $terms = array();
+ }
+
$depth++;
// We cache trees, so it's not CPU-intensive to call get_tree() on a term
@@ -845,7 +849,7 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $depth = -1) {
$tree[] = $term;
if (!empty($children[$vid][$child])) {
- $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $max_depth, $depth));
+ $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $max_depth, $reset, $depth));
}
}
}
@@ -898,61 +902,47 @@ function taxonomy_get_synonym_root($synonym, $reset = FALSE) {
* Count the number of published nodes classified by a term.
*
* @param $tid
- * The term's ID
- *
+ * The term ID
* @param $type
- * The $node->type. If given, taxonomy_term_count_nodes only counts
+ * (Optional) The $node->type. If given, taxonomy_term_count_nodes only counts
* nodes of $type that are classified with the term $tid.
+ * @param $reset
+ * (Optional) Boolean to indicated whether to reset the internal cache.
*
- * @return int
+ * @return
* An integer representing a number of nodes.
* Results are statically cached.
*/
-function taxonomy_term_count_nodes($tid, $type = 0) {
- static $count;
-
- if (!isset($count[$type])) {
- // $type == 0 always evaluates TRUE if $type is a string
- if (is_numeric($type)) {
- $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 GROUP BY t.tid'));
- }
- else {
- $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
- }
- $count[$type] = array();
- while ($term = db_fetch_object($result)) {
- $count[$type][$term->tid] = $term->c;
- }
- }
- $children_count = 0;
- foreach (_taxonomy_term_children($tid) as $c) {
- $children_count += taxonomy_term_count_nodes($c, $type);
+function taxonomy_term_count_nodes($tid, $type = NULL, $reset = FALSE) {
+ static $count = array();
+ if ($reset) {
+ $count = array();
}
- return $children_count + (isset($count[$type][$tid]) ? $count[$type][$tid] : 0);
-}
-/**
- * Helper for taxonomy_term_count_nodes(). Used to find out
- * which terms are children of a parent term.
- *
- * @param $tid
- * The parent term's ID
- *
- * @return array
- * An array of term IDs representing the children of $tid.
- * Results are statically cached.
- *
- */
-function _taxonomy_term_children($tid) {
- static $children;
+ // If $type is NULL, change it to 0 to allow it to be used as an array key
+ // for the static cache.
+ $type = empty($type) ? 0 : $type;
- if (!isset($children)) {
- $result = db_query('SELECT tid, parent FROM {taxonomy_term_hierarchy}');
- while ($term = db_fetch_object($result)) {
- $children[$term->parent][] = $term->tid;
+ if (!isset($count[$type][$tid])) {
+ $term = taxonomy_term_load($tid);
+ $tree = taxonomy_get_tree($term->vid, $tid, NULL, $reset);
+ $tids = array($tid);
+ foreach ($tree as $descendent) {
+ $tids[] = $descendent->tid;
+ }
+
+ $query = db_select('taxonomy_term_node', 't');
+ $query->addExpression('COUNT(DISTINCT(n.nid))', 'nid_count');
+ $query->join('node', 'n', 't.vid = n.vid');
+ $query->condition('t.tid', $tids, 'IN');
+ $query->condition('n.status', 1);
+ if (!is_numeric($type)) {
+ $query->condition('n.type', $type);
}
+ $query->addTag('term_access');
+ $count[$type][$tid] = $query->execute()->fetchField();
}
- return isset($children[$tid]) ? $children[$tid] : array();
+ return $count[$type][$tid];
}
/**