Commit 0475d4fb authored by Dries's avatar Dries
Browse files

Patch by Marco:

- rewrote taxonomy_get_tree() for improved performance and cleaner code
- fixed a bug in _taxonomy_term_select() with multiple parents
- added hooks in vocabulary and term insert, update and delete
- fixed a bug in taxonomy_save_vocabulary() (cache_clear_all() was never
called)
parent cc01e613
......@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) {
if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'");
return t("update vocabulary '%name'.", array("%name" => $edit["name"]));
module_invoke_all("taxonomy", "update", "vocabulary", $edit);
$message = t("updated vocabulary '%name'.", array("%name" => $edit["name"]));
}
else if ($edit["vid"]) {
return taxonomy_del_vocabulary($edit["vid"]);
$message = taxonomy_del_vocabulary($edit["vid"]);
}
else {
$data["vid"] = db_next_id("vocabulary");
db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
return t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
module_invoke_all("taxonomy", "insert", "vocabulary", $edit);
$message = t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
}
cache_clear_all();
return $message;
}
function taxonomy_del_vocabulary($vid) {
......@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid);
}
module_invoke_all("taxonomy", "delete", "vocabulary", $vocabulary);
cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
......@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%d'", $edit["tid"]);
module_invoke_all("taxonomy", "update", "term", $edit);
$message = t("the term '%a' has been updated.", array("%a" => $edit["name"]));
}
else if ($edit["tid"]) {
......@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) {
$edit["tid"] = db_next_id("term_data");
$data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]);
db_query("INSERT INTO term_data ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
module_invoke_all("taxonomy", "insert", "term", $edit);
$message = t("created new term '%name'.", array("%name" => $edit["name"]));
}
......@@ -232,6 +241,7 @@ function taxonomy_del_term($tid) {
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
module_invoke_all("taxonomy", "delete", "term", $term);
cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name));
......@@ -431,37 +441,34 @@ function taxonomy_get_children($tid, $vid = 0, $key = "tid") {
// hierarchy: get whole family, with tid, parent and depth; useful to show
function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") {
static $children, $parents, $terms, $tree;
if ($depth == -1) {
$children = array();
$parents = array();
$terms = array();
$tree = array();
}
static $children, $parents, $terms;
$depth++;
if (!count($children)) {
// we cache trees, so it's not cpu-intensive to call get_tree on a term and its children too
if (!isset($children[$vocabulary_id])) {
$children[$vocabulary_id] = array();
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%d' ORDER BY weight, name", $vocabulary_id);
while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid;
$parents[$term->tid][] = $term->parent;
$terms[$term->tid] = $term;
$children[$vocabulary_id][$term->parent][] = $term->tid;
$parents[$vocabulary_id][$term->tid][] = $term->parent;
$terms[$vocabulary_id][$term->tid] = $term;
}
}
if ($children[$parent]) {
foreach ($children[$parent] as $child) {
$terms[$child]->depth = $depth;
unset($terms[$child]->parent); // this would show just one parent
$terms[$child]->parents = $parents[$child];
$tree[] = $terms[$child];
if ($children[$vocabulary_id][$parent]) {
foreach ($children[$vocabulary_id][$parent] as $child) {
$terms[$vocabulary_id][$child]->depth = $depth;
unset($terms[$vocabulary_id][$child]->parent); // this is not useful as it would show one parent only
$terms[$vocabulary_id][$child]->parents = $parents[$vocabulary_id][$child];
$tree[] = $terms[$vocabulary_id][$child];
taxonomy_get_tree($vocabulary_id, $child, $depth, $key);
$tree = array_merge($tree, taxonomy_get_tree($vocabulary_id, $child, $depth));
}
}
return $tree;
return $tree ? $tree : array();
}
// synonyms: return array of synonyms
......@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
$tree = taxonomy_get_tree($vocabulary_id);
if ($blank) {
$options[0] = $blank;
$options[] = array("tid" => 0, "name" => $blank);
}
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
$options[$term->tid] = _taxonomy_depth($term->depth, '-').$term->name;
$options[] = array("tid" => $term->tid, "name" => _taxonomy_depth($term->depth, '-').$term->name);
}
}
if (!$blank && !$value) {
......@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
}
if (count($options) > 0) {
foreach ($options as $key=>$choice) {
$select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($key == $value ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</option>";
foreach ($options as $option) {
$select .= "<option value=\"".$option["tid"]."\"". (is_array($value) ? (in_array($option["tid"], $value) ? " selected=\"selected\"" : "") : ($option["tid"] == $value ? " selected=\"selected\"" : "")) .">". check_form($option["name"]) ."</option>";
}
$size = min(12, count($options));
......
......@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) {
if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'");
return t("update vocabulary '%name'.", array("%name" => $edit["name"]));
module_invoke_all("taxonomy", "update", "vocabulary", $edit);
$message = t("updated vocabulary '%name'.", array("%name" => $edit["name"]));
}
else if ($edit["vid"]) {
return taxonomy_del_vocabulary($edit["vid"]);
$message = taxonomy_del_vocabulary($edit["vid"]);
}
else {
$data["vid"] = db_next_id("vocabulary");
db_query("INSERT INTO vocabulary ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
return t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
module_invoke_all("taxonomy", "insert", "vocabulary", $edit);
$message = t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
}
cache_clear_all();
return $message;
}
function taxonomy_del_vocabulary($vid) {
......@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid);
}
module_invoke_all("taxonomy", "delete", "vocabulary", $vocabulary);
cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
......@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]);
db_query("UPDATE term_data SET ". _prepare_update($data) ." WHERE tid = '%d'", $edit["tid"]);
module_invoke_all("taxonomy", "update", "term", $edit);
$message = t("the term '%a' has been updated.", array("%a" => $edit["name"]));
}
else if ($edit["tid"]) {
......@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) {
$edit["tid"] = db_next_id("term_data");
$data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]);
db_query("INSERT INTO term_data ". _prepare_insert($data, 1) ." VALUES ". _prepare_insert($data, 2));
module_invoke_all("taxonomy", "insert", "term", $edit);
$message = t("created new term '%name'.", array("%name" => $edit["name"]));
}
......@@ -232,6 +241,7 @@ function taxonomy_del_term($tid) {
db_query("DELETE FROM term_synonym WHERE tid = '%d'", $tid);
db_query("DELETE FROM term_node WHERE tid = '%d'", $tid);
module_invoke_all("taxonomy", "delete", "term", $term);
cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name));
......@@ -431,37 +441,34 @@ function taxonomy_get_children($tid, $vid = 0, $key = "tid") {
// hierarchy: get whole family, with tid, parent and depth; useful to show
function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") {
static $children, $parents, $terms, $tree;
if ($depth == -1) {
$children = array();
$parents = array();
$terms = array();
$tree = array();
}
static $children, $parents, $terms;
$depth++;
if (!count($children)) {
// we cache trees, so it's not cpu-intensive to call get_tree on a term and its children too
if (!isset($children[$vocabulary_id])) {
$children[$vocabulary_id] = array();
$result = db_query("SELECT t.*, parent FROM term_data t, term_hierarchy h WHERE t.tid = h.tid AND t.vid = '%d' ORDER BY weight, name", $vocabulary_id);
while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid;
$parents[$term->tid][] = $term->parent;
$terms[$term->tid] = $term;
$children[$vocabulary_id][$term->parent][] = $term->tid;
$parents[$vocabulary_id][$term->tid][] = $term->parent;
$terms[$vocabulary_id][$term->tid] = $term;
}
}
if ($children[$parent]) {
foreach ($children[$parent] as $child) {
$terms[$child]->depth = $depth;
unset($terms[$child]->parent); // this would show just one parent
$terms[$child]->parents = $parents[$child];
$tree[] = $terms[$child];
if ($children[$vocabulary_id][$parent]) {
foreach ($children[$vocabulary_id][$parent] as $child) {
$terms[$vocabulary_id][$child]->depth = $depth;
unset($terms[$vocabulary_id][$child]->parent); // this is not useful as it would show one parent only
$terms[$vocabulary_id][$child]->parents = $parents[$vocabulary_id][$child];
$tree[] = $terms[$vocabulary_id][$child];
taxonomy_get_tree($vocabulary_id, $child, $depth, $key);
$tree = array_merge($tree, taxonomy_get_tree($vocabulary_id, $child, $depth));
}
}
return $tree;
return $tree ? $tree : array();
}
// synonyms: return array of synonyms
......@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
$tree = taxonomy_get_tree($vocabulary_id);
if ($blank) {
$options[0] = $blank;
$options[] = array("tid" => 0, "name" => $blank);
}
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
$options[$term->tid] = _taxonomy_depth($term->depth, '-').$term->name;
$options[] = array("tid" => $term->tid, "name" => _taxonomy_depth($term->depth, '-').$term->name);
}
}
if (!$blank && !$value) {
......@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
}
if (count($options) > 0) {
foreach ($options as $key=>$choice) {
$select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($key == $value ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</option>";
foreach ($options as $option) {
$select .= "<option value=\"".$option["tid"]."\"". (is_array($value) ? (in_array($option["tid"], $value) ? " selected=\"selected\"" : "") : ($option["tid"] == $value ? " selected=\"selected\"" : "")) .">". check_form($option["name"]) ."</option>";
}
$size = min(12, count($options));
......
......@@ -59,7 +59,8 @@
"2002-11-08" => "update_44",
"2002-11-20" => "update_45",
"2002-12-10" => "update_46",
"2002-12-22" => "update_47"
"2002-12-22" => "update_47",
"2002-12-29" => "update_48"
);
// Update functions
......@@ -655,6 +656,12 @@ function update_47() {
);");
}
function update_48() {
if ($max = db_result(db_query("SELECT MAX(tid) FROM vocabulary"))) {
update_sql("REPLACE INTO sequences VALUES ('vocabulary', $max)");
}
}
function update_upgrade3() {
update_sql("INSERT INTO system VALUES ('archive.module','archive','module','',1)");
update_sql("INSERT INTO system VALUES ('block.module','block','module','',1)");
......
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