? nat_related_synonym.patch
Index: nat.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nat/nat.module,v
retrieving revision 1.14.2.7
diff -u -p -r1.14.2.7 nat.module
--- nat.module	7 May 2007 06:14:40 -0000	1.14.2.7
+++ nat.module	30 May 2007 18:48:39 -0000
@@ -86,6 +86,11 @@ function nat_nodeapi(&$node, $op, $tease
       $node->nat = nat_get_terms($node->nid);
       break;
     case 'insert':
+      $node_arr = (array)$node;
+      foreach($nat_config['types'][$node->type] as $nat_vid){
+        $nat_related[$nat_vid] = $node_arr['nat_related_'.$nat_vid];
+      } 
+      
       $body = $node->body;
       // Copying over the node body is optional.
       $body = isset($nat_config['body'][$node->type]) ? $body : '';
@@ -97,13 +102,18 @@ function nat_nodeapi(&$node, $op, $tease
       _nat_save_association($node->nid, $terms);
       break;
     case 'update':
+      $node_arr = (array)$node;
+      foreach($node->nat as $nat_term){
+        $nat_related[$nat_term->vid] = $node_arr['nat_related_'.$nat_term->vid];
+      } 
+      
       $body = $node->body;
       // Copying over the node body is optional.
       $body = isset($nat_config['body'][$node->type]) ? $body : '';
 
       // 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, $nat_related, $node->nat_synonyms);
       break;
     case 'delete':
       // Deleting the associated term when a node is deleted is optional.
@@ -117,6 +127,61 @@ function nat_nodeapi(&$node, $op, $tease
 }
 
 /**
+ * hook_form_alter()
+ */
+function nat_form_alter($form_id, &$form) {
+  // should show warning on term pages - with link to node for editing.
+  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,
+         );
+       
+        // 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']['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']['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;
+      }
+    }
+  }
+  elseif($form_id == 'taxonomy_form_term') {
+    $config = variable_get('nat_config', array());
+    foreach($config['types'] as $type){
+      if(in_array($form['vid']['#value'], $type)){
+        drupal_set_message(t('This category is managed by Node Auto Taxonomy (NAT), editing this page may cause problems!'));
+        break;
+      }
+    }
+  }
+}
+
+/**
  * Menu Callback: NAT module settings form.
  */
 function nat_settings_form() {
@@ -159,6 +224,12 @@ function nat_settings_form() {
       '#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('Synonyms and Relations'),
+      '#default_value' => isset($nat_config['options_'.$type]) ? $nat_config['options_'.$type] : 0,
+      '#description' => t('Allow users to define synonyms and related terms when they create and edit these nodes. *Requires a vocabulary to be selected.'),
+    );
   }
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
 
@@ -414,13 +485,18 @@ function _nat_get_vocabularies() {
  * @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
   );
 
+  // 'synonyms' => string
+  if(!empty($synonyms)){
+    $edit['synonyms'] = $synonyms;
+  }
+
   $tids = array();
 
   foreach ($vids as $vid) {
@@ -435,6 +511,12 @@ function _nat_add_terms($vids, $title, $
     else {
       $edit['parent'] = array();
     }
+    
+    // 'relations' => array
+    if(count($relations)){
+      $edit['relations'] = $relations[$vid];
+    }
+
     taxonomy_save_term($edit);
     $tids[] = $edit;
   }
@@ -455,12 +537,17 @@ function _nat_add_terms($vids, $title, $
  *   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
   );
+  
+  // 'synonyms' => string
+  if(!empty($synonyms)){
+    $edit['synonyms'] = $synonyms;
+  }
 
   foreach ($terms as $term) {
     if (isset($hierarchy[$term->vid])) {
@@ -469,6 +556,12 @@ function _nat_update_terms($terms, $titl
     else {
       $edit['parent'] = array();
     }
+    
+    // 'relations' => array
+    if(count($relations)){
+      $edit['relations'] = $relations[$term->vid];
+    }
+    
     $edit['tid'] = $term->tid;
     taxonomy_save_term($edit);
   }
