Index: webform2sugar.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform2sugar/webform2sugar.module,v
retrieving revision 1.1.4.2.2.6
diff -u -p -r1.1.4.2.2.6 webform2sugar.module
--- webform2sugar.module	3 Apr 2009 20:55:53 -0000	1.1.4.2.2.6
+++ webform2sugar.module	6 Jan 2010 08:28:35 -0000
@@ -3,35 +3,38 @@
 function webform2sugar_submit_form($form_id, &$values){	
 	$sugarcrmurl = variable_get('sugarcrm_url','');
 	
-	//We want to send to sugar if an URL is entered only.
-	if (!empty($sugarcrmurl)){
+	//We only want to send to sugar if an URL is entered and if the submitted_tree is part of form values.
+	//submitted_tree will not be part of the $form_values if it is not the final step of a multi-step webform.
+	if (!empty($sugarcrmurl) && array_key_exists('submitted_tree', $values['values'])) {
         $form_values = $values['values'];
 
     	$result = db_query("SELECT campaign, sugar_user, lead_source from {webform2sugar} WHERE vid = %d", $form_values['details']['nid']);
     	$row = db_fetch_array($result);
     	$campaignID = $row['campaign'];
-	  
+	  	
 	    //Build the SugarCRM URL and check if there is already an http:// in the url, and add it if not:
     	if (!strstr($sugarcrmurl,'http://')) {
     	  $sugarcrmurl = 'http://'.$sugarcrmurl;
     	} 
  	    $sugarcrmurl .= '/index.php?entryPoint=WebToLeadCapture';
- 	    
     	// combine the sugar parameters
     	$combine = '';
     	$combine .= webform2sugar_flatten_form($form_values['submitted_tree']);
-    	$combine .= empty($form_values['submitted_tree']['last_name']) ? '&last_name=empty' : $combine['last_name'];
     	$combine .= '&campaign_id='.$campaignID;
     	$combine .= variable_get('sugarcrm_version', 0) == 1 ? '&team_id=1' : '';
 
+		if (strpos($combine, 'last_name') == false) {
+		    $combine .= '&last_name=empty';
+		}
+
     	$data = ltrim($combine,'&');
+    	
     	   
     	// send off to sugar
         //drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3)
         $result = drupal_http_request($sugarcrmurl,array('Content-Type'=>'application/x-www-form-urlencoded',
     																						 'User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9'),
     																						 'POST',$data);
-   
       if ($result->code==200){
         $message = t('Valid response recieved from SugarCRM<br /><br />Data posted : !posted<br /><br />Content of response (an empty response is a good thing!): !response',
                       array('!posted'=>$data,'!response'=>$result->data));
@@ -46,30 +49,45 @@ function webform2sugar_submit_form($form
 
 function webform2sugar_flatten_form($values){
 
-	include_once(drupal_get_path('module', 'webform2sugar') . "/webform2sugar_fields.inc");
-	
+	include(drupal_get_path('module', 'webform2sugar') . "/webform2sugar_fields.inc");
 	$descriptions = '';
 	$addresses='';
 	foreach($values as $key => $value){
 		if(array_key_exists($key, $webform2sugar_available_fields) && $key!='none'){
-			if (is_array($value)){
-				$data .= webform2sugar_flatten_form($value);
+			//Special treathment for arrays. Probably a checkbox or selectbox.
+			if (is_array($value)) {
+					//If its empty then we can just let it go.				
+					if (empty($value)) {
+						continue;
+					//We have a sugarfield with multiple values.  Combine them together.
+					} else {
+						$combinedValue = '';
+						foreach($value as $array_key => $array_value) {
+							$combinedValue .= $array_value . ' ';
+						}
+						$data .= '&'.$key.'='.urlencode($combinedValue);
+					}
 			}
-			else {
-				//Special treathment for addresses. This enables multiple adddresslines in forms combined to one addressfield in SugarCRM.
-				if (strstr($key,'primary_address_street')){				
+			//Special treathment for addresses. This enables multiple adddresslines in forms combined to one addressfield in SugarCRM.
+			else if (strstr($key,'primary_address_street')){				
 					$addresses .= $value.' ';
-				}
-				//Special treathment for Descriptions. This enables multiple descriptions in forms combined to one description in SugarCRM.
-				else if (strstr($key,'description')){				
-					$descriptions .= $value.' ';
-				}
-				//Normal treathment
-				else {
-					$data .= '&'.$key.'='.urlencode($value);
-				}
-			} //end else
-		} //end if
+			}
+			//Special treathment for Descriptions. This enables multiple descriptions in forms combined to one description in SugarCRM.
+			else if (strstr($key,'description_')){				
+				$descriptions .= $value.' ';
+			}
+			//Special treathment for opt-out. If the user selected to opt-in AND there is an opt-out (most likely from the hidden opt-out field) 
+			//we should treat as opt-int and ignore the opt-out.
+			else if (strstr($key,'email_opt_out') && (strpos($data, 'email_opt_in') != false)){				
+				continue; //there is already an opt-in, so don't opt out.
+			}
+			//Normal treathment
+			else {
+				$data .= '&'.$key.'='.urlencode($value);
+			}
+		} else if (is_array($value)){
+			$data .= webform2sugar_flatten_form($value);
+		} 
 	}  // end for each
 	
   //If there are (combined)addresses, add them to the output
@@ -119,7 +137,7 @@ function webform2sugar_form_alter(&$form
 		
 		// Since SugarCRM by default only looks for an Opt-out, we need to make sure that we
 		// set a positive Opt-Out in the event of the user not electing to Opt-in.  
-		if ( isset($form['submitted']['email_opt_in'])) {
+		if (array_key_exists_r('email_opt_in', $form)) {
 			// add it to the submitted fieldset for consistency with when the user adds an 
 			// Opt-Out field directly
 			$form['submitted']['email_opt_out'] = array(
@@ -285,3 +303,18 @@ function webform2sugar_admin_settings() 
 	
 	return system_settings_form($form);
 }
+
+function array_key_exists_r($needle, $haystack)
+{
+    $result = array_key_exists($needle, $haystack);
+    if ($result)
+        return $result;
+    foreach ($haystack as $v)
+    {
+        if (is_array($v) || is_object($v))
+            $result = array_key_exists_r($needle, $v);
+        if ($result)
+        return $result;
+    }
+    return $result;
+}
\ No newline at end of file
Index: webform2sugar_fields.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/webform2sugar/webform2sugar_fields.inc,v
retrieving revision 1.1.4.1.2.1
diff -u -p -r1.1.4.1.2.1 webform2sugar_fields.inc
--- webform2sugar_fields.inc	28 Mar 2009 15:28:41 -0000	1.1.4.1.2.1
+++ webform2sugar_fields.inc	6 Jan 2010 08:28:35 -0000
@@ -19,7 +19,7 @@ $webform2sugar_available_fields = array(
 	
 	// lead  info for SugarCRM
 	'account_name' => 'Account Name',
-	'description' => 'Description',
+	'description_0' => 'Description',
 	'lead_source_description' => 'Lead Source Description',
 	'refered_by' => 'Referred By',
 	'assigned_user_name' => 'Assigned To',
@@ -68,9 +68,3 @@ $webform2sugar_available_fields = array(
 	'description_15' => 'Description 15',
 
 );
-
-function load_custom_fields () {
-	$custom_fields = variable_get('sugarwebform_custom_fields', null);
-	if ( is_array($custom_fields) ) return $custom_fields ;
-	else return array();
-}
\ No newline at end of file
