You are here

function hubspot_webform_submit in HubSpot 7.3

Same name and namespace in other branches
  1. 7.2 hubspot_webform/hubspot_webform.module \hubspot_webform_submit()

Form handler for pardot_webform_form_alter().

Intercepts the WebForm submission and send it off to HubSpot.

1 string reference to 'hubspot_webform_submit'
hubspot_webform_form_alter in hubspot_webform/hubspot_webform.module
Implements hook_form_alter().

File

hubspot_webform/hubspot_webform.module, line 31
Sends Webform results to HubSpot's Forms API.

Code

function hubspot_webform_submit($form, &$form_state) {
  $node = $form['#node'];
  if (empty($node)) {
    return;
  }
  $hubspot_portalid = variable_get('hubspot_portalid', '');
  $hubspot_form_guid = db_query_range("SELECT hubspot_guid FROM {hubspot} h WHERE h.nid = :nid", 0, 1, array(
    ':nid' => $node->nid,
  ))
    ->fetchField();
  if (empty($hubspot_portalid) || empty($hubspot_form_guid)) {
    return;
  }
  $fields = array();
  $sql = "SELECT webform_field, hubspot_field FROM {hubspot} h WHERE h.nid = :nid AND h.hubspot_guid = :guid AND hubspot_field != :empty";
  $result = db_query($sql, array(
    ':nid' => $node->nid,
    ':guid' => $hubspot_form_guid,
    ':empty' => '--donotmap--',
  ));
  $field_mappings = array();
  while ($row = $result
    ->fetchObject()) {
    $field_mappings[$row->webform_field] = array(
      'key' => $row->hubspot_field,
    );
  }
  $post_fields = automate_webform_values($form, $form_state);
  $fields = automate_map_webform_values($post_fields, $field_mappings);

  // These fields must be submitted with each request.
  $hs_context = array(
    'hutk' => isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : '',
    'ipAddress' => ip_address(),
    'pageName' => $node->title,
    'pageUrl' => url('node/' . $node->nid, array(
      'absolute' => TRUE,
    )),
  );
  $fields['hs_context'] = drupal_json_encode($hs_context);
  $result = hubspot_webform_insert_lead($hubspot_portalid, $hubspot_form_guid, $fields);
  if ($result['HTTPCode'] == '204') {
    watchdog('hubspot', 'Webform "%form" results succesfully submitted to HubSpot. Post: @post - Response: @msg', array(
      '@post' => strip_tags($result['POST']),
      '@msg' => strip_tags($result['Data']),
      '%form' => $node->title,
    ), WATCHDOG_INFO);
  }
  elseif (!empty($result['Error'])) {
    watchdog('hubspot', 'HTTP error when submitting HubSpot data from Webform "%form": @error', array(
      '@error' => $result['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' => $result['Error'],
        'hubspot_url' => $hubspot_url,
        'node_title' => $node->title,
      ), variable_get('site_mail', ''));
    }
  }
  else {
    watchdog('hubspot', 'HubSpot error when submitting Webform "%form": @error', array(
      '@error' => $result['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' => $result['Data'],
        'hubspot_url' => $hubspot_url,
        'node_title' => $node->title,
      ), variable_get('site_mail', ''));
    }
  }
}