View source
<?php
define('ELOQUA_SCHEMA_SAVED_POSTS', 'eloqua_saved_posts');
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');
define('ELOQUA_VARIABLE_SCRIPTS_DIRECTORY', 'eloqua_scripts_directory');
define('ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT', 'sites/all/libraries/elqNow');
define('ELOQUA_VARIABLE_LAST_POST_TIME', 'eloqua_last_post_time');
define('ELOQUA_VARIABLE_LAST_POST_TIME_DEFAULT', '0');
define('ELOQUA_VARIABLE_POST_INTERVAL', 'eloqua_post_interval');
define('ELOQUA_VARIABLE_SITE_ID', 'eloqua_site_id');
define('ELOQUA_VARIABLE_SITE_ID_DEFAULT', 0);
define('ELOQUA_VARIABLE_POST_INTERVAL_DEFAULT', 86400);
define('ELOQUA_QUERY_OPTION_UNSERIALIZE_DATA', 'unserialize_data');
define('ELOQUA_WATCHDOG_CATEGORY', 'eloqua');
function eloqua_get_last_post_time() {
return variable_get(ELOQUA_VARIABLE_LAST_POST_TIME, ELOQUA_VARIABLE_LAST_POST_TIME_DEFAULT);
}
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;
}
function eloqua_get_site_id() {
$site_id = variable_get(ELOQUA_VARIABLE_SITE_ID, ELOQUA_VARIABLE_SITE_ID_DEFAULT);
return $site_id;
}
function eloqua_get_update_time_interval() {
return variable_get(ELOQUA_VARIABLE_POST_INTERVAL, ELOQUA_VARIABLE_POST_INTERVAL_DEFAULT);
}
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;
}
function eloqua_webform_load($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 NULL;
}
$webform = _eloqua_webform_load($nid);
if (empty($webform)) {
return NULL;
}
foreach (module_implements('eloqua_form_load') as $module_name) {
$method = $module_name . '_eloqua_form_load';
$method($webform);
}
return $webform;
}
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) {
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;
}
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;
}
if (empty($webform->nid)) {
return FALSE;
}
foreach (module_implements('eloqua_form_update') as $module_name) {
$method = $module_name . '_eloqua_form_update';
$method($webform);
}
$result = _eloqua_webform_update($webform);
return $result;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
function eloqua_get_scripts_directory() {
$value = variable_get(ELOQUA_VARIABLE_SCRIPTS_DIRECTORY, ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT);
return $value;
}
function eloqua_get_batch_size() {
$batch_size = (int) variable_get(ELOQUA_SETTINGS_BATCH_SIZE, ELOQUA_SETTINGS_BATCH_SIZE_DEFAULT);
return $batch_size;
}
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;
}
function eloqua_post_load($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 NULL;
}
$post = _eloqua_post_load($post_id);
if (empty($post)) {
return NULL;
}
foreach (module_implements('eloqua_post_load') as $module_name) {
$method = $module_name . '_eloqua_post_load';
$method($post);
}
return $post;
}
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) {
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;
}
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;
}
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);
}
$result = _eloqua_post_update($post);
return $result;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
function _eloqua_modify_query($sql, $params) {
$result = db_query($sql, $params);
if ($result === FALSE || $result === NULL) {
$result = false;
}
if ($result) {
$rows_changed = db_affected_rows();
$result = $rows_changed > 0;
}
return $result;
}
function _eloqua_select_query($sql, $params, $options = array()) {
$result = db_query($sql, $params);
if ($result !== FALSE) {
$row = db_fetch_object($result);
if (!empty($row)) {
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;
}
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)) {
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;
}