function taxonomy_manager_merge in Taxonomy Manager 5
Same name and namespace in other branches
- 6.2 taxonomy_manager.admin.inc \taxonomy_manager_merge()
- 6 taxonomy_manager.admin.inc \taxonomy_manager_merge()
merges terms into another term (main term), all merged term get added to the main term as synonyms. term_node relations are updated automatically (node with one of merging terms gets main term assigned) after all opterions are done (adding of hierarchies, relations is optional) merging terms get deleted
Parameters
$main_term: id of term where other terms get merged into
$merging_terms: array of term ids, which get merged into main term and afterwards deleted
$options: array with additional options, possible values: 'collect_parents': if true, all parents of merging terms get added to main term (only possible with multi hierarchies) 'collect_children': if true, all children of merging terms get added to main term 'collect_relations': if true, all relations of merging terms are transfered to main term
1 call to taxonomy_manager_merge()
- taxonomy_manager_form_submit in ./
taxonomy_manager.module - submits the taxonomy manager form
2 string references to 'taxonomy_manager_merge'
- taxonomy_manager_merge_get_main_term in ./
taxonomy_manager.module - helper function for getting out the main term of former merged term (which no long exists)
- taxonomy_manager_merge_history_update_cache in ./
taxonomy_manager.module - sets / updates cache for merging history
File
- ./
taxonomy_manager.module, line 1231 - Taxonomy Manager
Code
function taxonomy_manager_merge($main_term, $merging_terms, $options = array(), $new_inserted = TRUE) {
$vid = db_result(db_query("SELECT vid FROM {term_data} WHERE tid = %d", $main_term));
$voc = taxonomy_get_vocabulary($vid);
$merging_terms_parents = array();
if ($voc->hierarchy == 2 && $new_inserted && $options['collect_parents']) {
db_query("DELETE FROM {term_hierarchy} WHERE parent = 0 AND tid = %d", $main_term);
}
//TODO: add hook, so that other modules can consider changes
foreach ($merging_terms as $merge_term) {
if ($merge_term != $main_term) {
//update node-relations
$sql = db_query("SELECT nid FROM {term_node} WHERE tid = %d", $merge_term);
while ($obj = db_fetch_object($sql)) {
$nid = $obj->nid;
db_query("DELETE FROM {term_node} WHERE tid = %d AND nid = %d", $merge_term, $nid);
if (!db_result(db_query("SELECT COUNT(*) FROM {term_node} WHERE tid = %d AND nid = %d", $main_term, $nid))) {
db_query("INSERT INTO {term_node} (tid, nid) VALUES (%d, %d)", $main_term, $nid);
}
}
if ($voc->hierarchy == 1) {
//sinlge hierarchy
$parents = taxonomy_get_parents($merge_term);
$parent = array_shift($parents);
$merging_terms_parents[$parent->tid] = $parent->tid;
}
if ($options['collect_parents']) {
$parents = taxonomy_get_parents($merge_term);
foreach ($parents as $parent_tid => $parent_term) {
if (!db_result(db_query("SELECT COUNT(*) FROM {term_hierarchy} WHERE tid = %d AND parent = %d", $main_term, $parent_tid))) {
db_query("INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)", $main_term, $parent_tid);
}
}
}
if ($options['collect_children']) {
$children = taxonomy_get_children($merge_term);
foreach ($children as $child_tid => $child_term) {
if (!db_result(db_query("SELECT COUNT(*) FROM {term_hierarchy} WHERE tid = %d AND parent = %d", $child_tid, $main_term))) {
db_query("INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)", $child_tid, $main_term);
}
}
}
if ($options['collect_relations']) {
$relations = taxonomy_get_related($merge_term);
foreach ($relations as $related_tid => $relation) {
if ($relation->tid1 == $merge_term) {
if (!db_result(db_query("SELECT COUNT(*) FROM {term_relation} WHERE tid1 = %d AND tid2 = %d", $main_term, $related_tid))) {
db_query("INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)", $main_term, $related_tid);
}
}
else {
if ($relation->tid2 == $merge_term) {
if (!db_result(db_query("SELECT COUNT(*) FROM {term_relation} WHERE tid2 = %d AND tid1 = %d", $main_term, $related_tid))) {
db_query("INSERT INTO {term_relation} (tid2, tid1) VALUES (%d, %d)", $main_term, $related_tid);
}
}
}
}
}
//save merged term (and synonomys of merged term) as synonym
$term = taxonomy_get_term($merge_term);
$merge_term_synonyms = taxonomy_get_synonyms($merge_term);
$merge_term_synonyms[] = $term->name;
foreach ($merge_term_synonyms as $syn) {
if (!db_result(db_query("SELECT COUNT(*) FROM {term_synonym} WHERE tid = %d AND name = '%s'", $main_term, $syn))) {
db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $main_term, $syn);
}
}
taxonomy_manager_delete_terms(array(
$merge_term,
));
}
}
if ($voc->hierarchy == 1 && count($merging_terms_parents) == 1 && $new_inserted) {
db_query("UPDATE {term_hierarchy} SET parent = %d WHERE tid = %d", array_shift($merging_terms_parents), $main_term);
}
taxonomy_manager_merge_history_update($main_term, $merging_terms);
}