1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
81b16cc2
|
Assos Assos
|
* Add validation rules to webforms.
|
6 |
85ad3d82
|
Assos Assos
|
*/
|
7 |
|
|
|
8 |
81b16cc2
|
Assos Assos
|
include_once 'webform_validation.validators.inc';
|
9 |
|
|
include_once 'webform_validation.rules.inc';
|
10 |
85ad3d82
|
Assos Assos
|
|
11 |
|
|
/**
|
12 |
|
|
* Implements hook_menu().
|
13 |
|
|
*/
|
14 |
|
|
function webform_validation_menu() {
|
15 |
|
|
$items = array();
|
16 |
|
|
|
17 |
|
|
$items['node/%webform_menu/webform/validation'] = array(
|
18 |
|
|
'title' => 'Form validation',
|
19 |
|
|
'page callback' => 'webform_validation_manage',
|
20 |
|
|
'page arguments' => array(1),
|
21 |
|
|
'access callback' => 'node_access',
|
22 |
|
|
'access arguments' => array('update', 1),
|
23 |
|
|
'file' => 'webform_validation.admin.inc',
|
24 |
|
|
'weight' => 3,
|
25 |
|
|
'type' => MENU_LOCAL_TASK,
|
26 |
|
|
);
|
27 |
|
|
|
28 |
|
|
$items['node/%webform_menu/webform/validation/add/%'] = array(
|
29 |
|
|
'title' => 'Add validation',
|
30 |
|
|
'page callback' => 'drupal_get_form',
|
31 |
|
|
'page arguments' => array('webform_validation_manage_rule', 1, 'add', 5),
|
32 |
|
|
'access callback' => 'node_access',
|
33 |
|
|
'access arguments' => array('update', 1),
|
34 |
|
|
'file' => 'webform_validation.admin.inc',
|
35 |
|
|
'type' => MENU_CALLBACK,
|
36 |
|
|
);
|
37 |
|
|
|
38 |
|
|
$items['node/%webform_menu/webform/validation/edit/%/%webform_validation_rule'] = array(
|
39 |
|
|
'title' => 'Edit rule',
|
40 |
|
|
'page callback' => 'drupal_get_form',
|
41 |
|
|
'page arguments' => array('webform_validation_manage_rule', 1, 'edit', 5, 6),
|
42 |
|
|
'access callback' => 'node_access',
|
43 |
|
|
'access arguments' => array('update', 1),
|
44 |
|
|
'file' => 'webform_validation.admin.inc',
|
45 |
|
|
'type' => MENU_CALLBACK,
|
46 |
|
|
);
|
47 |
|
|
|
48 |
|
|
$items['node/%webform_menu/webform/validation/delete/%webform_validation_rule'] = array(
|
49 |
|
|
'title' => 'Delete rule',
|
50 |
|
|
'page callback' => 'drupal_get_form',
|
51 |
|
|
'page arguments' => array('webform_validation_delete_rule', 5),
|
52 |
|
|
'access callback' => 'node_access',
|
53 |
|
|
'access arguments' => array('update', 1),
|
54 |
|
|
'file' => 'webform_validation.admin.inc',
|
55 |
|
|
'type' => MENU_CALLBACK,
|
56 |
|
|
);
|
57 |
|
|
|
58 |
|
|
return $items;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
81b16cc2
|
Assos Assos
|
* Loads validation rule from menu parameter.
|
63 |
85ad3d82
|
Assos Assos
|
*/
|
64 |
|
|
function webform_validation_rule_load($ruleid) {
|
65 |
|
|
return webform_validation_get_rule($ruleid);
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Implements hook_theme().
|
70 |
|
|
*/
|
71 |
|
|
function webform_validation_theme() {
|
72 |
|
|
return array(
|
73 |
|
|
'webform_validation_manage_add_rule' => array(
|
74 |
|
|
'variables' => array(
|
75 |
|
|
'nid' => NULL,
|
76 |
|
|
),
|
77 |
|
|
),
|
78 |
a2baadd1
|
Assos Assos
|
'webform_validation_manage_overview_form' => array(
|
79 |
|
|
'render element' => 'form',
|
80 |
85ad3d82
|
Assos Assos
|
),
|
81 |
|
|
);
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
/**
|
85 |
76bdcd04
|
Assos Assos
|
* Implements hook_form_BASE_FORM_ID_alter().
|
86 |
85ad3d82
|
Assos Assos
|
*/
|
87 |
76bdcd04
|
Assos Assos
|
function webform_validation_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
|
88 |
|
|
$form['#validate'][] = 'webform_validation_validate';
|
89 |
85ad3d82
|
Assos Assos
|
}
|
90 |
|
|
|
91 |
a2baadd1
|
Assos Assos
|
/**
|
92 |
|
|
* Implements hook_i18n_string_info().
|
93 |
|
|
*/
|
94 |
|
|
function webform_validation_i18n_string_info() {
|
95 |
|
|
$groups = array();
|
96 |
|
|
$groups['webform_validation'] = array(
|
97 |
|
|
'title' => t('Webform Validation'),
|
98 |
|
|
'description' => t('Translatable strings for webform validation translation'),
|
99 |
81b16cc2
|
Assos Assos
|
// This group doesn't have strings with format.
|
100 |
|
|
'format' => FALSE,
|
101 |
|
|
// This group cannot list all strings.
|
102 |
|
|
'list' => FALSE,
|
103 |
a2baadd1
|
Assos Assos
|
'refresh callback' => 'webform_validation_i18n_string_refresh',
|
104 |
|
|
);
|
105 |
|
|
return $groups;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
85ad3d82
|
Assos Assos
|
/**
|
109 |
81b16cc2
|
Assos Assos
|
* Webform validation handler to validate against the given rules.
|
110 |
85ad3d82
|
Assos Assos
|
*/
|
111 |
|
|
function webform_validation_validate($form, &$form_state) {
|
112 |
4f315dab
|
Assos Assos
|
$static_error_messages = &drupal_static(__FUNCTION__, array());
|
113 |
85ad3d82
|
Assos Assos
|
$page_count = 1;
|
114 |
|
|
$nid = $form_state['values']['details']['nid'];
|
115 |
|
|
$node = node_load($nid);
|
116 |
|
|
$values = isset($form_state['values']['submitted']) ? $form_state['values']['submitted'] : NULL;
|
117 |
|
|
$flat_values = _webform_client_form_submit_flatten($node, $values);
|
118 |
|
|
$rules = webform_validation_get_node_rules($nid);
|
119 |
|
|
$sid = empty($form_state['values']['details']['sid']) ? 0 : $form_state['values']['details']['sid'];
|
120 |
|
|
|
121 |
81b16cc2
|
Assos Assos
|
// Get number of pages for this webform.
|
122 |
85ad3d82
|
Assos Assos
|
if (isset($form_state['webform']['page_count'])) {
|
123 |
|
|
$page_count = $form_state['webform']['page_count'];
|
124 |
|
|
}
|
125 |
|
|
elseif (isset($form_state['storage']['page_count'])) {
|
126 |
|
|
$page_count = $form_state['storage']['page_count'];
|
127 |
|
|
}
|
128 |
|
|
|
129 |
81b16cc2
|
Assos Assos
|
// Filter out rules that don't apply to this step in the multistep form.
|
130 |
85ad3d82
|
Assos Assos
|
if ($values && $page_count && $page_count > 1) {
|
131 |
81b16cc2
|
Assos Assos
|
$validators = webform_validation_get_validators();
|
132 |
|
|
foreach ($rules as $ruleid => $rule) {
|
133 |
|
|
// Skip the rule if it does not have any components on the current page.
|
134 |
|
|
if (!array_intersect_key($flat_values, $rule['components'])) {
|
135 |
|
|
unset($rules[$ruleid]);
|
136 |
|
|
}
|
137 |
|
|
// For validators that require at least 2 components, skip the rule if any
|
138 |
|
|
// of the components are on a page past the current page.
|
139 |
|
|
elseif (isset($validators[$rule['validator']]['min_components']) && $validators[$rule['validator']]['min_components'] > 1) {
|
140 |
|
|
foreach (array_keys($rule['components']) as $cid) {
|
141 |
|
|
if ($node->webform['components'][$cid]['page_num'] > $form_state['webform']['page_num']) {
|
142 |
|
|
unset($rules[$ruleid]);
|
143 |
|
|
break;
|
144 |
85ad3d82
|
Assos Assos
|
}
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
if ($rules) {
|
151 |
e4c061ad
|
Assos Assos
|
// Remove hidden components.
|
152 |
a2bb1a14
|
Assos Assos
|
if (defined('WebformConditionals::componentShown')) {
|
153 |
|
|
// New conditionals system.
|
154 |
|
|
$sorter = webform_get_conditional_sorter($node);
|
155 |
|
|
// If the form was retrieved from the form cache, the conditionals may not
|
156 |
|
|
// have been executed yet.
|
157 |
|
|
if (!$sorter->isExecuted()) {
|
158 |
|
|
$sorter->executeConditionals(array(), 0);
|
159 |
|
|
}
|
160 |
76bdcd04
|
Assos Assos
|
foreach ($node->webform['components'] as $key => $component) {
|
161 |
1f623f01
|
Assos Assos
|
if ($sorter->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
|
162 |
9df8b457
|
Assos Assos
|
unset($flat_values[$key]);
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
a2bb1a14
|
Assos Assos
|
}
|
166 |
|
|
else {
|
167 |
|
|
// Old conditionals system removed in Webform 7.x-4.8.
|
168 |
81b16cc2
|
Assos Assos
|
// Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE.
|
169 |
|
|
// Define if needed.
|
170 |
a2bb1a14
|
Assos Assos
|
if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
|
171 |
|
|
define('WEBFORM_CONDITIONAL_INCLUDE', 1);
|
172 |
|
|
}
|
173 |
76bdcd04
|
Assos Assos
|
foreach ($node->webform['components'] as $key => $component) {
|
174 |
81b16cc2
|
Assos Assos
|
// In Webform 7.x-3.x, _webform_client_form_rule_check() returns
|
175 |
|
|
// boolean.
|
176 |
9df8b457
|
Assos Assos
|
// Cast to int so that the function behaves as it does in 7.x-4.x.
|
177 |
|
|
if (isset($flat_values[$key]) && (int) _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) {
|
178 |
|
|
unset($flat_values[$key]);
|
179 |
|
|
}
|
180 |
e4c061ad
|
Assos Assos
|
}
|
181 |
|
|
}
|
182 |
a2bb1a14
|
Assos Assos
|
|
183 |
85ad3d82
|
Assos Assos
|
foreach ($rules as $rule) {
|
184 |
81b16cc2
|
Assos Assos
|
// Create a list of components that need validation against this rule
|
185 |
|
|
// (component id => user submitted value).
|
186 |
85ad3d82
|
Assos Assos
|
$items = array();
|
187 |
|
|
foreach ($rule['components'] as $cid => $component) {
|
188 |
a2baadd1
|
Assos Assos
|
if (array_key_exists($cid, $flat_values)) {
|
189 |
85ad3d82
|
Assos Assos
|
$items[$cid] = $flat_values[$cid];
|
190 |
|
|
}
|
191 |
|
|
}
|
192 |
|
|
$rule['sid'] = $sid;
|
193 |
81b16cc2
|
Assos Assos
|
// Have the submitted values validated.
|
194 |
76bdcd04
|
Assos Assos
|
$errors = module_invoke_all("webform_validation_validate", $rule['validator'], webform_validation_prefix_keys($items), webform_validation_prefix_keys($node->webform['components']), $rule);
|
195 |
85ad3d82
|
Assos Assos
|
if ($errors) {
|
196 |
|
|
$errors = webform_validation_unprefix_keys($errors);
|
197 |
76bdcd04
|
Assos Assos
|
// Create hook_webform_validation_validate_alter(). Allow other modules
|
198 |
|
|
// to alter error messages.
|
199 |
|
|
$context = array(
|
200 |
|
|
'validator_name' => $rule['validator'],
|
201 |
|
|
'items' => $items,
|
202 |
|
|
'components' => $node->webform['components'],
|
203 |
|
|
'rule' => $rule,
|
204 |
|
|
);
|
205 |
|
|
drupal_alter('webform_validation_validate', $errors, $context);
|
206 |
|
|
|
207 |
85ad3d82
|
Assos Assos
|
foreach ($errors as $item_key => $error) {
|
208 |
81b16cc2
|
Assos Assos
|
// Do not set error message if an identical message has already been
|
209 |
|
|
// set.
|
210 |
4f315dab
|
Assos Assos
|
if (in_array($error, $static_error_messages, TRUE)) {
|
211 |
|
|
continue;
|
212 |
|
|
}
|
213 |
|
|
$static_error_messages[] = $error;
|
214 |
|
|
|
215 |
81b16cc2
|
Assos Assos
|
// Build the proper form element error key, taking into account
|
216 |
|
|
// hierarchy.
|
217 |
76bdcd04
|
Assos Assos
|
$error_key = 'submitted][' . webform_validation_parent_tree($item_key, $node->webform['components']) . $node->webform['components'][$item_key]['form_key'];
|
218 |
a2baadd1
|
Assos Assos
|
if (is_array($error)) {
|
219 |
|
|
foreach ($error as $sub_item_key => $sub_error) {
|
220 |
|
|
form_set_error($error_key . '][' . $sub_item_key, $sub_error);
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
else {
|
224 |
81b16cc2
|
Assos Assos
|
// filter_xss() is run in _webform_validation_i18n_error_message().
|
225 |
|
|
// @ignore security_form_set_error.
|
226 |
a2baadd1
|
Assos Assos
|
form_set_error($error_key, $error);
|
227 |
|
|
}
|
228 |
85ad3d82
|
Assos Assos
|
}
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
}
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
/**
|
235 |
81b16cc2
|
Assos Assos
|
* Helper function to get all field keys (including fields in fieldsets).
|
236 |
|
|
*
|
237 |
65389548
|
Assos Assos
|
* @deprecated in webform_validation:7.x-1.14 and is removed from
|
238 |
|
|
* webform_validation:7.x-2.0. No longer used.
|
239 |
|
|
* @see https://www.drupal.org/project/webform_validation/issues/2841817
|
240 |
85ad3d82
|
Assos Assos
|
*/
|
241 |
|
|
function webform_validation_get_field_keys($submitted, $node) {
|
242 |
|
|
static $fields = array();
|
243 |
|
|
foreach (element_children($submitted) as $child) {
|
244 |
|
|
if (is_array($submitted[$child]) && element_children($submitted[$child])) {
|
245 |
81b16cc2
|
Assos Assos
|
// Only keep searching recursively if it's a fieldset.
|
246 |
85ad3d82
|
Assos Assos
|
$group_components = _webform_validation_get_group_types();
|
247 |
|
|
if (in_array(_webform_validation_get_component_type($node, $child), $group_components)) {
|
248 |
|
|
webform_validation_get_field_keys($submitted[$child], $node);
|
249 |
|
|
}
|
250 |
|
|
else {
|
251 |
|
|
$fields[$child] = $child;
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
}
|
255 |
|
|
else {
|
256 |
|
|
$fields[$child] = $child;
|
257 |
|
|
}
|
258 |
|
|
}
|
259 |
|
|
return $fields;
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
/**
|
263 |
76bdcd04
|
Assos Assos
|
* Recursively add the parents for the element.
|
264 |
|
|
*
|
265 |
|
|
* These are used as the first argument to form_set_error().
|
266 |
85ad3d82
|
Assos Assos
|
*/
|
267 |
|
|
function webform_validation_parent_tree($cid, $components) {
|
268 |
|
|
$output = '';
|
269 |
|
|
if ($pid = $components[$cid]['pid']) {
|
270 |
|
|
$output .= webform_validation_parent_tree($pid, $components);
|
271 |
|
|
$output .= $components[$pid]['form_key'] . '][';
|
272 |
|
|
}
|
273 |
|
|
return $output;
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
/**
|
277 |
81b16cc2
|
Assos Assos
|
* Get array of formkeys for all components that have been assigned to a rule.
|
278 |
|
|
*
|
279 |
65389548
|
Assos Assos
|
* @deprecated in webform_validation:7.x-1.14 and is removed from
|
280 |
|
|
* webform_validation:7.x-2.0. No longer used.
|
281 |
|
|
* @see https://www.drupal.org/project/webform_validation/issues/2841817
|
282 |
85ad3d82
|
Assos Assos
|
*/
|
283 |
|
|
function webform_validation_rule_get_formkeys($rule) {
|
284 |
|
|
$formkeys = array();
|
285 |
|
|
if (isset($rule['components'])) {
|
286 |
|
|
foreach ($rule['components'] as $cid => $component) {
|
287 |
|
|
$formkeys[] = $component['form_key'];
|
288 |
|
|
}
|
289 |
|
|
}
|
290 |
|
|
return $formkeys;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
/**
|
294 |
76bdcd04
|
Assos Assos
|
* Prefix numeric array keys to avoid them being reindexed.
|
295 |
|
|
*
|
296 |
|
|
* Reindexing done in module_invoke_all().
|
297 |
|
|
*
|
298 |
|
|
* Opposite of webform_validation_unprefix_keys().
|
299 |
85ad3d82
|
Assos Assos
|
*/
|
300 |
|
|
function webform_validation_prefix_keys($arr) {
|
301 |
|
|
$ret = array();
|
302 |
|
|
foreach ($arr as $k => $v) {
|
303 |
|
|
$ret['item_' . $k] = $v;
|
304 |
|
|
}
|
305 |
|
|
return $ret;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/**
|
309 |
76bdcd04
|
Assos Assos
|
* Undo prefixing numeric array keys.
|
310 |
|
|
*
|
311 |
|
|
* Opposite of webform_validation_prefix_keys().
|
312 |
85ad3d82
|
Assos Assos
|
*/
|
313 |
|
|
function webform_validation_unprefix_keys($arr) {
|
314 |
|
|
$ret = array();
|
315 |
|
|
foreach ($arr as $k => $v) {
|
316 |
|
|
$new_key = str_replace('item_', '', $k);
|
317 |
|
|
$ret[$new_key] = $v;
|
318 |
|
|
}
|
319 |
|
|
return $ret;
|
320 |
|
|
}
|
321 |
81b16cc2
|
Assos Assos
|
|
322 |
85ad3d82
|
Assos Assos
|
/**
|
323 |
81b16cc2
|
Assos Assos
|
* Theme the 'add rule' list.
|
324 |
85ad3d82
|
Assos Assos
|
*/
|
325 |
|
|
function theme_webform_validation_manage_add_rule($variables) {
|
326 |
|
|
$nid = $variables['nid'];
|
327 |
|
|
$output = '';
|
328 |
|
|
$validators = webform_validation_get_validators();
|
329 |
|
|
|
330 |
|
|
if ($validators) {
|
331 |
a2baadd1
|
Assos Assos
|
$results = db_query('SELECT DISTINCT type FROM {webform_component} WHERE nid = :nid', array('nid' => $nid));
|
332 |
|
|
$types = array();
|
333 |
|
|
while ($item = $results->fetch()) {
|
334 |
|
|
$types[] = $item->type;
|
335 |
|
|
}
|
336 |
|
|
|
337 |
85ad3d82
|
Assos Assos
|
$output = '<h3>' . t('Add a validation rule') . '</h3>';
|
338 |
|
|
$output .= '<dl>';
|
339 |
|
|
foreach ($validators as $validator_key => $validator_info) {
|
340 |
76bdcd04
|
Assos Assos
|
$validator_types = webform_validation_valid_component_types($validator_key);
|
341 |
|
|
$title = $validator_info['name'];
|
342 |
|
|
if (array_intersect($types, $validator_types)) {
|
343 |
|
|
$url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
|
344 |
|
|
$title = l($title, $url, array('query' => drupal_get_destination()));
|
345 |
a2baadd1
|
Assos Assos
|
$component_list_postfix = '';
|
346 |
|
|
}
|
347 |
|
|
else {
|
348 |
5c451ca3
|
Assos Assos
|
$component_list_postfix = '; ' . t('none present in this form');
|
349 |
85ad3d82
|
Assos Assos
|
}
|
350 |
a2baadd1
|
Assos Assos
|
$item = '<dt>' . $title . '</dt>';
|
351 |
|
|
$item .= '<dd>';
|
352 |
|
|
$item .= $validator_info['description'];
|
353 |
76bdcd04
|
Assos Assos
|
$item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_types) . $component_list_postfix)) . '</dd>';
|
354 |
85ad3d82
|
Assos Assos
|
$output .= $item;
|
355 |
|
|
}
|
356 |
|
|
$output .= '</dl>';
|
357 |
|
|
}
|
358 |
|
|
return $output;
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
/**
|
362 |
|
|
* Implements hook_webform_validation().
|
363 |
|
|
*/
|
364 |
|
|
function webform_validation_webform_validation($type, $op, $data) {
|
365 |
|
|
if ($type == 'rule' && in_array($op, array('add', 'edit'))) {
|
366 |
e4c061ad
|
Assos Assos
|
if (module_exists('i18n_string') && isset($data['error_message'])) {
|
367 |
a2baadd1
|
Assos Assos
|
i18n_string_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
|
368 |
85ad3d82
|
Assos Assos
|
}
|
369 |
|
|
}
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
/**
|
373 |
|
|
* Implements hook_node_insert().
|
374 |
|
|
*/
|
375 |
|
|
function webform_validation_node_insert($node) {
|
376 |
5c451ca3
|
Assos Assos
|
if (module_exists('clone') && in_array($node->type, webform_variable_get('webform_node_types'))) {
|
377 |
|
|
webform_validation_node_clone($node);
|
378 |
85ad3d82
|
Assos Assos
|
}
|
379 |
|
|
}
|
380 |
|
|
|
381 |
|
|
/**
|
382 |
|
|
* Implements hook_node_delete().
|
383 |
|
|
*/
|
384 |
|
|
function webform_validation_node_delete($node) {
|
385 |
|
|
$rules = webform_validation_get_node_rules($node->nid);
|
386 |
|
|
if ($rules) {
|
387 |
|
|
foreach (array_keys($rules) as $ruleid) {
|
388 |
|
|
webform_dynamic_delete_rule($ruleid);
|
389 |
|
|
}
|
390 |
|
|
}
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
/**
|
394 |
81b16cc2
|
Assos Assos
|
* Adds support for node_clone module.
|
395 |
85ad3d82
|
Assos Assos
|
*/
|
396 |
|
|
function webform_validation_node_clone($node) {
|
397 |
5c451ca3
|
Assos Assos
|
if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
|
398 |
|
|
return;
|
399 |
|
|
}
|
400 |
85ad3d82
|
Assos Assos
|
if (isset($node->clone_from_original_nid)) {
|
401 |
|
|
$original_nid = $node->clone_from_original_nid;
|
402 |
81b16cc2
|
Assos Assos
|
// Get existing rules for original node.
|
403 |
85ad3d82
|
Assos Assos
|
$rules = webform_validation_get_node_rules($original_nid);
|
404 |
|
|
if ($rules) {
|
405 |
|
|
foreach ($rules as $orig_ruleid => $rule) {
|
406 |
|
|
unset($rule['ruleid']);
|
407 |
|
|
$rule['action'] = 'add';
|
408 |
81b16cc2
|
Assos Assos
|
// Attach existing rules to new node.
|
409 |
|
|
$rule['nid'] = $node->nid;
|
410 |
85ad3d82
|
Assos Assos
|
$rule['rule_components'] = $rule['components'];
|
411 |
|
|
webform_validation_rule_save($rule);
|
412 |
|
|
}
|
413 |
|
|
}
|
414 |
|
|
}
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
/**
|
418 |
81b16cc2
|
Assos Assos
|
* Save a validation rule.
|
419 |
|
|
*
|
420 |
|
|
* Data comes from the admin form or nodeapi function in case of node clone.
|
421 |
a2baadd1
|
Assos Assos
|
*
|
422 |
|
|
* @param array $values
|
423 |
|
|
* An associative array containing:
|
424 |
|
|
* - action: "add" or "edit".
|
425 |
|
|
* - ruleid: ID of the rule to edit. Do not set for "add".
|
426 |
|
|
* - nid: Node ID of the Webform.
|
427 |
|
|
* - validator: Machine name of the validator used by this validation rule.
|
428 |
|
|
* - rulename: Human-readable name for this validation rule.
|
429 |
|
|
* - rule_components: An array in which the keys and the values are the cid's
|
430 |
|
|
* of the Webform components that this rule applies to.
|
431 |
|
|
*
|
432 |
|
|
* @return int
|
433 |
|
|
* The $ruleid of the rule added or edited.
|
434 |
85ad3d82
|
Assos Assos
|
*/
|
435 |
81b16cc2
|
Assos Assos
|
function webform_validation_rule_save(array $values) {
|
436 |
a2baadd1
|
Assos Assos
|
if ($values['action'] === 'add') {
|
437 |
|
|
$primary_keys = array();
|
438 |
|
|
}
|
439 |
|
|
elseif ($values['action'] === 'edit') {
|
440 |
|
|
$primary_keys = array('ruleid');
|
441 |
85ad3d82
|
Assos Assos
|
}
|
442 |
a2baadd1
|
Assos Assos
|
else {
|
443 |
|
|
return FALSE;
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
drupal_write_record('webform_validation_rule', $values, $primary_keys);
|
447 |
85ad3d82
|
Assos Assos
|
|
448 |
a2baadd1
|
Assos Assos
|
// Delete existing component records for this ruleid.
|
449 |
|
|
if ($values['action'] === 'edit') {
|
450 |
85ad3d82
|
Assos Assos
|
db_delete('webform_validation_rule_components')
|
451 |
a2baadd1
|
Assos Assos
|
->condition('ruleid', $values['ruleid'])
|
452 |
|
|
->execute();
|
453 |
85ad3d82
|
Assos Assos
|
}
|
454 |
a2baadd1
|
Assos Assos
|
|
455 |
|
|
$components = array_filter($values['rule_components']);
|
456 |
|
|
if ($values['ruleid'] && $components) {
|
457 |
|
|
webform_validation_save_rule_components($values['ruleid'], $components);
|
458 |
|
|
module_invoke_all('webform_validation', 'rule', $values['action'], $values);
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
return $values['ruleid'];
|
462 |
85ad3d82
|
Assos Assos
|
}
|
463 |
|
|
|
464 |
|
|
/**
|
465 |
a2baadd1
|
Assos Assos
|
* Save components attached to a specific rule.
|
466 |
|
|
*
|
467 |
|
|
* @param int $ruleid
|
468 |
|
|
* The ruleid of the rule being saved.
|
469 |
|
|
* @param array $components
|
470 |
81b16cc2
|
Assos Assos
|
* An array in which the keys are the cid's of the components attached to the
|
471 |
|
|
* rule.
|
472 |
|
|
*
|
473 |
a2baadd1
|
Assos Assos
|
* @return array
|
474 |
|
|
* An array of the return statuses for each query keyed by cid.
|
475 |
85ad3d82
|
Assos Assos
|
*/
|
476 |
81b16cc2
|
Assos Assos
|
function webform_validation_save_rule_components($ruleid, array $components) {
|
477 |
a2baadd1
|
Assos Assos
|
$return_status = array();
|
478 |
85ad3d82
|
Assos Assos
|
foreach ($components as $cid => $component) {
|
479 |
a2baadd1
|
Assos Assos
|
$return_status[$cid] = db_merge('webform_validation_rule_components')
|
480 |
|
|
->key(array(
|
481 |
|
|
'ruleid' => $ruleid,
|
482 |
|
|
'cid' => $cid,
|
483 |
|
|
))
|
484 |
|
|
->fields(array(
|
485 |
|
|
'ruleid' => $ruleid,
|
486 |
|
|
'cid' => $cid,
|
487 |
|
|
))
|
488 |
|
|
->execute();
|
489 |
85ad3d82
|
Assos Assos
|
}
|
490 |
a2baadd1
|
Assos Assos
|
return $return_status;
|
491 |
85ad3d82
|
Assos Assos
|
}
|
492 |
|
|
|
493 |
|
|
/**
|
494 |
81b16cc2
|
Assos Assos
|
* Given a webform node, get the component type based on a given component key.
|
495 |
85ad3d82
|
Assos Assos
|
*/
|
496 |
|
|
function _webform_validation_get_component_type($node, $component_key) {
|
497 |
|
|
if ($node->webform['components']) {
|
498 |
|
|
foreach ($node->webform['components'] as $component) {
|
499 |
|
|
if ($component['form_key'] == $component_key) {
|
500 |
|
|
return $component['type'];
|
501 |
|
|
}
|
502 |
|
|
}
|
503 |
|
|
}
|
504 |
|
|
return FALSE;
|
505 |
|
|
}
|
506 |
|
|
|
507 |
|
|
/**
|
508 |
81b16cc2
|
Assos Assos
|
* Get all webform components that are defined as a group.
|
509 |
85ad3d82
|
Assos Assos
|
*/
|
510 |
|
|
function _webform_validation_get_group_types() {
|
511 |
|
|
$types = array();
|
512 |
|
|
foreach (webform_components() as $name => $component) {
|
513 |
|
|
if (isset($component['features']['group']) && $component['features']['group']) {
|
514 |
|
|
$types[] = $name;
|
515 |
|
|
}
|
516 |
|
|
}
|
517 |
|
|
return $types;
|
518 |
|
|
}
|
519 |
|
|
|
520 |
|
|
/**
|
521 |
|
|
* Implements hook_webform_validator_alter().
|
522 |
|
|
*/
|
523 |
|
|
function webform_validation_webform_validator_alter(&$validators) {
|
524 |
81b16cc2
|
Assos Assos
|
// Add support for the Select (or Other) module.
|
525 |
85ad3d82
|
Assos Assos
|
if (module_exists('select_or_other')) {
|
526 |
81b16cc2
|
Assos Assos
|
// If this module exists, all select components can now except user input.
|
527 |
|
|
// Thus we provide those components the same rules as a textfield.
|
528 |
85ad3d82
|
Assos Assos
|
if ($validators) {
|
529 |
|
|
foreach ($validators as $validator_name => $validator_info) {
|
530 |
|
|
if (in_array('textfield', $validator_info['component_types'])) {
|
531 |
|
|
$validators[$validator_name]['component_types'][] = 'select';
|
532 |
|
|
}
|
533 |
a2baadd1
|
Assos Assos
|
$validators[$validator_name]['component_types'] = array_unique($validators[$validator_name]['component_types']);
|
534 |
85ad3d82
|
Assos Assos
|
}
|
535 |
|
|
}
|
536 |
|
|
}
|
537 |
|
|
}
|
538 |
65389548
|
Assos Assos
|
|
539 |
|
|
/**
|
540 |
|
|
* Implements hook_uuid_node_features_export_alter().
|
541 |
|
|
*/
|
542 |
|
|
function webform_validation_uuid_node_features_export_alter(&$data, $node, $module) {
|
543 |
|
|
$nid = reset(entity_get_id_by_uuid('node', array($node->uuid)));
|
544 |
|
|
if (webform_validation_get_node_rules($nid)) {
|
545 |
|
|
$data['dependencies']['webform_validation'] = 'webform_validation';
|
546 |
|
|
}
|
547 |
|
|
}
|
548 |
|
|
|
549 |
|
|
/**
|
550 |
|
|
* Implements hook_uuid_node_features_export_render_alter().
|
551 |
|
|
*/
|
552 |
|
|
function webform_validation_uuid_node_features_export_render_alter(&$export, $node, $module) {
|
553 |
|
|
if (!empty($node->webform)) {
|
554 |
|
|
$rules = webform_validation_get_node_rules_assoc($node->nid);
|
555 |
|
|
foreach ($rules as &$rule) {
|
556 |
|
|
unset($rule['nid']);
|
557 |
|
|
unset($rule['ruleid']);
|
558 |
|
|
}
|
559 |
|
|
$export->webform['validation'] = $rules;
|
560 |
|
|
}
|
561 |
|
|
}
|
562 |
|
|
|
563 |
|
|
/**
|
564 |
|
|
* Implements hook_uuid_entity_uuid_save().
|
565 |
|
|
*/
|
566 |
|
|
function webform_validation_entity_uuid_save($node, $entity_type) {
|
567 |
|
|
if ($entity_type == 'node') {
|
568 |
|
|
if (isset($node->webform['validation'])) {
|
569 |
|
|
$rules = $node->webform['validation'];
|
570 |
|
|
$orig_rules = webform_validation_get_node_rules_assoc($node->nid);
|
571 |
|
|
// Delete obsolete rules.
|
572 |
|
|
$delete = array_diff_key($orig_rules, $rules);
|
573 |
|
|
foreach ($delete as $rule) {
|
574 |
|
|
webform_dynamic_delete_rule($rule['ruleid']);
|
575 |
|
|
}
|
576 |
|
|
// Add new rules.
|
577 |
|
|
$new = array_diff_key($rules, $orig_rules);
|
578 |
|
|
foreach ($new as $rule) {
|
579 |
|
|
$rule['action'] = 'add';
|
580 |
|
|
$rule['nid'] = $node->nid;
|
581 |
|
|
$rule['rule_components'] = $rule['components'];
|
582 |
|
|
webform_validation_rule_save($rule);
|
583 |
|
|
}
|
584 |
|
|
// Update existing rules.
|
585 |
|
|
$existing = array_diff_key($rules, $new + $delete);
|
586 |
|
|
foreach ($existing as $name => $rule) {
|
587 |
|
|
$orig_rule = $orig_rules[$name];
|
588 |
|
|
$rule['nid'] = $orig_rule['nid'];
|
589 |
|
|
$rule['ruleid'] = $orig_rule['ruleid'];
|
590 |
|
|
if ($rule != $orig_rule) {
|
591 |
|
|
$rule['action'] = 'edit';
|
592 |
|
|
$rule['rule_components'] = $rule['components'];
|
593 |
|
|
webform_validation_rule_save($rule);
|
594 |
|
|
}
|
595 |
|
|
}
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
} |