sugarcrm_action) ) {
return ;
}
//If the node has a sugar action, add an additional submit handler to the form.
if ($node->sugarcrm_action){
$form['#submit'][] = 'sugarwebform_submit_form';
//Add the target
$form['sugarcrm_action'] = array(
'#type' => 'hidden',
'#value' => $node->sugarcrm_action,
);
}
// add the configured properties to set the lead source and the SugarCRM user
$form['lead_source'] = array(
'#type' => 'hidden',
'#value' => $node->sugarcrm_lead_source,
);
$form['user'] = array(
'#type' => 'hidden',
'#value' => $node->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', NULL, NULL, 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['mailsettings']['#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']->sugarcrm_action) ? '' : $form['#node']->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']->sugarcrm_lead_source) ? '' : $form['#node']->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']->sugarcrm_user) ? '' : $form['#node']->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
$pos = array_search('mailsettings', array_keys($form)) + 1;
$form = array_merge(array_slice($form, 0, $pos), $sugar_form, array_slice($form, $pos));
}
// handle editing fields on the webform
elseif ( $form_id = 'webform_edit_field_form' ) {
// if the user has specified the SugarCRM action, then we will be posting the form to SugarCRM
if ( !empty($form['node']['sugarcrm_action']['#value']) ) {
include_once(drupal_get_path('module', 'sugarwebform') . "/sugarwebform_fields.inc");
$new_form_key = array(
'#type' => 'select',
'#title' => $form['field']['form_key']['#title'],
'#multiple' => FALSE,
'#options' => array_merge($sugarwebform_available_fields, sugarwebform_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['field']['form_key']['#weight'],
'#default_value' => empty($form['field']['form_key']['#weight']) ? '' : $form['field']['form_key']['#weight'],
);
$form['field']['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->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->sugarcrm_action, $node->sugarcrm_user, $node->sugarcrm_lead_source);
}
break;
case 'validate':
if ( !empty($node->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->sugarcrm_user) ) {
form_set_error('sugarcrm_user', t('To submit a form to SugarCRM you must specify the SugarCRM user'));
}
if ( empty($node->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->sugarcrm_action = $row['action'];
$node->sugarcrm_user = $row['sugar_user'];
$node->sugarcrm_lead_source = $row['lead_source'];
}
break;
}
}
}
/**
* Add menu item for administering the module.
*/
function sugarwebform_menu() {
$items['admin/settings/sugarwebform'] = array(
'title' => 'SugarCRM Webform',
'description' => 'Configure SugarCRM webform integration',
'page callback' => 'drupal_get_form',
'page arguments' => array('sugarwebform_admin_settings'),
'access callback' => 'user_access',
'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['#validate'] = array ('parse_custom_field_spec' => $form);
return system_settings_form($form);
}
//function parse_custom_field_spec ($form_id, $form_values, $form) {
// $custom_fields = array();
// $custom_field_spec = $form_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_set_value($form['field_settings']['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;
}
/**
* Hanlde the submit of the webform - ping a POST request over to sugar
*/
function sugarwebform_submit_form($id,$values){
$url = $values['sugarcrm_action']; $and_sign = false;
$data = sugarwebform_flatten_form($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;
}