summaryrefslogtreecommitdiff
path: root/modules/taxonomy/taxonomy.module
diff options
context:
space:
mode:
Diffstat (limited to 'modules/taxonomy/taxonomy.module')
-rw-r--r--modules/taxonomy/taxonomy.module70
1 files changed, 44 insertions, 26 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 48919e3ee..b6f370a51 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -658,7 +658,7 @@ function taxonomy_form_alter($form_id, &$form) {
$node = $form['#node'];
if (!isset($node->taxonomy)) {
- $terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node->nid);
+ $terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node);
}
else {
$terms = $node->taxonomy;
@@ -730,8 +730,8 @@ function taxonomy_form_alter($form_id, &$form) {
/**
* Find all terms associated with the given node, within one vocabulary.
*/
-function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
- $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
+function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') {
+ $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $node->vid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
@@ -742,17 +742,17 @@ function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
/**
* Find all terms associated with the given node, ordered by vocabulary and term weight.
*/
-function taxonomy_node_get_terms($nid, $key = 'tid') {
+function taxonomy_node_get_terms($node, $key = 'tid') {
static $terms;
- if (!isset($terms[$nid])) {
- $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $nid);
- $terms[$nid] = array();
+ if (!isset($terms[$node->vid])) {
+ $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $node->vid);
+ $terms[$node->vid] = array();
while ($term = db_fetch_object($result)) {
- $terms[$nid][$term->$key] = $term;
+ $terms[$node->vid][$term->$key] = $term;
}
}
- return $terms[$nid];
+ return $terms[$node->vid];
}
/**
@@ -777,8 +777,9 @@ function taxonomy_node_validate(&$node) {
/**
* Save term associations for a given node.
*/
-function taxonomy_node_save($nid, $terms) {
- taxonomy_node_delete($nid);
+function taxonomy_node_save($node, $terms) {
+
+ taxonomy_node_delete_revision($node);
// Free tagging vocabularies do not send their tids in the form,
// so we'll detect them here and process them independently.
@@ -820,7 +821,7 @@ function taxonomy_node_save($nid, $terms) {
// Defend against duplicate, differently cased tags
if (!isset($inserted[$typed_term_tid])) {
- db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $typed_term_tid);
+ db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $typed_term_tid);
$inserted[$typed_term_tid] = TRUE;
}
}
@@ -832,15 +833,15 @@ function taxonomy_node_save($nid, $terms) {
if (is_array($term)) {
foreach ($term as $tid) {
if ($tid) {
- db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
+ db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $tid);
}
}
}
else if (is_object($term)) {
- db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term->tid);
+ db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $term->tid);
}
else if ($term) {
- db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
+ db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $term);
}
}
}
@@ -849,8 +850,15 @@ function taxonomy_node_save($nid, $terms) {
/**
* Remove associations of a node to its terms.
*/
-function taxonomy_node_delete($nid) {
- db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
+function taxonomy_node_delete($node) {
+ db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid);
+}
+
+/**
+ * Remove associations of a node to its terms.
+ */
+function taxonomy_node_delete_revision($node) {
+ db_query('DELETE FROM {term_node} WHERE vid = %d', $node->vid);
}
/**
@@ -1036,10 +1044,10 @@ function taxonomy_term_count_nodes($tid, $type = 0) {
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 {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
+ $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {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 {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
+ $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {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);
}
while ($term = db_fetch_object($result)) {
$count[$type][$term->tid] = $term->c;
@@ -1217,14 +1225,14 @@ function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $p
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';
+ $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = t.vid 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.vid = tn.vid 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';
+ $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.vid = tn'. $index .'.vid';
$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;
@@ -1266,22 +1274,32 @@ function taxonomy_render_nodes($result) {
function taxonomy_nodeapi($node, $op, $arg = 0) {
switch ($op) {
case 'load':
- $output['taxonomy'] = taxonomy_node_get_terms($node->nid);
+ $output['taxonomy'] = taxonomy_node_get_terms($node);
return $output;
+
case 'insert':
- taxonomy_node_save($node->nid, $node->taxonomy);
+ taxonomy_node_save($node, $node->taxonomy);
break;
+
case 'update':
- taxonomy_node_save($node->nid, $node->taxonomy);
+ taxonomy_node_save($node, $node->taxonomy);
break;
+
case 'delete':
- taxonomy_node_delete($node->nid);
+ taxonomy_node_delete($node);
+ break;
+
+ case 'delete revision':
+ taxonomy_node_delete_revision($node);
break;
+
case 'validate':
taxonomy_node_validate($node);
break;
+
case 'rss item':
return taxonomy_rss_item($node);
+
case 'update index':
return taxonomy_node_update_index($node);
}