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'),
'group' => t('Webform'),
'variables' => _webform_rules_event_variables(),
'access callback' => 'rules_node_integration_access',
),
'webform_rules_submit' => array(
'label' => t('After a webform has been submitted'),
'group' => t('Webform'),
'variables' => _webform_rules_event_variables(),
'access callback' => 'rules_node_integration_access',
),
'webform_rules_insert' => array(
'label' => t('After a submission draft has been submitted'),
'group' => t('Webform'),
'variables' => _webform_rules_event_variables(),
'access callback' => 'rules_node_integration_access',
),
'webform_rules_update' => array(
'label' => t('After a webform submission has been updated'),
'group' => t('Webform'),
'variables' => _webform_rules_event_variables(),
'access callback' => 'rules_node_integration_access',
),
'webform_rules_delete' => array(
'label' => t('After a webform submission has been deleted'),
'group' => t('Webform'),
'variables' => _webform_rules_event_variables(),
'access callback' => 'rules_node_integration_access',
),
);
}
function webform_rules_rules_condition_info() {
return array(
'webform_has_id' => array(
'label' => t('Webform has name'),
'parameter' => array(
'form_id' => array(
'type' => 'text',
'label' => t('The form id of the submitted form'),
),
'selected_webform' => array(
'type' => 'list<text>',
'label' => t('Webforms'),
'options list' => 'webform_rules_get_webforms_as_options',
'description' => t('The name of the webform to check for.'),
'restriction' => 'input',
),
),
'help' => t('This condition compares the id of the submitted form with the selected value(s).'),
'group' => t('Webform'),
'base' => 'webform_rules_condition_webform_has_id',
),
'node_is_webform' => array(
'label' => t('Content is webform-enabled'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Content'),
'description' => t('The content to verify.'),
),
),
'help' => t('This condition verifies a node contains a webform.'),
'group' => t('Webform'),
'base' => 'webform_rules_condition_node_is_webform',
),
'data_nid' => array(
'label' => t('Submission to a particular webform'),
'parameter' => array(
'webform' => array(
'type' => 'webform',
'label' => t('Current submission data'),
'description' => t('Specifiy the submission to check. Value data.'),
'restriction' => 'selector',
),
'nid' => array(
'type' => 'list<integer>',
'label' => t('Webforms'),
'description' => t('The webforms to check for.'),
'options list' => 'webform_rules_get_webforms_as_options',
'restriction' => 'input',
),
),
'help' => t('Use this condition to expose webform components for a particular webform.'),
'group' => t('Webform'),
'base' => 'webform_rules_condition_data_nid',
),
);
}
function webform_rules_rules_action_info() {
$defaults = array(
'group' => t('Webform'),
'access callback' => 'rules_node_admin_access',
'parameter' => array(
'entity' => array(
'type' => 'node',
'label' => t('Webform'),
'description' => t('A webform-enabled node.'),
'save' => TRUE,
'optional' => TRUE,
),
'selected_webform' => array(
'type' => 'list<text>',
'label' => t('Webforms'),
'options list' => 'webform_rules_get_webforms_as_options',
'description' => t('List of webforms to open.'),
'restriction' => 'input',
'optional' => TRUE,
),
),
'callbacks' => array(
'validate' => 'webform_rules_webform_statuschange_validate',
),
);
$actions = array(
'webform_open' => $defaults + array(
'label' => t('Open webforms'),
'base' => 'webform_rules_webform_open',
),
'webform_close' => $defaults + array(
'label' => t('Close webforms'),
'base' => 'webform_rules_webform_close',
),
'webform_submissions_load' => array(
'label' => t('Fetch webform submissions'),
'base' => 'webform_rules_submissions_load',
'group' => t('Webform'),
'access callback' => 'rules_node_admin_access',
'parameter' => array(
'nid' => array(
'type' => 'integer',
'label' => t('Node ID'),
'description' => t('The ID of the webform node to load the submission for.'),
),
'sid' => array(
'type' => 'integer',
'label' => t('Submission ID'),
'description' => t('The ID of a webform submission. If omitted all submissions of the specified node ID will be fetched.'),
'optional' => TRUE,
),
),
'provides' => array(
'submissions' => array(
'label' => t('Fetched submissions'),
'type' => 'list<webform_data>',
),
),
),
);
$actions['webform_close']['parameter']['selected_webform']['description'] = t('The name of the webforms to close.');
return $actions;
}
function webform_rules_rules_data_info() {
return array(
'webform' => array(
'label' => t('webform data'),
'group' => t('Webform'),
'token type' => 'webform',
'wrapper class' => 'WebformRulesSubmissionWrapper',
'wrap' => TRUE,
'is wrapped' => FALSE,
'property info' => array(
'sid' => array(
'type' => 'integer',
'label' => 'Submission ID',
),
'submitted' => array(
'type' => 'date',
'label' => 'Date Submitted',
),
'remote_addr' => array(
'type' => 'text',
'label' => 'IP Address of the submitter',
),
'uid' => array(
'type' => 'integer',
'label' => 'User ID of the submitter',
),
'name' => array(
'type' => 'text',
'label' => 'Username of the submitter',
),
'is_draft' => array(
'type' => 'boolean',
'label' => 'Whether the submission was finalized',
),
),
),
);
}
function webform_rules_condition_data_nid($submission, $selection, $settings, $state, $condition, $op) {
$first_key = key($submission);
if (empty($submission) || !$submission[$first_key]->nid) {
drupal_set_message('No submission found!', 'error');
return FALSE;
}
if (is_array($selection)) {
if (!empty($selection['webform-client-form-' . $submission[$first_key]->nid])) {
return TRUE;
}
else {
drupal_set_message('Submission is not in webform!', 'error');
return FALSE;
}
}
elseif (is_string($selected_webform)) {
if ('webform-client-form-' . $submission[$first_key]->nid == $selection) {
return TRUE;
}
else {
drupal_set_message('Submission is not in webform(string)!', 'error');
return FALSE;
}
}
return FALSE;
}
function webform_rules_condition_data_nid_assertions($element) {
if ($wrapper = $element
->applyDataSelector($element->settings['webform:select'])) {
$assertion = array(
'property info' => array(),
);
$nids = str_replace('webform-client-form-', '', $element->settings['nid']);
foreach ($nids as $nid) {
$assertion['property info'] += _webform_rules_component_property_info($nid);
}
if (!empty($assertion['property info'])) {
return array(
$element->settings['webform:select'] => $assertion,
);
}
}
}
function _webform_rules_component_property_info($nid) {
$info = array();
$webform = node_load($nid);
foreach ($webform->webform['components'] as $cid => $component) {
$id = "{$component['form_key']}";
$info[$id] = array(
'label' => $component['name'],
'description' => t('@component_name from @title(@nid)', array(
'@component_name' => $component['name'],
'@title' => $webform->title,
'@nid' => $webform->nid,
)),
'type' => 'list<text>',
'getter callback' => 'webform_rules_submission_data_get',
);
}
return $info;
}
function webform_rules_submission_data_get($submission, $options, $name, $type, $context) {
if (is_array($submission) && array_key_exists($name, $submission)) {
if ($name == 'data' && strpos($context['type'], 'list') === FALSE) {
return current($submission[$name]);
}
return $submission[$name];
}
elseif (is_object($submission) && property_exists($submission, $name)) {
return $submission->{$name};
}
}
function webform_rules_webform_statuschange_validate($element) {
if (empty($element->settings['entity:select']) && empty($element->settings['selected_webform'])) {
throw new RulesIntegrityException(t('At least one parameter needs to be set.'), array(
$element,
'parameter',
'node',
));
}
}
function webform_rules_condition_webform_has_id($form_id, $selected_webform) {
if (is_array($selected_webform)) {
return in_array($form_id, $selected_webform);
}
elseif (is_string($selected_webform)) {
return $form_id == $selected_webform;
}
return FALSE;
}
function webform_rules_condition_node_is_webform($node) {
$query = db_select('node', 'n')
->fields('n', array(
'nid',
'title',
))
->condition('n.nid', $node->nid);
$query
->join('webform', 'w', 'n.nid = w.nid');
$query
->join('webform_component', 'c', 'n.nid = c.nid');
if ($query
->execute()
->rowCount() > 0) {
return TRUE;
}
return FALSE;
}
function webform_rules_webform_open($entity = FALSE, $selected_webforms = array()) {
_webform_rules_webform_set_status($entity, $selected_webforms);
}
function webform_rules_webform_close($entity = FALSE, $selected_webforms = array()) {
_webform_rules_webform_set_status($entity, $selected_webforms, FALSE);
}
function _webform_rules_webform_set_status($entity = FALSE, $selected_webforms = array(), $open = TRUE) {
if (!empty($entity->nid) && webform_rules_condition_node_is_webform($entity)) {
$entity->webform['status'] = $open == TRUE ? 1 : 0;
}
module_load_include('inc', 'rules', 'modules/entity.eval');
foreach ($selected_webforms as $form_id) {
$nid = str_replace('webform-client-form-', '', $form_id);
$result = rules_action_entity_fetch('node', $nid, NULL);
$webform = $result['entity_fetched'];
if (!empty($webform->nid) && webform_rules_condition_node_is_webform($webform)) {
$webform->webform['status'] = $open == TRUE ? 1 : 0;
node_save($webform);
}
}
}
function webform_rules_submissions_load($nid, $sid = NULL) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
$filters = array(
'nid' => $nid,
);
if (!empty($sid)) {
$filters['sid'] = $sid;
}
$submissions = webform_get_submissions($filters);
return array(
'submissions' => $submissions,
);
}
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',
'label' => t('The submitted webform data'),
),
'form_id' => array(
'type' => 'text',
'label' => t('The form id of the submitted form'),
'hidden' => TRUE,
),
'selected_webform' => array(
'type' => 'list<text>',
'label' => t('Webforms'),
'options list' => 'webform_rules_get_webforms_as_options',
'description' => t('The name of the webform to check for.'),
'restriction' => 'input',
'hidden' => TRUE,
'optional' => TRUE,
),
);
}
class WebformRulesSubmissionWrapper extends RulesIdentifiableDataWrapper {
public function __construct($type, $data = NULL, $info = array()) {
if (!is_null($data)) {
$info['property info alter'] = array(
'WebformRulesSubmissionWrapper',
'webformSubmissionValues',
);
}
parent::__construct($type, $data, $info);
$this
->setData($data);
}
public static function webformSubmissionValues($wrapper, $property_info) {
$submission = $wrapper
->value();
$properties = $property_info['properties'];
if (!empty($submission->nid)) {
$properties += _webform_rules_component_property_info($submission->nid);
}
return array(
'properties' => $properties,
);
}
protected function setData($data) {
parent::setData($data);
if (!is_array($data)) {
return;
}
$submission = current($data);
if (!isset($submission->sid)) {
return;
}
$s = webform_get_submissions(array(
'sid' => $submission->sid,
));
if (empty($s) || !is_array($s)) {
return;
}
$s = current($s);
$this->id = $s->sid;
$this->data = $s;
$webform = node_load($this->data->nid);
$components = $webform->webform['components'];
foreach ($s->data as $cid => $val) {
$c_key = $components[$cid]['form_key'];
$id = "{$c_key}";
$this->data->{$id} = array_values($val);
}
}
protected function extractIdentifier($data) {
return $data->sid;
}
public function getIdentifier() {
return $this
->dataAvailable() && $this
->value() ? $this->id : NULL;
}
protected function load($data) {
if (is_array($data)) {
$sid = $data['sid'];
}
else {
$sid = $data;
}
module_load_include('inc', 'webform', 'includes/webform.submissions');
return webform_get_submissions(array(
'sid' => $sid,
));
}
}