Index: sugarwebform.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/sugarwebform/sugarwebform.module,v retrieving revision 1.1.2.2 diff -u -p -r1.1.2.2 sugarwebform.module --- sugarwebform.module 8 May 2008 02:29:56 -0000 1.1.2.2 +++ sugarwebform.module 1 Jan 2009 09:25:25 -0000 @@ -19,12 +19,18 @@ function sugarwebform_form_alter($form_i 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']='sugarwebform_submit_form'; + + //Add the target + $form['sugarcrm_action'] = array( + '#type' => 'hidden', + '#value' => $node->sugarcrm_action, + ); + } - // change the action to submit directly to sugar - $form['#action'] = url($node->sugarcrm_action); - // flatten the submitted fields so that sugar can handle them - $form['submitted']['#tree'] = FALSE; // add the configured properties to set the lead source and the SugarCRM user $form['lead_source'] = array( @@ -251,5 +257,44 @@ function create_custom_field_spec ($cust 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; +}