Index: sf_webform.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/sf_webform/sf_webform.info,v
retrieving revision 1.2
diff -u -p -r1.2 sf_webform.info
--- sf_webform.info	7 Aug 2009 14:38:57 -0000	1.2
+++ sf_webform.info	12 Aug 2009 13:58:00 -0000
@@ -2,15 +2,6 @@
 name = Salesforce Webform
 description = Integrates Webform module with the SalesForce API.
 dependencies[] = salesforce_api
-dependencies[] = sf_node
 dependencies[] = webform
 package = Salesforce
 core = 6.x
-version = "6.x-1-0.x-beta"
-
-; Information added by drupal.org packaging script on 2009-04-27
-version = "6.x-1.x-dev"
-core = "6.x"
-project = "sf_webform"
-datestamp = "1240835090"
-
Index: sf_webform.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/sf_webform/sf_webform.module,v
retrieving revision 1.4
diff -u -p -r1.4 sf_webform.module
--- sf_webform.module	7 Aug 2009 15:11:46 -0000	1.4
+++ sf_webform.module	12 Aug 2009 13:58:01 -0000
@@ -25,8 +25,8 @@ define('SALESFORCE_WEBFORM_FIELD_SFID', 
  */
 function sf_webform_menu()  {
   $items[SALESFORCE_PATH_FIELDMAPS ."/%/webform"] = array(
-    'title' => 'Select Webform Node',
-    'description' => 'Associate fieldmap to webform node.',
+    'title' => 'Webform node settings',
+    'description' => 'Configure user login status for the webform.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('sf_webform_associate_webform_node', 4),
     'access arguments' => array('administer salesforce'),
@@ -60,28 +60,28 @@ function sf_webform_form_alter(&$form, $
     $form['#submit'][0] = 'sf_webform_fieldmap_add_form_submit';
   }
 
-  if ("salesforce_api_fieldmap_delete_form" == $form_id) {
+  elseif ("salesforce_api_fieldmap_delete_form" == $form_id) {
     // override normal delete function to perform redirect
-    $form['#submit'][1] = $form['#submit'][0];
-    $form['#submit'][0] = 'sf_webform_fieldmap_delete_form_submit';
+    //$form['#submit'][1] = $form['#submit'][0];
+    $form['#submit'][] = 'sf_webform_fieldmap_delete_form_submit';
   }  
 
-  if ("webform_submission_delete_form" == $form_id) {
+  elseif ("webform_submission_delete_form" == $form_id) {
     // add additional submit function to delete entry in salesforce_ids table
     $form['#submit'][] = 'sf_webform_submission_delete';
   }
   
-  if (strpos($form_id, "webform_client") !== FALSE) {
+  elseif (strpos($form_id, "webform_client") !== FALSE) {
     // add additional submit function to webform to process salesforce submission
     $form['#submit'][] = 'sf_webform_webform_submit';
   }
 
-  if ("salesforce_api_fieldmap_edit_form" == $form_id) {
+  elseif ("salesforce_api_fieldmap_edit_form" == $form_id) {
 
     $map = salesforce_api_fieldmap_load($form['fieldmap_index']['#value']);
-
-    if (isset($map['webform_nid'])) {
-      $webform_node = node_load($map['webform_nid']);
+    if (!empty($map['fields']) && preg_match('/^node_webform_(\d+)$/', $map['drupal'], $matches)) {
+      $nid = $matches[1];
+      $webform_node = node_load($nid);
       // show the fieldmap and edit link
       $form['webform_fieldmap'] = array(
         '#type' => 'fieldset',
@@ -92,13 +92,14 @@ function sf_webform_form_alter(&$form, $
       );
       
       $form['webform_fieldmap']['information'] = array(
-        '#value' => t('This fieldmap is associated with webform node %title (nid %nid) - !link', array("%title" => $webform_node->title, "%nid" => $webform_node->nid, '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS ."/". $map['index'] ."/webform", $options = array("query" => "edit=1&destination=". SALESFORCE_PATH_FIELDMAPS ."/". $map['index'] ."/edit"))))
+        '#value' => t('This fieldmap is associated with webform node %title (nid %nid) - !link', array("%title" => $webform_node->title, "%nid" => $webform_node->nid, '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS ."/".  $map['fieldmap'] ."/webform", $options = array("query" => "edit=1&destination=". SALESFORCE_PATH_FIELDMAPS ."/". $map['fieldmap'] ."/edit"))))
       );
     }
   }
 
-  if ("sf_node_salesforce_form" == $form_id) {
-    $node = node_load($form['nid']['#value']);
+  elseif ("sf_node_salesforce_form" == $form_id) {
+    $nid = $form['nid']['#value'];
+    $node = node_load($nid);
 
     if ($node->type == 'webform')  {
       $form['fieldmap'] = array(
@@ -108,9 +109,10 @@ function sf_webform_form_alter(&$form, $
         '#collapsible' => FALSE,
         '#collapsed' => FALSE,
       );
-      if (isset($form['export']['fieldmap']['#options'])) {
+      $fmid = db_result(db_query("SELECT fieldmap FROM {salesforce_field_map} WHERE drupal = '%s'", 'node_webform_' . $nid));
+      if (!empty($fmid)) {
         $form['fieldmap']['information'] = array(
-          '#value' => t('This webform is  associated with fieldmap %fmid - !link', array("%fmid" => key($form['export']['fieldmap']['#options']), '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS .'/'. key($form['export']['fieldmap']['#options']) .'/webform')))
+          '#value' => t('This webform is associated with fieldmap %fmid - !link', array("%fmid" => $fmid, '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS .'/'. $fmid .'/edit')))
         );
       }
       else {
@@ -122,7 +124,7 @@ function sf_webform_form_alter(&$form, $
     }
   }
 
-  if ($form_id == 'salesforce_api_settings_form') {
+  elseif ($form_id == 'salesforce_api_settings_form') {
     $form['sf_webform'] = array(
       '#type' => 'fieldset',
       '#title' => t('Webform Integration'),
@@ -174,9 +176,9 @@ function sf_webform_salesforce_form($for
     SALESFORCE_WEBFORM_EXPORT_LOGIN_AUTH => t('Authenticated users only')
   );
 
-  foreach($node->salesforce as $access => $values) {
+  foreach ($node->salesforce as $access => $values) {
     $form['fieldmap']['information'] = array(
-      '#value' => t('This webform node is associated with fieldmap %s for %type- !link', array("%s" => $values['fmid'], '%type' => $access_roles[$access], '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS .'/'. $values['fmid'] .'/edit')))
+      '#value' => t('This webform node is associated with fieldmap %s for %type - !link', array("%s" => $values['fmid'], '%type' => $access_roles[$access], '!link' => l("edit", SALESFORCE_PATH_FIELDMAPS .'/'. $values['fmid'] .'/edit')))
     );    
   }
 
@@ -267,14 +269,14 @@ function theme_sf_webform_salesforce_for
  * Implementation of hook_theme().
  */
 function sf_webform_theme() {
-    $themes = array(
-        'sf_webform_salesforce_form' => array(
-            'arguments' => array('form'),
-            'function' => 'theme_sf_webform_salesforce_form',
-        ),
-    );
+  $themes = array(
+    'sf_webform_salesforce_form' => array(
+      'arguments' => array('form'),
+      'function' => 'theme_sf_webform_salesforce_form',
+    ),
+  );
 
-    return $themes;
+  return $themes;
 }
 
 /**
@@ -355,6 +357,10 @@ function sf_webform_fieldmap_add_form_su
 
   // Create the new fieldmap.
   $index = salesforce_api_fieldmap_create($form_state['values']['drupal_object'], $form_state['values']['salesforce_object'], $form_state['values']['fieldmap_action']);
+  if (preg_match('/^node_webform_(\d+)$/', $form_state['values']['drupal_object'], $matches)) {
+    $nid = $matches[1];
+    _sf_webform_set_webform_index($nid, $index, SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH);
+  }
 
   // Redirect to its edit form.
   if ("node_webform" == $form_state['values']['drupal_object']) {
@@ -373,13 +379,10 @@ function sf_webform_fieldmap_objects($ty
   
   // Define the data fields available for Drupal objects.
   if ($type == 'drupal')  {
-    // get the fieldmap id object and load webform node
-    // loading the fieldmap id from the URL args
-    if ("" != arg(4) && is_numeric(arg(4))) {
-      $nid = _sf_webform_get_webform_nid(arg(4));
-      // if this fieldmap is related to a webform node
-      $objects['node_webform'] = _sf_webform_get_fieldmap_object($nid);
-      
+    $result = db_query("SELECT nid FROM {node} WHERE type = 'webform'");
+    while ($nid = db_result($result)) {
+      $key = 'node_webform_' . $nid;
+      $objects[$key] = _sf_webform_get_fieldmap_object($nid);
     }
   }
 
@@ -389,18 +392,22 @@ function sf_webform_fieldmap_objects($ty
 /** 
  * Implementation of hook_fieldmap_objects_alter
  */
+/*
 function sf_webform_fieldmap_objects_alter(&$objects) {
   if ("node" == arg(0) && is_numeric(arg(1))) $objects['drupal']['node_webform'] = _sf_webform_get_fieldmap_object(arg(1));
 }
+*/
 
 /**
- * Form to relate webform nodes to fieldmaps
+ * Form to relate webform nodes to fieldmaps.
  */
 function sf_webform_associate_webform_node(&$form_state, $index) {
 
+  $fieldmap = _sf_webform_get_webform_fieldmap_info($index);
+  $node = node_load($fieldmap['nid']);
   $form['webform'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Select Webform'),
+    '#title' => t('Webform: %title', array('%title' => check_plain($node->title))),
     '#weight' => 0,
     '#collapsible' => TRUE,
     '#collapsed' => FALSE,
@@ -408,53 +415,30 @@ function sf_webform_associate_webform_no
   
   $form['webform']['fieldmap_index'] = array('#type' => 'hidden', '#value' => $index);
   
-  // check if the fieldmap is being editted
-  $edit = isset($_GET['edit']) ? TRUE : FALSE;
-  $form['webform']['fieldmap_edit'] = array('#type' => 'hidden', '#value' => $edit);
-  
-  $webform_nodes = _sf_webform_get_webforms($associated);
-  
-  if (empty($webform_nodes)) {
-    $form['webform']['fieldmap']['information'] = array(
-      '#value' => t('There are no available webform nodes.')
-    );
-  }
-  else {
-    
-    if ($edit) {
-      $fieldmap = _sf_webform_get_webform_fieldmap_info($index);
-    }
-    
-    // get all webform nodes that are not associated with a node already
-    $form['webform']['webform_nid'] = array(
-      '#type' => 'select',
-      '#title' => t('Webform node'),
-      '#default_value' => isset($fieldmap['nid']) ? $fieldmap['nid'] : array(),
-      '#options' => $webform_nodes,
-      '#description' => t('Select the webform node that this fieldmap should export/import'),
-    );
-    
-    if ($edit) $form['webform']['fieldmap']['information'] = array('#value' => t('Note: Changing the webform selection will remove any association of webform components and salesforce fields.')
-    );
-    
-    // get all webform nodes that are not associated with a node already
-    $form['webform']['login_status'] = array(
-      '#type' => 'radios',
-      '#title' => t('User login status'),
-      '#default_value' => isset($fieldmap['type']) ? $fieldmap['type'] : SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH,
-      '#options' => array(
-        SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH => t('Both anonymous & authenticated users'),
-        SALESFORCE_WEBFORM_EXPORT_LOGIN_ANON => t('Anonymous users only'), 
-        SALESFORCE_WEBFORM_EXPORT_LOGIN_AUTH => t('Authenticated users only')
-        ),
-      '#description' => t('Select the user login status when this fieldmap should be used.'),
-    );
+  // Get all webform nodes that are not associated with a node already.
+  $form['webform']['webform_nid'] = array(
+    '#type' => 'hidden',
+    '#title' => t('Webform node'),
+    '#default_value' => isset($fieldmap['nid']) ? $fieldmap['nid'] : 0,
+    '#description' => t('Select the webform node that this fieldmap should export/import'),
+  );
+  
+  $form['webform']['login_status'] = array(
+    '#type' => 'radios',
+    '#title' => t('User login status'),
+    '#default_value' => isset($fieldmap['type']) ? $fieldmap['type'] : SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH,
+    '#options' => array(
+      SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH => t('Both anonymous & authenticated users'),
+      SALESFORCE_WEBFORM_EXPORT_LOGIN_ANON => t('Anonymous users only'), 
+      SALESFORCE_WEBFORM_EXPORT_LOGIN_AUTH => t('Authenticated users only')
+      ),
+    '#description' => t('Select the user login status when this fieldmap should be used.'),
+  );
 
-    $form['submit'] = array(
-      '#type' => 'submit',
-      '#value' => t('Save'),
-    );
-  }
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save'),
+  );
 
   return $form;
 }
@@ -468,49 +452,30 @@ function sf_webform_associate_webform_no
   $fieldmap = _sf_webform_get_webform_fieldmap_info($form_state['values']['fieldmap_index']);
 
   $existing_fieldmaps = _sf_webform_get_webform_fieldmapid($form_state['values']['webform_nid']);
+  // Remove the old setting for this fieldmap from the array of existing ones.
+  unset($existing_fieldmaps[$fieldmap['type']]);
   $existing_types = array_keys($existing_fieldmaps);
 
-  // if editting
   if (!empty($fieldmap)) {
-    // if type not same as existing
+    // If the type not same as existing.
     if ($fieldmap['type'] != $form_state['values']['login_status']) {
 
-      // if existing contains ‘both’ do not allow any additional fieldmaps
-      if (in_array(0, $existing_types)) {
+      // If an existing fieldmap contains ‘both’ do not allow any additional fieldmaps.
+      if (in_array(SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH, $existing_types)) {
         form_set_error('login_status', t('A fieldmap has already been created for both anonymous and authenticated users.'), $reset = FALSE);
       }
 
-      // do not allow any new fieldmaps with same type
+      // Do not allow any new fieldmaps with same type.
       if (in_array($form_state['values']['login_status'], $existing_types)) {
-        // set error
         form_set_error('login_status', t('This webform is already associated to a fieldmap for this login status'), $reset = FALSE);
       }
 
-      // if existing types not empty and new type is 'both'
-      if (!empty($existing_types) && $form_state['values']['login_status'] == 0)  {
+      // If existing types not empty and new type is 'both'.
+      if (!empty($existing_types) && $form_state['values']['login_status'] == SALESFORCE_WEBFORM_EXPORT_LOGIN_BOTH)  {
         form_set_error('login_status', t('A fieldmap has already been created for both anonymous and authenticated users.'), $reset = FALSE);
       }
     }
   }
-  // if new fieldmap
-  else {
-    // if existing is ‘both’ do not allow any additional fieldmaps
-    if (in_array(0, $existing_types)) {
-      // set error
-      form_set_error('login_status', t('A fieldmap has already been created for either anonymous and authenticated users (or both).'), $reset = FALSE);
-    }
-
-    // do not allow any new fieldmaps with same type
-    if (in_array($form_state['values']['login_status'], $existing_types)) {
-      // set error
-      form_set_error('login_status', t('This webform is already associated to a fieldmap for this login status'), $reset = FALSE);
-    }
-
-    // if existing types not empty and new type is 'both'
-    if (!empty($existing_types) && $form_state['values']['login_status'] == 0)  {
-      form_set_error('login_status', t('A fieldmap has already been created for both anonymous and authenticated users.'), $reset = FALSE);
-    }
-  }
 }
 
 /**
@@ -520,13 +485,8 @@ function sf_webform_associate_webform_no
 
   $map = salesforce_api_fieldmap_load($form_state['values']['fieldmap_index']);
 
-  //$map['webform_nid'] = $form_state['values']['webform_nid'];
-  //$map['webform_login_status'] = $form_state['values']['login_status'];
-
-  // if the fieldmap is being editted then delete existing field association
-  if ($form_state['values']['fieldmap_edit']) {
-    _sf_webform_remove_webform_index($form_state['values']['webform_nid'], $form_state['values']['fieldmap_index']);
-  }
+  // If the fieldmap is being edited then delete existing field association.
+  _sf_webform_remove_webform_index($form_state['values']['webform_nid'], $form_state['values']['fieldmap_index']);
 
   $map['fields'] = array();
 
@@ -737,7 +697,8 @@ function _sf_webform_get_fieldmap_object
   $webform_node = node_load($nid);
 
   // get fields from webform node and add to fieldmap form
-  if ("webform" == $webform_node->type) {
+  if ($webform_node->type == "webform") {
+    $objects['label'] = t('Webform:') . ' ' . $webform_node->title;
     foreach ($webform_node->webform['components'] as $component_id => $component) {
       $objects['fields']['webform_'. $component_id] = array(
         'label' => $component['name'],
@@ -828,7 +789,7 @@ function _sf_webform_get_webforms($assoc
  */
 function _sf_webform_get_webform_fieldmapid($nid) {
   $retval = array();
-  $sql = db_query("SELECT fmid, nid, type FROM {salesforce_webform} WHERE `nid` = %d", $nid);
+  $sql = db_query("SELECT fmid, nid, type FROM {salesforce_webform} WHERE nid = %d", $nid);
   while ($row = db_fetch_array($sql)) {
     $retval[$row['type']] = $row;
   }
@@ -909,11 +870,5 @@ function sf_webform_cron() {
  * Delete a webform / fieldmap association
  */
 function sf_webform_fieldmap_delete_form_submit($form, $form_state) {
-
-  $fieldmap = variable_get("salesforce_fieldmap_". $form_state['values']['fieldmap_index'], 0);
-
-  if (is_array($fieldmap) && $fieldmap['drupal'] == 'node_webform')  {
-    return db_query("DELETE FROM {salesforce_webform} WHERE `fmid` = %d", $form_state['values']['fieldmap_index']);
-	}
-
-}
\ No newline at end of file
+  return db_query("DELETE FROM {salesforce_webform} WHERE fmid = %d", $form_state['values']['fieldmap_index']);
+}
