-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebform_validation.admin.inc
342 lines (311 loc) · 11.2 KB
/
webform_validation.admin.inc
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/**
* @file
* Manages validation rules administration UI
*/
/**
* Menu callback function to show an overview of the existing validation rules, and the option to add a rule
*/
function webform_validation_manage($node) {
$rules = webform_validation_get_webform_rules($node);
$output = array();
$output['webform_validation_manage_overview_form'] = drupal_get_form('webform_validation_manage_overview_form', $rules, $node);
$output['webform_validation_manage_add_rule'] = array(
'#theme' => 'webform_validation_manage_add_rule',
'#nid' => $node->nid,
);
return $output;
}
/**
* Get the list of rules associated with the webform
*/
function webform_validation_get_webform_rules($node) {
if (in_array($node->type, webform_variable_get('webform_node_types'))) {
$webform_nid = $node->nid;
$rules = webform_validation_get_node_rules($node->nid);
}
return $rules;
}
/**
* Themable function to list and re-order the rules assigned to a webform
*/
function theme_webform_validation_manage_overview_form($variables) {
$form = $variables['form'];
$header = array(t('Rule name'), t('Validator'), t('Components'), t('Weight'), array(
'data' => t('Operations'),
'colspan' => 2,
));
$rows = array();
foreach (element_children($form) as $rule) {
$row = array();
foreach (element_children($form[$rule]) as $item) {
//unset the titles of the form elements, since we are displaying them in a table with a header.
unset($form[$rule][$item]['#title']);
//add a class to the weight field
$form[$rule]['weight']['#attributes']['class'] = array('ruleid-weight');
$row[] = array(
'data' => drupal_render($form[$rule][$item]),
);
}
if (count($row) > 1) {
$rows[] = array(
'data' => $row,
'class' => array('draggable'),
);
}
// Hide any fieldsets, since we are displaying the form in a table.
if (isset($form[$rule]['#type']) && $form[$rule]['#type'] === 'fieldset') {
hide($form[$rule]);
}
}
$drag = TRUE;
if (!$rows) {
$drag = FALSE;
$rows[][] = array(
'data' => t('No validation rules available.'),
'colspan' => 5,
);
}
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'webform-validation-overview-form')));
$output .= drupal_render_children($form);
if ($drag) {
drupal_add_tabledrag('webform-validation-overview-form', 'order', 'sibling', 'ruleid-weight');
}
return $output;
}
/**
* Form to list and reorder the rules assigned to a webform
*/
function webform_validation_manage_overview_form($form, &$form_state, $rules, $node) {
$form = array();
$form['#tree'] = TRUE;
$validators = webform_validation_get_validators_selection();
foreach ($rules as $rule) {
$component_info = webform_validation_rule_components_basic($rule['components']);
$form[$rule['ruleid']] = array(
'#type' => 'fieldset',
);
$form[$rule['ruleid']]['name'] = array(
'#type' => 'item',
'#title' => t('Name'),
'#markup' => check_plain($rule['rulename']),
);
$form[$rule['ruleid']]['validator'] = array(
'#type' => 'item',
'#title' => t('Validator'),
'#markup' => $validators[$rule['validator']],
);
$form[$rule['ruleid']]['components'] = array(
'#type' => 'item',
'#title' => t('Components'),
'#markup' => theme('item_list', array('items' => $component_info)),
);
$form[$rule['ruleid']]['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $rule['weight'],
'#description' => t('Optional. By changing the order of validation rules, you can use code that relies on earlier validation functions being completed.'),
);
$form[$rule['ruleid']]['actions'] = array('#type' => 'actions');
$form[$rule['ruleid']]['actions']['edit'] = array(
'#type' => 'item',
'#markup' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
);
$form[$rule['ruleid']]['actions']['delete'] = array(
'#type' => 'item',
'#markup' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
);
}
if (count($rules) > 1) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save rule order'),
);
}
return $form;
}
/**
* Submit function for rule overview form.
*/
function webform_validation_manage_overview_form_submit($form, $form_state) {
//Save the rule weights.
foreach ($form_state['values'] as $ruleid => $value) {
if (is_numeric($ruleid)) {
$update = db_update('webform_validation_rule')
->fields(array(
'weight' => $value['weight'],
))
->condition('ruleid', $ruleid)
->execute();
}
}
drupal_set_message(t('The order of the validation rules has been saved.'));
}
/**
* Callback function to add or edit a validation rule
*/
function webform_validation_manage_rule($form, $form_state, $node, $action = 'add', $validator, $rule = NULL) {
$form = array();
$rule_validator = webform_validation_get_validator_info($validator);
$form['rule'] = array(
'#type' => 'fieldset',
'#title' => ($action == 'edit') ? t('Edit rule') : t('Add rule'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['rule']['validator'] = array(
'#type' => 'hidden',
'#value' => $validator,
);
$form['rule']['action'] = array(
'#type' => 'hidden',
'#value' => $action,
);
if ($action == 'edit' && $rule) {
$form['rule']['ruleid'] = array(
'#type' => 'hidden',
'#value' => $rule['ruleid'],
);
$form['rule']['nid'] = array(
'#type' => 'hidden',
'#value' => $rule['nid'],
);
}
else {
$form['rule']['nid'] = array(
'#type' => 'hidden',
'#value' => $node->nid,
);
}
$form['rule']['rulename'] = array(
'#type' => 'textfield',
'#title' => t('Rule name'),
'#default_value' => (isset($rule['rulename'])) ? $rule['rulename'] : '',
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 255,
'#weight' => 1,
);
$form['rule']['rule_components'] = array(
'#type' => 'checkboxes',
'#title' => t('Components'),
'#weight' => 3,
'#description' => t('Select the components to be validated by this validation rule'),
'#options' => webform_validation_get_webform_components($node, $validator),
'#default_value' => (isset($rule['components'])) ? array_keys($rule['components']) : array(),
);
if (isset($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
$required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
$form['rule']['data'] = array(
'#type' => 'textfield',
'#title' => $rule_validator['custom_data']['label'],
'#description' => $rule_validator['custom_data']['description'],
'#required' => ($required !== FALSE) ? TRUE : FALSE,
'#size' => 60,
'#maxlength' => NULL,
'#default_value' => $rule['data'],
'#weight' => 4,
);
}
if (isset($rule_validator['negatable'])) {
$form['rule']['negate'] = array(
'#type' => 'checkbox',
'#title' => t('Negate rule'),
'#description' => t('Validate the inverse of the rule.'),
'#default_value' => $rule['negate'],
'#weight' => 5,
);
}
if (isset($rule_validator['custom_error'])) {
$form['rule']['error_message'] = array(
'#type' => 'textfield',
'#title' => t('Custom error message'),
'#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
'#required' => TRUE,
'#size' => 60,
'#maxlength' => 255,
'#default_value' => $rule['error_message'],
'#weight' => 5,
);
}
$form['rule']['submit'] = array(
'#type' => 'submit',
'#value' => (isset($rule['ruleid'])) ? t('Save rule') : t('Add rule'),
'#weight' => 25,
);
return $form;
}
/**
* Validation handler to add / edit a rule
*/
function webform_validation_manage_rule_validate($form, &$form_state) {
$values = $form_state['values'];
if ($values['action'] == 'edit') {
if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
}
}
$rule_validator = webform_validation_get_validator_info($values['validator']);
$selected_components = count(array_filter($values['rule_components']));
// Check validator min_components and min_components property when they are equal.
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']) {
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'));
}
// check validator min_components property
elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
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'));
}
// check validator max_components property
elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
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'));
}
}
/**
* Submit handler to add / edit a rule
*/
function webform_validation_manage_rule_submit($form, &$form_state) {
$values = $form_state['values'];
webform_validation_rule_save($values);
}
/**
* Get a list of components for a specific webform, filtered by the validator settings
*/
function webform_validation_get_webform_components($node, $validator) {
$ret = array();
$components = $node->webform['components'];
$valid_components = webform_validation_valid_component_types($validator);
if ($components) {
foreach ($components as $cid => $component) {
if (in_array($component['type'], $valid_components)) {
$ret[$cid] = $component['name'];
}
}
}
return $ret;
}
/**
* Confirmation form to delete a rule
*/
function webform_validation_delete_rule($form, &$form_state, $rule) {
if (isset($rule['ruleid'])) {
$form['ruleid'] = array(
'#type' => 'value',
'#value' => $rule['ruleid'],
);
}
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')
);
}
/**
* Submit handler to delete a rule
*/
function webform_validation_delete_rule_submit($form, &$form_state) {
$ruleid = $form_state['values']['ruleid'];
module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
webform_dynamic_delete_rule($ruleid);
}