forked from prashantdsala/webform_rules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebform_rules.rules.inc
More file actions
316 lines (300 loc) · 9.5 KB
/
Copy pathwebform_rules.rules.inc
File metadata and controls
316 lines (300 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* @file
* Functions for rules integration.
*/
/**
* Implements of hook_rules_event_info().
* @ingroup rules
*/
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',
),
);
}
/**
* Implementation of hook_condition_info().
*/
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',
),
);
}
/**
* Implements hook_rules_action_info().
*
*/
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' => t('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<list>',
),
),
),
);
// Modify description of closing action.
$actions['webform_close']['parameter']['selected_webform']['description'] = t('The name of the webforms to close.');
return $actions;
}
/**
* Implements hook_rules_data_info().
*/
function webform_rules_data_info() {
return array(
'webform_data' => array(
'label' => t('webform data'),
'group' => t('Webform'),
'token type' => 'webform',
'property info' => array(),
),
);
}
/**
* Validation callback for actions 'webform_rules_webform_open' and
* 'webform_rules_webform_close'.
*/
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 to compare the form id of the submitted webform with the selected
* form.
*/
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;
}
/**
* Condition callback to determine whether a node contains a webform.
*/
function webform_rules_condition_node_is_webform($node) {
// Get a list of all nodes that are configured to use a webform.
$query = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.nid', $node->nid);
// Join to limit result list to node that really have a webform.
$query->join('webform', 'w', 'n.nid = w.nid');
// Join {webform_component} to prevent listing unconfigured webforms.
$query->join('webform_component', 'c', 'n.nid = c.nid');
// Get the result list.
if ($query->execute()->rowCount() > 0) {
return TRUE;
}
return FALSE;
}
/**
* Rules action callback to open webforms.
*/
function webform_rules_webform_open($entity = FALSE, $selected_webforms = array()) {
_webform_rules_webform_set_status($entity, $selected_webforms);
}
/**
* Rules action callback to close webforms.
*/
function webform_rules_webform_close($entity = FALSE, $selected_webforms = array()) {
_webform_rules_webform_set_status($entity, $selected_webforms, FALSE);
}
/**
* Helper method to update the status of a webform.
*/
function _webform_rules_webform_set_status($entity = FALSE, $selected_webforms = array(), $open = TRUE) {
if (!empty($entity->nid) && webform_rules_condition_node_is_webform($entity)) {
// Set new status.
$entity->webform['status'] = ($open == TRUE) ? 1 : 0;
}
module_load_include('inc', 'rules', 'modules/entity.eval');
// Try to close all selected webforms.
foreach ($selected_webforms as $form_id) {
// Try to get node id from form_id.
$nid = str_replace('webform-client-form-', '', $form_id);
// Load the node object.
$result = rules_action_entity_fetch('node', $nid, NULL);
$webform = $result['entity_fetched'];
if (!empty($webform->nid) && webform_rules_condition_node_is_webform($webform)) {
// Set new status.
$webform->webform['status'] = ($open == TRUE) ? 1 : 0;
// Save the webform node. Maybe we can find a better way how to save?
$webform->save();
}
}
}
/**
* Rules action to load a list of webform submissions.
*
* @param int $nid
* ID of node to load the submissions for.
* @param int $sid
* (optional) Submission ID.
*
* @return array
* List of loaded webform submissions.
*/
function webform_rules_submissions_load($nid, $sid = NULL) {
// Make sure the needed functions are available.
module_load_include('inc', 'webform', 'includes/webform.submissions');
$filters = array(
'nid' => $nid,
);
if (!empty($sid)) {
$filters['sid'] = $sid;
}
// Fetch submissions.
$submissions = webform_get_submissions($filters);
return array('submissions' => $submissions);
}
/**
* Helper function for event variables.
*
* @return
* All available variables for the rules events provided by webform_rules.
*/
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,
),
);
}
?>