Commit 551782a5 authored by Dries's avatar Dries

- Modified patch #10424 by Jeremy: deleting terms can orphan child terms in the database.

  I rewrote this patch for simplicity.
parent b3451d02
...@@ -294,6 +294,21 @@ function taxonomy_save_term($edit) { ...@@ -294,6 +294,21 @@ function taxonomy_save_term($edit) {
} }
function taxonomy_del_term($tid) { function taxonomy_del_term($tid) {
$tids = array($tid);
while ($tids) {
$children_tids = $orphans = array();
foreach ($tids as $tid) {
// See if any of the term's children are about to be become orphans:
if ($children = taxonomy_get_children($tid)) {
foreach ($children as $child) {
// If the term has multiple parents, we don't delete it.
$parents = taxonomy_get_parents($child->tid);
if (count($parents) == 1) {
$orphans[] = $child->tid;
}
}
}
$term = taxonomy_get_term($tid); $term = taxonomy_get_term($tid);
db_query('DELETE FROM {term_data} WHERE tid = %d', $tid); db_query('DELETE FROM {term_data} WHERE tid = %d', $tid);
...@@ -303,10 +318,13 @@ function taxonomy_del_term($tid) { ...@@ -303,10 +318,13 @@ function taxonomy_del_term($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); module_invoke_all('taxonomy', 'delete', 'term', $term);
drupal_set_message(t('deleted term "%name".', array('%name' => $term->name)));
}
cache_clear_all(); $tids = $orphans;
}
return t('deleted term "%name".', array('%name' => $term->name)); cache_clear_all();
} }
function _taxonomy_confirm_del_term($tid) { function _taxonomy_confirm_del_term($tid) {
...@@ -318,7 +336,9 @@ function _taxonomy_confirm_del_term($tid) { ...@@ -318,7 +336,9 @@ function _taxonomy_confirm_del_term($tid) {
$form .= form_submit(t('Delete')); $form .= form_submit(t('Delete'));
$form .= form_submit(t('Cancel')); $form .= form_submit(t('Cancel'));
return form(form_item(t('Delete term "%name"', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term?'))); $output = form(form_item(t('Delete term "%name" and all its children', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term and all its children (if any)?')));
return $output;
} }
/** /**
......
...@@ -294,6 +294,21 @@ function taxonomy_save_term($edit) { ...@@ -294,6 +294,21 @@ function taxonomy_save_term($edit) {
} }
function taxonomy_del_term($tid) { function taxonomy_del_term($tid) {
$tids = array($tid);
while ($tids) {
$children_tids = $orphans = array();
foreach ($tids as $tid) {
// See if any of the term's children are about to be become orphans:
if ($children = taxonomy_get_children($tid)) {
foreach ($children as $child) {
// If the term has multiple parents, we don't delete it.
$parents = taxonomy_get_parents($child->tid);
if (count($parents) == 1) {
$orphans[] = $child->tid;
}
}
}
$term = taxonomy_get_term($tid); $term = taxonomy_get_term($tid);
db_query('DELETE FROM {term_data} WHERE tid = %d', $tid); db_query('DELETE FROM {term_data} WHERE tid = %d', $tid);
...@@ -303,10 +318,13 @@ function taxonomy_del_term($tid) { ...@@ -303,10 +318,13 @@ function taxonomy_del_term($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); module_invoke_all('taxonomy', 'delete', 'term', $term);
drupal_set_message(t('deleted term "%name".', array('%name' => $term->name)));
}
cache_clear_all(); $tids = $orphans;
}
return t('deleted term "%name".', array('%name' => $term->name)); cache_clear_all();
} }
function _taxonomy_confirm_del_term($tid) { function _taxonomy_confirm_del_term($tid) {
...@@ -318,7 +336,9 @@ function _taxonomy_confirm_del_term($tid) { ...@@ -318,7 +336,9 @@ function _taxonomy_confirm_del_term($tid) {
$form .= form_submit(t('Delete')); $form .= form_submit(t('Delete'));
$form .= form_submit(t('Cancel')); $form .= form_submit(t('Cancel'));
return form(form_item(t('Delete term "%name"', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term?'))); $output = form(form_item(t('Delete term "%name" and all its children', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term and all its children (if any)?')));
return $output;
} }
/** /**
......
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