diff -up /Users/alex/Desktop/term_merge/term_merge.info term_merge/term_merge.info --- /Users/alex/Desktop/term_merge/term_merge.info 2009-04-27 08:28:10.000000000 -0400 +++ term_merge/term_merge.info 2010-02-02 14:05:39.000000000 -0500 @@ -1,3 +1,5 @@ +; $Id$ + name = "Term Merge" description = "Offers ability to merge all the items tagged with term A and all those with term B." dependencies[] = taxonomy @@ -8,5 +10,4 @@ core = 6.x version = "6.x-1.x-dev" core = "6.x" project = "term_merge" -datestamp = "1240835290" - +datestamp = "1240835290" \ No newline at end of file diff -up /Users/alex/Desktop/term_merge/term_merge.module term_merge/term_merge.module --- /Users/alex/Desktop/term_merge/term_merge.module 2009-04-27 00:21:16.000000000 -0400 +++ term_merge/term_merge.module 2010-02-02 14:05:19.000000000 -0500 @@ -1,153 +1,144 @@ 'Merges one Term into anothers', - 'access arguments' => array('administer taxonomy'), - 'type' => MENU_CALLBACK, - 'page callback' => 'term_merge_merge' - ); + $items['term_merge/merge'] = array( + 'title' => 'Merges one Term into another', + 'access arguments' => array('administer taxonomy'), + 'type' => MENU_CALLBACK, + 'page callback' => 'drupal_get_form', + 'page arguments' => array('term_merge_merge_form'), + ); return $items; -} - - - -function term_merge_merge($from_tid) { - return drupal_get_form('term_merge_merge_form',$from_tid); } - /** * Presents the form to select which node to merge into. */ function term_merge_merge_form(&$form_state, $tid){ - //$form[] = array('#value' => $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')); + $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' => "
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.
Note that this operation is not reversible!
"); - return $form; + return $form; } /** * Actually merges the terms */ -function term_merge_merge_form_submit($form, &$form_state) { - //print implode(",",$form_state['values']); - $to_term = taxonomy_get_term($form_state['values']['to_tid']); - $from_term = taxonomy_get_term($form_state['values']['from_tid']); - - $old_max_exec = ini_get('max_execution_time'); - if ($old_max_exec < 30) $old_max_exec = 30; - set_time_limit(100); - - // Copy any Synonyms from from_term to to_term. - // I'd love to do this with a Drupal func, but taxonomy_save_term() looks like way more trouble than it's worth - db_query("INSERT INTO {term_synonym} (tid, name) (SELECT '%d',name from {term_synonym} where tid = '%d')", $to_term->tid, $from_term->tid); - //TODO: This could lead to duplicate synonyms. - - - - // save a list of nodes we need to update to new term - $nodes_to_update = _term_merge_select_nodes($from_term->tid); - - // Add from_node as a synonym to to_node to prevent future duplication - db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $to_term->tid, $from_term->name); - //TODO: This could lead to duplicate synonyms. - - // update old nodes to new nodes +function term_merge_merge_form_submit($form, &$form_state) { + $old_max_exec = ini_get('max_execution_time'); + if ($old_max_exec < 30) $old_max_exec = 30; + set_time_limit(100); + + $to_tid = $form_state['values']['to_tid']; + $from_tid = $form_state['values']['from_tid']; + + $to_term = taxonomy_get_term($to_tid); + $from_term = taxonomy_get_term($from_tid); + $merged_syn = array_unique(array_merge(taxonomy_get_synonyms($to_tid), taxonomy_get_synonyms($from_tid), array($from_term->name))); + + $merged = (array)$to_term; + $merged['synonyms'] = implode("\n",$merged_syn); + $merged['relations'] = taxonomy_get_related($to_tid); + $merged['parent'] = array(); + + $parents = taxonomy_get_parents($to_tid); + foreach ($parents as $parent){ + $merged['parent'][] = $parent->tid; + } + + taxonomy_save_term($merged); + + // save a list of nodes we need to update to new term + $nodes_to_update = _term_merge_select_nodes($from_term->tid); + + // update old nodes to new nodes while ($node_record = db_fetch_object($nodes_to_update)) { $node = node_load($node_record->nid); // gotta use node_load to get the taxonomies in there $save_node_changes = TRUE; $new_taxonomy = array(); foreach ($node->taxonomy as $term) { - if ($term->tid == $to_term->tid) { - // If the node already has the destination tag, we don't need to change a thing! - // (taxonomy_del_term will take care of removing the old tag) - $save_node_changes = FALSE; - break; - } - - if ($term->tid == $from_term->tid) { - $new_taxonomy[] = $to_term; // make the switch! - } else { - $new_taxonomy[] = $term; - } - + if ($term->tid == $to_term->tid) { + // If the node already has the destination tag, we don't need to change a thing! + // (taxonomy_del_term will take care of removing the old tag) + $save_node_changes = FALSE; + break; + } + + if ($term->tid == $from_term->tid) { + $new_taxonomy[] = $to_term; // make the switch! + } else { + $new_taxonomy[] = $term; + } + + } + + if ($save_node_changes) { + taxonomy_node_save($node, $new_taxonomy); } - - if ($save_node_changes) - taxonomy_node_save($node, $new_taxonomy); - // note for 6.x: taxonomy_node_save was changed to take a $node object instead of $nid } - - 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.')); - $form_state['redirect'] = 'admin/content/taxonomy/' . $to_term->vid; -} + taxonomy_del_term($from_term->tid); + set_time_limit($old_max_exec); + + drupal_set_message(t('The term @from has been merged into @to. Have a great day.', array('@to' => $to_term->name, '@from' => $from_term->name))); + $form_state['redirect'] = 'admin/content/taxonomy/' . $to_term->vid; +} /** -* Implementation of hook_form_alter(). -*/ + * Implementation of hook_form_alter(). + */ function term_merge_form_alter(&$form, &$form_state, $form_id) { - // Add the link to the term edit form if ($form_id == 'taxonomy_form_term') { - - $tid = $form['tid']['#value']; - //$tid= arg(5); - $form['merge'] = array( '#value' => '