
# HG changeset patch
# User Rich Siomporas <rsiomporas@cantongroup.com>
# Date 1243995598 14400
# Node ID 16ff252e5d3c0e8ff7d9789f1f357c6981af6e55
# Parent  d2c8ecfb9ed24770efb5785c8623d258bc2753cc
Second attempt. Besides porting over the Drupal 5 patch to D6
and cleaning it up a tiny bit, I added a conditional to
nat_form_alter() to not remove any taxonomy terms from the
form in the case that the current content type has auto
association turned on. The biggest pain was figuring out
why the application would whitescreen/die looping over
taxonomy_get_parents() in the taxonomy module - I found
it to be related to passing in the parent to taxonomy_save_term()
from _nat_update_terms(); it appears to pass the tid in, not
the actual parent, causing an endless loop. I commented out
the line declaring $edit['parent'], and it seems like it
fixed it, though I'll need someone with a different setup to
test it out.

--- 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,17 @@ 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, $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);
       }
@@ -119,30 +119,32 @@ function nat_form_alter(&$form, &$form_s
 
     foreach ($config['types'] as $type => $associations) {
       if (count($associations) && $form_id == $type .'_node_form') {
         $nat_terms = array();
 
         // If this is a node update, remove this node's associated terms from
         // its associated vocabularies.
         // N.B. Free-tag vocabularies are unaffected by this.
-        if (isset($form['#node']->nid)) {
-          foreach ($form['#node']->nat as $tid => $term) {
-            $nat_terms[$term->vid] = $tid;
-            // Remove from taxonomy form.
-            if (isset($form['taxonomy'])) {
-              foreach ($form['taxonomy'] as $vid => $values) {
-                if ($term->vid == $vid) {
-                  foreach ($values['#options'] as $id => $option) {
-                    if (isset($option->option[$term->tid])) {
-                      unset($form['taxonomy'][$vid]['#options'][$id]);
-                      break;
+        if(!isset($config['node_term'][$type])){
+          if (isset($form['#node']->nid)) {
+            foreach ($form['#node']->nat as $tid => $term) {
+              $nat_terms[$term->vid] = $tid;
+              // Remove from taxonomy form.
+              if (isset($form['taxonomy'])) {
+                foreach ($form['taxonomy'] as $vid => $values) {
+                  if ($term->vid == $vid) {
+                    foreach ($values['#options'] as $id => $option) {
+                      if (isset($option->option[$term->tid])) {
+                        unset($form['taxonomy'][$vid]['#options'][$id]);
+                        break;
+                      }
                     }
+                    break;
                   }
-                  break;
                 }
               }
             }
           }
         }
 
         // Related terms.
         if (isset($config['related'][$type])) {
@@ -530,17 +532,16 @@ function _nat_update_terms($node) {
   );
 
   $hierarchy = isset($node->taxonomy) ? $node->taxonomy : array();
   $terms = nat_get_terms($node->nid);
   foreach ($terms as $term) {
     $edit['tid'] = $term->tid;
     $edit['vid'] = $term->vid;
     $edit['weight'] = $term->weight;
-    $edit['parent'] = isset($hierarchy[$term->vid]) ? $hierarchy[$term->vid] : array();
     $edit['relations'] = isset($node->nat) && isset($node->nat['related'][$term->vid]) ? $node->nat['related'][$term->vid] : array();
     $edit['synonyms'] = isset($node->nat) ? $node->nat['synonyms'] : '';
 
     taxonomy_save_term($edit);
   }
 }
 
 /**
@@ -552,30 +553,52 @@ 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 $node
+  *   Node object.
+  * @param $terms
+  *   NAT-terms of the above 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);
+   }
+  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 +617,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;
 }

