webform_rules.rules.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @file
  4. * Functions for rules integration.
  5. */
  6. /**
  7. * Implements of hook_rules_event_info().
  8. * @ingroup rules
  9. */
  10. function webform_rules_rules_event_info() {
  11. return array(
  12. 'webform_rules_submit_as_draft' => array(
  13. 'label' => t('After a webform has been saved as draft'),
  14. 'group' => t('Webform'),
  15. 'variables' => _webform_rules_event_variables(),
  16. 'access callback' => 'rules_node_integration_access',
  17. ),
  18. 'webform_rules_submit' => array(
  19. 'label' => t('After a webform has been submitted'),
  20. 'group' => t('Webform'),
  21. 'variables' => _webform_rules_event_variables(),
  22. 'access callback' => 'rules_node_integration_access',
  23. ),
  24. 'webform_rules_insert' => array(
  25. 'label' => t('After a submission draft has been submitted'),
  26. 'group' => t('Webform'),
  27. 'variables' => _webform_rules_event_variables(),
  28. 'access callback' => 'rules_node_integration_access',
  29. ),
  30. 'webform_rules_update' => array(
  31. 'label' => t('After a webform submission has been updated'),
  32. 'group' => t('Webform'),
  33. 'variables' => _webform_rules_event_variables(),
  34. 'access callback' => 'rules_node_integration_access',
  35. ),
  36. 'webform_rules_delete' => array(
  37. 'label' => t('After a webform submission has been deleted'),
  38. 'group' => t('Webform'),
  39. 'variables' => _webform_rules_event_variables(),
  40. 'access callback' => 'rules_node_integration_access',
  41. ),
  42. );
  43. }
  44. /**
  45. * Implementation of hook_condition_info().
  46. */
  47. function webform_rules_rules_condition_info() {
  48. return array(
  49. 'webform_has_id' => array(
  50. 'label' => t('Webform has name'),
  51. 'parameter' => array(
  52. 'form_id' => array(
  53. 'type' => 'text',
  54. 'label' => t('The form id of the submitted form'),
  55. ),
  56. 'selected_webform' => array(
  57. 'type' => 'list<text>',
  58. 'label' => t('Webforms'),
  59. 'options list' => 'webform_rules_get_webforms',
  60. 'description' => t('The name of the webform to check for.'),
  61. 'restriction' => 'input',
  62. ),
  63. ),
  64. 'help' => t('This condition just compares two texts. It returns TRUE, if both texts are equal.'),
  65. 'group' => t('Webform'),
  66. 'base' => 'webform_rules_condition_webform_has_id',
  67. ),
  68. );
  69. }
  70. /**
  71. * Implements hook_rules_data_info().
  72. */
  73. function webform_rules_data_info() {
  74. return array(
  75. 'webform_data' => array(
  76. 'label' => t('webform data'),
  77. 'group' => t('Webform'),
  78. 'token type' => 'webform',
  79. 'property info' => array(),
  80. ),
  81. );
  82. }
  83. /**
  84. * Function to compare the form id of the submitted webform with the selected
  85. * form.
  86. */
  87. function webform_rules_condition_webform_has_id($form_id, $selected_webform) {
  88. if (is_array($selected_webform)) {
  89. return in_array($form_id, $selected_webform);
  90. }
  91. elseif (is_string($selected_webform)) {
  92. return $form_id == $selected_webform;
  93. }
  94. return FALSE;
  95. }
  96. /**
  97. * Get an option list of all webforms.
  98. */
  99. function webform_rules_get_webforms() {
  100. // Get a list of all webform-enabled content types.
  101. $webform_types = webform_variable_get('webform_node_types');
  102. // Get a list of all nodes that are configured to use a webform.
  103. $query = db_select('node', 'n')
  104. ->fields('n', array('nid', 'title'))
  105. ->condition('n.type', $webform_types, 'IN');
  106. // Join to limi result list to node that really have a webform.
  107. $query->join('webform', 'w', 'n.nid = w.nid');
  108. // Get the result list.
  109. $results = $query->execute();
  110. $options = array();
  111. foreach ($results as $record) {
  112. $options["webform-client-form-{$record->nid}"] = $record->title;
  113. }
  114. return $options;
  115. }
  116. /**
  117. * Helper function for event variables.
  118. *
  119. * @return
  120. * All available variables for the rules events provided by webform_rules.
  121. */
  122. function _webform_rules_event_variables() {
  123. return array(
  124. 'user' => array(
  125. 'type' => 'user',
  126. 'label' => t('User, who submitted the webform'),
  127. ),
  128. 'node' => array(
  129. 'type' => 'node',
  130. 'label' => t('The webform node'),
  131. ),
  132. 'data' => array(
  133. 'type' => 'webform',
  134. 'label' => t('The submitted webform data'),
  135. ),
  136. 'form_id' => array(
  137. 'type' => 'text',
  138. 'label' => t('The form id of the submitted form'),
  139. 'hidden' => TRUE,
  140. ),
  141. 'selected_webform' => array(
  142. 'type' => 'list<text>',
  143. 'label' => t('Webforms'),
  144. 'options list' => 'webform_rules_get_webforms',
  145. 'description' => t('The name of the webform to check for.'),
  146. 'restriction' => 'input',
  147. 'hidden' => TRUE,
  148. 'optional' => TRUE,
  149. ),
  150. );
  151. }