[Drupal 7 Commerce] Webform2Sugar module customizations

Customizations to the Drupal contrib module Webform2Sugar, a tool for sending webform submissions to Sugar CRM as leads.

  • Added some additional admin settings unique to our client's specific SugarCRM configuration.
  • Provided a basic understanding of sending POST data to remote databases.
Completion Date
Webform2Sugar Modifications
Platform(s)/Language(s)
Code Snippet
diff --git a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.admin.inc b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.admin.inc
index 1b222c0..78f863f 100644
--- a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.admin.inc
+++ b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.admin.inc
@@ -38,12 +38,12 @@ function commercesite_webform2sugar_manage($node) {
   foreach ($sugar_components as $sugar_component) {
     $delete_url = url('node/' . $node->nid . '/webform/commercesite_webform2sugar/delete/' . $sugar_component->sid);
     $component = $components[$sugar_component->cid];
-    $mappings[] = array($component->name, $component->form_key, $sugar_component->sugarkey, "<a href='$delete_url'>Delete</a>");
+    $mappings[] = array($component->name, $component->form_key, $sugar_component->sugarname, $sugar_component->sugarkey, "<a href='$delete_url'>Delete</a>");
   }
 
   $render_array['mappings'] = array(
     '#theme' => 'table',
-    '#header' => array('Form Field', 'Form Key', 'Sugar Field', 'Operations'),
+    '#header' => array('Drupal Field Display Name', 'Drupal Field Machine Name', 'Sugar Field Display Name', 'Sugar Field Machine Name', 'Operations'),
     '#rows' => $mappings,
     '#empty' => 'No Mappings Yet'
   );
@@ -81,11 +81,19 @@ function theme_commercesite_webform2sugar_manage_overview($variables) {
 }
 
 function commercesite_webform2sugar_component_add_validate($form, &$form_state) {
-  if ($form_state['values']['sugar_key'] == 'See Below' && !$form_state['values']['sugar_other']) {
-    form_set_error('sugar_other', 'Field is required when Other is selected for Sugar Field');
+  if ($form_state['values']['sugar_key'] == 'See Below' && !$form_state['values']['sugar_other_name']) {
+    form_set_error('sugar_other_name', 'Field is required when Other is selected for Sugar Field');
   }
 
-  if ($form_state['values']['sugar_other'] && $form_state['values']['sugar_key'] != 'See Below') {
+  if ($form_state['values']['sugar_other_name'] && $form_state['values']['sugar_key'] != 'See Below') {
+    form_set_error('sugar_key', 'Sugar Field must be Other if using a custom mapping');
+  }
+
+  if ($form_state['values']['sugar_key'] == 'See Below' && !$form_state['values']['sugar_other_key']) {
+    form_set_error('sugar_other_key', 'Field is required when Other is selected for Sugar Field');
+  }
+
+  if ($form_state['values']['sugar_other_key'] && $form_state['values']['sugar_key'] != 'See Below') {
     form_set_error('sugar_key', 'Sugar Field must be Other if using a custom mapping');
   }
 }
@@ -95,11 +103,14 @@ function commercesite_webform2sugar_component_add_submit($form, &$form_state) {
   $values['nid'] = $form_state['values']['nid'];
   $values['cid'] = $form_state['values']['component_id'];
 
-  if ($form_state['values']['sugar_other']) {
-    $values['sugarkey'] = $form_state['values']['sugar_other'];
+  if ($form_state['values']['sugar_other_key']) {
+    $values['sugarname'] = $form_state['values']['sugar_other_name'];
+    $values['sugarkey'] = $form_state['values']['sugar_other_key'];
   }
   else {
     $values['sugarkey'] = $form_state['values']['sugar_key'];
+    include(drupal_get_path('module', 'commercesite_webform2sugar') . "/commercesite_webform2sugar_fields.inc");
+    $values['sugarname'] = $commercesite_webform2sugar_available_fields[$values['sugarkey']];
   }
 
   try {
@@ -155,12 +166,20 @@ function commercesite_webform2sugar_component_add($form, &$form_state, $node) {
     '#description' => 'Field to map to in SugarCRM',
   );
 
-  $form['sugar_other'] = array(
+  $form['sugar_other_name'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Other Sugar Field (Custom Mapping) Display Name'),
+    '#maxlength' => 255,
+    '#required' => FALSE,
+    '#description' => "If 'Other' is selected above, enter custom sugar mapping display name.",
+  );
+
+  $form['sugar_other_key'] = array(
     '#type' => 'textfield',
-    '#title' => t('Other Sugar Field (Custom Mapping)'),
+    '#title' => t('Other Sugar Field (Custom Mapping) Machine Name'),
     '#maxlength' => 255,
     '#required' => FALSE,
-    '#description' => "If 'Other' is selected above, enter custom sugar mapping",
+    '#description' => "If 'Other' is selected above, enter custom sugar mapping display name. No spaces.",
   );
 
   $form['settings']['submit'] = array(
@@ -184,6 +203,17 @@ function commercesite_webform2sugar_settings_form_submit($form, &$form_state) {
   $values['campaign'] = $form_state['values']['campaign'];
   $values['active'] = $form_state['values']['active'];
 
+  //Assigned User ID addition:
+  $values['assigned_user_id'] = $form_state['values']['assigned_user_id'];
+  //Source Lead addition:
+  $values['lead_source'] = $form_state['values']['lead_source'];
+  //Drupal Webform addition:
+  $values['drupal_webform'] = $form_state['values']['drupal_webform'];
+  //Submission Domain addition:
+  $values['webform_domain'] = $form_state['values']['webform_domain'];
+  //Submission Language addition:
+  $values['webform_language'] = $form_state['values']['webform_language'];
+  
   try {
     if ($sugar_webform) {
       $count = db_update('commercesite_webform2sugar_webform')
@@ -262,6 +292,73 @@ function commercesite_webform2sugar_settings_form($form, &$form_state, $node, $sugar_w
     '#required' => TRUE,
     '#description' => t('Enter the id of your SugarCRM Campaign (Ex: b93f96b1-0e52-eac7-cff1-4f0e1fcadc39)'),
   );
+  
+  //Assigned User ID field added by Eric:
+  $form['settings']['assigned_user_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Default SugarCRM Assigned User Id'),
+    '#default_value' => $sugar_webform ? $sugar_webform->assigned_user_id : '',
+    '#size' =>  80,
+    '#maxlength' => 64,
+    '#required' => TRUE,
+    '#description' => t('Enter your SugarCRM User ID (Ex: b93f96b1-0e52-eac7-cff1-4f0e1fcadc39)'),
+  );
+
+  //Lead Source field added by Eric:
+  $form['settings']['lead_source'] = array(
+    '#type' => 'select',
+    '#title' => t('Default SugarCRM Lead Source'),
+    '#default_value' => $sugar_webform ? $sugar_webform->lead_source : 'Web Site',
+    '#required' => TRUE,
+    '#description' => t('Select the SugarCRM Lead Source'),
+    '#options' => array(
+      'Cold Call' => t('Cold Call'),
+      'Existing Customer' => t('Existing Customer'),
+      'Self Generated' => t('Self Generated'),
+      'Employee' => t('Employee'),
+      'Partner' => t('Partner'),
+      'Public Relations' => t('Public Relations'),
+      'Direct Mail' => t('Direct Mail'),
+      'Conference' => t('Conference'),
+      'Trade Show' => t('Trade Show'),
+      'Web Site' => t('Web Site'),
+      'Word of mouth' => t('Word of mouth'),
+      'Email' => t('Email'),
+      'Campaign' => t('Campaign'),
+      'Other' => t('Other'),
+    ),
+    //'#empty_option' => t('- Blank -'),
+  );
+
+  //Drupal Webform field added by Eric:
+  $form['settings']['drupal_webform'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Default Webform Description to send to SugarCRM'),
+    '#default_value' => $sugar_webform ? $sugar_webform->drupal_webform : '',
+    '#size' =>  80,
+    '#maxlength' => 64,
+    '#description' => t('Webform Description to send to Sugar field: drupal_webform_c'),
+  );
+
+  //Submission Domain field added by Eric:
+  $form['settings']['webform_domain'] = array(
+    '#type' => 'textfield',
+    '#title' => t('SugarCRM Field to map to webform submission domain'),
+    '#default_value' => $sugar_webform ? $sugar_webform->webform_domain : '',
+    '#size' =>  80,
+    '#maxlength' => 64,
+    '#description' => t('Enter a SugarCRM Machine Name (Ex: submission_domain_c)'),
+  );
+
+  //Submission Language field added by Eric:
+  $form['settings']['webform_language'] = array(
+    '#type' => 'textfield',
+    '#title' => t('SugarCRM Field to map to webform submission language code'),
+    '#default_value' => $sugar_webform ? $sugar_webform->webform_language : '',
+    '#size' =>  80,
+    '#maxlength' => 64,
+    '#description' => t('Enter a SugarCRM Machine Name (Ex: language_code_c)'),
+  );
 
   $form['settings']['submit'] = array(
     '#type' => 'submit',
diff --git a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.install b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.install
index 6231757..46131dc 100644
--- a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.install
+++ b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.install
@@ -28,7 +28,37 @@ function commercesite_webform2sugar_schema() {
         'not null' => TRUE,
       ),
       'campaign' => array(
-        'description' => 'The URL to the SugarCRM Server', 
+        'description' => 'SugarCRM Server Campaign ID', 
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'assigned_user_id' => array(
+        'description' => 'SugarCRM Server Assigned User ID',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'lead_source' => array(
+        'description' => 'SugarCRM Server Lead Source',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'drupal_webform' => array(
+        'description' => 'Webform Description',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'webform_domain' => array(
+        'description' => 'Webform submission domain',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
+      'webform_language' => array(
+        'description' => 'Webform submission language code',
         'type' => 'varchar',
         'length' => 255,
         'not null' => TRUE,
@@ -71,6 +101,12 @@ function commercesite_webform2sugar_schema() {
         'length' => 255,
         'not null' => TRUE,
       ),
+      'sugarname' => array(
+        'description' => 'The SugarCRM Server field display name',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+      ),
     ),
     'foreign keys' => array(
       'webform_component' => array(
diff --git a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.module b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.module
index df104bb..0516d11 100644
--- a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.module
+++ b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar.module
@@ -100,9 +100,15 @@ function commercesite_webform2sugar_webform_submission_insert($node, $submission) {
         //Usually there will just be one value, per field, but this could also be
         //a multi-value field so we need to loop through in case there is more than one
         foreach ($formvalues as $formvalue) {
-          //Seperate out each multiple value with a space and append to our value after checking
-          //to make sure there is no 'safe-key' mapping.
-          $value .= ' ' . commercesite_webform2sugar_check_safe_key($sugar_component, $formvalue, $node);
+          //Add line breaks instead of spaces for extra address lines:
+          if($key == 'primary_address_street' || $key == 'alt_address_street'){
+            $value .= "\r\n" . commercesite_webform2sugar_check_safe_key($sugar_component, $formvalue, $node);
+          }
+          else {
+            //Seperate out each multiple value with a space and append to our value after checking
+            //to make sure there is no 'safe-key' mapping.
+            $value .= ' ' . commercesite_webform2sugar_check_safe_key($sugar_component, $formvalue, $node);
+          }
         }
       }
 
@@ -110,8 +116,22 @@ function commercesite_webform2sugar_webform_submission_insert($node, $submission) {
       //Only include key if it has a valid value.
       //Blank values (Such as opt-out checkboxes that were NOT checked) should not be sent.
       if (trim($value)) {
-        //Now trim it up , and urlencode it.
-        $data .= '&' . $key . '=' . urlencode(ltrim($value));
+        //Trim up $value
+        $value = ltrim($value);
+        // Check to see if $formvalues is of type addressfield.  If it is, break
+        // the addressfield into formatted components:
+        if($key == 'primary_address' || $key == 'alt_address'){
+          $address_data = _explode_out_addressfield($value, $key);
+          //Append formatted address components to the $data string
+          $data .= $address_data;
+        }
+        else{
+          //urlencode $value
+          $value = urlencode($value);
+          //Append $key and $value to the $data string
+          $data .= '&' . $key . '=' . $value;
+        }
+        
       } else if ($key == 'email_opt_in' && !$formvalue) {
         //If the value was blank, AND it was email_opt_in, AND the safe_key was blank, we need a special case to actually send the opt_out flag!
         $data .= '&email_opt_out=1';
@@ -122,6 +142,31 @@ function commercesite_webform2sugar_webform_submission_insert($node, $submission) {
     if (strpos ($data, 'campaign_id') === FALSE) {
       $data .= '&campaign_id=' . $sugar_webform->campaign;
     }
+
+    //Check to see if the default assigned_user_id was overriden in the form.  If not, use the default one.
+    if (strpos ($data, 'assigned_user_id') === FALSE) {
+      $data .= '&assigned_user_id=' . urlencode($sugar_webform->assigned_user_id);
+    }
+
+    //Check to see if the default lead_source was overriden in the form.  If not, use the default one.
+    if (strpos ($data, 'lead_source') === FALSE) {
+      $data .= '&lead_source=' . urlencode($sugar_webform->lead_source);
+    }
+    
+    //Check to see if the default drupal_webform_c was overriden in the form.  If not, use the default one.
+    if (strpos ($data, 'drupal_webform_c') === FALSE) {
+      $data .= '&drupal_webform_c=' . urlencode($sugar_webform->drupal_webform);
+    }
+
+    //Check to see if a SugarCRM machine name was provided for the webform's global domain variable
+    if (!empty($sugar_webform->webform_domain)) {
+      $data .= '&' . $sugar_webform->webform_domain . '=' . urlencode($GLOBALS['language']->domain);
+    }
+    
+    //Check to see if a SugarCRM machine name was provided for the webform's global language variable
+    if (!empty($sugar_webform->webform_language)) {
+      $data .= '&' . $sugar_webform->webform_language . '=' . urlencode($GLOBALS['language']->language);
+    }
 
     //Setup Headers for Posting to SugarCRM
     $url_headers = array(
@@ -218,3 +263,33 @@ function commercesite_webform2sugar_node_delete($node) {
       ->execute();
   }
 }
+
+function _explode_out_addressfield($address, $address_type) {
+  //Search $value for each Addressfield component and reformat correctly:
+  //Break the serialized address values into an array
+  $address_array = unserialize($address);
+  $revised_value = '';
+  foreach($address_array as $key => $field){
+    switch($key){
+      case 'thoroughfare':
+        $revised_value .= '&' . $address_type . '_street=' . urlencode($field);
+        break;
+      case 'premise':
+        $revised_value .= '%0D%0A' . urlencode($field);
+        break;
+      case 'locality':
+        $revised_value .= '&' . $address_type . '_city=' . urlencode($field);
+        break;
+      case 'administrative_area':
+        $revised_value .= '&' . $address_type . '_state=' . urlencode($field);
+        break;
+      case 'postal_code':
+        $revised_value .= '&' . $address_type . '_postalcode=' . urlencode($field);
+        break;
+      case 'country':
+        $revised_value .= '&' . $address_type . '_country=' . urlencode($field);
+        break;
+    }
+  }
+  return $revised_value;
+}
diff --git a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar_fields.inc b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar_fields.inc
index 2f2584e..a74cd32 100644
--- a/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar_fields.inc
+++ b/sites/all/modules/custom/commercesite_webform2sugar/commercesite_webform2sugar_fields.inc
@@ -23,9 +23,7 @@ $commercesite_webform2sugar_available_fields = array(
   // lead  info for SugarCRM
   'account_name' => 'Account Name',
   'description_0' => 'Description',
-  'lead_source_description' => 'Lead Source Description',
   'refered_by' => 'Referred By',
-  'assigned_user_id' => 'Assigned To (id)',
 
   // phone numbers
   'phone_home' => 'Home Phone', 
@@ -40,16 +38,22 @@ $commercesite_webform2sugar_available_fields = array(
   'primary_address_country' => 'Primary Address Country',
   'primary_address_postalcode' => 'Primary Address Postal Code',
   'primary_address_state' => 'Primary Address State',
-  'primary_address_street' => 'Primary Address Line 1',
+  'primary_address_street' => 'Primary Address Street',
   'alt_address_city' => 'Alternate Address City',
   'alt_address_country' => 'Alternate Address Country',
   'alt_address_postalcode' => 'Alternate Address Postal Code',
   'alt_address_state' => 'Alternate Address State',
-  'alt_address_street' => 'Alternate Address Line 1',
+  'alt_address_street' => 'Alternate Address Street',
 
+  // Added by Eric for addressfield module handling:
+  'primary_address' => 'Primary Address',
+  'alt_address' => 'Alternate Address',
 
   // extra
   'birthdate' => 'Birthdate',
   'description' => 'Description',
   'campaign_id' => 'Campaign ID Override',
+  'assigned_user_id' => 'Assigned User ID Override',
+  'lead_source' => 'Lead Source Override',
+  'drupal_webform_c' => 'Drupal Webform Description Override',
 );
Attachment Size
webform2sugar-custom.patch 17.57 KB

Drupal Association Individual Member      Drupal Association Individual Member      #DrupalCares Supporter      Acquia Certified Site Builder