Index: INSTALL.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/salesforcewebform/INSTALL.txt,v
retrieving revision 1.2.2.1
diff -u -r1.2.2.1 INSTALL.txt
--- INSTALL.txt	11 Jan 2009 05:58:04 -0000	1.2.2.1
+++ INSTALL.txt	27 Jan 2010 03:39:59 -0000
@@ -40,11 +40,13 @@
 7. Create or edit a webform to match your lead form.  Under 'SalesForce Settings', select
    the 'Yes' option to post the form to SalesForce.
 
+   At a minimum, a Lead form must include Last Name, Email, and Company fields.
+   
    Under 'Webform Advanced Settings', add the following code for 'Additional Processing':
 
 	<?php
 	require_once(drupal_get_path('module', 'salesforcewebform') . '/salesforcewebform.module');
-	$form_state = salesforcewebform_process($form, $form_state);
+	$form_state = salesforcewebform_process($node, $form, $form_state);
 	?>
 
    For each component, you must select a SalesForce field to map to.  The default fields
Index: salesforcewebform.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/salesforcewebform/salesforcewebform.install,v
retrieving revision 1.2.2.1
diff -u -r1.2.2.1 salesforcewebform.install
--- salesforcewebform.install	11 Jan 2009 05:58:04 -0000	1.2.2.1
+++ salesforcewebform.install	25 Jan 2010 00:47:20 -0000
@@ -60,4 +60,24 @@
 	variable_del("salesforcewebform_user");
 	variable_del("salesforcewebform_pw");
 	variable_del("salesforcewebform_custom_fields");
-}
\ No newline at end of file
+}
+
+/**
+ * Update databse to allow object type selection
+ */
+function salesforcewebform_update_6001() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysqli':
+    case 'mysql':
+      $ret[] = update_sql("ALTER TABLE {salesforcewebform} ADD COLUMN
+object char(1) NOT NULL default ''");
+      break;
+    case 'pgsql':
+      db_add_column($ret, 'salesforce', 'description',
+'char(1)', array('default' => '', 'not null' => TRUE));
+      break;
+  }
+  return $ret;
+}
+ 
\ No newline at end of file
Index: salesforcewebform.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/salesforcewebform/salesforcewebform.module,v
retrieving revision 1.14.2.4
diff -u -r1.14.2.4 salesforcewebform.module
--- salesforcewebform.module	22 Apr 2009 02:49:16 -0000	1.14.2.4
+++ salesforcewebform.module	10 Feb 2010 04:06:13 -0000
@@ -38,14 +38,24 @@
 		  );
 		
 		  $salesforce_form['salesforcesettings']['use_salesforce'] = array (
-			'#type' => 'radios',
-			'#title' => t('Submit to SalesForce'),
-			'#options' => array(
-				1=>t('Yes'), 0=>t('No')
-			),
-			'#description' => t('If yes, the form will be sent via SOAP to SalesForce.  Make sure you have specified the username & password in Administer->Site Configuration->SalesForce Webform first.'),
-			'#default_value' => ($form['#node']->use_salesforce) ? $form['#node']->use_salesforce : 0,
+        '#type' => 'radios',
+        '#title' => t('Submit to SalesForce'),
+        '#options' => array(
+          1=>t('Yes'), 0=>t('No')
+        ),
+        '#description' => t('If yes, the form will be sent via SOAP to SalesForce.  Make sure you have specified the username & password in Administer->Site Configuration->SalesForce Webform first.'),
+        '#default_value' => ($form['#node']->use_salesforce) ? $form['#node']->use_salesforce : 0,
 		  );
+		  
+		  $salesforce_form['salesforcesettings']['salesforce_object'] = array(
+        '#type' => 'select',
+        '#title' => t('Object Type'),
+        '#options' => array(
+          'l' => t('Lead'), 'c' => t('Contact'), 'a' => t('Account'), 'p' => t('Person Account'), 'm' => t('Account + Contact'),
+        ),
+        '#description' => t('Select which object type to submit to.'),
+        '#default_value' => ($form['#node']->salesforce_object) ? $form['#node']->salesforce_object : 'l',
+      );
 		  /* End SalesForce settings form */
 
 		  // integrate the SalesForce form into the form
@@ -60,8 +70,18 @@
 		if ( $form['#parameters'][2]->use_salesforce == 1 ) {
 	    module_load_include('inc', 'salesforcewebform', 'salesforcewebform_fields');
 		  foreach(salesforcewebform_available_fields() as $sfwafkey=>$sfwafvalue) {
-			$sfwafkey = strtolower($sfwafkey);
-			$sfwaf_array[$sfwafkey] = $sfwafvalue;
+        if (!is_array($sfwafvalue)) {
+          $sfwafkey = strtolower($sfwafkey);
+          $sfwaf_array[$sfwafkey] = $sfwafvalue;
+        }
+        else {
+          $subarray = array();
+          foreach($sfwafvalue as $subkey => $subvalue) {
+            $subkey = strtolower($subkey);
+            $subarray[$subkey] = $subvalue;
+          }
+          $sfwaf_array[$sfwafkey] = $subarray;
+        }
 		  }
 		  $valid_fields = array_merge($sfwaf_array, salesforcewebform_load_custom_fields('webform'));
 		  $new_form_key = array(
@@ -91,21 +111,22 @@
 				if ( isset($node->use_salesforce) ) {
 					// store the SalesForce fields
 					$result = db_query("DELETE FROM {salesforcewebform} where `vid` = %d", $node->vid);
-					$result = db_query("INSERT INTO {salesforcewebform} (`vid`, `use`) " .
-    					 							 "VALUES (%d, %d)", $node->vid, $node->use_salesforce);
+					$result = db_query("INSERT INTO {salesforcewebform} (`vid`, `use`, `object`) " .
+    					 							 "VALUES (%d, %d,'%s')", $node->vid, $node->use_salesforce, $node->salesforce_object);
 				}
 				break;
 			case 'validate':
 
 				break;	
 			case 'load':
-				$result = db_query("SELECT `use` from {salesforcewebform} ".
+				$result = db_query("SELECT `use`, `object` from {salesforcewebform} ".
 			           					 "WHERE `vid` = %d", $node->vid); 
 				// either $result has 0 or 1 rows, if we have one then add it to the node
 				$row = db_fetch_array($result);
 
 			  if ( $row['use'] == '1' ) {
 					$node->use_salesforce = TRUE;
+					$node->salesforce_object = $row['object'];
 				}
 				else {
 					$node->use_salesforce = FALSE;
@@ -208,8 +229,27 @@
 	return $spec;
 }
 
-function salesforcewebform_process($form, $form_state) {
-
+function salesforcewebform_process($node, $form, $form_state) {
+  $object_typecode = ($node->salesforce_object) ? $node->salesforce_object : "l";
+  $multiple_objects = FALSE;
+  switch($object_typecode) {
+    case "l":
+      $object_type = "Lead";
+    break;
+    case "c":
+      $object_type = "Contact";
+    break;
+    case "p":
+      $object_type = "PAccount";
+    break;
+    case "a":
+      $object_type = "Account";
+    break;
+    case "m":
+      $object_type = "AccountM";
+      $multiple_objects = TRUE;
+    break;
+  }
   $form_values = $form_state['values'];
   
   //Get the path to the Toolkit, set in the options on install.
@@ -221,23 +261,46 @@
 	require_once($toolkit_path.'/soapclient/SforceHeaderOptions.php');
 	module_load_include('inc', 'salesforcewebform', '/salesforcewebform_fields');
 	
-	// Salesforce Login information
-	$wsdl = $toolkit_path . '/soapclient/partner.wsdl.xml';
-	$userName = variable_get('salesforcewebform_user', NULL);
-	$password = variable_get('salesforcewebform_pw', NULL);
-	
-	// Process of logging on and getting a salesforce.com session
-	$client = new SforcePartnerClient();
-	$client->createConnection($wsdl);
-	$loginResult = $client->login($userName, $password);
-
 	$all_fields = array();
 	$master_fields = array_merge(salesforcewebform_available_fields(), salesforcewebform_load_custom_fields(NULL));
 	$submission = array();
 	foreach($master_fields as $mkey => $mvalue) {
-		if(array_key_exists(strtolower($mkey), $form_values['submitted_tree'])) {
-			$submission[$mkey] = $form_values['submitted_tree'][strtolower($mkey)];
-		}
+    if (!is_array($mvalue)) {
+      $parents = _salesforcewebform_array_key_exists_r(strtolower($mkey), $form_values['submitted_tree'], NULL);
+      if (is_array($parents)) {
+          if (count($parents) == 1) {
+            $submission[$mkey] = $form_values['submitted_tree'][$parents[0]][strtolower($mkey)];
+          }
+          elseif (count($parents) == 2) {
+            $submission[$mkey] = $form_values['submitted_tree'][$parents[0]][$parents[1]][strtolower($mkey)];
+          }
+          elseif (count($parents) == 3) {
+            $submission[$mkey] = $form_values['submitted_tree'][$parents[0]][$parents[1]][$parents[2]][strtolower($mkey)];
+          }
+      }
+      elseif ($parents === TRUE) {
+        $submission[$mkey] = $form_values['submitted_tree'][strtolower($mkey)];
+      }
+    }
+    else {
+      foreach ($mvalue as $skey => $svalue) {
+        $parents = _salesforcewebform_array_key_exists_r(strtolower($skey), $form_values['submitted_tree'], NULL);
+        if (is_array($parents)) {
+          if (count($parents) == 1) {
+            $submission[$skey] = $form_values['submitted_tree'][$parents[0]][strtolower($skey)];
+          }
+          elseif (count($parents) == 2) {
+            $submission[$skey] = $form_values['submitted_tree'][$parents[0]][$parents[1]][strtolower($skey)];
+          }
+          elseif (count($parents) == 3) {
+            $submission[$skey] = $form_values['submitted_tree'][$parents[0]][$parents[1]][$parents[2]][strtolower($skey)];
+          }
+        }
+        elseif ($parents === TRUE) {
+          $submission[$skey] = $form_values['submitted_tree'][strtolower($skey)];
+        }
+      }
+    }
   }
   $sid = empty($form_values['submitted_tree']['webformsid__c']) ? _salesforcewebform_generate_sid() : $form_values['submitted_tree']['webformsid__c'];
   $all_fields['OWNERID'] = $client->userId;
@@ -272,13 +335,35 @@
     }
 	}
 
+  // Salesforce Login information
+	$wsdl = $toolkit_path . '/soapclient/partner.wsdl.xml';
+	$userName = variable_get('salesforcewebform_user', NULL);
+	$password = variable_get('salesforcewebform_pw', NULL);
+	  
+	// Process of logging on and getting a salesforce.com session
+	$client = new SforcePartnerClient();
+	$client->createConnection($wsdl);
+	$loginResult = $client->login($userName, $password);
+	
   $sObjects = array();
 
   $sObject = new sObject();
-  $sObject->type = 'Lead'; // Salesforce Table or object that you will perform the upsert on
+  $sObject->type = $object_type; // Salesforce Table or object that you will perform the upsert on
   $sObject->fields = $all_fields;
+  if ($sObject->type == "PAccount") {
+    $sObject->type = "Account";
+  }
+  if ($sObject->type == "AccountM") {
+    //Exceptions
+    $exceptions = array('LastName', 'FirstName', 'Email', 'LeadSource', 'HomePhone', 'MobilePhone', 'MailingCity', 'MailingPostalCode', 'MailingCountry', 'ContactOwner');
+    foreach ($exceptions as $keyname) {
+      if (array_key_exists($keyname, $sObject->fields)) {
+        unset($sObject->fields[$keyname]);
+      }
+    }
+    $sObject->type = "Account";
+  }
   array_push($sObjects, $sObject);
-
   //This passes the client = the login to sales force
   // the $sObjects = data to upsert
   // $file_updated = accounts which are updated
@@ -286,6 +371,22 @@
   // $file_failed =  accounts which failed
   $success = _salesforcewebform_upsert_accounts($client, $sObjects, $file_updated, $file_created, $file_failed);
 
+  if (is_string($success) && $multiple_objects == TRUE) {
+    //Submit the contact with the account id as the account.
+    $sObject = new sObject();
+    $sObjects = array();
+    $sObject->type = "Contact"; // Salesforce Table or object that you will perform the upsert on
+    $all_fields['AccountId'] = $success;
+    $exceptions = array('AccountOwner', 'Name', 'Phone', 'OwnerId', 'BillingCity', 'BillingPostalCode', 'BillingCountry');
+    foreach ($exceptions as $keyname) {
+      if (array_key_exists($keyname, $all_fields)) {
+        unset($all_fields[$keyname]);
+      }
+    }
+    $sObject->fields = $all_fields;
+    array_push($sObjects, $sObject);
+    $success = _salesforcewebform_upsert_accounts($client, $sObjects, $file_updated, $file_created, $file_failed);
+  }
   // Update the overall counts
   if (is_array($success))
   {
@@ -321,11 +422,12 @@
             // The string is the same, regardless of the result
             $data2 = $sObjects[$k]->fields['NAME'] . ", " . $sObjects[$k]->fields['SA_ID__C'];
 
-            if ($result->success)
+            if (!is_array($result) && !is_object($result))
             {
-                if ($result->created)
+                if (strlen($result) == 18)
                 {
                     $accounts_created++;
+                    return $result;
                    // file_put_contents($file_created, $data2 . "\n", FILE_APPEND);
                 }
                 else
@@ -338,7 +440,7 @@
             {
                 $accounts_failed++;
                 // The errors object also contains fields and status_code
-                $errMessage = $result->errors->message;
+                $errMessage = $result->message;
                // file_put_contents($file_failed, $data2 . ", " . $errMessage . "\n", FILE_APPEND);
             }
             $k++;
@@ -346,6 +448,9 @@
         // Put the result counts into an array to pass back as the result.
         $success = array();
         array_push($success, $accounts_created, $accounts_updated, $accounts_failed);
+        if($errMessage) {
+          drupal_set_message($errMessage);
+        }
         return $success;
     }
     catch (exception $e)
@@ -353,6 +458,7 @@
         // This is reached if there is a major problem in the data or with
         // the salesforce.com connection. Normal data errors are caught by
         // salesforce.com
+         drupal_set_message($e->faultstring);
          return $e;
     }
 }
@@ -362,3 +468,22 @@
   return substr($sid, 0, 8);
 }
 
+function _salesforcewebform_array_key_exists_r($needle, $haystack, $parents = array()) {
+  $result = array_key_exists($needle, $haystack);
+  if ($result) {
+    if (count($parents) == 0) {
+      return $result;
+    }
+    else {
+      return $parents;
+    }
+  }
+  foreach ($haystack as $u => $v) {
+    if (is_array($v)) {
+        $parents[] = $u;
+        $result = _salesforcewebform_array_key_exists_r($needle, $v, $parents);
+    }
+    if ($result) return $result;
+  }
+  return $result;
+}
Index: salesforcewebform_fields.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/salesforcewebform/salesforcewebform_fields.inc,v
retrieving revision 1.6.2.2
diff -u -r1.6.2.2 salesforcewebform_fields.inc
--- salesforcewebform_fields.inc	22 Apr 2009 02:49:16 -0000	1.6.2.2
+++ salesforcewebform_fields.inc	10 Feb 2010 04:19:49 -0000
@@ -6,51 +6,60 @@
   return array(	
     //Special Field to block Processing
     0 => '--Do Not Map to SalesForce--',
-
-    // names
-    'Title' => 'Title',
-    'Salutation' => 'Salutation',
-    'FirstName' => 'First Name',
-    'LastName' => 'Last Name',
-    'Company' => 'Company',
-      
-    // email addresses and website
-    'Email' => 'Email',
-    'HasOptedOutOfEmail' => 'E-Mail Opt Out',
-    'Website' => 'Website',
     
-    // lead  info for SalesForce
-    'Description' => 'Description',
-    'LeadSource' => 'Lead Source',
-    'ReferredBy' => 'Referred By',
-    'RecordType' => 'Lead Record Type',
+    //Common fields for all form types
+    'Common Fields' => array(
+      'FirstName' => 'First Name',
+      'LastName' => 'Last Name*',
+      'Website' => 'Website',
+      'Fax' => 'Fax',
+      'Email' => 'Email',
+      'RecordTypeID' => 'Record Type',
+      'LeadSource' => 'Lead Source',      
+      'WebformSID__c' => 'Webform SID Key',
+    ),
+
+    // Lead
+    'Lead Fields' => array(
+      'Phone' => 'Phone',
+      'OwnerId' => 'Owner ID',
+      'MobilePhone' => 'Mobile',
+      'City' => 'City',
+      'PostalCode' => 'Postal Code',
+      'Country' => 'Country',    
+      'Company' => 'Company',
+    ),
     
-    // phone numbers
-    'Phone' => 'Phone', 
-    'MobilePhone' => 'Mobile',
-    'Fax' => 'Fax',
-    'DoNotCall' => 'Do Not Call',
-    'HasOptedOutOfFax' => 'Fax Opt Out',
+    //Contact settings
+    'Contact Fields' => array(
+      'HomePhone' => 'Home Phone',
+      'MobilePhone' => 'Mobile',
+      'MailingCity' => 'City',
+      'MailingPostalCode' => 'Postal Code',
+      'MailingCountry' => 'Country',
+      'ContactOwner' => 'Contact Owner',
+    ),
     
-    // adddresses
-    'City' => 'City',
-    'Country' => 'Country',
-    'PostalCode' => 'Zip',
-    'State' => 'State/Province',
-    'Street' => 'Address',
-
-    // extra
-    'Birthdate' => 'Birthdate',
-    'Description' => 'Description',
-    'Industry' => 'Industry',
-    'Rating' => 'Rating',
-    'AnnualRevenue' => 'Annual Revenue',
-    'NumberOfEmployees' => 'Employees',
-    'Campaign' => 'Campaign_ID',
+    //Account settings
+    'Account Fields' => array(
+      'AccountOwner' => 'Account Owner',
+      'Name' => 'Name*',
+      'Phone' => 'Phone',
+      'OwnerId' => 'Owner ID',
+      'BillingCity' => 'City',
+      'BillingPostalCode' => 'Postal Code',
+      'BillingCountry' => 'Country',
+    ),
     
-    //Special SID Field to map for editing
-    'WebformSID__c' => 'Webform SID Key',
-	
+    //Person Account fields
+    'Person Account Fields' => array(
+      'PersonEmail' => 'Email',
+      'PersonHomePhone' => 'Home Phone',
+      'PersonMobilePhone' => 'Mobile',
+      'PersonMailingCity' => 'City',
+      'PersonPostalCode' => 'Postal Code',
+      'PersonMailingCountry' => 'Country',
+    ),
   );
 }
 

