? taxonomy_similar_5x.patch
Index: taxonomy_similar.info
===================================================================
RCS file: taxonomy_similar.info
diff -N taxonomy_similar.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ taxonomy_similar.info	10 May 2007 03:31:17 -0000
@@ -0,0 +1,5 @@
+; $Id$
+name = Taxonomy Similar
+description = Allows users to choose similar tags after content submission.
+requirements = taxonomy
+version = "$Name$"
Index: taxonomy_similar.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/taxonomy_similar/taxonomy_similar.module,v
retrieving revision 1.6
diff -u -p -r1.6 taxonomy_similar.module
--- taxonomy_similar.module	18 May 2005 18:42:09 -0000	1.6
+++ taxonomy_similar.module	10 May 2007 03:31:17 -0000
@@ -9,12 +9,10 @@
 /**
  * Implementation of hook_help().
  */
-function taxonomy_similar_help($section = 'admin/help#taxonomy_similar') {
+function taxonomy_similar_help($section) {
   switch ($section) {
-    case 'taxonomy_similar/node/' . arg(2):
-      return t('Edit your tags below based on their similiarity to others. By choosing similar or stronger tags with higher usage, you\'ll make it easier for your users to find relevant and related material based on your content. This step is entirely optional - skipping it will keep your tags exactly as you\'ve entered them.');
-    case 'admin/modules#description':
-      return t('Allows users to choose similar tags after content submission.');
+    case 'taxonomy_similar/node/'. arg(2):
+      return t("Edit your tags below based on their similiarity to others. By choosing similar or stronger tags with higher usage, you'll make it easier for your users to find relevant and related material based on your content. This step is entirely optional - skipping it will keep your tags exactly as you've entered them.");
   }
 }
 
@@ -25,11 +23,12 @@ function taxonomy_similar_menu($may_cach
   $items = array();
 
   if ($may_cache) {
-    $items[] = array('title'    => t('similar tags'),
-                     'path'     => 'taxonomy_similar/node',
-                     'callback' => 'taxonomy_similar_overview',
-                     'access'   => user_access('choose similar tags'),
-                     'type'     => MENU_CALLBACK);
+    $items[] = array('title'              => t('Similar tags'),
+                     'path'               => 'taxonomy_similar/node',
+                     'callback'           => 'drupal_get_form',
+                     'callback arguments' => array('taxonomy_similar_overview'),
+                     'access'             => user_access('choose similar tags'),
+                     'type'               => MENU_CALLBACK);
   }
 
   return $items;
@@ -69,7 +68,7 @@ function taxonomy_similar_nodeapi($node,
         if (is_array($terms)) {
           foreach ($terms as $term) {
             if (!$vocabulary[$term->vid]->tags) { continue; } // not free tags.
-            $_REQUEST['destination'] = url('taxonomy_similar/node/'.$node->nid);
+            $_REQUEST['destination'] = check_url('taxonomy_similar/node/'. $node->nid);
             return; // if we're this far, our destination has been set.
           }
         }
@@ -80,46 +79,63 @@ function taxonomy_similar_nodeapi($node,
 /**
  * Display a form list of all similar terms for the passed node.
  */
-function taxonomy_similar_overview($nid) {
-  if (!$nid) { drupal_not_found(); return; }
+function taxonomy_similar_overview($nid = 0) {
+  if (!$nid || !is_numeric($nid)) { drupal_not_found(); return; }
 
-  // got new terms? send 'em down!
-  if ($_POST['op'] == 'Submit') {
-    $edit        = $_POST['edit'];
-    taxonomy_similar_node_save($nid, $edit['taxonomy']);
-    drupal_goto("node/$nid");
-  }
-  
   // load up all our requisite data. loading of the entire node
   // is really a waste here, but makes the title seem nicer.
   $vocabulary = taxonomy_get_vocabularies();
   $terms      = taxonomy_node_get_terms($nid);
-  $node       = node_load(array('nid' => $nid));
-  drupal_set_title(t("'%name' similar tags", array('%name' => check_plain($node->title))));
-  
+  $node       = node_load($nid);
+  drupal_set_title(t('%name similar tags', array('%name' => $node->title)));
+ 
   // loop through every term associated with this node,
   // find similar keywords, and display checkboxes.
-  $output = NULL; // tested for data a bit later.
   if (is_array($terms)) { // perform if array.
     foreach ($terms as $term) {
       if (!$vocabulary[$term->vid]->tags) { continue; }
       $similars = taxonomy_similar_tags($term);
 
-      $output .= form_checkbox(t('%name (%matches) is just perfect, so keep it.', array('%name' => '<strong>'.check_plain($term->name).'</strong>', '%matches' => format_plural(taxonomy_similar_term_count_nodes($term->tid), '1 match', '%count matches'))), 'taxonomy][', $term->tid, 1, NULL, NULL);
+      $form['taxonomy'][$term->tid] = array(
+        '#type'          => 'checkbox',
+        '#title'         => t('%name (@matches) is just perfect, so keep it.', array('%name' => $term->name, '@matches' => format_plural(taxonomy_similar_term_count_nodes($term->tid), '1 match', '@count matches'))),
+        '#default_value' => 1,
+      );
 
       if (is_array($similars)) {
         foreach ($similars as $similar) {
-          $output .= form_checkbox(t('Use similar tag: %name (%matches).', array('%name' => '<strong>'.check_plain($similar->name).'</strong>', '%matches' => format_plural($similar->count, '1 match', '%count matches'))), 'taxonomy][', $similar->tid, 0, NULL, array('style' => 'margin-left: 1.5em;'));
+          $form['taxonomy'][$similar->tid] = array(
+            '#type' => 'checkbox',
+            '#title' => t('Use similar tag: %name (@matches).', array('%name' => $similar->name, '@matches' => format_plural($similar->count, '1 match', '@count matches'))),
+            '#default_value' => 0,
+            '#attributes' => array('style' => 'margin-left: 1.5em;'),
+          );
         }
       }
       else {
-        $output .= '<div style="margin-left: 1.5em;">'.t('There were no similar tags found for %name.', array('%name' => '<strong>'.check_plain($term->name).'</strong>')).'</div>';
+        $form['taxonomy'][$term->tid] = array(
+          '#value' => '<div style="margin-left: 1.5em;">'. t('There were no similar tags found for %name', array('%name' => $term->name)) .'</div>',
+        );
       }
     }
   }
   
-  $output .= form_submit(t('Submit'), 'op', array('style' => 'margin-top: 1.5em;'));
-  return form($output);
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#name' => t('Submit'),
+    '#attributes' => array('style' => 'margin-top: 1.5em;'),
+  );
+  $form['#submit']['taxonomy_similar_overview_submit'] = array($nid);
+
+  return $form;
+}
+
+/**
+ * Submit callback; saves term associations and redirects back to node view.
+ */
+function taxonomy_similar_overview_submit($form_id, &$form_values, $nid) {
+  taxonomy_similar_node_save($nid, $form_values['taxonomy']);
+  return "node/$nid"; 
 }
 
 /**
@@ -128,7 +144,7 @@ function taxonomy_similar_overview($nid)
  * term_relations or merging one term with another. 
  */
 function taxonomy_similar_node_save($nid, $terms) {
-  if (!$nid) { continue; }
+  if (!$nid) { return; }
 
   // remove keys that has a value of 0. these are
   // created by the zeroing out of checkbox items.
@@ -232,5 +248,3 @@ function _sort_by_percentage($a, $b) {
   if ($a->percentage == $b->percentage) { return 0; }
   return ($a->percentage > $b->percentage) ? -1 : 1;
 }
-
-?>
