1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Manages validation rules administration UI.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Menu callback function.
|
10
|
*
|
11
|
* Shows an overview of the existing validation rules and the option to add a
|
12
|
* rule.
|
13
|
*/
|
14
|
function webform_validation_manage($node) {
|
15
|
$rules = webform_validation_get_node_rules($node->nid);
|
16
|
$output = array();
|
17
|
$output['webform_validation_manage_overview_form'] = drupal_get_form('webform_validation_manage_overview_form', $rules, $node);
|
18
|
$output['webform_validation_manage_add_rule'] = array(
|
19
|
'#theme' => 'webform_validation_manage_add_rule',
|
20
|
'#nid' => $node->nid,
|
21
|
);
|
22
|
return $output;
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
* Get the list of rules associated with the webform.
|
27
|
*
|
28
|
* @deprecated in webform_validation:7.x-1.18 and is removed from webform_validation:7.x-2.0. Use webform_validation_get_node_rules().
|
29
|
* @see https://www.drupal.org/project/webform_validation/issues/3104316
|
30
|
*/
|
31
|
function webform_validation_get_webform_rules($node) {
|
32
|
if (in_array($node->type, webform_variable_get('webform_node_types'))) {
|
33
|
return webform_validation_get_node_rules($node->nid);
|
34
|
}
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Themable function to list and re-order the rules assigned to a webform.
|
39
|
*/
|
40
|
function theme_webform_validation_manage_overview_form($variables) {
|
41
|
$form = $variables['form'];
|
42
|
$header = array(
|
43
|
t('Rule name'),
|
44
|
t('Validator'),
|
45
|
t('Components'),
|
46
|
t('Weight'), array(
|
47
|
'data' => t('Operations'),
|
48
|
'colspan' => 2,
|
49
|
),
|
50
|
);
|
51
|
|
52
|
$rows = array();
|
53
|
foreach (element_children($form) as $rule) {
|
54
|
$row = array();
|
55
|
foreach (element_children($form[$rule]) as $item) {
|
56
|
// Unset the titles of the form elements, since we are displaying them in
|
57
|
// a table with a header.
|
58
|
unset($form[$rule][$item]['#title']);
|
59
|
// Add a class to the weight field.
|
60
|
$form[$rule]['weight']['#attributes']['class'] = array('ruleid-weight');
|
61
|
$row[] = array(
|
62
|
'data' => drupal_render($form[$rule][$item]),
|
63
|
);
|
64
|
}
|
65
|
if (count($row) > 1) {
|
66
|
$rows[] = array(
|
67
|
'data' => $row,
|
68
|
'class' => array('draggable'),
|
69
|
);
|
70
|
}
|
71
|
// Hide any fieldsets, since we are displaying the form in a table.
|
72
|
if (isset($form[$rule]['#type']) && $form[$rule]['#type'] === 'fieldset') {
|
73
|
hide($form[$rule]);
|
74
|
}
|
75
|
}
|
76
|
$drag = TRUE;
|
77
|
if (!$rows) {
|
78
|
$drag = FALSE;
|
79
|
$rows[][] = array(
|
80
|
'data' => t('No validation rules available.'),
|
81
|
'colspan' => 5,
|
82
|
);
|
83
|
}
|
84
|
$output = theme('table', array(
|
85
|
'header' => $header,
|
86
|
'rows' => $rows,
|
87
|
'attributes' => array(
|
88
|
'id' => 'webform-validation-overview-form',
|
89
|
),
|
90
|
));
|
91
|
$output .= drupal_render_children($form);
|
92
|
if ($drag) {
|
93
|
drupal_add_tabledrag('webform-validation-overview-form', 'order', 'sibling', 'ruleid-weight');
|
94
|
}
|
95
|
return $output;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Form to list and reorder the rules assigned to a webform.
|
100
|
*/
|
101
|
function webform_validation_manage_overview_form($form, &$form_state, $rules, $node) {
|
102
|
$form = array();
|
103
|
$form['#tree'] = TRUE;
|
104
|
$validators = webform_validation_get_validators_selection();
|
105
|
|
106
|
foreach ($rules as $rule) {
|
107
|
$component_info = webform_validation_rule_components_basic($rule['components']);
|
108
|
$form[$rule['ruleid']] = array(
|
109
|
'#type' => 'fieldset',
|
110
|
);
|
111
|
$form[$rule['ruleid']]['name'] = array(
|
112
|
'#type' => 'item',
|
113
|
'#title' => t('Name'),
|
114
|
'#markup' => check_plain($rule['rulename']),
|
115
|
);
|
116
|
$form[$rule['ruleid']]['validator'] = array(
|
117
|
'#type' => 'item',
|
118
|
'#title' => t('Validator'),
|
119
|
'#markup' => $validators[$rule['validator']],
|
120
|
);
|
121
|
$form[$rule['ruleid']]['components'] = array(
|
122
|
'#type' => 'item',
|
123
|
'#title' => t('Components'),
|
124
|
'#markup' => theme('item_list', array('items' => $component_info)),
|
125
|
);
|
126
|
$form[$rule['ruleid']]['weight'] = array(
|
127
|
'#type' => 'weight',
|
128
|
'#title' => t('Weight'),
|
129
|
'#default_value' => $rule['weight'],
|
130
|
'#description' => t('Optional. By changing the order of validation rules, you can use code that relies on earlier validation functions being completed.'),
|
131
|
);
|
132
|
$form[$rule['ruleid']]['actions'] = array('#type' => 'actions');
|
133
|
$form[$rule['ruleid']]['actions']['edit'] = array(
|
134
|
'#type' => 'item',
|
135
|
'#markup' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
|
136
|
);
|
137
|
$form[$rule['ruleid']]['actions']['delete'] = array(
|
138
|
'#type' => 'item',
|
139
|
'#markup' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
|
140
|
);
|
141
|
}
|
142
|
if (count($rules) > 1) {
|
143
|
$form['submit'] = array(
|
144
|
'#type' => 'submit',
|
145
|
'#value' => t('Save rule order'),
|
146
|
);
|
147
|
}
|
148
|
|
149
|
return $form;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Submit function for rule overview form.
|
154
|
*/
|
155
|
function webform_validation_manage_overview_form_submit($form, $form_state) {
|
156
|
// Save the rule weights.
|
157
|
foreach ($form_state['values'] as $ruleid => $value) {
|
158
|
if (is_numeric($ruleid)) {
|
159
|
$update = db_update('webform_validation_rule')
|
160
|
->fields(array(
|
161
|
'weight' => $value['weight'],
|
162
|
))
|
163
|
->condition('ruleid', $ruleid)
|
164
|
->execute();
|
165
|
}
|
166
|
}
|
167
|
drupal_set_message(t('The order of the validation rules has been saved.'));
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Callback function to add or edit a validation rule.
|
172
|
*/
|
173
|
function webform_validation_manage_rule($form, $form_state, $node, $action, $validator, $rule = NULL) {
|
174
|
$form = array();
|
175
|
$rule_validator = webform_validation_get_validator_info($validator);
|
176
|
|
177
|
if ($action === 'edit') {
|
178
|
$title = t('Edit validation rule %title', array('%title' => $rule['rulename']));
|
179
|
}
|
180
|
else {
|
181
|
$title = t('Add validation rule');
|
182
|
}
|
183
|
drupal_set_title($title, PASS_THROUGH);
|
184
|
|
185
|
$form['rule']['validator'] = array(
|
186
|
'#type' => 'hidden',
|
187
|
'#value' => $validator,
|
188
|
);
|
189
|
|
190
|
$form['rule']['action'] = array(
|
191
|
'#type' => 'hidden',
|
192
|
'#value' => $action,
|
193
|
);
|
194
|
|
195
|
if ($action == 'edit' && $rule) {
|
196
|
$form['rule']['ruleid'] = array(
|
197
|
'#type' => 'hidden',
|
198
|
'#value' => $rule['ruleid'],
|
199
|
);
|
200
|
|
201
|
$form['rule']['nid'] = array(
|
202
|
'#type' => 'hidden',
|
203
|
'#value' => $rule['nid'],
|
204
|
);
|
205
|
}
|
206
|
else {
|
207
|
$form['rule']['nid'] = array(
|
208
|
'#type' => 'hidden',
|
209
|
'#value' => $node->nid,
|
210
|
);
|
211
|
}
|
212
|
|
213
|
$form['rule']['type'] = array(
|
214
|
'#type' => 'item',
|
215
|
'#title' => t('Rule type'),
|
216
|
'#markup' => $rule_validator['name'],
|
217
|
);
|
218
|
|
219
|
$form['rule']['rulename'] = array(
|
220
|
'#type' => 'textfield',
|
221
|
'#title' => t('Rule name'),
|
222
|
'#default_value' => isset($rule['rulename']) ? $rule['rulename'] : NULL,
|
223
|
'#required' => TRUE,
|
224
|
'#size' => 60,
|
225
|
'#maxlength' => 255,
|
226
|
'#weight' => 1,
|
227
|
);
|
228
|
|
229
|
$form['rule']['rule_components'] = array(
|
230
|
'#type' => 'checkboxes',
|
231
|
'#title' => t('Components'),
|
232
|
'#weight' => 3,
|
233
|
'#description' => t('Select the components to be validated by this validation rule'),
|
234
|
'#options' => webform_validation_get_webform_components($node, $validator),
|
235
|
'#default_value' => isset($rule['components']) ? array_keys($rule['components']) : array(),
|
236
|
);
|
237
|
|
238
|
if (!empty($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
|
239
|
$valid_types = array('textfield', 'textarea');
|
240
|
$type = isset($rule_validator['custom_data']['type']) ? $rule_validator['custom_data']['type'] : 'textfield';
|
241
|
if (!in_array($type, $valid_types)) {
|
242
|
$type = 'textfield';
|
243
|
}
|
244
|
|
245
|
$required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
|
246
|
$form['rule']['data'] = array(
|
247
|
'#type' => $type,
|
248
|
'#title' => $rule_validator['custom_data']['label'],
|
249
|
'#description' => $rule_validator['custom_data']['description'],
|
250
|
'#required' => (bool) $required,
|
251
|
'#size' => 60,
|
252
|
'#maxlength' => NULL,
|
253
|
'#default_value' => isset($rule['data']) ? $rule['data'] : NULL,
|
254
|
'#weight' => 4,
|
255
|
);
|
256
|
}
|
257
|
|
258
|
if (!empty($rule_validator['negatable'])) {
|
259
|
$form['rule']['negate'] = array(
|
260
|
'#type' => 'checkbox',
|
261
|
'#title' => t('Negate rule'),
|
262
|
'#description' => t('Validate the inverse of the rule.'),
|
263
|
'#default_value' => isset($rule['negate']) ? $rule['negate'] : NULL,
|
264
|
'#weight' => 5,
|
265
|
);
|
266
|
}
|
267
|
|
268
|
if (!empty($rule_validator['custom_error'])) {
|
269
|
$form['rule']['error_message'] = array(
|
270
|
'#type' => 'textfield',
|
271
|
'#title' => t('Custom error message'),
|
272
|
'#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
|
273
|
'#required' => TRUE,
|
274
|
'#size' => 60,
|
275
|
'#maxlength' => 2048,
|
276
|
'#default_value' => isset($rule['error_message']) ? $rule['error_message'] : NULL,
|
277
|
'#weight' => 5,
|
278
|
);
|
279
|
}
|
280
|
|
281
|
$form['rule']['submit'] = array(
|
282
|
'#type' => 'submit',
|
283
|
'#value' => isset($rule['ruleid']) ? t('Save rule') : t('Add rule'),
|
284
|
'#weight' => 25,
|
285
|
);
|
286
|
|
287
|
$destination = drupal_get_destination();
|
288
|
$form['rule']['cancel'] = array(
|
289
|
'#markup' => l(t('Cancel'), $destination['destination']),
|
290
|
'#weight' => 26,
|
291
|
);
|
292
|
|
293
|
return $form;
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* Validation handler to add / edit a rule.
|
298
|
*/
|
299
|
function webform_validation_manage_rule_validate($form, &$form_state) {
|
300
|
$values = $form_state['values'];
|
301
|
if ($values['action'] == 'edit') {
|
302
|
if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
|
303
|
form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
|
304
|
}
|
305
|
}
|
306
|
|
307
|
$rule_validator = webform_validation_get_validator_info($values['validator']);
|
308
|
|
309
|
// Validate custom data.
|
310
|
if (!empty($values['data']) && !empty($rule_validator['custom_data']['validate_regex']) && !preg_match($rule_validator['custom_data']['validate_regex'], $values['data'])) {
|
311
|
form_set_error('data', $rule_validator['custom_data']['label'] . ' ' . t('is invalid.'));
|
312
|
}
|
313
|
|
314
|
$selected_components = count(array_filter($values['rule_components']));
|
315
|
// Check validator min_components and min_components property when they are
|
316
|
// equal.
|
317
|
if (isset($rule_validator['min_components']) && isset($rule_validator['max_components']) && $rule_validator['min_components'] === $rule_validator['max_components'] && $selected_components !== $rule_validator['min_components']) {
|
318
|
form_set_error('rule_components', format_plural($rule_validator['min_components'], 'You need to select exactly @count component.', 'You need to select exactly @count components.'));
|
319
|
}
|
320
|
// Check validator min_components property.
|
321
|
elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
|
322
|
form_set_error('rule_components', format_plural($rule_validator['min_components'], 'You need to select at least @count component.', 'You need to select at least @count components.'));
|
323
|
}
|
324
|
// Check validator max_components property.
|
325
|
elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
|
326
|
form_set_error('rule_components', format_plural($rule_validator['max_components'], 'You can select @count component at most.', 'You can select @count components at most.'));
|
327
|
}
|
328
|
}
|
329
|
|
330
|
/**
|
331
|
* Submit handler to add / edit a rule.
|
332
|
*/
|
333
|
function webform_validation_manage_rule_submit($form, &$form_state) {
|
334
|
$values = $form_state['values'];
|
335
|
webform_validation_rule_save($values);
|
336
|
}
|
337
|
|
338
|
/**
|
339
|
* Get a filtered list of components for a specific webform.
|
340
|
*
|
341
|
* List is filtered by the validator settings.
|
342
|
*/
|
343
|
function webform_validation_get_webform_components($node, $validator) {
|
344
|
form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
|
345
|
|
346
|
$ret = array();
|
347
|
$components = $node->webform['components'];
|
348
|
if ($components) {
|
349
|
$valid_components = webform_validation_valid_component_types($validator);
|
350
|
$component_names = webform_component_list($node, NULL, 'path');
|
351
|
foreach ($components as $cid => $component) {
|
352
|
if (in_array($component['type'], $valid_components)) {
|
353
|
$ret[$cid] = $component_names[$cid];
|
354
|
}
|
355
|
}
|
356
|
}
|
357
|
return $ret;
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* Confirmation form to delete a rule.
|
362
|
*/
|
363
|
function webform_validation_delete_rule($form, &$form_state, $rule) {
|
364
|
if (isset($rule['ruleid'])) {
|
365
|
$form['ruleid'] = array(
|
366
|
'#type' => 'value',
|
367
|
'#value' => $rule['ruleid'],
|
368
|
);
|
369
|
}
|
370
|
|
371
|
return confirm_form(
|
372
|
$form,
|
373
|
t('Are you sure you want to delete the rule %name?', array('%name' => $rule['rulename'])),
|
374
|
'node/' . $rule['nid'] . '/webform/validation',
|
375
|
NULL,
|
376
|
t('Delete'));
|
377
|
}
|
378
|
|
379
|
/**
|
380
|
* Submit handler to delete a rule.
|
381
|
*/
|
382
|
function webform_validation_delete_rule_submit($form, &$form_state) {
|
383
|
$ruleid = $form_state['values']['ruleid'];
|
384
|
module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
|
385
|
webform_dynamic_delete_rule($ruleid);
|
386
|
}
|