Skip to content
Snippets Groups Projects
Commit 0475d4fb authored by Dries Buytaert's avatar Dries Buytaert
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
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) { ...@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) {
if ($edit["vid"] && $edit["name"]) { if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'"); 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"]) { else if ($edit["vid"]) {
return taxonomy_del_vocabulary($edit["vid"]); $message = taxonomy_del_vocabulary($edit["vid"]);
} }
else { else {
$data["vid"] = db_next_id("vocabulary");
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"])); module_invoke_all("taxonomy", "insert", "vocabulary", $edit);
$message = t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
} }
cache_clear_all(); cache_clear_all();
return $message;
} }
function taxonomy_del_vocabulary($vid) { function taxonomy_del_vocabulary($vid) {
...@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) { ...@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid); taxonomy_del_term($term->tid);
} }
module_invoke_all("taxonomy", "delete", "vocabulary", $vocabulary);
cache_clear_all(); cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name)); return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
...@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) { ...@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]); $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"]); 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"])); $message = t("the term '%a' has been updated.", array("%a" => $edit["name"]));
} }
else if ($edit["tid"]) { else if ($edit["tid"]) {
...@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) { ...@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) {
$edit["tid"] = db_next_id("term_data"); $edit["tid"] = db_next_id("term_data");
$data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]); $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)); 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"])); $message = t("created new term '%name'.", array("%name" => $edit["name"]));
} }
...@@ -232,6 +241,7 @@ function taxonomy_del_term($tid) { ...@@ -232,6 +241,7 @@ 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);
module_invoke_all("taxonomy", "delete", "term", $term);
cache_clear_all(); cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name)); return t("deleted term '%name'.", array("%name" => $term->name));
...@@ -431,37 +441,34 @@ function taxonomy_get_children($tid, $vid = 0, $key = "tid") { ...@@ -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 // hierarchy: get whole family, with tid, parent and depth; useful to show
function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") { function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") {
static $children, $parents, $terms, $tree; static $children, $parents, $terms;
if ($depth == -1) {
$children = array();
$parents = array();
$terms = array();
$tree = array();
}
$depth++; $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); $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)) { while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid; $children[$vocabulary_id][$term->parent][] = $term->tid;
$parents[$term->tid][] = $term->parent; $parents[$vocabulary_id][$term->tid][] = $term->parent;
$terms[$term->tid] = $term; $terms[$vocabulary_id][$term->tid] = $term;
} }
} }
if ($children[$parent]) { if ($children[$vocabulary_id][$parent]) {
foreach ($children[$parent] as $child) { foreach ($children[$vocabulary_id][$parent] as $child) {
$terms[$child]->depth = $depth; $terms[$vocabulary_id][$child]->depth = $depth;
unset($terms[$child]->parent); // this would show just one parent unset($terms[$vocabulary_id][$child]->parent); // this is not useful as it would show one parent only
$terms[$child]->parents = $parents[$child]; $terms[$vocabulary_id][$child]->parents = $parents[$vocabulary_id][$child];
$tree[] = $terms[$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 // synonyms: return array of synonyms
...@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti ...@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
$tree = taxonomy_get_tree($vocabulary_id); $tree = taxonomy_get_tree($vocabulary_id);
if ($blank) { if ($blank) {
$options[0] = $blank; $options[] = array("tid" => 0, "name" => $blank);
} }
if ($tree) { if ($tree) {
foreach ($tree as $term) { foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) { 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) { if (!$blank && !$value) {
...@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti ...@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
} }
if (count($options) > 0) { if (count($options) > 0) {
foreach ($options as $key=>$choice) { foreach ($options as $option) {
$select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($key == $value ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</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)); $size = min(12, count($options));
......
...@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) { ...@@ -88,17 +88,22 @@ function taxonomy_save_vocabulary($edit) {
if ($edit["vid"] && $edit["name"]) { if ($edit["vid"] && $edit["name"]) {
db_query("UPDATE vocabulary SET ". _prepare_update($data) ." WHERE vid = '". check_query($edit["vid"]) ."'"); 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"]) { else if ($edit["vid"]) {
return taxonomy_del_vocabulary($edit["vid"]); $message = taxonomy_del_vocabulary($edit["vid"]);
} }
else { else {
$data["vid"] = db_next_id("vocabulary");
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"])); module_invoke_all("taxonomy", "insert", "vocabulary", $edit);
$message = t("created new vocabulary '%name'.", array("%name" => $edit["name"]));
} }
cache_clear_all(); cache_clear_all();
return $message;
} }
function taxonomy_del_vocabulary($vid) { function taxonomy_del_vocabulary($vid) {
...@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) { ...@@ -110,6 +115,8 @@ function taxonomy_del_vocabulary($vid) {
taxonomy_del_term($term->tid); taxonomy_del_term($term->tid);
} }
module_invoke_all("taxonomy", "delete", "vocabulary", $vocabulary);
cache_clear_all(); cache_clear_all();
return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name)); return t("deleted vocabulary '%name'.", array("%name" => $vocabulary->name));
...@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) { ...@@ -173,6 +180,7 @@ function taxonomy_save_term($edit) {
$data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"]); $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"]); 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"])); $message = t("the term '%a' has been updated.", array("%a" => $edit["name"]));
} }
else if ($edit["tid"]) { else if ($edit["tid"]) {
...@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) { ...@@ -182,6 +190,7 @@ function taxonomy_save_term($edit) {
$edit["tid"] = db_next_id("term_data"); $edit["tid"] = db_next_id("term_data");
$data = array("tid" => $edit["tid"], "name" => $edit["name"], "description" => $edit["description"], "vid" => $edit["vid"], "weight" => $edit["weight"]); $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)); 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"])); $message = t("created new term '%name'.", array("%name" => $edit["name"]));
} }
...@@ -232,6 +241,7 @@ function taxonomy_del_term($tid) { ...@@ -232,6 +241,7 @@ 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);
module_invoke_all("taxonomy", "delete", "term", $term);
cache_clear_all(); cache_clear_all();
return t("deleted term '%name'.", array("%name" => $term->name)); return t("deleted term '%name'.", array("%name" => $term->name));
...@@ -431,37 +441,34 @@ function taxonomy_get_children($tid, $vid = 0, $key = "tid") { ...@@ -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 // hierarchy: get whole family, with tid, parent and depth; useful to show
function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") { function taxonomy_get_tree($vocabulary_id, $parent = 0, $depth = -1, $key = "tid") {
static $children, $parents, $terms, $tree; static $children, $parents, $terms;
if ($depth == -1) {
$children = array();
$parents = array();
$terms = array();
$tree = array();
}
$depth++; $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); $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)) { while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid; $children[$vocabulary_id][$term->parent][] = $term->tid;
$parents[$term->tid][] = $term->parent; $parents[$vocabulary_id][$term->tid][] = $term->parent;
$terms[$term->tid] = $term; $terms[$vocabulary_id][$term->tid] = $term;
} }
} }
if ($children[$parent]) { if ($children[$vocabulary_id][$parent]) {
foreach ($children[$parent] as $child) { foreach ($children[$vocabulary_id][$parent] as $child) {
$terms[$child]->depth = $depth; $terms[$vocabulary_id][$child]->depth = $depth;
unset($terms[$child]->parent); // this would show just one parent unset($terms[$vocabulary_id][$child]->parent); // this is not useful as it would show one parent only
$terms[$child]->parents = $parents[$child]; $terms[$vocabulary_id][$child]->parents = $parents[$vocabulary_id][$child];
$tree[] = $terms[$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 // synonyms: return array of synonyms
...@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti ...@@ -575,13 +582,13 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
$tree = taxonomy_get_tree($vocabulary_id); $tree = taxonomy_get_tree($vocabulary_id);
if ($blank) { if ($blank) {
$options[0] = $blank; $options[] = array("tid" => 0, "name" => $blank);
} }
if ($tree) { if ($tree) {
foreach ($tree as $term) { foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) { 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) { if (!$blank && !$value) {
...@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti ...@@ -591,8 +598,8 @@ function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $descripti
} }
if (count($options) > 0) { if (count($options) > 0) {
foreach ($options as $key=>$choice) { foreach ($options as $option) {
$select .= "<option value=\"$key\"". (is_array($value) ? (in_array($key, $value) ? " selected=\"selected\"" : "") : ($key == $value ? " selected=\"selected\"" : "")) .">". check_form($choice) ."</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)); $size = min(12, count($options));
......
...@@ -59,7 +59,8 @@ ...@@ -59,7 +59,8 @@
"2002-11-08" => "update_44", "2002-11-08" => "update_44",
"2002-11-20" => "update_45", "2002-11-20" => "update_45",
"2002-12-10" => "update_46", "2002-12-10" => "update_46",
"2002-12-22" => "update_47" "2002-12-22" => "update_47",
"2002-12-29" => "update_48"
); );
// Update functions // Update functions
...@@ -655,6 +656,12 @@ function update_47() { ...@@ -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() { function update_upgrade3() {
update_sql("INSERT INTO system VALUES ('archive.module','archive','module','',1)"); update_sql("INSERT INTO system VALUES ('archive.module','archive','module','',1)");
update_sql("INSERT INTO system VALUES ('block.module','block','module','',1)"); update_sql("INSERT INTO system VALUES ('block.module','block','module','',1)");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment