View source
<?php
module_load_include('inc', 'eloqua');
function eloqua_init() {
$directory = variable_get(ELOQUA_VARIABLE_SCRIPTS_DIRECTORY, ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT);
foreach (array(
'elqCfg',
'elqImg',
) as $file) {
if (!file_exists($directory . '/' . $file . '.js')) {
drupal_set_message(t('You must place the !file.js file in !directory for the eloqua module to function.', array(
'!file' => $file,
'!directory' => $directory,
)), 'error');
}
else {
drupal_add_js($directory . '/' . $file . '.js');
}
}
}
function eloqua_menu() {
$items = array();
$items['admin/settings/eloqua'] = array(
'title' => 'Eloqua',
'description' => 'Create and edit Eloqua settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'eloqua_admin_form',
),
'access arguments' => array(
'access administration pages',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'eloqua.admin.inc',
);
return $items;
}
function eloqua_cron() {
module_load_include('inc', 'eloqua', 'eloqua.cron');
_eloqua_cron();
}
function _eloqua_webform_component_get_exempt_types() {
return array(
'fieldset',
'pagebreak',
);
}
function _eloqua_is_valid_component_type($test_type) {
$invalid_types = _eloqua_webform_component_get_exempt_types();
$result = in_array($test_type, $invalid_types);
$result = !$result;
return $result;
}
function eloqua_form_webform_component_edit_form_alter(&$form, $form_state) {
$component = array_key_exists(3, $form['#parameters']) ? $form['#parameters'][3] : NULL;
if ($component === NULL) {
return;
}
$form['eloqua'] = array(
'#type' => 'fieldset',
'#title' => t('Eloqua'),
'#tree' => TRUE,
);
if (!_eloqua_is_valid_component_type($component['type'])) {
$form['eloqua']['msg'] = array(
'#type' => 'markup',
'#value' => t('This component type is not compatible with the Eloqua Module.'),
);
return;
}
$form['eloqua']['key'] = array(
'#type' => 'textfield',
'#default_value' => isset($component['extra']['eloqua']['key']) ? $component['extra']['eloqua']['key'] : '',
'#title' => t('Eloqua Field Name'),
'#description' => t('The eloqua field name if it contains symbols or characters not compatible with the defaults above. Leave empty to use the default above. Can not contain spaces.'),
'#weight' => 0,
);
$form['eloqua']['query_name'] = array(
'#type' => 'textfield',
'#default_value' => isset($component['extra']['eloqua']['query_name']) ? $component['extra']['eloqua']['query_name'] : '',
'#description' => t('If this value can be populated by a query parameter, the key that is to be used. Can only contain the characters a-z A-Z 0-9 and _.'),
'#title' => t('Request Parameter Key'),
);
$form[]['#validate'] = 'eloqua_form_webform_component_edit_form_validate';
}
function eloqua_webform_component_presave(&$component) {
if (array_key_exists('eloqua', $component) && isset($component['eloqua'])) {
$component['extra']['eloqua'] = $component['eloqua'];
}
}
function eloqua_form_webform_component_edit_form_validate($form, $form_state) {
if (preg_match('![\\x00-\\x20 ]!', $form_state['values']['eloqua']['key'])) {
form_set_error('eloqua][key', t('The eloqua field key %field_key is invalid. Please do not include spaces.', array(
'%field_key' => $form_state['values']['eloqua']['key'],
)));
}
$values = $form_state['values'];
if (!empty($values['eloqua']['query_name'])) {
if (!preg_match('/^[a-zA-Z0-9_]+$/', $values['eloqua']['query_name'])) {
form_set_error('eloqua][query_name', t('The query name provided can only contain the characters a-z A-Z 0-9 and underscore (_).'));
}
}
}
function eloqua_form_webform_configure_form_alter(&$form, $form_state) {
$form['#submit'][] = 'eloqua_form_webform_configure_form_submit';
$form['#validate'][] = 'eloqua_form_webform_configure_form_validate';
$settings = new stdClass();
if (!empty($form['nid']['#value'])) {
$settings = eloqua_webform_load($form['nid']['#value']);
}
if (!isset($settings->{ELOQUA_WEBFORM_FIELD_ACTIVE})) {
$settings->{ELOQUA_WEBFORM_FIELD_ACTIVE} = 0;
}
if (!isset($settings->{ELOQUA_WEBFORM_FIELD_FORM_NAME})) {
$settings->{ELOQUA_WEBFORM_FIELD_FORM_NAME} = '';
}
if (!isset($settings->{ELOQUA_WEBFORM_FIELD_DATA})) {
$settings->{ELOQUA_WEBFORM_FIELD_DATA} = new stdClass();
}
if (!isset($settings->{ELOQUA_WEBFORM_FIELD_DATA}->process_every_page)) {
$settings->{ELOQUA_WEBFORM_FIELD_DATA}->process_every_page = 0;
}
$form['eloqua'] = array(
'#type' => 'fieldset',
'#description' => t('Eloqua Integration Options'),
'#title' => t('Eloqua'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'elq_enabled' => array(
'#type' => 'checkbox',
'#default_value' => $settings->{ELOQUA_WEBFORM_FIELD_ACTIVE},
'#description' => t('Whether this form is eloqua enabled'),
'#title' => t('Enabled'),
),
'elq_form_name' => array(
'#type' => 'textfield',
'#default_value' => $settings->{ELOQUA_WEBFORM_FIELD_FORM_NAME},
'#description' => t('The form name as it is defined in Eloqua'),
'#title' => t('Eloqua Form Name'),
),
'elq_process_every_page' => array(
'#type' => 'checkbox',
'#default_value' => $settings->{ELOQUA_WEBFORM_FIELD_DATA}->process_every_page,
'#description' => t('Process every page submit on a multi-page form, instead of one combined submit upon completion.'),
'#title' => t('Process subpages seperately'),
),
);
}
function eloqua_form_webform_configure_form_validate($form, $form_state) {
$values = $form_state['values'];
if ($values['elq_enabled']) {
if (empty($values['elq_form_name'])) {
form_set_error('elq_form_name', t('The Eloqua Form Name must be set if the form is to be eloqua-enabled.' . ' This can only contain the characters a-zA-Z and _'));
}
}
}
function eloqua_form_webform_configure_form_submit($form, &$form_state) {
$nid = $form_state['values']['nid'];
$values = $form_state['values'];
$webform = new stdClass();
$webform->{ELOQUA_WEBFORM_FIELD_NODE_ID} = $nid;
$webform->{ELOQUA_WEBFORM_FIELD_ACTIVE} = $values['elq_enabled'];
$webform->{ELOQUA_WEBFORM_FIELD_FORM_NAME} = $values['elq_form_name'];
$webform->{ELOQUA_WEBFORM_FIELD_DATA} = (object) array(
'process_every_page' => $values['elq_process_every_page'],
);
eloqua_webform_create($webform);
}
function eloqua_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'webform_client_form') !== 0) {
return;
}
if (!isset($form['details']['nid']['#value']) || empty($form['details']['nid']['#value'])) {
return;
}
$nid = $form['details']['nid']['#value'];
$form_settings = (array) eloqua_webform_load($nid);
if (empty($form_settings) || !$form_settings[ELOQUA_WEBFORM_FIELD_ACTIVE]) {
return;
}
_eloqua_form_url_map($form, $form['#parameters'][2]->webform['components']);
$form['#submit'][] = 'eloqua_webform_submit';
$form['eloqua'] = array();
$process_every_page = is_object($form_settings[ELOQUA_SETTINGS_FIELD_DATA]) && isset($form_settings[ELOQUA_SETTINGS_FIELD_DATA]->process_every_page) ? $form_settings[ELOQUA_SETTINGS_FIELD_DATA]->process_every_page : FALSE;
$form['eloqua']['process_every_page'] = array(
'#type' => 'value',
'#value' => $process_every_page,
);
$form['eloqua']['elqFormName'] = _eloqua_form_hidden($form_settings[ELOQUA_WEBFORM_FIELD_FORM_NAME]);
$form['eloqua']['elqSiteId'] = _eloqua_form_hidden(eloqua_get_site_id());
$form['eloqua']['elqDefaultTargetURL'] = _eloqua_form_hidden('');
$form['eloqua']['elqPost'] = _eloqua_form_hidden('');
$form['eloqua']['elqCustomerGUID'] = _eloqua_form_hidden('');
$form['eloqua']['elqCookieWrite'] = _eloqua_form_hidden('0');
$form['eloqua']['script']['setting'] = array(
'#type' => 'markup',
'#value' => '<script type="text/javascript" language="JavaScript">var elqPPS="70";</script>',
);
$directory = variable_get(ELOQUA_VARIABLE_SCRIPTS_DIRECTORY, ELOQUA_VARIABLE_SCRIPTS_DIRECTORY_DEFAULT);
$js_file = base_path() . $directory . '/elqScr.js';
$form['eloqua']['script']['external'] = array(
'#type' => 'markup',
'#value' => '<script type="text/javascript" language="JavaScript" src="' . $js_file . '"></script>',
);
$initScript = <<<EOJS
<script type="text/javascript" language="JavaScript">
\$(document).ready(function() {
if (GetElqCustomerGUID) {
\$('#edit-elqCustomerGUID').val(GetElqCustomerGUID());
}
});
// </script>
EOJS;
$form['eloqua']['script']['init'] = array(
'#type' => 'markup',
'#value' => $initScript,
);
$user_headers = _eloqua_webform_get_user_headers();
$form['eloqua']['user_headers'] = array(
'#type' => 'hidden',
'#default_value' => serialize($user_headers),
);
foreach (module_implements('eloqua_form_alter') as $module_name) {
$method = $module_name . '_eloqua_form_alter';
$method($form, $form_state, $form_id);
}
}
function _eloqua_webform_get_user_headers() {
if (!function_exists('apache_request_headers')) {
return array();
}
$headers = apache_request_headers();
if (!$headers) {
return array();
}
$filter = array(
'host',
'cookie',
);
$result = array();
foreach ($headers as $key => $value) {
if (!in_array(strtolower($key), $filter)) {
$result[strtolower($key)] = $value;
}
}
return $result;
}
function eloqua_webform_submit($form, $form_state) {
$process_every_page = $form_state['values']['process_every_page'];
if (!$process_every_page) {
if (!array_key_exists('webform_completed', $form_state) || !$form_state['webform_completed']) {
return;
}
}
$values = array();
$values['form_post'] = $form_state['values'];
$values['user_headers'] = unserialize($form_state['values']['user_headers']);
if ($values['user_headers'] == FALSE) {
$values['user_headers'] = array();
}
unset($values['form_post']['user_headers']);
$info = new stdClass();
$info->{ELOQUA_POST_FIELD_FORM_ID} = $form_state['values']['form_id'];
$info->{ELOQUA_POST_FIELD_POST_TIME} = time();
$info->{ELOQUA_POST_FIELD_STATUS} = ELOQUA_STATUS_NEW;
$info->{ELOQUA_POST_FIELD_DATA} = $values;
$post_id = eloqua_post_create($info);
if (!$post_id) {
$message = t('Unable to save submitted entry. <pre>!data</pre>');
$vars = array(
'!data' => var_export($info, TRUE),
);
watchdog(ELOQUA_WATCHDOG_CATEGORY, $message, $vars, WATCHDOG_NOTICE);
}
}
function _eloqua_form_hidden($value) {
return array(
'#type' => 'hidden',
'#default_value' => (string) $value,
);
}
function eloqua_get_nesting($components, $cid) {
if ($components[$cid]['pid'] == 0) {
return array(
$components[$cid]['form_key'],
);
}
else {
return array_merge(eloqua_get_nesting($components, $components[$cid]['pid']), array(
$components[$cid]['form_key'],
));
}
}
function _eloqua_form_url_map(&$form, $component_list) {
foreach ($component_list as $component) {
if (empty($component['extra']['eloqua']['query_name'])) {
continue;
}
$field_param_name = $component['extra']['eloqua']['query_name'];
if (!array_key_exists($field_param_name, $_REQUEST)) {
continue;
}
$path = _eloqua_form_url_get_component_path($component_list, $component);
$target_element =& $form['submitted'];
$pass = TRUE;
foreach ($path as $level) {
if (array_key_exists($level, $target_element)) {
$target_element =& $target_element[$level];
}
else {
$pass = FALSE;
continue;
}
}
if ($pass) {
$conversion_method = '_eloqua_form_url_map_' . $component['type'];
if (function_exists($conversion_method)) {
$conversion_method($_REQUEST[$field_param_name], $target_element);
}
else {
$target_element['#default_value'] = $_REQUEST[$field_param_name];
}
}
unset($target_element);
}
}
function _eloqua_form_url_map_date($value, &$element) {
$split = explode('-', $value);
if (count($split) != 3 || !is_numeric($split[0]) || !is_numeric($split[1]) || !is_numeric($split[2])) {
return;
}
$year = (int) $split[0];
$month = (int) $split[1];
$day = (int) $split[2];
if ($year < 0 || $year > 9999 || !checkdate($month, $day, $year)) {
return;
}
$element['#default_value'] = array(
'year' => $year,
'month' => $month,
'day' => $day,
);
}
function _eloqua_form_url_map_time($value, &$element) {
$split = explode(':', $value);
if (count($split) != 2 || !is_numeric($split[0]) || !is_numeric($split[1])) {
return;
}
$split[0] = (int) $split[0];
$split[1] = (int) $split[1];
if ($split[0] > 23 || $split[1] > 59 || $split[1] < 0 || $split[0] < 0) {
return;
}
$hour = $split[0];
$minute = $split[1];
if (array_key_exists('ampm', $element)) {
$ampm = $hour >= 12 ? 'pm' : 'am';
$element['ampm']['#default_value'] = $ampm;
$hour = $hour > 12 || $hour == 0 ? abs($hour - 12) : $hour;
}
$element['hour']['#default_value'] = $hour;
$element['minute']['#default_value'] = $minute;
}
function _eloqua_form_url_get_component_path($component_list, $target_component) {
$path = array(
$target_component['form_key'],
);
$current_component = $target_component;
while ($current_component != NULL && $current_component['pid'] != 0) {
$current_component = $component_list[$current_component['pid']];
array_unshift($path, $current_component['form_key']);
}
return $path;
}