Révision a2baadd1
Ajouté par Assos Assos il y a environ 10 ans
drupal7/sites/all/modules/webform_validation/webform_validation.module | ||
---|---|---|
76 | 76 |
'nid' => NULL, |
77 | 77 |
), |
78 | 78 |
), |
79 |
'webform_validation_manage_overview' => array( |
|
80 |
'variables' => array( |
|
81 |
'rules' => NULL, |
|
82 |
'node' => NULL, |
|
83 |
), |
|
79 |
'webform_validation_manage_overview_form' => array( |
|
80 |
'render element' => 'form', |
|
84 | 81 |
), |
85 | 82 |
); |
86 | 83 |
} |
... | ... | |
94 | 91 |
} |
95 | 92 |
} |
96 | 93 |
|
94 |
/** |
|
95 |
* Implements hook_i18n_string_info(). |
|
96 |
*/ |
|
97 |
function webform_validation_i18n_string_info() { |
|
98 |
$groups = array(); |
|
99 |
$groups['webform_validation'] = array( |
|
100 |
'title' => t('Webform Validation'), |
|
101 |
'description' => t('Translatable strings for webform validation translation'), |
|
102 |
'format' => FALSE, // This group doesn't have strings with format |
|
103 |
'list' => FALSE, // This group cannot list all strings |
|
104 |
'refresh callback' => 'webform_validation_i18n_string_refresh', |
|
105 |
); |
|
106 |
return $groups; |
|
107 |
} |
|
108 |
|
|
97 | 109 |
/** |
98 | 110 |
* Webform validation handler to validate against the given rules |
99 | 111 |
*/ |
... | ... | |
145 | 157 |
// create a list of components that need validation against this rule (component id => user submitted value) |
146 | 158 |
$items = array(); |
147 | 159 |
foreach ($rule['components'] as $cid => $component) { |
148 |
if (isset($flat_values[$cid])) {
|
|
160 |
if (array_key_exists($cid, $flat_values)) {
|
|
149 | 161 |
$items[$cid] = $flat_values[$cid]; |
150 | 162 |
} |
151 | 163 |
} |
... | ... | |
161 | 173 |
foreach ($errors as $item_key => $error) { |
162 | 174 |
// build the proper form element error key, taking into account hierarchy |
163 | 175 |
$error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key']; |
164 |
form_set_error($error_key, $error); |
|
176 |
if (is_array($error)) { |
|
177 |
foreach ($error as $sub_item_key => $sub_error) { |
|
178 |
form_set_error($error_key . '][' . $sub_item_key, $sub_error); |
|
179 |
} |
|
180 |
} |
|
181 |
else { |
|
182 |
// @ignore security_form_set_error. filter_xss() is run in _webform_validation_i18n_error_message(). |
|
183 |
form_set_error($error_key, $error); |
|
184 |
} |
|
165 | 185 |
} |
166 | 186 |
} |
167 | 187 |
} |
... | ... | |
248 | 268 |
$validators = webform_validation_get_validators(); |
249 | 269 |
|
250 | 270 |
if ($validators) { |
271 |
$results = db_query('SELECT DISTINCT type FROM {webform_component} WHERE nid = :nid', array('nid' => $nid)); |
|
272 |
$types = array(); |
|
273 |
while ($item = $results->fetch()) { |
|
274 |
$types[] = $item->type; |
|
275 |
} |
|
276 |
|
|
251 | 277 |
$output = '<h3>' . t('Add a validation rule') . '</h3>'; |
252 | 278 |
$output .= '<dl>'; |
253 | 279 |
foreach ($validators as $validator_key => $validator_info) { |
254 |
$item = ''; |
|
255 | 280 |
$url = 'node/' . $nid . '/webform/validation/add/' . $validator_key; |
256 |
$components = ' (' . implode(', ', $validator_info['component_types']) . ')'; |
|
257 |
$item = '<dt>' . l($validator_info['name'], $url, array("query" => drupal_get_destination())) . '</dt>'; |
|
258 |
$item .= '<dd>'; |
|
259 |
if ($validator_info['description']) { |
|
260 |
$item .= $validator_info['description'] . ' '; |
|
281 |
if (array_intersect($types, $validator_info['component_types'])) { |
|
282 |
$title = l($validator_info['name'], $url, array('query' => drupal_get_destination())); |
|
283 |
$component_list_postfix = ''; |
|
284 |
} |
|
285 |
else { |
|
286 |
$title = $validator_info['name']; |
|
287 |
$component_list_postfix = '; none present in this form'; |
|
261 | 288 |
} |
262 |
$item .= $components . '</dd>'; |
|
289 |
$item = '<dt>' . $title . '</dt>'; |
|
290 |
$item .= '<dd>'; |
|
291 |
$item .= $validator_info['description']; |
|
292 |
$item .= ' Works with: ' . implode(', ', $validator_info['component_types']) . $component_list_postfix . '.</dd>'; |
|
263 | 293 |
$output .= $item; |
264 | 294 |
} |
265 | 295 |
$output .= '</dl>'; |
... | ... | |
272 | 302 |
*/ |
273 | 303 |
function webform_validation_webform_validation($type, $op, $data) { |
274 | 304 |
if ($type == 'rule' && in_array($op, array('add', 'edit'))) { |
275 |
if (module_exists('i18nstrings') && isset($data['error_message'])) {
|
|
276 |
i18nstrings_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
|
|
305 |
if (module_exists('i18n') && isset($data['error_message'])) { |
|
306 |
i18n_string_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
|
|
277 | 307 |
} |
278 | 308 |
} |
279 | 309 |
} |
... | ... | |
322 | 352 |
} |
323 | 353 |
|
324 | 354 |
/** |
325 |
* Save a validation rule. Data comes from the admin form |
|
326 |
* or nodeapi function in case of node clone |
|
355 |
* Save a validation rule. Data comes from the admin form or nodeapi function in |
|
356 |
* case of node clone. |
|
357 |
* |
|
358 |
* @param array $values |
|
359 |
* An associative array containing: |
|
360 |
* - action: "add" or "edit". |
|
361 |
* - ruleid: ID of the rule to edit. Do not set for "add". |
|
362 |
* - nid: Node ID of the Webform. |
|
363 |
* - validator: Machine name of the validator used by this validation rule. |
|
364 |
* - rulename: Human-readable name for this validation rule. |
|
365 |
* - rule_components: An array in which the keys and the values are the cid's |
|
366 |
* of the Webform components that this rule applies to. |
|
367 |
* |
|
368 |
* @return int |
|
369 |
* The $ruleid of the rule added or edited. |
|
327 | 370 |
*/ |
328 | 371 |
function webform_validation_rule_save($values) { |
329 |
// save rules data |
|
330 |
if ($values['action'] == 'add') { |
|
331 |
drupal_write_record('webform_validation_rule', $values); |
|
332 |
$ruleid = $values['ruleid']; |
|
333 |
if ($ruleid && array_filter($values['rule_components'])) { |
|
334 |
webform_validation_save_rule_components($ruleid, array_filter($values['rule_components'])); |
|
335 |
module_invoke_all('webform_validation', 'rule', 'add', $values); |
|
336 |
} |
|
372 |
if ($values['action'] === 'add') { |
|
373 |
$primary_keys = array(); |
|
374 |
} |
|
375 |
elseif ($values['action'] === 'edit') { |
|
376 |
$primary_keys = array('ruleid'); |
|
337 | 377 |
} |
378 |
else { |
|
379 |
return FALSE; |
|
380 |
} |
|
381 |
|
|
382 |
drupal_write_record('webform_validation_rule', $values, $primary_keys); |
|
338 | 383 |
|
339 |
if ($values['action'] == 'edit') { |
|
340 |
drupal_write_record('webform_validation_rule', $values, 'ruleid'); |
|
341 |
$ruleid = $values['ruleid']; |
|
342 |
// delete earlier component records for this rule id*/ |
|
384 |
// Delete existing component records for this ruleid. |
|
385 |
if ($values['action'] === 'edit') { |
|
343 | 386 |
db_delete('webform_validation_rule_components') |
344 |
->condition('ruleid', $ruleid) |
|
345 |
->execute(); |
|
346 |
if ($components = array_filter($values['rule_components'])) { |
|
347 |
webform_validation_save_rule_components($ruleid, $components); |
|
348 |
module_invoke_all('webform_validation', 'rule', 'edit', $values); |
|
349 |
} |
|
387 |
->condition('ruleid', $values['ruleid']) |
|
388 |
->execute(); |
|
350 | 389 |
} |
390 |
|
|
391 |
$components = array_filter($values['rule_components']); |
|
392 |
if ($values['ruleid'] && $components) { |
|
393 |
webform_validation_save_rule_components($values['ruleid'], $components); |
|
394 |
module_invoke_all('webform_validation', 'rule', $values['action'], $values); |
|
395 |
} |
|
396 |
|
|
397 |
return $values['ruleid']; |
|
351 | 398 |
} |
352 | 399 |
|
353 | 400 |
/** |
354 |
* Save components attached to a specific rule |
|
401 |
* Save components attached to a specific rule. |
|
402 |
* |
|
403 |
* @param int $ruleid |
|
404 |
* The ruleid of the rule being saved. |
|
405 |
* @param array $components |
|
406 |
* An array in which the keys are the cid's of the components attached to the rule. |
|
407 |
* @return array |
|
408 |
* An array of the return statuses for each query keyed by cid. |
|
355 | 409 |
*/ |
356 | 410 |
function webform_validation_save_rule_components($ruleid, $components) { |
411 |
$return_status = array(); |
|
357 | 412 |
foreach ($components as $cid => $component) { |
358 |
$id = db_insert('webform_validation_rule_components') |
|
359 |
->fields(array( |
|
360 |
'ruleid' => $ruleid, |
|
361 |
'cid' => $cid, |
|
362 |
)) |
|
363 |
->execute(); |
|
413 |
$return_status[$cid] = db_merge('webform_validation_rule_components') |
|
414 |
->key(array( |
|
415 |
'ruleid' => $ruleid, |
|
416 |
'cid' => $cid, |
|
417 |
)) |
|
418 |
->fields(array( |
|
419 |
'ruleid' => $ruleid, |
|
420 |
'cid' => $cid, |
|
421 |
)) |
|
422 |
->execute(); |
|
364 | 423 |
} |
424 |
return $return_status; |
|
365 | 425 |
} |
366 | 426 |
|
367 | 427 |
/** |
... | ... | |
404 | 464 |
if (in_array('textfield', $validator_info['component_types'])) { |
405 | 465 |
$validators[$validator_name]['component_types'][] = 'select'; |
406 | 466 |
} |
467 |
$validators[$validator_name]['component_types'] = array_unique($validators[$validator_name]['component_types']); |
|
407 | 468 |
} |
408 | 469 |
} |
409 | 470 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules