You are here

function taxonomy_manager_merge in Taxonomy Manager 6.2

Same name and namespace in other branches
  1. 5 taxonomy_manager.module \taxonomy_manager_merge()
  2. 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_merge_submit in ./taxonomy_manager.admin.inc
Submit handler for merging terms
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.admin.inc, line 2141
Taxonomy Manager Admin

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_vocabulary_load($vid);
  $merging_terms_parents = array();
  foreach ($merging_terms as $merge_term) {
    if ($merge_term != $main_term) {

      //hook, to inform other modules about the changes
      module_invoke_all('taxonomy_manager_term', 'merge', $main_term, $merge_term);

      //update node-relations
      $sql = db_query("SELECT * FROM {term_node} WHERE tid = %d", $merge_term);
      while ($obj = db_fetch_object($sql)) {
        db_query("DELETE FROM {term_node} WHERE tid = %d AND vid = %d", $obj->tid, $obj->vid);
        if (!db_result(db_query("SELECT COUNT(*) FROM {term_node} WHERE tid = %d AND vid = %d", $main_term, $obj->vid))) {
          $obj->tid = $main_term;
          drupal_write_record('term_node', $obj);
        }
      }
      if ($options['collect_parents']) {
        $parents = taxonomy_get_parents($merge_term);
        foreach ($parents as $parent_tid => $parent_term) {
          $merging_terms_parents[$parent_tid] = $parent_tid;
          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,
      ));
    }
  }
  taxonomy_manager_update_voc($vid, $merging_terms_parents);
  if ($options['collect_parents'] && db_result(db_query("SELECT COUNT(*) FROM {term_hierarchy} WHERE tid = %d", $main_term)) > 1) {
    db_query("DELETE FROM {term_hierarchy} WHERE parent = 0 AND tid = %d", $main_term);
  }
  taxonomy_manager_merge_history_update($main_term, $merging_terms);
}