--- sites/all/modules/nat/nat-old.module	Sat Oct  6 13:54:47 2007
+++ sites/all/modules/nat/nat.module	Sat Oct  6 13:55:02 2007
@@ -87,6 +87,11 @@ function nat_nodeapi(&$node, $op, $tease
       $node->nat = nat_get_terms($node->nid);
       break;
     case 'insert':
+      // if terms already exist then this node was created from a sync
+      if(isset($node->nat)) {
+        break;
+      }
+      
       $body = $node->body;
       // Copying over the node body is optional.
       $body = isset($nat_config['body'][$node->type]) ? $body : '';
@@ -288,13 +293,13 @@ function nat_sync_form() {
   $form['sync'] = array(
     '#type' => 'fieldset',
     '#title' => t('Sync associations'),
-    '#description' => t('The Sync operation will create NAT associations (and terms) for nodes (marked for NAT association) not present in the NAT table.'),
+    '#description' => t('The Sync operation will create NAT associations (and terms) for nodes (marked for NAT association) not present in the NAT table. Nodes will be created for terms that do not have an association'),
     '#collapsible' => TRUE
   );
   $form['sync']['vocabularies'] = array(
     '#type' => 'checkboxes',
-    '#title' => t('Select the vocabularies to sync with associated node tables'),
-    '#description' => t('Any nodes not already NAT associated with the selected vocabularies will be associated.'),
+    '#title' => t('Select the vocabulary<->node pair to sync'),
+    '#description' => t('Any nodes not already NAT associated with the selected vocabularies will be associated. Any terms missing nodes will have those nodes created'),
     '#required' => TRUE,
     '#options' => $options
   );
@@ -612,14 +617,16 @@ function _nat_delete_association($nid) {
 
 /**
  * Synchronize NAT node-term relationships. Create associated terms for node
- * where missing.
+ * where missing. Create associated nodes where missing.
  *
  * @param $associations
  *   Associative array denoting the node-vocabulary pair that is to be synced.
  */
 function _nat_sync_associations($associations) {
+  global $user;
   $nat_config = variable_get('nat_config', array());
 
+  // find nodes missing terms
   $counter = 0;
   foreach ($associations as $association) {
     $association = explode('|', $association);
@@ -638,5 +645,49 @@ function _nat_sync_associations($associa
       $counter++;
     }
   }
-  drupal_set_message(t('NAT sync complete: %count nodes synced.', array('%count' => $counter)));
+  
+  if($counter > 0) {
+    drupal_set_message(t('NAT sync complete: %count nodes->terms synced.', array('%count' => $counter)));
+  }
+  
+  // find terms with no associated nodes -> create the nodes
+  $counter = 0;
+  foreach ($associations as $association) {
+    $association = explode('|', $association); // [0] = node type, [1] = vid
+
+    // get terms for vocabulary
+    $terms = db_query('SELECT td.tid, td.name, td.description FROM {term_data} td WHERE td.vid = %d', $association[1]);
+
+    while($term = db_fetch_object($terms)) {
+    
+      // find node nid associated with term
+      $natnid = db_fetch_object(db_query('SELECT n.nid FROM {nat} n WHERE n.tid = %d', $term->tid));
+      
+      // if no associated node - then create and save node
+      if(!is_numeric($natnid->nid)) {
+      
+        // build node
+        $new_node = (object)$node;
+        $new_node->type = $association[0];
+        node_object_prepare($new_node); 
+        $new_node->title = $term->name;
+        $new_node->status = 1;
+        $new_node->body = isset($nat_config['body'][$association[0]]) ? $term->description : '';        
+        $new_node->uid = $user->uid;
+        $new_node->nat = 1; // set so that new term is not created when node_api hook is called
+
+        // save node
+        node_save($new_node);
+        
+        // save association now that we have a nid
+        _nat_save_association($new_node->nid, array(array('tid'=>$term->tid,'vid'=>$association[1])));
+        
+        $counter++;
+      } 
+    }
+  }
+
+  if($counter > 0) {
+    drupal_set_message(t('NAT sync complete: %count terms->nodes synced.', array('%count' => $counter)));
+  }
 }
