Index: nat.module
===================================================================
--- nat.module	(revision 83)
+++ nat.module	(working copy)
@@ -104,7 +104,7 @@
 
       // Update node title in term(s).
       $terms = nat_get_terms($node->nid);
-      _nat_update_terms($terms, trim($node->title), $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
+      _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.
@@ -546,7 +546,7 @@
  */
 function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
   $edit = array(
-    'name' => $title,
+    'name' => trim($title),
     'description' => $body,
     'weight' => 0
   );
Index: nat.module
===================================================================
--- nat.module	(revision 83)
+++ nat.module	(working copy)
@@ -104,7 +104,7 @@
 
       // Update node title in term(s).
       $terms = nat_get_terms($node->nid);
-      _nat_update_terms($terms, trim($node->title), $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
+      _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.
@@ -546,7 +546,7 @@
  */
 function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
   $edit = array(
-    'name' => $title,
+    'name' => trim($title),
     'description' => $body,
     'weight' => 0
   );
Index: nat.module
===================================================================
--- nat.module	(revision 83)
+++ nat.module	(working copy)
@@ -1,5 +1,5 @@
 <?php
-// $Id: nat.module,v 1.26 2007/06/30 13:57:10 karthik Exp $
+// $Id: nat.module,v 1.29 2007/08/14 16:25:59 karthik Exp $
 
 /**
  * @file
@@ -77,7 +77,7 @@
  * Implementation of hook_nodeapi().
  */
 function nat_nodeapi(&$node, $op, $teaser, $page) {
-  $nat_config = variable_get('nat_config', array());
+  $nat_config = _nat_variable_get();
   if (!isset($nat_config['types'][$node->type]) || empty($nat_config['types'][$node->type])) {
     return;
   }
@@ -104,7 +104,7 @@
 
       // Update node title in term(s).
       $terms = nat_get_terms($node->nid);
-      _nat_update_terms($terms, trim($node->title), $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
+      _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.
@@ -122,7 +122,7 @@
  */
 function nat_form_alter($form_id, &$form) {
   if ($form['#id'] == 'node-form') {
-    $config = variable_get('nat_config', array());
+    $config = _nat_variable_get();
 
     foreach ($config['types'] as $type => $associations) {
       if (count($associations) && $form_id == $type .'_node_form' && $config['related'][$type] == 1) {
@@ -166,7 +166,7 @@
  * Implementation of hook_link_alter().
  */
 function nat_link_alter(&$node, &$links) {
-  $nat_config = variable_get('nat_config', array());
+  $nat_config = _nat_variable_get();
   if (isset($nat_config['node_links'][$node->type])) {
     foreach ($links as $module => $link) {
       // $link['title'] will be empty during node previews at which point
@@ -195,7 +195,7 @@
     drupal_goto('admin/settings/taxonomy');
   }
 
-  $nat_config = variable_get('nat_config', array());
+  $nat_config = _nat_variable_get();
 
   foreach ($types as $type => $type_object) {
     $collapsed = (!isset($nat_config['types'][$type])) || (empty($nat_config['types'][$type]));
@@ -271,7 +271,7 @@
     drupal_goto('admin/settings/taxonomy');
   }
 
-  $nat_config = variable_get('nat_config', array());
+  $nat_config = _nat_variable_get();
   $options = array();
   foreach ($nat_config['types'] as $type => $associations) {
     if (!empty($associations)) {
@@ -339,6 +339,64 @@
 }
 
 /**
+ * Retrieve the NAT terms associated with a node restricted by vocabulary.
+ *
+ * @param $nid
+ *   The node ID of the node whose NAT-terms are to be retrieved.
+ * @param $vocabularies
+ *   An array of vocabulary IDs used to optionally retrict the retrieved terms
+ * to a defined set of vocabularies.
+ */
+function nat_get_terms_by_vocabulary($nid, $vocabularies = array()) {
+  $terms = nat_get_terms($nid);
+  if (!empty($vocabularies)) {
+    foreach ($terms as $tid => $term) {
+      if (!in_array($term->vid, $vocabularies)) {
+        unset($terms[$tid]);
+      }
+    }
+  }
+
+  return $terms;
+}
+
+/**
+ * Retrieve the first / single NAT term associated with a node optionally
+ * restricted by vocabulary.
+ *
+ * @param $nid
+ *   The node ID of the node whose NAT-term is to be retrieved.
+ * @param $vocabularies
+ *   An optional array of vocabulary IDs used to optionally retrict the
+ * retrieved term to a defined set of vocabularies.
+ */
+function nat_get_term($nid, $vocabularies = array()) {
+  $terms = empty($vocabularies) ? nat_get_terms($nid) : nat_get_terms_by_vocabulary($nid, $vocabularies);
+
+  return array_shift($terms);
+}
+
+/**
+ * Retrieve all all the children of a node via its NAT association.
+ * Note: taxonomy_select_nodes is a rather resource hungry function.
+ *
+ * @param $nid
+ *   The node ID of the node whose child nodes are to be retrieved.
+ * @param $types
+ *   Unknown.
+ * @param $vocabularies
+ *   Retrict children to nodes categorised using provided vocabularies.
+ * @return
+ *   The resource identifier returned by taxonomy_select_nodes.
+ */
+function nat_get_children($nid, $types = array(), $vocabularies = array()) {
+  $terms = nat_get_terms_by_vocabulary($nid, $vocabularies);
+  $tids = array_keys($terms);
+
+  return taxonomy_select_nodes($tids);
+}
+
+/**
  * Gets node IDs/nodes associated with a term.
  *
  * @param $tids
@@ -419,45 +477,51 @@
 }
 
 /**
- * Retrieve the NAT terms associated with a node restricted by vocabulary.
+ * Update the NAT config to include node->vocabulary associations and related
+ * settings. Commonly used in .install files to register associations and save
+ * the admin some work.
  *
- * @param $nid
- *   The node ID of the node whose NAT-terms are to be retrieved.
- * @param $vocabularies
- *   An array of vocabulary IDs used to optionally retrict the retrieved terms
- * to a defined set of vocabularies.
+ * @param $type
+ *   The node type.
+ * @param $vids
+ *   Array of vocabulary IDs that the above node type is to be associated with
+ * via NAT.
+ * @param $delete
+ *   Boolean to indicate if associated term should be deleted when a node is
+ * deleted.
+ * @param $links
+ *   Boolean to indicate if links to NAT terms should point to the associated
+ * nodes instead.
+ * @param $body
+ *   Boolean to indicated if the node body should be in sync with the term
+ * description field.
+ * @param $related
+ *   Boolean to indicate if related terms and synonyms can be set during node
+ * creation.
  */
-function nat_get_terms_by_vocabulary($nid, $vocabularies = array()) {
-  $terms = nat_get_terms($nid);
-  if (!empty($vocabularies)) {
-    foreach ($terms as $tid => $term) {
-      if (!in_array($term->vid, $vocabularies)) {
-        unset($terms[$tid]);
-      }
-    }
+function nat_set_config($type, $vids, $delete = TRUE, $links = TRUE, $body = FALSE, $related = FALSE) {
+  $nat_config = _nat_variable_get();
+  if (!isset($nat_config['types'][$type])) {
+    $nat_config['types'][$type] = array();
   }
+  foreach ($vids as $vid) {
+    $nat_config['types'][$type][$vid] = $vid;
+  }
 
-  return $terms;
-}
+  if ($delete) {
+    $nat_config['delete'][$type] = TRUE;
+  }
+  if ($links) {
+    $nat_config['links'][$type] = TRUE;
+  }
+  if ($body) {
+    $nat_config['body'][$type] = TRUE;
+  }
+  if ($related) {
+    $nat_config['related'][$type] = TRUE;
+  }
 
-/**
- * Retrieve all all the children of a node via its NAT association.
- * Note: taxonomy_select_nodes is a rather resource hungry function.
- *
- * @param $nid
- *   The node ID of the node whose child nodes are to be retrieved.
- * @param $types
- *   Unknown.
- * @param $vocabularies
- *   Retrict children to nodes categorised using provided vocabularies.
- * @return
- *   The resource identifier returned by taxonomy_select_nodes.
- */
-function nat_get_children($nid, $types = array(), $vocabularies = array()) {
-  $terms = nat_get_terms_by_vocabulary($nid, $vocabularies);
-  $tids = array_keys($terms);
-
-  return taxonomy_select_nodes($tids);
+  variable_set('nat_config', $nat_config);
 }
 
 /**
@@ -546,7 +610,7 @@
  */
 function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
   $edit = array(
-    'name' => $title,
+    'name' => trim($title),
     'description' => $body,
     'weight' => 0
   );
@@ -618,7 +682,7 @@
  *   Associative array denoting the node-vocabulary pair that is to be synced.
  */
 function _nat_sync_associations($associations) {
-  $nat_config = variable_get('nat_config', array());
+  $nat_config = _nat_variable_get();
 
   $counter = 0;
   foreach ($associations as $association) {
@@ -640,3 +704,28 @@
   }
   drupal_set_message(t('NAT sync complete: %count nodes synced.', array('%count' => $counter)));
 }
+
+/**
+ * Return a NAT module variable.
+ *
+ * @param $name
+ *   The name of the variable to retrieve.
+ * @return
+ *   The value of the variable requested.
+ */
+function _nat_variable_get($name = NULL) {
+  static $variables = array();
+
+  if (empty($variables)) {
+    $defaults = array(
+      'types' => array(),
+      'body' => array(),
+      'delete' => array(),
+      'node_links' => array()
+    );
+    $variables = variable_get('nat_config', array());
+    $variables = array_merge($defaults, $variables);
+  }
+
+  return $name ? $variables[$name] : $variables;
+}
Index: nat.module
===================================================================
--- nat.module	(revision 100)
+++ nat.module	(working copy)
@@ -610,7 +610,7 @@
  */
 function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
   $edit = array(
-    'name' => $title,
+    'name' => trim($title),
     'description' => $body,
     'weight' => 0
   );
