? nat_related_synonym_2.patch
Index: nat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nat/nat.module,v
retrieving revision 1.14.2.9
diff -u -r1.14.2.9 nat.module
--- nat.module	2 Jun 2007 02:14:14 -0000	1.14.2.9
+++ nat.module	4 Jun 2007 21:11:35 -0000
@@ -91,7 +91,7 @@
       $body = isset($nat_config['body'][$node->type]) ? $body : '';
 
       // Add node title as terms.
-      $terms = _nat_add_terms($nat_config['types'][$node->type], $node->title, $body, $node->taxonomy);
+      $terms = _nat_add_terms($nat_config['types'][$node->type], $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
 
       // Save node-term association in the NAT table.
       _nat_save_association($node->nid, $terms);
@@ -103,7 +103,7 @@
 
       // Update node title in term(s).
       $terms = nat_get_terms($node->nid);
-      _nat_update_terms($terms, $node->title, $body, $node->taxonomy);
+      _nat_update_terms($terms, $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
       break;
     case 'delete':
       // Deleting the associated term when a node is deleted is optional.
@@ -117,6 +117,52 @@
 }
 
 /**
+ * Implementation of hook_form_alter()
+ */
+function nat_form_alter($form_id, &$form) {
+  if ($form['#id'] == 'node-form') {
+    $config = variable_get('nat_config', array());
+    
+    foreach ($config['types'] as $type => $vals) {
+      if (count($vals) && $form_id == $type .'_node_form' && $config['options_'. $type] == 1) {
+       
+        $form['nat'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Term Information'),
+          '#collapsible' => TRUE,
+          '#collapsed' => FALSE,
+          '#weight' => -2,
+          '#tree' => TRUE,
+          );
+       
+        // Related terms.
+        if (isset($form['#node']->nat)) { 
+          foreach ($form['#node']->nat as $term) {
+            $terms[$term->vid] = $term->tid;
+          }
+        }
+        foreach ($vals as $vocabulary_id) {
+          $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
+          if ($vocabulary->relations) {
+            $form['nat']['related'][$vocabulary_id] = _taxonomy_term_select(t('Related !terms', array('!terms' => $vocabulary->name)), 'relations', array_keys(taxonomy_get_related($terms[$term->vid])), $vocabulary_id, NULL, 1, '<'. t('none') .'>', array($term->tid));
+          }
+        }
+        
+        // Synonyms.
+        $form['nat']['synonyms'] = array(
+          '#type' => 'textarea',
+          '#title' => t('Synonyms'),
+          '#default_value' => implode("\n", taxonomy_get_synonyms($term->tid)),
+          '#description' => t('<a href="@help-url">Synonyms</a> of this term, one synonym per line.', array('@help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
+                        
+        // This can only match once, so we just break the foreach.
+        break;
+      }
+    }
+  }
+}
+
+/**
  * Menu Callback: NAT module settings form.
  */
 function nat_settings_form() {
@@ -159,6 +205,11 @@
       '#default_value' => isset($nat_config['delete'][$type]) ? $nat_config['delete'][$type] : 0,
       '#parents' => array('delete', $type)
     );
+    $form['nat_'. $type]['options_'. $type] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Allow users to define synonyms and related terms when they create and edit nodes.'),
+      '#default_value' => isset($nat_config['options_'. $type]) ? $nat_config['options_'. $type] : 0,
+    );
     $form['nat_'. $type]['node_links_'. $type] = array(
       '#type' => 'checkbox',
       '#title' => t('Make NAT terms in %type node views point to the associated node rather than the taxonomy page.', array('%type' => $type)),
@@ -201,7 +252,7 @@
   $options = array();
   foreach ($nat_config['types'] as $type => $associations) {
     if (!empty($associations)) {
-      foreach($associations as $vid) {
+      foreach ($associations as $vid) {
         $options[$type .'|'. $vid] = t('@type &lsaquo;-&rsaquo; !vocabulary', array('@type' => $type, '!vocabulary' => $vocabularies[$vid]));
       }
     }
@@ -440,13 +491,17 @@
  * @return $tids
  *   An array of term objects.
  */
-function _nat_add_terms($vids, $title, $body, $hierarchy = array()) {
+function _nat_add_terms($vids, $title, $body, $hierarchy = array(), $relations = array(), $synonyms = null) {
   $edit = array(
     'name' => $title,
     'description' => $body,
     'weight' => 0
   );
 
+  if (!empty($synonyms)) {
+    $edit['synonyms'] = $synonyms;
+  }
+
   $tids = array();
 
   foreach ($vids as $vid) {
@@ -461,6 +516,11 @@
     else {
       $edit['parent'] = array();
     }
+    
+    if (count($relations)) {
+      $edit['relations'] = $relations[$vid];
+    }
+
     taxonomy_save_term($edit);
     $tids[] = $edit;
   }
@@ -481,12 +541,16 @@
  *   The taxonomy array for the node in question. This is used to, if set,
  * insert the new term as a child term of the selected parent.
  */
-function _nat_update_terms($terms, $title, $body, $hierarchy) {
+function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
   $edit = array(
     'name' => $title,
     'description' => $body,
     'weight' => 0
   );
+  
+  if (!empty($synonyms)) {
+    $edit['synonyms'] = $synonyms;
+  }
 
   foreach ($terms as $term) {
     if (isset($hierarchy[$term->vid])) {
@@ -495,6 +559,11 @@
     else {
       $edit['parent'] = array();
     }
+    
+    if (count($relations)) {
+      $edit['relations'] = $relations[$term->vid];
+    }
+    
     $edit['tid'] = $term->tid;
     taxonomy_save_term($edit);
   }
