webform_rules.module in Webform Rules 6
Same filename and directory in other branches
Adds rules integration for webform submissions.
File
webform_rules.moduleView source
<?php
/**
* @file
* Adds rules integration for webform submissions.
*/
/**
* Implementation of hook_webform_submission_insert().
*
* @param $node
* The webform node.
* @param $submission
* The webform submission.
*/
function webform_rules_webform_submission_insert($node, $submission) {
// Invoke event.
webform_rules_rules_invoke_event($submission, $node, 'insert');
}
/**
* Implementation of hook_webform_submission_update().
*
* @param $node
* The webform node.
* @param $submission
* The webform submission.
*/
function webform_rules_webform_submission_update($node, $submission) {
// Invoke event.
webform_rules_rules_invoke_event($submission, $node, 'update');
}
/**
* Implementation of hook_webform_submission_update().
*
* @param $node
* The webform node.
* @param $submission
* The webform submission.
*/
function webform_rules_webform_submission_delete($node, $submission) {
// Invoke event.
webform_rules_rules_invoke_event($submission, $node, 'delete');
}
/**
* Implementation of hook_form_alter().
*/
function webform_rules_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'webform_client_form_') !== FALSE) {
// Add custom submit handler to webform form.
$form['#submit'][] = 'webform_rules_client_form_submit';
}
}
/**
* Custom submit handler for webform submissions.
*
* This is needed to catch submissions of saved webform drafts as
* hook_webform_submission_insert() only fires once and its not possible in
* hook_webform_submission_update() to check if the data has been submitted
* before (e.g. saved as draft).
*/
function webform_rules_client_form_submit($form, &$form_state) {
// If the webform is NOT completed, don't run the submit handler!
// This is relevant for multistep forms.
if (!$form_state['webform_completed']) {
return;
}
// If we've got to this point, then we are not mid-way through a form submission.
$values = $form_state['values'];
// Check if user is submitting as a draft.
if ($values['op'] == t('Save Draft')) {
// Saving the webform as draft is handled by hook_webform_submission_insert().
return;
}
if ($form['#is_draft'] && isset($form_state['values']['details']['sid'])) {
$submission = $form['#submission'];
// Map submitted data to submission data.
foreach ($form_state['values']['submitted'] as $cid => $value) {
if (isset($submission->data[$cid])) {
$submission->data[$cid]['value'] = $value;
}
}
// Invoke event.
webform_rules_rules_invoke_event($submission, $form['#node'], 'submit');
}
return;
}
/**
* Invoke rules event with submitted data.
*
* @param $submission_data
* Data from webform prepared by webform_submission_data().
* @param $node
* The submitted webform node.
* @param $op
* Type of submission: 'insert', 'update', 'delete', 'submit.
*/
function webform_rules_rules_invoke_event($submission, $node, $op = 'insert') {
global $user;
if (!is_array($submission->data) || count($submission->data) == 0) {
return;
}
$is_draft = isset($submission->is_draft) && $submission->is_draft;
$webform = $node->webform;
$data = array(
'op' => $op,
'sid' => $submission->sid,
'components' => array(),
);
$form_id = 'webform-client-form-' . $webform['nid'];
// Map values to field names.
foreach ($submission->data as $cid => $value) {
$component = $webform['components'][$cid];
$data['components'][$component['form_key']]['value'] = $value['value'];
$data['components'][$component['form_key']]['component'] = $component;
}
// Invoke the rules event.
switch ($op) {
case 'insert':
if ($is_draft) {
rules_invoke_event('webform_rules_submit_as_draft', array(
'user' => $user,
'node' => $node,
'data' => $data,
'form_id' => $form_id,
));
}
else {
rules_invoke_event('webform_rules_submit', array(
'user' => $user,
'node' => $node,
'data' => $data,
'form_id' => $form_id,
));
}
break;
case 'submit':
rules_invoke_event('webform_rules_insert', array(
'user' => $user,
'node' => $node,
'data' => $data,
'form_id' => $form_id,
));
break;
case 'update':
rules_invoke_event('webform_rules_update', array(
'user' => $user,
'node' => $node,
'data' => $data,
'form_id' => $form_id,
));
break;
case 'delete':
rules_invoke_event('webform_rules_delete', array(
'user' => $user,
'node' => $node,
'data' => $data,
'form_id' => $form_id,
));
break;
}
}
/**
* Implementation of hook_token_list().
*/
function webform_rules_token_list($type) {
if ($type == 'webform' || $type == 'all') {
$list['webform']['sid'] = t('The submission id.');
$list['webform']['data'] = t('The submitted webform data.');
$list['webform']['data-raw'] = t('The unfiltered submitted webform data.');
$list['webform']['{component}-title'] = t('The title of the selected component, e.g. "email-title".');
$list['webform']['{component}-value'] = t('The value of the selected component, e.g. "email-value".');
$list['webform']['{component}-value-html'] = t('The value of the selected component rendered as html, e.g. "email-value-html".');
$list['webform']['{component}-value-raw'] = t('The unfiltered value of the selected component, e.g. "email-value-raw".');
$list['webform']['{component}-display'] = t('Title and value of the selected component, e.g. "email-display".');
$list['webform']['{component}-display-html'] = t('Title and value of the selected component rendered as html, e.g. "email-display-html".');
return $list;
}
}
/**
* Implementation of hook_token_values().
*/
function webform_rules_token_values($type, $object = NULL, $options = array()) {
$values = array();
if (!is_array($object) || !isset($object['sid']) || !isset($object['components'])) {
return $values;
}
switch ($type) {
case 'webform':
// Get webform data as token values.
$values['sid'] = $object['sid'];
$values['data'] = webform_rules_prepare_component_value($object);
$values['data-raw'] = webform_rules_prepare_component_value($object, TRUE);
// Process components.
$components = array_keys($object['components']);
foreach ($components as $key) {
$component = $object['components'][$key]['component'];
$component_value = $object['components'][$key]['value'];
// Get component title as token value.
$values[$key . '-title'] = $component['name'];
// Get value as token and render as text.
// Full display (title: value).
$values[$key . '-display'] = webform_rules_render_component($component, $component_value);
// Get component value as token value.
$values[$key . '-value'] = webform_rules_render_component($component, $component_value, 'text', FALSE);
// Get value as token and render as html.
// Full display (title: value).
$values[$key . '-display-html'] = webform_rules_render_component($component, $component_value, 'html');
// Get component value as token value.
$values[$key . '-value-html'] = webform_rules_render_component($component, $component_value, 'html', FALSE);
// Raw value.
$values[$key . '-value-raw'] = webform_rules_prepare_component_value($component_value, TRUE);
}
break;
}
return $values;
}
/**
* Prepare component value for output.
* Code adapted from drupal_to_js().
*
* @param $component_value
* Value of submitted component.
* @param $raw
* If TRUE, leave all values unfiltered.
*/
function webform_rules_prepare_component_value($component_value, $raw = FALSE) {
switch (gettype($component_value)) {
case 'boolean':
return $component_value ? 'true' : 'false';
// Lowercase necessary!
case 'integer':
case 'double':
return $var;
case 'resource':
case 'string':
return $raw ? $component_value : check_plain($component_value);
case 'array':
// If the array is empty or if it has sequential whole number keys
// starting with 0, it's not associative so we can go ahead and
// convert it as an array.
if (empty($component_value) || array_keys($component_value) === range(0, sizeof($component_value) - 1)) {
$output = array();
foreach ($component_value as $v) {
$output[] = webform_rules_prepare_component_value($v, $raw);
}
return implode(', ', $output);
}
// Otherwise, fall through to convert the array as an object.
// This is usefull for more readable output.
case 'object':
$output = array();
foreach ($component_value as $k => $v) {
$output[] = webform_rules_prepare_component_value(strval($k), $raw) . ': ' . webform_rules_prepare_component_value($v, $raw);
}
return implode(', ', $output);
default:
return 'null';
}
}
/**
* Render value of component.
*
* @param $component
* Webform component to render.
* @param $value
* Submitted value of webform component.
* @param $format
* How to render the components value ('html' or 'text'). Defaults to 'text'.
* @param $title
* Renders the component title if set to TRUE.
*
* @return
* The rendered component value.
*/
function webform_rules_render_component($component, $value, $format = 'text', $title = TRUE) {
module_load_include('inc', 'webform', 'includes/webform.components');
if ($format != 'text') {
$format = 'html';
}
$display_element = webform_component_invoke($component['type'], 'display', $component, $value, $format);
$display_element['#parents'] = array(
'submitted',
$component['form_key'],
);
if (!isset($display_element['#id'])) {
$display_element['#id'] = form_clean_id('edit-' . implode('-', $display_element['#parents']));
}
if (!$title) {
$display_element['#title'] = NULL;
}
return drupal_render($display_element);
}
Functions
Name | Description |
---|---|
webform_rules_client_form_submit | Custom submit handler for webform submissions. |
webform_rules_form_alter | Implementation of hook_form_alter(). |
webform_rules_prepare_component_value | Prepare component value for output. Code adapted from drupal_to_js(). |
webform_rules_render_component | Render value of component. |
webform_rules_rules_invoke_event | Invoke rules event with submitted data. |
webform_rules_token_list | Implementation of hook_token_list(). |
webform_rules_token_values | Implementation of hook_token_values(). |
webform_rules_webform_submission_delete | Implementation of hook_webform_submission_update(). |
webform_rules_webform_submission_insert | Implementation of hook_webform_submission_insert(). |
webform_rules_webform_submission_update | Implementation of hook_webform_submission_update(). |