--- sf_node.module	31 Aug 2009 15:00:22 -0000	1.2.2.12
+++ sf_node.module	29 Sep 2009 16:27:30 -0000
@@ -118,11 +118,29 @@ function sf_node_fieldmap_objects($type)
         'type' => array('label' => t('Node type')),
         'status' => array('label' => t('Is the node published?')),
         'promote' => array('label' => t('Is the node promoted?')),
-        'created' => array('label' => t('Created timestamp')),
+        'created' => array(
+          'label' => t('Created timestamp'),
+          'export' => '_sf_node_export_date',
+          'import' => '_sf_node_import_date',
+        ),
         'uid' => array('label' => t("Author's user ID")),
-        'mail' => array('label' => t("Author's email")),
-        'name' => array('label' => t("Author's username")),
-        'blank' => array('label' => t("(blank)")),
+        // "name" property on $node gets set by drupal for export, 
+        // but we still need an import handler.
+        'name' => array(
+          'label' => t("Author's username"),
+          'import' => '_sf_node_import_author_name',
+        ),
+        // "mail" and blank need both import and export handlers.
+        'mail' => array(
+          'label' => t("Author's email"),
+          'export' => '_sf_node_export_author_email',
+          'import' => '_sf_node_import_author_email',
+        ),
+        'blank' => array(
+          'label' => t("(blank)"),
+          'export' => '_sf_node_export_blank',
+          'import' => '_sf_node_import_blank',
+        ),
       );
     }
 
@@ -210,6 +228,69 @@ function _sf_node_import_cck_todate(&$no
   $node->$key = $data;
 }
 
+// Returns the email address of the node's author, given by node->uid
+function  _sf_node_export_author_email($source, $field) {
+  $uid = $source->uid;
+  if(!is_numeric($uid)) {
+    return null;
+  }
+  return db_result(db_query('SELECT mail FROM {users} WHERE uid = %d', $uid));
+}
+
+// Attempt to update the node's author, given the author's email.
+// DO NOT change the author's email address.
+// is this really what we want to do here?
+function _sf_node_import_author_email(&$node, $key, $source, $field) {
+  $new_mail = $source->$field;
+  $node->$key = $new_mail;
+  $old_mail = db_result(db_query('SELECT mail FROM {users} WHERE uid = %d', $node->uid));
+  if($new_mail != $old_mail) {
+    $new_uid = db_result(db_query('SELECT uid FROM {users} WHERE mail = "%s"', $new_mail));
+    if(is_numeric($new_uid)) {
+      $node->uid = $new_uid;
+      return;
+    }
+  }
+  watchdog('sf_node', 'No user found with email ' . $new_mail);
+}
+
+// Attempt to update the node's author, given the author's username.
+// DO NOT change the author's username.
+// is this really what we want to do here?
+function _sf_node_import_author_name(&$node, $key, $source, $field) {
+  $new_name = $source->$field;
+  $node->$key = $new_name;
+  $old_name = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $node->uid));
+  if($new_name != $old_name) {
+    $new_uid = db_result(db_query('SELECT uid FROM {users} WHERE name = "%s"', $new_name));
+    if(is_numeric($new_uid)) {
+      $node->uid = $new_uid;
+      return;
+    }
+  }
+  watchdog('sf_node', 'No user found with username ' . $new_name);
+}
+
+// Export a timestamp in a format SalesForce comprehends.
+function _sf_node_export_date($source, $field) {
+  return gmdate(DATE_ATOM, $source->$field);
+}
+
+// Given a SalesForce time, import a node timestamp.
+function _sf_node_import_date(&$node, $key, $source, $field) {
+  $node->$key = strtotime($source->$field);
+}
+
+// Export the text value "(blank)".
+function _sf_node_export_blank($source, $field) {
+  return '(blank)';
+}
+
+// Import the text value "(blank)".
+function _sf_node_import_blank(&$node, $key, $source, $field) {
+  $node->$key = '(blank)';
+}
+
 // Displays the Salesforce synchronization form.
 function sf_node_salesforce_form(&$form_state, $nid) {
   $node = node_load($nid);
@@ -451,13 +532,6 @@ function sf_node_export($node, $fieldmap
   if (is_numeric($node)) {
     $node = node_load($node);
   }
-  // Add the email address, making this suitable for bio/profile-node to Contact/Lead maps.
-  $node->mail = db_result(db_query('SELECT mail FROM {users} WHERE uid = %d', $node->uid));
-  // Correct timestamp formatting for SF
-  $node->created = gmdate(DATE_ATOM, $node->created);
-  // Inlcude the "blank" value
-  $node->blank = '(blank)';
-
 
   // Create an object for export based on the specified fieldmap.
   $object = salesforce_api_fieldmap_export_create($fieldmap, $node);
