View source
<?php
function webform_rules_rules_event_info() {
return array(
'webform_rules_submit_as_draft' => array(
'label' => t('After a webform has been saved as draft'),
'module' => t('Webform'),
'arguments' => _webform_rules_event_variables(),
),
'webform_rules_submit' => array(
'label' => t('After a webform has been submitted'),
'module' => t('Webform'),
'arguments' => _webform_rules_event_variables(),
),
'webform_rules_insert' => array(
'label' => t('After a submission draft has been submitted'),
'module' => t('Webform'),
'arguments' => _webform_rules_event_variables(),
),
'webform_rules_update' => array(
'label' => t('After a webform submission has been updated'),
'module' => t('Webform'),
'arguments' => _webform_rules_event_variables(),
),
'webform_rules_delete' => array(
'label' => t('After a webform submission has been deleted'),
'module' => t('Webform'),
'arguments' => _webform_rules_event_variables(),
),
);
}
function webform_rules_rules_condition_info() {
return array(
'webform_rules_condition_webform_id' => array(
'label' => t('Select a webform'),
'arguments' => array(
'form_id' => array(
'type' => 'value',
'label' => t('The form id of the submitted form'),
),
'selected_webform' => array(
'label' => t('Webform'),
'type' => 'string',
),
),
'help' => t('This condition just compares two texts. It returns TRUE, if both texts are equal.'),
'module' => 'Webform',
),
);
}
function webform_rules_data_type_info() {
return array(
'webform_data' => array(
'label' => t('webform data'),
'class' => 'webform_rules_data_type_webform_data',
'savable' => TRUE,
'identifiable' => FALSE,
'use_input_form' => FALSE,
'module' => 'Webform',
'token type' => 'webform',
),
);
}
function webform_rules_condition_webform_id($form_id, $selected_webform) {
return $form_id == $selected_webform;
}
function webform_rules_condition_webform_id_form($settings, &$form) {
$webform_types = webform_variable_get('webform_node_types');
$query = db_query('SELECT n.nid, n.title FROM {node} n INNER JOIN {webform} w ON n.nid = w.nid WHERE type IN (' . db_placeholders($webform_types) . ')', $webform_types);
$options = array();
while ($webform = db_fetch_array($query)) {
$form_id = 'webform-client-form-' . $webform['nid'];
$options[$form_id] = $webform['title'];
}
$form['settings']['selected_webform'] = array(
'#type' => 'select',
'#title' => t('Webform'),
'#options' => $options,
'#default_value' => isset($settings['selected_webform']) ? $settings['selected_webform'] : '',
'#required' => TRUE,
);
}
function _webform_rules_event_variables() {
return array(
'user' => array(
'type' => 'user',
'label' => t('User, who submitted the webform'),
),
'node' => array(
'type' => 'node',
'label' => t('The webform node'),
),
'data' => array(
'type' => 'webform_data',
'label' => t('The submitted webform data'),
'save' => TRUE,
),
'form_id' => array(
'type' => 'string',
'label' => t('The form id of the submitted form'),
'hidden' => TRUE,
),
'selected_webform' => array(
'type' => 'string',
'label' => t('The webform'),
'hidden' => TRUE,
),
);
}
class webform_rules_data_type_webform_data extends rules_data_type {
function save() {
$changed = FALSE;
$submission =& $this
->get();
if (!$submission || !is_array($submission)) {
return FALSE;
}
$sid = $submission['sid'];
$saved_submission = $this
->load($sid);
if (count($saved_submission) != 1) {
return FALSE;
}
$new_submission = $saved_submission[$sid];
$node = node_load($new_submission->nid);
if (!$node) {
return FALSE;
}
foreach ($submission['components'] as $key => $value) {
$cid = $value['component']['cid'];
$new_val = is_array($value['value']) ? $value['value'] : array(
$value['value'],
);
if ($new_val != $new_submission->data[$cid]['value']) {
$changed = TRUE;
}
$new_submission->data[$cid]['value'] = $new_val;
}
if ($changed) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_update($node, $new_submission);
}
return TRUE;
}
function load($sid) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
return webform_get_submissions(array(
'sid' => $sid,
));
}
function get_identifier() {
$submission =& $this
->get();
if (is_array($submission)) {
$submission = (object) $submission;
}
return $submission->sid;
}
}