webform['sugarsettings']['sugarcrm_action'];
$and_sign = false;
/**
* The following will take any webform field that is not a standard field in CRM and
* add that key/value to the description field in CRM so that data is not lost
*/
include_once(drupal_get_path('module', 'sugarwebform') . "/sugarwebform_fields.inc");
$submitted_vals = $form_state['values']['submitted_tree'];
//go through each component and if it's not represented in CRM dump it all in the description field
foreach ($form['#parameters'][2]->webform['components'] as $component) {
$form_key = $component['form_key'];
$field_name = $component['name'];
if (!$sugarwebform_available_fields[$form_key]) {
$form_state['values']['description'] .= $field_name . ': ';
if (is_array($submitted_vals[$form_key])) {
$form_state['values']['description'] .= "\n";
foreach ($submitted_vals[$form_key] as $k => $v) {
if ($v)
$form_state['values']['description'] .= $v."\n";
}
}
else
$form_state['values']['description'] .= $submitted_vals[$form_key]."\n";
}
}
$data = sugarwebform_flatten_form($form_state['values']);
$data = ltrim($data,'&');
if ($url){
$result = drupal_http_request($url,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
Data posted : !posted
Content of response (an empty response is a good thing!): !response',
array('!posted'=>$data,'!response'=>$result->data));
watchdog('SugarCRM',$message);
}
else{
$message = t('Failed to send request to !url response code was !code',array('url'=>$url,'code'=>$result->code));
watchdog('SugarCRM',$message,WATCHDOG_ERROR);
}
}
/**
* Flatten out the values from a form post and turn them into something that can be sent via http_request
*/
function sugarwebform_flatten_form($values){
foreach($values as $key=>$value){
if (is_array($value)){
$data .= sugarwebform_flatten_form($value);
}
else {
$data .= '&'.$key.'='.urlencode($value);
}
}
return $data;
}
/**
* Allow for editing webforms with SugarCRM connection information and modifying the
* each webform to post to SugarCRM.
*/
function sugarwebform_form_alter(&$form, $form_state, $form_id) {
// handle webform client forms (instances of webforms that are submitted)
// webform names client forms with the pattern web_form_client_form_$nid, use this knowledge to
// manipulate the forms that webform displays
if ( ereg('^webform_client_form_', $form_id) ) {
global $base_url;
// debug output shows that the node is the third parameter -- fragile but the only way we can access it
$node = $form['#parameters'][2];
// only deal with this form if it is supposed to go to sugar
if ( empty($node->webform['sugarsettings']['sugarcrm_action']) ) {
return ;
}
$form['#submit'][] = 'sugarwebform_submit_form';
// add the configured properties to set the lead source and the SugarCRM user
$form['lead_source'] = array(
'#type' => 'hidden',
'#value' => $node->webform['sugarsettings']['sugarcrm_lead_source'],
);
$form['user'] = array(
'#type' => 'hidden',
'#value' => $node->webform['sugarsettings']['sugarcrm_user'],
);
// all leads coming from this form should be treated as new on the SugarCRM side
$form['status'] = array(
'#type' => 'hidden',
'#value' => 'New',
);
// if the form requests the full name from the user, we need to let the lead capture
// script know to split it into first and last names
if ( isset($form['submitted']['name']) ) {
$form['_splitName'] = array(
'#type' => 'hidden',
'#value' => 'yes',
);
}
// 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'])) {
// add it to the submitted fieldset for consistency with when the user adds an
// Opt-Out field directly
$form['submitted']['email_opt_out'] = array(
'#type' => 'hidden',
'#value' => 'yes',
);
}
// use the redirect/confirmation data from the original web form to tell SugarCRM where
// to redirect the user this is the same logic that the client_form_submit function
// uses to determine whether or not to redirect
if (valid_url(trim($node->confirmation), true)) {
$redirect = trim($node->confirmation);
}
elseif (preg_match('/^internal:/', $node->confirmation)) {
$path = preg_replace('/^internal:/', $base_url .'/', $node->confirmation);
$redirect = trim($path);
}
else {
// create an absolute URL for the SugarCRM lead capture page to send the user back to
$redirect = url('node/'. $node->nid .'/done', array('absolute' => true));
}
$form['redirect'] = array(
'#type' => 'hidden',
'#value' => $redirect,
);
}
// handle editing of webform nodes
elseif ( $form_id == 'webform_node_form' ) {
/* Collapse the email info on the page assuming that it is going to be a SugarCRM form */
$form['webform']['mail_settings']['#collapsed'] = TRUE;
/* Start SugarCRM Settings Form */
$sugar_form['sugarsettings'] = array(
'#type' => 'fieldset',
'#title' => t('SugarCRM Settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -2,
);
$sugar_form['sugarsettings']['sugarcrm_action'] = array(
'#type' => 'textfield',
'#title' => t('Sugar Form is posted to'),
'#default_value' => empty($form['#node']->webform['sugarsettings']['sugarcrm_action']) ? '' : $form['#node']->webform['sugarsettings']['sugarcrm_action'],
'#description' => t('The URL the form will be posted to. The confirmation option above MUST be a redirect URL in order to return from SugarCRM.'),
);
$sugar_form['sugarsettings']['sugarcrm_lead_source'] = array(
'#type' => 'textfield',
'#title' => t('Lead Source'),
'#default_value' => empty($form['#node']->webform['sugarsettings']['sugarcrm_lead_source']) ? '' : $form['#node']->webform['sugarsettings']['sugarcrm_lead_source'],
'#description' => t('The name SugarCRM uses to identify the source of the lead, eg. Web Site.'),
);
$sugar_form['sugarsettings']['sugarcrm_user'] = array(
'#type' => 'textfield',
'#title' => t('SugarCRM User'),
'#default_value' => empty($form['#node']->webform['sugarsettings']['sugarcrm_user']) ? '' : $form['#node']->webform['sugarsettings']['sugarcrm_user'],
'#description' => t('The name of the user on the SugarCRM System the form uses to connect.'),
);
/* End SugarCRM settings form */
// integrate the sugar form into the form
$webform_fieldset = $form['webform'];
$pos = array_search('mail_settings', array_keys($webform_fieldset)) + 1;
$form['webform'] = array_merge(array_slice($webform_fieldset, 0, $pos), $sugar_form, array_slice($webform_fieldset, $pos));
}
// handle editing fields on the webform
elseif ( $form_id = 'webform_component_edit_form' ) {
// debug output shows that the node is the third parameter -- fragile but the only way we can access it
$node = $form['#parameters'][2];
// if the user has specified the SugarCRM action, then we will be posting the form to SugarCRM
if ( !empty($node->webform['sugarsettings']['sugarcrm_action']) && empty($form_state['post']) && !empty($form['form_key']) ) {
include_once(drupal_get_path('module', 'sugarwebform') . "/sugarwebform_fields.inc");
$new_form_key = array(
'#type' => 'select',
'#title' => $form['form_key']['#title'],
'#multiple' => FALSE,
'#options' => array_merge($sugarwebform_available_fields, load_custom_fields()),
'#description' => t('Select a SugarCRM field that this form field will map to. If you switch the form to an email for this will be used as the machine readable key.'),
'#weight' => $form['form_key']['#weight'],
'#default_value' => empty($form['form_key']['#default_value']) ? '' : $form['form_key']['#default_value'],
);
$form['form_key'] = $new_form_key;
}
}
}
/**
* Intercept operations on the webform node to assure that the SugarCRM fields are tracked.
*/
function sugarwebform_nodeapi(&$node, $op, $form = NULL, $page = NULL) {
if ( $node->type == 'webform') {
switch ($op) {
case 'insert':
case 'update':
if ( !empty($node->webform['sugarsettings']['sugarcrm_action']) ) {
// store the SugarCRM fields
$result = db_query("DELETE FROM {sugarwebform} where vid = %d", $node->vid);
$result = db_query("INSERT INTO {sugarwebform} (vid, action, sugar_user, lead_source) " .
"VALUES (%d, '%s', '%s', '%s')", $node->vid, $node->webform['sugarsettings']['sugarcrm_action'],
$node->webform['sugarsettings']['sugarcrm_user'], $node->webform['sugarsettings']['sugarcrm_lead_source']);
}
break;
case 'validate':
if ( !empty($node->webform['sugarcrm_action']) ) {
if (!valid_url(trim($node->sugarcrm_action), true)) {
form_set_error('sugarcrm_action', t('The SugarCRM action must be the valid URL to your SugarCRM lead capture page'));
}
if ( empty($node->webform['sugarcrm_user']) ) {
form_set_error('sugarcrm_user', t('To submit a form to SugarCRM you must specify the SugarCRM user'));
}
if ( empty($node->webform['sugarcrm_lead_source']) ) {
form_set_error('sugarcrm_lead_source', t('To submit a form to SugarCRM you must specify the lead source'));
}
}
break;
case 'load':
$result = db_query("SELECT action, sugar_user, lead_source from {sugarwebform} ".
"WHERE vid = %d", $node->vid);
// either their is 0 or 1, if we have one then add it to the node
$row = db_fetch_array($result);
if ( !empty($row) ) {
$node->webform['sugarsettings']['sugarcrm_action'] = $row['action'];
$node->webform['sugarsettings']['sugarcrm_user'] = $row['sugar_user'];
$node->webform['sugarsettings']['sugarcrm_lead_source'] = $row['lead_source'];
}
break;
}
}
}
/**
* Add menu item for administering the module.
*/
function sugarwebform_menu() {
$items['admin/settings/sugarwebform'] = array(
'title' => t('SugarCRM Webform'),
'description' => t('Configure SugarCRM webform integration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('sugarwebform_admin_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
function sugarwebform_admin_settings() {
$form['field_settings'] = array(
'#type' => 'fieldset',
'#title' => t('SugarCRM custom field settings'),
'#description' => t('Allows you to configure your custom field settings to show in the field selection for SugarCRM webforms'),
);
$form['field_settings']['sugarwebform_custom_fields'] = array(
'#type' => 'textarea',
'#title' => t("Custom Fields"),
'#default_value' => create_custom_field_spec(variable_get('sugarwebform_custom_fields', null)),
'#description' => t('A list of additional fields configured for leads on your instances of SugarCRM. One field per line, with field name and description seperated by pipes. i.e. safe_key|Some readable option'),
'#rows' => 5,
'#weight' => -2,
);
$form['#submit'] = array ('parse_custom_field_spec');
return system_settings_form($form);
}
function parse_custom_field_spec ($form, &$form_state) {
$custom_fields = array();
$custom_field_spec = $form_state['values']['sugarwebform_custom_fields'];
if ( !empty($custom_field_spec) ) {
$custom_field_tmp = explode("\n", $custom_field_spec);
foreach ( $custom_field_tmp as $field ) {
if ( empty($field) ) break ;
$new_field = explode("|", $field);
$custom_fields[$new_field[0]] = $new_field[1];
}
}
$form_state['values']['sugarwebform_custom_fields'] = $custom_fields;
}
function create_custom_field_spec ($custom_fields) {
if ( empty($custom_fields) ) {
return "";
}
$spec = "";
foreach ( array_keys($custom_fields) as $field ) {
$field_string = $field . "|" . $custom_fields[$field] . "\n";
$spec .= $field_string;
}
return $spec;
}