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