View source
<?php
function term_merge_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Allows site admins to merge two terms, making one a synonym of another.');
}
}
function term_merge_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'term_merge/merge',
'title' => t('Merges one Term into anothers'),
'access' => user_access('administer taxonomy'),
'type' => MENU_CALLBACK,
'callback' => 'term_merge_merge',
);
}
return $items;
}
function term_merge_merge($from_tid) {
return drupal_get_form('term_merge_merge_form', $from_tid);
}
function term_merge_merge_form($tid) {
$from_term = taxonomy_get_term($tid);
$from_name = check_plain($from_term->name);
$term = taxonomy_get_term($tid);
$form['from_tid'] = array(
'#type' => 'value',
'#value' => $tid,
);
$form['to_tid'] = _taxonomy_term_select(t("Merge all '{$from_name}' items into"), 'to_tid', -1, $term->vid, '', FALSE, NULL, array(
$tid,
));
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Merge'),
);
$form['info'] = array(
'#value' => "<p>As a result of this operation, '{$from_name}' will be deleted and all the nodes tagged with '{$from_name}' will now be tagged with the selection in the box above. In addition '{$from_name}' will be listed as a synonym for the selected node.</p><p>Note that this operation is not reversible!</p>",
);
return $form;
}
function term_merge_merge_form_submit($form_id, $form_values) {
$to_term = taxonomy_get_term($form_values['to_tid']);
$from_term = taxonomy_get_term($form_values['from_tid']);
$old_max_exec = ini_get('max_execution_time');
if ($old_max_exec < 30) {
$old_max_exec = 30;
}
set_time_limit(100);
db_query("INSERT INTO {term_synonym} (tid, name) (SELECT '%d',name from {term_synonym} where tid = '%d')", $to_term->tid, $from_term->tid);
$nodes_to_update = _term_merge_select_nodes($from_term->tid);
db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $to_term->tid, $from_term->name);
while ($node_record = db_fetch_object($nodes_to_update)) {
$node = node_load($node_record->nid);
$save_node_changes = TRUE;
$new_taxonomy = array();
foreach ($node->taxonomy as $term) {
if ($term->tid == $to_term->tid) {
$save_node_changes = FALSE;
break;
}
if ($term->tid == $from_term->tid) {
$new_taxonomy[] = $to_term;
}
else {
$new_taxonomy[] = $term;
}
}
if ($save_node_changes) {
taxonomy_node_save($node->nid, $new_taxonomy);
}
}
taxonomy_del_term($from_term->tid);
set_time_limit($old_max_exec);
drupal_set_message(t('The terms have been merged. Have a great day.'));
return 'admin/content/taxonomy/' . $to_term->vid;
}
function term_merge_form_alter($form_id, &$form) {
if ($form_id == 'taxonomy_form_term') {
$tid = $form['tid']['#value'];
$form['merge'] = array(
'#value' => '<div><strong>' . l(t('Merge this term into another another'), "term_merge/merge/{$tid}") . '</strong></div>',
);
}
}
function _term_merge_select_nodes($tid) {
$descendant_tids = array();
$term = taxonomy_get_term($tid);
$tree = taxonomy_get_tree($term->vid, $tid, -1, NULL);
$descendant_tids[] = array_merge(array(
$tid,
), array_map('_taxonomy_get_tid_from_term', $tree));
$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 . ')';
$result = db_query(db_rewrite_sql($sql));
return $result;
}