You are here

function hubspot_webform_submission_insert in HubSpot 6

Same name and namespace in other branches
  1. 6.2 hubspot.module \hubspot_webform_submission_insert()
  2. 7 hubspot.module \hubspot_webform_submission_insert()

Intercepts the WebForm submission and send it off to HubSpot. Implements hook_webform_submission_insert().

File

./hubspot.module, line 30
Sends Webform results to HubSpot's Leads API by using Webform's provided hooks.

Code

function hubspot_webform_submission_insert($node, $submission) {
  if ($submission->is_draft == 1) {
    return;
  }
  foreach ($submission->data as $cid => $data) {
    if ($node->webform['components'][$cid]['type'] == 'hubspot_url') {
      $hubSpotURL = $data['value'][0];
      if (isset($node->webform['components'][$cid]['extra']['custom_fields'])) {
        $fields = array_filter($node->webform['components'][$cid]['extra']['custom_fields']);
      }
      else {
        $fields = array();
        foreach ($node->webform['components'] as $k => $v) {
          if ($cid != intval($v['cid'])) {
            $fields[$v['cid']] = $v['cid'];
          }
        }
      }
      $values = array();
      foreach ($fields as $cid) {
        $component = $node->webform['components'][$cid]['form_key'];
        $values[$component] = implode(',', $submission->data[$cid]['value']);
      }

      // These fields must be submitted with each request
      $values['UserToken'] = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : '';
      $values['IPAddress'] = ip_address();
      $r = hubspot_insert_lead($hubSpotURL, $values);
      if (empty($r['Error']) && strpos($r['Data'], 'successfully') !== FALSE) {
        watchdog('hubspot', 'Webform "%form" results succesfully submitted to HubSpot. Response: @msg', array(
          '@msg' => strip_tags($r['Data']),
          '%form' => $node->title,
        ), WATCHDOG_INFO);
      }
      elseif (!empty($r['Error'])) {
        watchdog('hubspot', 'HTTP error when submitting HubSpot data from Webform "%form": @error', array(
          '@error' => $r['Error'],
          '%form' => $node->title,
        ), WATCHDOG_ERROR);
        if (variable_get('hubspot_debug_on', 0)) {
          drupal_mail('hubspot', 'http_error', variable_get('hubspot_debug_email', variable_get('site_mail', '')), language_default(), array(
            'errormsg' => $r['Error'],
            'hubspot_url' => $hubSpotURL,
            'node_title' => $node->title,
          ), variable_get('site_mail', ''));
        }
      }
      else {
        watchdog('hubspot', 'HubSpot error when submitting Webform "%form": @error', array(
          '@error' => $r['Data'],
          '%form' => $node->title,
        ), WATCHDOG_ERROR);
        if (variable_get('hubspot_debug_on', 0)) {
          drupal_mail('hubspot', 'hub_error', variable_get('hubspot_debug_email', variable_get('site_mail', '')), language_default(), array(
            'errormsg' => $r['Data'],
            'hubspot_url' => $hubSpotURL,
            'node_title' => $node->title,
          ), variable_get('site_mail', ''));
        }
      }
    }
  }
}