Commit c7602236 authored by Dries's avatar Dries
Browse files

Patch by Marco:

- consistency in return order
(http://list.drupal.org/drupal-devel/2002-December/009522.html)
- new functions: taxonomy_get_term_by_name() and
taxonomy_get_vocabulary_by_name()
- caches are flushed after vocabulary and term edit/delete; this should
avoid problems like forums not up to date
parent b6b24c28
...@@ -94,6 +94,8 @@ function taxonomy_save_vocabulary($edit) { ...@@ -94,6 +94,8 @@ function taxonomy_save_vocabulary($edit) {
db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2)); db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
return t("created new vocabulary '%name'.", array("%name" => $edit["name"])); return t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
} }
cache_clear_all();
} }
function taxonomy_del_vocabulary($vid) { function taxonomy_del_vocabulary($vid) {
...@@ -105,6 +107,8 @@ function taxonomy_del_vocabulary($vid) { ...@@ -105,6 +107,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid); taxonomy_del_term($term->tid);
} }
cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name)); return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
} }
...@@ -213,6 +217,8 @@ function taxonomy_save_term($edit) { ...@@ -213,6 +217,8 @@ function taxonomy_save_term($edit) {
} }
} }
cache_clear_all();
return $message; return $message;
} }
...@@ -225,6 +231,8 @@ function taxonomy_del_term($tid) { ...@@ -225,6 +231,8 @@ function taxonomy_del_term($tid) {
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid); db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid); db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name)); return t("deleted term '%name'.", array("%name" => $term->name));
} }
...@@ -354,7 +362,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") { ...@@ -354,7 +362,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") {
static $terms; static $terms;
if (!isset($terms[$nid])) { if (!isset($terms[$nid])) {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight", $nid); $result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight, name", $nid);
$terms[$nid] = array(); $terms[$nid] = array();
while ($term = db_fetch_object($result)) { while ($term = db_fetch_object($result)) {
$terms[$nid][$term->$key] = $term; $terms[$nid][$term->$key] = $term;
...@@ -382,7 +390,7 @@ function taxonomy_node_delete($nid) { ...@@ -382,7 +390,7 @@ function taxonomy_node_delete($nid) {
// relations: return array of related terms // relations: return array of related terms
function taxonomy_get_related($tid, $key = "tid") { function taxonomy_get_related($tid, $key = "tid") {
if ($tid) { if ($tid) {
$result = db_query("SELECT t.*, tid1, tid2 FROM term_relation, term_data t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = '%d' OR tid2 = '%d') AND t.tid != '%d' ORDER BY weight", $tid, $tid, $tid); $result = db_query("SELECT t.*, tid1, tid2 FROM term_relation, term_data t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = '%d' OR tid2 = '%d') AND t.tid != '%d' ORDER BY weight, name", $tid, $tid, $tid);
$related = array(); $related = array();
while ($term = db_fetch_object($result)) { while ($term = db_fetch_object($result)) {
$related[$term->$key] = $term; $related[$term->$key] = $term;
...@@ -514,6 +522,44 @@ function _taxonomy_term_children($tid) { ...@@ -514,6 +522,44 @@ function _taxonomy_term_children($tid) {
return $children[$tid] ? $children[$tid] : array(); return $children[$tid] ? $children[$tid] : array();
} }
/**
* Try to map a string to existing vocabularies
* Provide case insensitive and trimmed map so as to
* maximize likelihood of successful mapping.
*
* @param string $name Name of the vocabulary to search
* @return array array of matching vocabularies, as objects
*/
function taxonomy_get_vocabulary_by_name($name) {
// LOWER is ANSI SQL-92
$db_result = db_query("SELECT * FROM vocabulary WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
$result = array();
while ($vocabulary = db_fetch_object($db_result)) {
$result[] = $vocabulary;
}
return $result;
}
/**
* Try to map a string to existing terms
* Provide case insensitive and trimmed map so as to
* maximize likelihood of successful mapping.
*
* @param string $name Name of the term to search
* @return array array of matching terms, as objects
*/
function taxonomy_get_term_by_name($name) {
// LOWER is ANSI SQL-92
$db_result = db_query("SELECT * FROM term_data WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
$result = array();
while ($term = db_fetch_object($db_result)) {
$result[] = $term;
}
return $result;
}
function taxonomy_get_vocabulary($vid) { function taxonomy_get_vocabulary($vid) {
// simple cache using a static var? // simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid)); return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid));
......
...@@ -94,6 +94,8 @@ function taxonomy_save_vocabulary($edit) { ...@@ -94,6 +94,8 @@ function taxonomy_save_vocabulary($edit) {
db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2)); db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
return t("created new vocabulary '%name'.", array("%name" => $edit["name"])); return t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
} }
cache_clear_all();
} }
function taxonomy_del_vocabulary($vid) { function taxonomy_del_vocabulary($vid) {
...@@ -105,6 +107,8 @@ function taxonomy_del_vocabulary($vid) { ...@@ -105,6 +107,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid); taxonomy_del_term($term->tid);
} }
cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name)); return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
} }
...@@ -213,6 +217,8 @@ function taxonomy_save_term($edit) { ...@@ -213,6 +217,8 @@ function taxonomy_save_term($edit) {
} }
} }
cache_clear_all();
return $message; return $message;
} }
...@@ -225,6 +231,8 @@ function taxonomy_del_term($tid) { ...@@ -225,6 +231,8 @@ function taxonomy_del_term($tid) {
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid); db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid); db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name)); return t("deleted term '%name'.", array("%name" => $term->name));
} }
...@@ -354,7 +362,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") { ...@@ -354,7 +362,7 @@ function taxonomy_node_get_terms($nid, $key = "tid") {
static $terms; static $terms;
if (!isset($terms[$nid])) { if (!isset($terms[$nid])) {
$result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight", $nid); $result = db_query("SELECT t.* FROM term_data t, term_node r WHERE r.tid = t.tid AND r.nid = '%d' ORDER BY weight, name", $nid);
$terms[$nid] = array(); $terms[$nid] = array();
while ($term = db_fetch_object($result)) { while ($term = db_fetch_object($result)) {
$terms[$nid][$term->$key] = $term; $terms[$nid][$term->$key] = $term;
...@@ -382,7 +390,7 @@ function taxonomy_node_delete($nid) { ...@@ -382,7 +390,7 @@ function taxonomy_node_delete($nid) {
// relations: return array of related terms // relations: return array of related terms
function taxonomy_get_related($tid, $key = "tid") { function taxonomy_get_related($tid, $key = "tid") {
if ($tid) { if ($tid) {
$result = db_query("SELECT t.*, tid1, tid2 FROM term_relation, term_data t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = '%d' OR tid2 = '%d') AND t.tid != '%d' ORDER BY weight", $tid, $tid, $tid); $result = db_query("SELECT t.*, tid1, tid2 FROM term_relation, term_data t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = '%d' OR tid2 = '%d') AND t.tid != '%d' ORDER BY weight, name", $tid, $tid, $tid);
$related = array(); $related = array();
while ($term = db_fetch_object($result)) { while ($term = db_fetch_object($result)) {
$related[$term->$key] = $term; $related[$term->$key] = $term;
...@@ -514,6 +522,44 @@ function _taxonomy_term_children($tid) { ...@@ -514,6 +522,44 @@ function _taxonomy_term_children($tid) {
return $children[$tid] ? $children[$tid] : array(); return $children[$tid] ? $children[$tid] : array();
} }
/**
* Try to map a string to existing vocabularies
* Provide case insensitive and trimmed map so as to
* maximize likelihood of successful mapping.
*
* @param string $name Name of the vocabulary to search
* @return array array of matching vocabularies, as objects
*/
function taxonomy_get_vocabulary_by_name($name) {
// LOWER is ANSI SQL-92
$db_result = db_query("SELECT * FROM vocabulary WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
$result = array();
while ($vocabulary = db_fetch_object($db_result)) {
$result[] = $vocabulary;
}
return $result;
}
/**
* Try to map a string to existing terms
* Provide case insensitive and trimmed map so as to
* maximize likelihood of successful mapping.
*
* @param string $name Name of the term to search
* @return array array of matching terms, as objects
*/
function taxonomy_get_term_by_name($name) {
// LOWER is ANSI SQL-92
$db_result = db_query("SELECT * FROM term_data WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
$result = array();
while ($term = db_fetch_object($db_result)) {
$result[] = $term;
}
return $result;
}
function taxonomy_get_vocabulary($vid) { function taxonomy_get_vocabulary($vid) {
// simple cache using a static var? // simple cache using a static var?
return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid)); return db_fetch_object(db_query("SELECT * FROM vocabulary WHERE vid = '%d'", $vid));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment