You are here

webform_remote_post.module in Webform Remote Post 6

Same filename and directory in other branches
  1. 7.2 webform_remote_post.module
  2. 7 webform_remote_post.module

Webform Remote Post module.

Webform Remote Post is a module that works along the Webform module. It eases the integration between Webforms and other web applications.

File

webform_remote_post.module
View source
<?php

/**
 * @file
 * Webform Remote Post module.
 *
 * Webform Remote Post is a module that works along the
 * @link http://drupal.org/project/webform Webform @endlink module.
 * It eases the integration between Webforms and other web
 * applications.
 */

/**
 * Implements hook_help().
 */
function webform_remote_post_help($path, $arg) {
  switch ($path) {
    case 'admin/help#webform_remote_post':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Webform Remote Post is a module that works along
                            with the Webform module. It eases the integration
                            between Webforms and other web applications like
                            Salesforce and Eloqua.') . '</p>';
      $output .= '<p>' . t('Webform Remote Post works by POSTing form
                            submissions to any arbitrary URL, presumably
                            an application or script that will use the form
                            data and perform further processing of it. It
                            respects the form\'s validation and will only
                            send submissions that passed validation and
                            are no longer in a draft state. Multiple
                            remote posts can be set up for each individual
                            form, allowing for the submission of data
                            to multiple systems at once.') . '</p>';
      $output .= '<p>' . t('Webform Remote Post will attempt POSTing until
                            sucessful three times before failing, and each
                            attempt is configured to timeout after 15 seconds.') . '</p>';
      $output .= '<h3>' . t('Use Cases') . '</h3>';
      $output .= '<ul>';
      $output .= '<li>' . t('CRM Integration &ndash; If you have a CRM like
                             Salesforce you can use this module to push
                             submissions using the web-to-lead mechanism to
                             create a Lead from every Webform submission in
                             your Drupal site.') . '</li>';
      $output .= '<li>' . t('Eloqua Integration &ndash; Create lead forms
                             to be submitted to Eloqua. Add the hidden fields
                             as indicated in Eloqua and it\'s ready to go.') . '</li>';
      $output .= '<li>' . t('Re-posting to any 3rd party system &ndash; This is
                             a general-purpose module . Do you need the form
                             data to be immediately submitted to another system
                             automatically? Add a remote post target to it!') . '</li>';
      $output .= '</ul>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 *
 * @see webform_menu_load()
 */
function webform_remote_post_menu() {
  $items = array();

  // Lists remote post targets associated with a given webform.
  $items['node/%webform_menu/webform/targets'] = array(
    'title' => 'Remote Post Targets',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'webform_remote_post_targets_form',
      1,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'admin webform remote posts',
    ),
    'file' => 'includes/webform_remote_post.targets.inc',
    'weight' => 1,
    'type' => MENU_LOCAL_TASK,
  );

  // Enables a remote post target.
  $items['node/%webform_menu/webform/targets/%/enable'] = array(
    'title' => 'Update Remote Post Target',
    'load arguments' => array(
      1,
    ),
    'page arguments' => array(
      'webform_remote_post_targets_update',
      1,
      4,
      'enable',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'admin webform remote posts',
    ),
    'type' => MENU_CALLBACK,
  );

  // Disables a remote post target.
  $items['node/%webform_menu/webform/targets/%/disable'] = array(
    'title' => 'Update Remote Post Target',
    'load arguments' => array(
      1,
    ),
    'page arguments' => array(
      'webform_remote_post_targets_update',
      1,
      4,
      'disable',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'admin webform remote posts',
    ),
    'type' => MENU_CALLBACK,
  );

  // Deletes a remote post target.
  $items['node/%webform_menu/webform/targets/%/delete'] = array(
    'title' => 'Delete remote post target',
    'load arguments' => array(
      1,
    ),
    'page arguments' => array(
      'webform_remote_post_targets_delete',
      1,
      4,
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'admin webform remote posts',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_webform_submission_insert().
 *
 * Respond to a Webform submission being inserted.
 *
 * Note that this hook is called after a submission has already been saved to
 * the database.
 *
 * This is the main purpose of this module, to emit HTTP POSTs based on the
 * saved webform data.
 *
 * @todo Support of Grid component.
 * @todo Support of File component.
 */
function webform_remote_post_webform_submission_insert($node, $submission) {

  // Skips forms in draft.
  if ($submission->is_draft) {
    return;
  }

  // Retrieves the associated remote post targets from the database.
  $query = db_query('SELECT * FROM {webform_remote_post_targets} WHERE nid = %d AND enabled = 1', $node->nid);
  $targets = array();
  while ($target = db_fetch_array($query)) {
    $targets[$target['tid']] = $target;
  }

  // Creates an array of webform components and component IDs.
  $component_map = array();
  foreach ($node->webform['components'] as $component) {
    $component_map[$component['cid']] = $component;
  }

  // Prepares the submission data for remote posting.
  $payload = array();
  foreach ($submission->data as $cid => $component_data) {
    $payload[$component_map[$cid]['form_key']] = implode(', ', $component_data['value']);
  }

  // Lists acceptable server response codes.
  $benign_reponse_codes = array(
    '200',
    '201',
    '202',
    '204',
    '301',
    '302',
    '307',
  );

  // Posts submission data to each remote post target.
  foreach ($targets as $tid => $target) {
    if ($target['type'] == 'json') {
      $post_data = json_encode($payload);
      $headers = array(
        'Content-Type' => 'application/json',
        'Accept' => '*/*',
      );
    }
    else {
      $post_data = http_build_query($payload, '', '&');
      $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
      );
    }
    $request = drupal_http_request($target['url'], $headers, 'POST', $post_data, 3, 15);

    // Logs errors.
    if (isset($request->code) && !in_array($request->code, $benign_reponse_codes)) {
      $log_msg = 'A remote (%type) post to %url by webform node ID %id returned a ';
      $log_msg .= '\'%code\' code, which is a different HTTP response code ';
      $log_msg .= 'than expected. Please make sure that the remote post URL ';
      $log_msg .= 'is correct in the Remote Posts webform settings, and that ';
      $log_msg .= 'the post was received in the remote system.';
      watchdog('webform_remote_post', $log_msg, array(
        '%id' => $node->nid,
        '%url' => $target['url'],
        '%code' => $request->code,
        '%type' => $target['type'],
      ), WATCHDOG_WARNING);
    }
  }
}

/**
 * Implements hook_perm().
 */
function webform_remote_post_perm() {
  return array(
    'admin webform remote posts',
  );
}

/**
 * Implements hook_theme().
 */
function webform_remote_post_theme($existing, $type, $theme, $path) {
  $theme = array(
    // webform_remote_posts.targets.inc.
    'webform_remote_post_targets_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'includes/webform_remote_post.targets.inc',
    ),
  );
  return $theme;
}