
# HG changeset patch
# User Rich Siomporas <rsiomporas@cantongroup.com>
# Date 1243983001 14400
# Node ID df5675543d3a2c5071421a74c3242580cb94a815
# Parent  05a2c5dd4d4585ea4bca2d224239d56d85e38f24
Hand applied nat-module-155411-34.patch from
http://drupal.org/node/155411#comment-902348 to
nat-6.x-1.x-dev downloaded 6/2/2009

updated to work with Drupal 6 API, tested, and it
seems to work.

--- a/nat.admin.inc
+++ b/nat.admin.inc
@@ -51,16 +51,22 @@ function nat_settings_form() {
       '#parents' => array('related', $type)
     );
     $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)),
       '#default_value' => isset($nat_config['node_links'][$type]) ? $nat_config['node_links'][$type] : 0,
       '#parents' => array('node_links', $type)
     );
+    $form['nat_'. $type]['node_term_'. $type] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Associate an automatically created NAT term with %type parent node?', array('%type' => $type)),
+      '#default_value' => isset($nat_config['node_term'][$type]) ? $nat_config['node_term'][$type] : 0,
+      '#parents' => array('node_term', $type)
+    );	
   }
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
 
   return $form;
 }
 
 /**
  * Process NAT settings form submissions.
@@ -69,16 +75,17 @@ function nat_settings_form_submit($form,
   $form_values = $form_state['values'];
 
   unset($form_values['form_id'], $form_values['form_build_id'], $form_values['submit'], $form_values['op'], $form_values['form_token']);
 
   $form_values['body'] = array_filter($form_values['body']);
   $form_values['delete'] = array_filter($form_values['delete']);
   $form_values['node_links'] = array_filter($form_values['node_links']);
   $form_values['related'] = array_filter($form_values['related']);
+  $form_values['node_term'] = array_filter($form_values['node_term']);
 
   variable_set('nat_config', $form_values);
 
   drupal_set_message(t('Configuration settings saved.'));
 }
 
 /**
  * Sync the NAT table with the node table for associated vocabularies.
@@ -139,21 +146,20 @@ function nat_sync_form_submit($form, &$f
  */
 function _nat_sync_associations($associations) {
   $nat_config = _nat_variable_get();
 
   $counter = 0;
   foreach ($associations as $association) {
     $association = explode('|', $association);
     // This query can possibly be improved.
-    $result = db_query("SELECT n.nid, n.type, n.title, nr.body FROM {node} n INNER JOIN {node_revisions} nr USING (vid) LEFT JOIN {nat} n1 ON (n.nid = n1.nid AND n1.vid = %d) LEFT JOIN {nat} n2 ON (n.nid = n2.nid AND n2.nid <> n1.nid) WHERE n.type = '%s' AND n1.nid IS NULL", $association[1], $association[0]);
+    $result = db_query("SELECT n.nid, n.title, n.type, nr.body, n1.vid FROM {node} n INNER JOIN {node_revisions} nr USING (vid) LEFT JOIN {nat} n1 ON (n.nid = n1.nid AND n1.vid = %d) LEFT JOIN {nat} n2 ON (n.nid = n2.nid AND n2.nid != n1.nid) WHERE n.type = '%s' AND ISNULL(n1.nid)", $association[1], $association[0]);	
     while ($node = db_fetch_object($result)) {
       // Add node title as terms.
       $terms = _nat_add_terms($node, array($association[1]));
 
       // Save node-term association in the NAT table.
-      _nat_save_association($node->nid, $terms);
-
+     _nat_save_association($node, $terms);
       $counter++;
     }
   }
   drupal_set_message(t('NAT sync complete: %count nodes synced.', array('%count' => $counter)));
 }
--- a/nat.module
+++ b/nat.module
@@ -84,17 +84,18 @@ function nat_nodeapi(&$node, $op, $tease
     case 'load':
       $node->nat = nat_get_terms($node->nid);
       break;
     case 'insert':
       // Add term(s).
       $terms = _nat_add_terms($node);
 
       // Save node-term association in the NAT table.
-      _nat_save_association($node->nid, $terms);
+      //_nat_save_association($node->nid, $terms);
+	  _nat_save_association($node, $terms);
       break;
     case 'update':
       // Ensure that this is a node form submission and not a direct node_save
       // operation. Ref: http://drupal.org/node/197532 and
       // http://drupal.org/node/188377 .
       if (isset($node->form_id)) {
         _nat_update_terms($node);
       }
@@ -552,30 +553,55 @@ function _nat_update_terms($node) {
  */
 function _nat_delete_terms($nid) {
   $terms = nat_get_terms($nid);
   foreach ($terms as $term) {
     taxonomy_del_term($term->tid);
   }
 }
 
-/**
- * Save node-term associations in the NAT table.
- *
- * @param Integer $nid
- *   Node ID of the node.
- * @param Array $terms
- *   NAT-term objects of the above node.
- */
-function _nat_save_association($nid, $terms) {
+
+ /*
+  * Save node-term associations in the NAT table.  Optionally, associate the terms with a given node in the
+  * node_term table if the option is set for a given type in NAT's settings.
+  *
+  * @param $nid
+  *   Node ID of the node.
+  * @param $terms
+  *   NAT-terms of the above node.
+  * @param $node_type
+  *   The type of the node.
+  */
+  
+  
+function _nat_save_association($node, $terms) {
+  $nat_config = _nat_variable_get();
   foreach ($terms as $term) {
-    $term['nid'] = $nid;
+    $term['nid'] = $node->nid;
     drupal_write_record('nat', $term);
+   }
+  $debug = '';
+  if (isset($nat_config['node_term'][$node->type])) {
+    $node_terms=taxonomy_node_get_terms($node->nid);
+    foreach ($terms as $term) {
+      $vocabulary=taxonomy_get_vocabularies($term['vid']);
+      if($vocabulary->multiple==0) {
+        $_terms=taxonomy_node_get_terms_by_vocabulary($node->nid,$vocabulary->vid);	
+        foreach ($_terms as $_term) {
+          unset($node_terms[$_term->tid]);
+        }
+      }
+      $node_terms[$term['tid']]= new stdClass();
+      $node_terms[$term['tid']]->tid=$term['tid'];
+	 
+    }
+    taxonomy_node_save($node,$node_terms);
   }
-}
+ }
+  
 
 /**
  * Delete node-term associations from the NAT table.
  *
  * @param $nid
  *   Node ID of the node which is to be removed from the NAT table.
  */
 function _nat_delete_association($nid) {
@@ -594,16 +620,17 @@ function _nat_variable_get($name = NULL)
   static $variables = array();
 
   if (empty($variables)) {
     $defaults = array(
       'types' => array(),
       'body' => array(),
       'delete' => array(),
       'related' => array(),
-      'node_links' => array()
+      'node_links' => array(),
+	  'node_term' => array()
     );
     $variables = variable_get('nat_config', array());
     $variables = array_merge($defaults, $variables);
   }
 
   return $name ? $variables[$name] : $variables;
 }

