You are here

function _eloqua_send_posts in Eloqua 7.2

Send posts to Eloqua

Parameters

object $post: Saved post.

2 calls to _eloqua_send_posts()
eloqua_webform_cron in eloqua_webform/eloqua_webform.module
Implements hook_cron().
eloqua_webform_submit in eloqua_webform/eloqua_webform.module
Form submission handler for eloqua_admin_form().

File

eloqua_webform/eloqua_webform.cron.inc, line 16
Cron Support Functions for Eloqua

Code

function _eloqua_send_posts($post = NULL) {
  $posts = array();

  // If post passed into this function, only process this post.
  if ($post) {
    $posts = array(
      $post,
    );
  }
  else {
    $result_set = db_select('eloqua_saved_posts')
      ->fields('eloqua_saved_posts')
      ->condition('status', ELOQUA_STATUS_NEW)
      ->range(0, (int) variable_get('batch_size', 50))
      ->execute()
      ->fetchAll();
    $posts = _eloqua_unserialize_data_column($result_set);
  }

  // If nothnig to do, or something funky happened, bail.
  if (empty($posts) || !is_array($posts)) {
    return;
  }
  foreach ($posts as $post) {
    if (!is_array($post->data)) {
      $post->data = array();
    }

    // Check for 1x submissions.
    if (!empty($post->data['form_post']['submitted'])) {
      watchdog('eloqua', 'Uanble to send post id !post_id in !module_name. You ' . 'will need to revert to the 1x version of this module to send this post.', array(
        '!module_name' => __FUNCTION__,
        '!post_id' => $post->post_id,
      ), WATCHDOG_DEBUG);
      continue;
    }
    if (!empty($post->data['form_post']['elqSiteId'])) {
      $elqSiteId = $post->data['form_post']['elqSiteId'];
    }
    else {

      // Bail if no site id, because we can't generate proper url without it.
      return;
    }
    $url = 'http://s' . $elqSiteId . '.t.eloqua.com/e/f2';
    $original_headers = $post->data['user_headers'];
    $headers = array(
      'Accept-Language' => array_key_exists('accept-language', $original_headers) ? $original_headers['accept-language'] : 'en',
      'User-Agent' => array_key_exists('user-agent', $original_headers) ? $original_headers['user-agent'] : 'User Relay',
      'Content-Type' => 'application/x-www-form-urlencoded',
    );

    // Fetch the post fields to send to Eloqua.
    $post_fields = $post->data['form_post'];
    $options = array(
      'method' => 'POST',
      'timeout' => 5,
      'headers' => $headers,
      'data' => drupal_http_build_query($post_fields),
    );
    $result = drupal_http_request($url, $options);

    // Update post data.
    $post->data['server_post'][] = array(
      'timestamp' => time(),
      'response' => $result,
      'http_status' => $result->code,
    );
    if ($result->code == '200') {
      $post->{'status'} = ELOQUA_STATUS_UPLOADED;
    }
    else {
      $post->{'status'} = ELOQUA_STATUS_FAILED;
    }
    eloqua_post_update($post);
  }
}