You are here

eloqua.inc in Eloqua 6

File

eloqua.inc
View source
<?php

/**
 * Table schema for saved posts
 */
define('ELOQUA_SCHEMA_SAVED_POSTS', 'eloqua_saved_posts');

/**
 * Table schemo for eloqua enabled webform data
 */
define('ELOQUA_SCHEMA_WEBFORM', 'eloqua_webform');
define('ELOQUA_POST_FIELD_STATUS', 'status');
define('ELOQUA_POST_FIELD_DATA', 'data');
define('ELOQUA_POST_FIELD_FORM_ID', 'form_id');
define('ELOQUA_POST_FIELD_POST_TIME', 'post_time');
define('ELOQUA_POST_FIELD_POST_ID', 'post_id');
define('ELOQUA_WEBFORM_FIELD_NODE_ID', 'nid');
define('ELOQUA_WEBFORM_FIELD_ACTIVE', 'is_active');
define('ELOQUA_WEBFORM_FIELD_FORM_NAME', 'form_name');
define('ELOQUA_WEBFORM_FIELD_REQUEST_NAME', 'request_name');
define('ELOQUA_WEBFORM_FIELD_DATA', 'data');
define('ELOQUA_SETTINGS_FIELD_KEY', 'e_key');
define('ELOQUA_SETTINGS_FIELD_DATA', 'data');
define('ELOQUA_SETTINGS_BATCH_SIZE', 'batch_size');
define('ELOQUA_SETTINGS_BATCH_SIZE_DEFAULT', 50);
define('ELOQUA_STATUS_NEW', 'new');
define('ELOQUA_STATUS_FAILED', 'failed');
define('ELOQUA_STATUS_UPLOADED', 'uploaded');

/**
 * Drupal variable constant that defines the location of the js files
 */
define('ELOQUA_VARIABLE_SCRIPTS_DIRECTORY', 'eloqua_scripts_directory');

/**
 * Default script location value
 */
define('ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT', 'sites/all/libraries/elqNow');

/**
 * Last post time to Eloqua
 */
define('ELOQUA_VARIABLE_LAST_POST_TIME', 'eloqua_last_post_time');

/**
 * Default value for last post time.
 */
define('ELOQUA_VARIABLE_LAST_POST_TIME_DEFAULT', '0');

/**
 * Eloqua Posting Interval
 */
define('ELOQUA_VARIABLE_POST_INTERVAL', 'eloqua_post_interval');

/**
 * Eloqua Site ID
 */
define('ELOQUA_VARIABLE_SITE_ID', 'eloqua_site_id');

/**
 * Default Elqoqua Site ID
 */
define('ELOQUA_VARIABLE_SITE_ID_DEFAULT', 0);

/**
 * Default Eloqua Posting Interval
 */
define('ELOQUA_VARIABLE_POST_INTERVAL_DEFAULT', 86400);
define('ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA', 'unserialize_data');

/**
 * Watchdog Category Key
 */
define('ELOQUA_WATCHDOG_CATEGORY', 'eloqua');

////////////////////////////////////////////////////////////////////////////////

//  VARIABLE ACCESS FUNCTIONS

/**
 * Returns the last post time to the Eloqua System
 * @return int
 *  Timestamp
 */
function eloqua_get_last_post_time() {
  return variable_get(ELOQUA_VARIABLE_LAST_POST_TIME, ELOQUA_VARIABLE_LAST_POST_TIME_DEFAULT);
}

/**
 * Sets the last post time
 * @param $value int
 *  Timestamp
 * @return boolean
 *  Successful update
 */
function eloqua_set_last_post_time($value) {
  if (!is_numeric($value)) {
    $message = t('Invalid value set for post time: !value');
    $variables = array(
      '!value' => '<pre>' . var_export($value, TRUE) . '</pre>',
    );
    watchdog(ELOQUA_WATCHDOG_CATEGORY, $message, $variables, WATCHDOG_NOTICE);
    return FALSE;
  }
  variable_set(ELOQUA_VARIABLE_LAST_POST_TIME, $value);
  return TRUE;
}

/**
 * Returns the Eloqua Site ID
 * @return int
 */
function eloqua_get_site_id() {
  $site_id = variable_get(ELOQUA_VARIABLE_SITE_ID, ELOQUA_VARIABLE_SITE_ID_DEFAULT);
  return $site_id;
}

/**
 * Gets the update interval
 * @return int
 *   Seconds
 */
function eloqua_get_update_time_interval() {
  return variable_get(ELOQUA_VARIABLE_POST_INTERVAL, ELOQUA_VARIABLE_POST_INTERVAL_DEFAULT);
}

/**
 * Sets the update interval
 * @param $seconds int
 *  Number of seconds to elapse between cron pushes
 * @return boolean
 *  Successful Update
 */
function eloqua_set_update_time_interval($seconds) {
  if (!is_numeric($seconds) || $seconds < 0) {
    $message = t('!method: Invalid value set for post time: !value');
    $variables = array(
      '!method' => __METHOD__,
      '!value' => '<pre>' . var_export($seconds, TRUE) . '</pre>',
    );
    watchdog(ELOQUA_WATCHDOG_CATEGORY, $message, $variables, WATCHDOG_NOTICE);
    return FALSE;
  }
  $seconds = (int) $seconds;
  variable_set(ELOQUA_VARIABLE_POST_INTERVAL, $seconds);
  return TRUE;
}

////////////////////////////////////////////////////////////////////////////////

//  Webform Setting Database Access Functions

/**
 * Loads a webform settings from the database
 * @hook eloqua_form_load ($webform)
 * @param $nid int
 * @return object
 */
function eloqua_webform_load($nid) {

  // Get the raw result from the db
  if (!is_numeric($nid)) {
    $type = gettype($nid);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return NULL;
  }
  $webform = _eloqua_webform_load($nid);
  if (empty($webform)) {
    return NULL;
  }

  // Send the object around to all of its friends
  foreach (module_implements('eloqua_form_load') as $module_name) {
    $method = $module_name . '_eloqua_form_load';
    $method($webform);
  }
  return $webform;
}

/**
 * Creates a webform settings from the database
 * @hook eloqua_form_create
 * @param $webform object
 * @return boolean
 */
function eloqua_webform_create($webform) {
  if (!is_object($webform)) {
    $type = gettype($webform);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )));
    return FALSE;
  }
  $result = _eloqua_webform_create($webform);
  if (!$result) {

    // if we were unable to create the payment plan.  Can't go on.
    return FALSE;
  }
  foreach (module_implements('eloqua_form_create') as $module_name) {
    $method = $module_name . '_eloqua_form_create';
    $method($webform);
  }
  _eloqua_webform_update($webform);
  return $result;
}

/**
 * Updates a webform settings from the database
 * @hook eloqua_form_update
 * @param $webform object
 * @return boolean
 */
function eloqua_webform_update($webform) {
  if (!is_object($webform)) {
    $type = gettype($webform);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return FALSE;
  }

  // Can't update the obvious invalid ppid of '0'...
  if (empty($webform->nid)) {
    return FALSE;
  }
  foreach (module_implements('eloqua_form_update') as $module_name) {
    $method = $module_name . '_eloqua_form_update';
    $method($webform);
  }

  // Save the result to the database
  $result = _eloqua_webform_update($webform);
  return $result;
}

/**
 * Deletes a webform settings object from the database
 * @param $nid integer
 * @return boolean
 */
function eloqua_webform_delete($nid) {
  if (!is_numeric($nid)) {
    $type = gettype($nid);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return FALSE;
  }

  // Can't update the obvious invalid ppid of '0'...
  if (empty($nid)) {
    return FALSE;
  }
  foreach (module_implements('eloqua_form_delete') as $module_name) {
    $method = $module_name . '_eloqua_form_delete';
    $method($nid);
  }
  $result = _eloqua_webform_delete($nid);
  return $result;
}

/**
 * Loads the webform object from the database
 * @param $nid int
 * @return object
 */
function _eloqua_webform_load($nid) {
  $sql = 'SELECT * FROM ' . ELOQUA_SCHEMA_WEBFORM . ' WHERE ' . ELOQUA_WEBFORM_FIELD_NODE_ID . ' = %d';
  $params = array(
    $nid,
  );
  $result = _eloqua_select_query($sql, $params, array(
    ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA => TRUE,
  ));
  return $result;
}

/**
 * Creates the webform object from the database
 * @param $webform
 * @return boolean
 */
function _eloqua_webform_create($webform) {
  $sql = 'INSERT INTO ' . ELOQUA_SCHEMA_WEBFORM . ' ( ' . ELOQUA_WEBFORM_FIELD_NODE_ID . ', ' . ELOQUA_WEBFORM_FIELD_FORM_NAME . ', ' . ELOQUA_WEBFORM_FIELD_ACTIVE . ', ' . ELOQUA_WEBFORM_FIELD_DATA . ' ) ' . ' VALUES ' . " (%d, '%s', %d, '%s') " . 'ON DUPLICATE KEY UPDATE ' . ELOQUA_WEBFORM_FIELD_FORM_NAME . " = '%s'," . ELOQUA_WEBFORM_FIELD_ACTIVE . ' = %d,' . ELOQUA_WEBFORM_FIELD_DATA . " = '%s'";
  $params = array(
    $webform->{ELOQUA_WEBFORM_FIELD_NODE_ID},
    $webform->{ELOQUA_WEBFORM_FIELD_FORM_NAME},
    $webform->{ELOQUA_WEBFORM_FIELD_ACTIVE},
    serialize($webform->{ELOQUA_WEBFORM_FIELD_DATA}),
    $webform->{ELOQUA_WEBFORM_FIELD_FORM_NAME},
    $webform->{ELOQUA_WEBFORM_FIELD_ACTIVE},
    serialize($webform->{ELOQUA_WEBFORM_FIELD_DATA}),
  );
  $result = _eloqua_insert_query($sql, $params);
  return $result;
}

/**
 * Updates the webform object from the database
 * @param $webform object
 * @return boolean
 */
function _eloqua_webform_update($webform) {
  $sql = 'UPDATE ' . ELOQUA_SCHEMA_WEBFORM . ' SET ' . ELOQUA_WEBFORM_FIELD_FORM_NAME . " = '%s'," . ELOQUA_WEBFORM_FIELD_ACTIVE . ' = %d, ' . ELOQUA_WEBFORM_FIELD_DATA . " = '%s'" . ' WHERE ' . ELOQUA_WEBFORM_FIELD_NODE_ID . ' = %d';
  $params = array(
    $webform->{ELOQUA_WEBFORM_FIELD_FORM_NAME},
    $webform->{ELOQUA_WEBFORM_FIELD_ACTIVE},
    serialize($webform->{ELOQUA_WEBFORM_FIELD_DATA}),
    $webform->{ELOQUA_WEBFORM_FIELD_NODE_ID},
  );
  $result = _eloqua_modify_query($sql, $params);
  return $result;
}

/**
 * Deletes a webform object from the database
 * @param $nid int
 * @return boolean
 */
function _eloqua_webform_delete($nid) {
  $sql = 'DELETE FROM ' . ELOQUA_SCHEMA_WEBFORM . ' WHERE ' . ELOQUA_WEBFORM_FIELD_NODE_ID . ' = %d';
  $params = array(
    $nid,
  );
  $result = _eloqua_modify_query($sql, $params);
  return $result;
}

/**
 * Returns the defined scripts directory
 * @return int
 */
function eloqua_get_scripts_directory() {
  $value = variable_get(ELOQUA_VARIABLE_SCRIPTS_DIRECTORY, ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT);
  return $value;
}

/**
 * Returns the cron processing batch size
 * @return int
 */
function eloqua_get_batch_size() {
  $batch_size = (int) variable_get(ELOQUA_SETTINGS_BATCH_SIZE, ELOQUA_SETTINGS_BATCH_SIZE_DEFAULT);
  return $batch_size;
}

////////////////////////////////////////////////////////////////////////////////

//  Completed Post Handling
function eloqua_post_get_batch() {
  $sql = "SELECT * FROM " . ELOQUA_SCHEMA_SAVED_POSTS . " WHERE " . ELOQUA_POST_FIELD_STATUS . " = '%s'" . " LIMIT %d";
  $batch_size = eloqua_get_batch_size();
  $params = array(
    ELOQUA_STATUS_NEW,
    $batch_size,
  );
  $options = array(
    ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA => TRUE,
  );
  $result = _eloqua_select_all_query($sql, $params, $options);
  return $result;
}

/**
 * Loads a post form the database
 * @hook eloqua_post_load ($post)
 * @param $post_id int
 * @return object
 */
function eloqua_post_load($post_id) {

  // Get the raw result from the db
  if (!is_numeric($post_id)) {
    $type = gettype($post_id);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return NULL;
  }
  $post = _eloqua_post_load($post_id);
  if (empty($post)) {
    return NULL;
  }

  // Send the object around to all of its friends
  foreach (module_implements('eloqua_post_load') as $module_name) {
    $method = $module_name . '_eloqua_post_load';
    $method($post);
  }
  return $post;
}

/**
 * Creates a post into the database
 * @hook eloqua_post_create
 * @param $post object
 * @return int
 *  Post ID
 */
function eloqua_post_create($post) {
  if (!is_object($post)) {
    $type = gettype($post);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )));
    return FALSE;
  }
  $result = _eloqua_post_create($post);
  if (!$result) {

    // if we were unable to create the payment plan.  Can't go on.
    return FALSE;
  }
  $post->{ELOQUA_POST_FIELD_POST_ID} = $result;
  foreach (module_implements('eloqua_post_create') as $module_name) {
    $method = $module_name . '_eloqua_post_create';
    $method($post);
  }
  _eloqua_post_update($post);
  return $result;
}

/**
 * Updates a post from the database
 * @hook eloqua_post_update
 * @param $post object
 * @return boolean
 */
function eloqua_post_update($post) {
  if (!is_object($post)) {
    $type = gettype($post);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return FALSE;
  }

  // Can't update the obvious invalid ppid of '0'...
  if (empty($post->{ELOQUA_POST_FIELD_POST_ID})) {
    return FALSE;
  }
  foreach (module_implements('eloqua_post_update') as $module_name) {
    $method = $module_name . '_eloqua_post_update';
    $method($post);
  }

  // Save the result to the database
  $result = _eloqua_post_update($post);
  return $result;
}

/**
 * Deletes a post object from the database
 * @param $post_id integer
 * @return boolean
 */
function eloqua_post_delete($post_id) {
  if (!is_numeric($post_id)) {
    $type = gettype($post_id);
    watchdog(ELOQUA_WATCHDOG_CATEGORY, t('Invalid argument sent to !module_name (!type).', array(
      '!module_name' => __FUNCTION__,
      '!type' => $type,
    )), array(), WATCHDOG_DEBUG);
    return FALSE;
  }

  // Can't update the obvious invalid ppid of '0'...
  if (empty($post_id)) {
    return FALSE;
  }
  foreach (module_implements('eloqua_post_delete') as $module_name) {
    $method = $module_name . '_eloqua_post_delete';
    $method($post_id);
  }
  $result = _eloqua_post_delete($post_id);
  return $result;
}

/**
 * Loads the post object from the database
 * @param $post_id int
 * @return object
 */
function _eloqua_post_load($post_id) {
  $sql = 'SELECT * FROM ' . ELOQUA_SCHEMA_SAVED_POSTS . ' WHERE ' . ELOQUA_POST_FIELD_POST_ID . ' = %d';
  $params = array(
    $post_id,
  );
  $result = _eloqua_select_query($sql, $params, array(
    ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA => TRUE,
  ));
  return $result;
}

/**
 * Creates the post object from the database
 * @param $post
 * @return int
 *  Newly created Post_id
 */
function _eloqua_post_create($post) {
  $sql = 'INSERT INTO ' . ELOQUA_SCHEMA_SAVED_POSTS . ' ( ' . ELOQUA_POST_FIELD_FORM_ID . ', ' . ELOQUA_POST_FIELD_POST_TIME . ', ' . ELOQUA_POST_FIELD_STATUS . ', ' . ELOQUA_POST_FIELD_DATA . ' ) ' . ' VALUES ' . " ('%s', %d, '%s', '%s') ";
  $params = array(
    $post->{ELOQUA_POST_FIELD_FORM_ID},
    $post->{ELOQUA_POST_FIELD_POST_TIME},
    $post->{ELOQUA_POST_FIELD_STATUS},
    serialize($post->{ELOQUA_POST_FIELD_DATA}),
  );
  $result = _eloqua_insert_query($sql, $params, ELOQUA_SCHEMA_SAVED_POSTS, ELOQUA_POST_FIELD_POST_ID);
  return $result;
}

/**
 * Updates the post object from the database
 * @param $post object
 * @return boolean
 */
function _eloqua_post_update($post) {
  $sql = 'UPDATE ' . ELOQUA_SCHEMA_SAVED_POSTS . ' SET ' . ELOQUA_POST_FIELD_FORM_ID . " = '%s'," . ELOQUA_POST_FIELD_POST_TIME . ' = %d, ' . ELOQUA_POST_FIELD_STATUS . " = '%s', " . ELOQUA_POST_FIELD_DATA . " = '%s' " . ' WHERE ' . ELOQUA_POST_FIELD_POST_ID . ' = %d';
  $params = array(
    $post->{ELOQUA_POST_FIELD_FORM_ID},
    $post->{ELOQUA_POST_FIELD_POST_TIME},
    $post->{ELOQUA_POST_FIELD_STATUS},
    serialize($post->{ELOQUA_POST_FIELD_DATA}),
    $post->{ELOQUA_POST_FIELD_POST_ID},
  );
  $result = _eloqua_modify_query($sql, $params);
  return $result;
}

/**
 * Deletes a post object from the database
 * @param $post_id int
 * @return boolean
 */
function _eloqua_post_delete($post_id) {
  $sql = 'DELETE FROM ' . ELOQUA_SCHEMA_SAVED_POSTS . ' WHERE ' . ELOQUA_POST_FIELD_POST_ID . ' = %d';
  $params = array(
    $post_id,
  );
  $result = _eloqua_modify_query($sql, $params);
  return $result;
}

////////////////////////////////////////////////////////////////////////////////

//  The following should probably be made into a module for easy access

/**
 * Inserts data into the database and returns the last insert ID, or TRUE
 * @param $sql string
 *  Query
 * @param $params array
 *  Query Parameters
 * @param $table string
 *  Table
 * @param $pid string|array
 *  Primary Key
 * @return boolean|int
 *  Success status, or the last inserted ID if the Table and Primary Key are filled
 */
function _eloqua_insert_query($sql, $params, $table = NULL, $pid = NULL) {
  $result = db_query($sql, $params);
  if ($result != FALSE) {
    if (!empty($table) && !empty($pid)) {
      $result = db_last_insert_id($table, $pid);
    }
    else {
      $result = db_affected_rows() > 0;
    }
  }
  return $result;
}

/**
 * Updates/Delete data into the database
 * Returns TRUE if the query succeeded and modified at least one row
 * @param $sql string
 *  Query
 * @param $params array
 *  Query Parameters
 * @return boolean
 *  Success Status
 *
 */
function _eloqua_modify_query($sql, $params) {
  $result = db_query($sql, $params);

  // The result must be the post_id or we'll end up with duplicate records in
  //   the eloqua_saved_posts table.
  if ($result === FALSE || $result === NULL) {
    $result = false;
  }
  if ($result) {
    $rows_changed = db_affected_rows();
    $result = $rows_changed > 0;
  }
  return $result;
}

/**
 * Performs a select query returning the result
 * If the option ('data') is set, then will unserialize the data column
 * @param $sql string
 * @param $params array
 * @param $options array
 * @return object|FALSE
 */
function _eloqua_select_query($sql, $params, $options = array()) {
  $result = db_query($sql, $params);
  if ($result !== FALSE) {
    $row = db_fetch_object($result);
    if (!empty($row)) {

      // Capture any unserialization errors, and validate the result
      if (!empty($options[ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA])) {
        $row->data = unserialize($row->data);
        if ($row->data === FALSE) {
          $row->data = new stdClass();
        }
      }
    }
    else {
      $row = FALSE;
    }
  }
  return $row;
}

/**
 * Performs a select query returning the result
 * If the option ('data') is set, then will unserialize the data column
 * @param $sql string
 * @param $params array
 * @param $options array
 * @return object|FALSE
 */
function _eloqua_select_all_query($sql, $params, $options = array()) {
  $result = db_query($sql, $params);
  $rowset = array();
  if ($result !== FALSE) {
    $row = FALSE;
    do {
      $row = db_fetch_object($result);
      if (!empty($row)) {

        // Capture any unserialization errors, and validate the result
        if (!empty($options[ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA])) {
          $row->data = unserialize($row->data);
          if ($row->data === FALSE) {
            $row->data = new stdClass();
          }
        }
        $rowset[] = $row;
      }
    } while (!empty($row));
  }
  return $rowset;
}

// End of file

Functions

Namesort descending Description
eloqua_get_batch_size Returns the cron processing batch size
eloqua_get_last_post_time Returns the last post time to the Eloqua System
eloqua_get_scripts_directory Returns the defined scripts directory
eloqua_get_site_id Returns the Eloqua Site ID
eloqua_get_update_time_interval Gets the update interval
eloqua_post_create Creates a post into the database @hook eloqua_post_create
eloqua_post_delete Deletes a post object from the database
eloqua_post_get_batch
eloqua_post_load Loads a post form the database @hook eloqua_post_load ($post)
eloqua_post_update Updates a post from the database @hook eloqua_post_update
eloqua_set_last_post_time Sets the last post time
eloqua_set_update_time_interval Sets the update interval
eloqua_webform_create Creates a webform settings from the database @hook eloqua_form_create
eloqua_webform_delete Deletes a webform settings object from the database
eloqua_webform_load Loads a webform settings from the database @hook eloqua_form_load ($webform)
eloqua_webform_update Updates a webform settings from the database @hook eloqua_form_update
_eloqua_insert_query Inserts data into the database and returns the last insert ID, or TRUE
_eloqua_modify_query Updates/Delete data into the database Returns TRUE if the query succeeded and modified at least one row
_eloqua_post_create Creates the post object from the database
_eloqua_post_delete Deletes a post object from the database
_eloqua_post_load Loads the post object from the database
_eloqua_post_update Updates the post object from the database
_eloqua_select_all_query Performs a select query returning the result If the option ('data') is set, then will unserialize the data column
_eloqua_select_query Performs a select query returning the result If the option ('data') is set, then will unserialize the data column
_eloqua_webform_create Creates the webform object from the database
_eloqua_webform_delete Deletes a webform object from the database
_eloqua_webform_load Loads the webform object from the database
_eloqua_webform_update Updates the webform object from the database

Constants