View source
<?php
function webform_conditional_confirmation_menu() {
$items = array();
$items['node/%webform_menu/webform/confirmation'] = array(
'title' => 'Confirmation messages',
'page callback' => 'webform_conditional_confirmation_list_confirmations',
'page arguments' => array(
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'file' => 'webform_conditional_confirmation.pages.inc',
'weight' => 3,
'type' => MENU_LOCAL_TASK,
);
$items['node/%webform_menu/webform/confirmation/add'] = array(
'title' => 'Add confirmation message',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'webform_conditional_confirmation_edit_confirmation_form',
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'file' => 'webform_conditional_confirmation.pages.inc',
'weight' => 0,
'type' => MENU_LOCAL_ACTION,
);
$items['node/%webform_menu/webform/confirmation/%/edit'] = array(
'title' => 'Edit confirmation message',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'webform_conditional_confirmation_edit_confirmation_form',
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'file' => 'webform_conditional_confirmation.pages.inc',
);
$items['node/%webform_menu/webform/confirmation/%/delete'] = array(
'title' => 'Delete confirmation message',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'webform_conditional_confirmation_delete_confirmation_form',
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'file' => 'webform_conditional_confirmation.pages.inc',
);
return $items;
}
function webform_conditional_confirmation_theme() {
return array(
'webform_conditional_confirmation_list_confirmations' => array(
'variables' => array(
'node' => NULL,
'form' => NULL,
),
'file' => 'webform_conditional_confirmation.pages.inc',
),
'webform_conditional_confirmation_list_form' => array(
'render element' => 'form',
'file' => 'webform_conditional_confirmation.pages.inc',
),
);
}
function _webform_conditional_confirmation_conditional_operators() {
$operators = module_invoke_all('wcc_conditional_operators');
$operators['='] = array(
'label' => t('Is one of'),
'comparator' => function ($input, $test) {
return $input == $test;
},
);
$operators['!='] = array(
'label' => t('Is not one of'),
'comparator' => function ($input, $test) {
return $input != $test;
},
);
$operators['<'] = array(
'label' => t('Less than'),
'comparator' => function ($input, $test) {
return $input < $test;
},
);
$operators['<='] = array(
'label' => t('Less than, or equal to'),
'comparator' => function ($input, $test) {
return $input <= $test;
},
);
$operators['>'] = array(
'label' => t('Greater than'),
'comparator' => function ($input, $test) {
return $input > $test;
},
);
$operators['>='] = array(
'label' => t('Greater than, or equal to'),
'comparator' => function ($input, $test) {
return $input >= $test;
},
);
$operators['LIKE'] = array(
'label' => t('Contains'),
'comparator' => function ($input, $test) {
return strpos($input, $test) !== FALSE;
},
);
return $operators;
}
function webform_conditional_confirmation_message_load($node, $msgid = NULL) {
if ($msgid == 'add') {
$defaults = new stdClass();
$defaults->name = '';
$defaults->conditionals = array();
$defaults->conditional_weight = 0;
$defaults->confirmation = $node->webform['confirmation'];
$defaults->confirmation_format = $node->webform['confirmation_format'];
$defaults->config = array(
'redirect_url' => $node->webform['redirect_url'],
);
return $defaults;
}
$query = db_select('webform_conditional_confirmation', 'wcc')
->fields('wcc')
->condition('nid', $node->nid);
if ($msgid) {
$defaults = $query
->condition('msgid', $msgid)
->execute()
->fetchObject();
$defaults->config = unserialize($defaults->config);
$defaults->conditionals = unserialize($defaults->conditionals);
return $defaults;
}
$result = $query
->orderBy('conditional_weight', 'ASC')
->orderBy('msgid', 'ASC')
->execute();
$records = array();
while ($record = $result
->fetchObject()) {
$record->config = unserialize($record->config);
$record->conditionals = unserialize($record->conditionals);
$records[$record->msgid] = $record;
}
return $records;
}
function webform_conditional_confirmation_message_save($message) {
$message->config = serialize($message->config);
$message->conditionals = serialize($message->conditionals);
if (!$message->msgid) {
if (lock_acquire('webform_conditional_confirmation_message_insert_' . $message->nid, 5)) {
$next_id_query = db_select('webform_conditional_confirmation')
->condition('nid', $message->nid);
$next_id_query
->addExpression('MAX(msgid) + 1', 'msgid');
$message->msgid = $next_id_query
->execute()
->fetchField();
if ($message->msgid == NULL) {
$message->msgid = 1;
}
lock_release('webform_conditional_confirmation_message_insert_' . $message->nid);
}
else {
watchdog('webform_conditional_confirmation', 'A Webform conditional message could not be saved because a timeout occurred while trying to acquire a lock for the node. Details: <pre>@message</pre>', array(
'@message' => print_r($message, TRUE),
));
return FALSE;
}
drupal_write_record('webform_conditional_confirmation', $message);
}
else {
drupal_write_record('webform_conditional_confirmation', $message, array(
'nid',
'msgid',
));
}
return $message->msgid;
}
function webform_conditional_confirmation_message_delete($nid, $msgid) {
db_delete('webform_conditional_confirmation')
->condition('nid', $nid)
->condition('msgid', $msgid)
->execute();
}
function webform_conditional_confirmation_get_message(&$node, $submission) {
$messages = webform_conditional_confirmation_message_load($node);
$operators = _webform_conditional_confirmation_conditional_operators();
if (empty($messages)) {
if ($node->webform['redirect_url'] == '<none>') {
drupal_goto($node->webform['redirect_url'] = "node/{$node->nid}");
}
if (!isset($node->webform_conditional_confirmation) && $node->webform['redirect_url'] != '<confirmation>') {
drupal_goto($node->webform['redirect_url']);
}
return;
}
if (empty($submission) || empty($submission->data)) {
return;
}
foreach ($messages as $msgid => $message) {
$conditionals = $message->conditionals;
$matches = 0;
foreach ($conditionals as $conditional) {
$data = $submission->data[$conditional['component']];
foreach ($data as $input_value) {
if (_webform_conditional_confirmation_test_match($input_value, $conditional, $operators)) {
$matches++;
break;
}
}
}
if ($matches === count($conditionals)) {
$node->webform['confirmation'] = $message->confirmation;
$node->webform['confirmation_format'] = $message->confirmation_format;
$node->webform['redirect_url'] = $message->config['redirect_url'];
break;
}
}
}
function _webform_conditional_confirmation_test_match($input_value, $conditional, $operators) {
if (!isset($operators[$conditional['operator']])) {
watchdog('webform_conditional_confirmation', 'Operator for %op not found.', array(
'%op' => $conditional['operator'],
));
return FALSE;
}
$comparator = $operators[$conditional['operator']]['comparator'];
$matches = array_map('trim', explode("\n", $conditional['values']));
foreach ($matches as $match) {
if ($comparator($input_value, $match)) {
return TRUE;
}
}
return FALSE;
}
function webform_conditional_confirmation_form_webform_client_form_alter(&$form, &$form_state) {
$form['#node']->webform_conditional_confirmation['redirect_url'] = $form['#node']->webform['redirect_url'];
$form['#node']->webform['redirect_url'] = '<confirmation>';
$form['#submit'][] = 'webform_conditional_confirmation_webform_client_form_submit';
}
function webform_conditional_confirmation_webform_client_form_submit($form, &$form_state) {
global $user;
$node = $form['#node'];
$sid = $form_state['values']['details']['sid'];
$is_draft = !empty($form_state['save_draft']);
if (empty($form_state['save_draft']) && empty($form_state['webform_completed'])) {
return;
}
if (empty($sid)) {
return;
}
module_load_include('inc', 'webform', 'includes/webform.submissions');
$submission = webform_get_submission($node->nid, $sid);
webform_conditional_confirmation_get_message($node, $submission);
$redirect_url = trim($node->webform['redirect_url']);
$redirect_url = webform_replace_tokens($redirect_url, $node, $submission, NULL, FALSE, TRUE);
$redirect_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'], '/') . '\\//', '', $redirect_url);
$redirect = $form_state['redirect'];
$external_url = FALSE;
$message = FALSE;
if (isset($form['actions']['draft']['#value']) && $form_state['values']['op'] == $form['actions']['draft']['#value']) {
$message = t('Submission saved. You may return to this form later and it will restore the current values.');
}
elseif (!empty($form_state['values']['details']['finished'])) {
$message = t('Submission updated.');
}
elseif ($redirect_url == '<none>') {
$redirect = NULL;
}
elseif ($redirect_url == '<confirmation>') {
$message = FALSE;
$query = array(
'sid' => $sid,
);
if ((int) $user->uid === 0) {
$query['token'] = webform_get_submission_access_token($submission);
}
$redirect = array(
'node/' . $node->nid . '/done',
array(
'query' => $query,
),
);
}
elseif (valid_url($redirect_url, TRUE)) {
$redirect = $redirect_url;
$external_url = TRUE;
}
elseif ($redirect_url && strpos($redirect_url, 'http://') !== FALSE) {
$url_parts = drupal_parse_url($redirect_url);
$url_parts['query'] ? $url_parts['query']['sid'] = $sid : ($url_parts['query'] = array(
'sid' => $sid,
));
$redirect = array(
$url_parts['path'],
array(
'query' => $url_parts['query'],
'fragment' => $url_parts['fragment'],
),
);
}
if (isset($message)) {
drupal_set_message($message);
}
elseif (!$is_draft && !$external_url && (!empty($redirect_url) && $redirect_url != '<confirmation>') && !empty($confirmation)) {
drupal_set_message(check_markup($confirmation, $node->webform['confirmation_format'], '', TRUE));
}
$form_state['redirect'] = $redirect;
}
function webform_conditional_confirmation_menu_alter(&$items) {
$items['node/%webform_menu/done']['page callback'] = 'webform_conditional_confirmation_display_message';
}
function webform_conditional_confirmation_display_message($node) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
drupal_set_title($node->title);
webform_set_breadcrumb($node);
$sid = isset($_GET['sid']) ? $_GET['sid'] : NULL;
$submission = webform_get_submission($node->nid, $sid);
webform_conditional_confirmation_get_message($node, $submission);
return theme(array(
'webform_confirmation_' . $node->nid,
'webform_confirmation',
), array(
'node' => $node,
'sid' => $sid,
));
}