Projet

Général

Profil

Paste
Télécharger (19,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.module @ 8916d7ec

1
<?php
2

    
3
/**
4
 * @file
5
 * Add validation rules to webforms.
6
 */
7

    
8
include_once 'webform_validation.validators.inc';
9
include_once 'webform_validation.rules.inc';
10

    
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
 * Loads validation rule from menu parameter.
63
 */
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
    'webform_validation_manage_overview_form' => array(
79
      'render element' => 'form',
80
    ),
81
  );
82
}
83

    
84
/**
85
 * Implements hook_form_BASE_FORM_ID_alter().
86
 */
87
function webform_validation_form_webform_client_form_alter(&$form, &$form_state, $form_id) {
88
  $form['#validate'][] = 'webform_validation_validate';
89
}
90

    
91
/**
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
    // This group doesn't have strings with format.
100
    'format' => FALSE,
101
    // This group cannot list all strings.
102
    'list' => FALSE,
103
    'refresh callback' => 'webform_validation_i18n_string_refresh',
104
  );
105
  return $groups;
106
}
107

    
108
/**
109
 * Webform validation handler to validate against the given rules.
110
 */
111
function webform_validation_validate($form, &$form_state) {
112
  $static_error_messages = &drupal_static(__FUNCTION__, array());
113
  $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
  // Get number of pages for this webform.
122
  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
  // Filter out rules that don't apply to this step in the multistep form.
130
  if ($values && $page_count && $page_count > 1) {
131
    $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
          }
145
        }
146
      }
147
    }
148
  }
149

    
150
  if ($rules) {
151
    // Remove hidden components.
152
    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
      foreach ($node->webform['components'] as $key => $component) {
161
        if ($sorter->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
162
          unset($flat_values[$key]);
163
        }
164
      }
165
    }
166
    else {
167
      // Old conditionals system removed in Webform 7.x-4.8.
168
      // Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE.
169
      // Define if needed.
170
      if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
171
        define('WEBFORM_CONDITIONAL_INCLUDE', 1);
172
      }
173
      foreach ($node->webform['components'] as $key => $component) {
174
        // In Webform 7.x-3.x, _webform_client_form_rule_check() returns
175
        // boolean.
176
        // 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
      }
181
    }
182

    
183
    foreach ($rules as $rule) {
184
      // Create a list of components that need validation against this rule
185
      // (component id => user submitted value).
186
      $items = array();
187
      foreach ($rule['components'] as $cid => $component) {
188
        if (array_key_exists($cid, $flat_values)) {
189
          $items[$cid] = $flat_values[$cid];
190
        }
191
      }
192
      $rule['sid'] = $sid;
193
      // Have the submitted values validated.
194
      $components = webform_validation_prefix_keys($node->webform['components']);
195
      // Allow translation for all components item names if available.
196
      if (module_exists('webform_localization')) {
197
        module_load_include('inc', 'webform_localization', 'includes/webform_localization.i18n');
198
        foreach ($components as &$component) {
199
          $dummy_element = array(
200
            '#title' => '',
201
          );
202
          _webform_localization_translate_component($dummy_element, $component);
203
          if (isset($dummy_element['#title']) && (string) $dummy_element['#title']) {
204
            $component['name'] = $dummy_element['#title'];
205
          }
206
        }
207
      }
208

    
209
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], webform_validation_prefix_keys($items), $components, $rule);
210
      if ($errors) {
211
        $errors = webform_validation_unprefix_keys($errors);
212
        // Create hook_webform_validation_validate_alter(). Allow other modules
213
        // to alter error messages.
214
        $context = array(
215
          'validator_name' => $rule['validator'],
216
          'items' => $items,
217
          'components' => $node->webform['components'],
218
          'rule' => $rule,
219
        );
220
        drupal_alter('webform_validation_validate', $errors, $context);
221

    
222
        foreach ($errors as $item_key => $error) {
223
          // Do not set error message if an identical message has already been
224
          // set.
225
          if (in_array($error, $static_error_messages, TRUE)) {
226
            continue;
227
          }
228
          $static_error_messages[] = $error;
229

    
230
          // Build the proper form element error key, taking into account
231
          // hierarchy.
232
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $node->webform['components']) . $node->webform['components'][$item_key]['form_key'];
233
          if (is_array($error)) {
234
            foreach ($error as $sub_item_key => $sub_error) {
235
              form_set_error($error_key . '][' . $sub_item_key, $sub_error);
236
            }
237
          }
238
          else {
239
            // filter_xss() is run in _webform_validation_i18n_error_message().
240
            // @ignore security_form_set_error.
241
            form_set_error($error_key, $error);
242
          }
243
        }
244
      }
245
    }
246
  }
247
}
248

    
249
/**
250
 * Helper function to get all field keys (including fields in fieldsets).
251
 *
252
 * @deprecated in webform_validation:7.x-1.14 and is removed from
253
 * webform_validation:7.x-2.0. No longer used.
254
 * @see https://www.drupal.org/project/webform_validation/issues/2841817
255
 */
256
function webform_validation_get_field_keys($submitted, $node) {
257
  static $fields = array();
258
  foreach (element_children($submitted) as $child) {
259
    if (is_array($submitted[$child]) && element_children($submitted[$child])) {
260
      // Only keep searching recursively if it's a fieldset.
261
      $group_components = _webform_validation_get_group_types();
262
      if (in_array(_webform_validation_get_component_type($node, $child), $group_components)) {
263
        webform_validation_get_field_keys($submitted[$child], $node);
264
      }
265
      else {
266
        $fields[$child] = $child;
267
      }
268

    
269
    }
270
    else {
271
      $fields[$child] = $child;
272
    }
273
  }
274
  return $fields;
275
}
276

    
277
/**
278
 * Recursively add the parents for the element.
279
 *
280
 * These are used as the first argument to form_set_error().
281
 */
282
function webform_validation_parent_tree($cid, $components) {
283
  $output = '';
284
  if ($pid = $components[$cid]['pid']) {
285
    $output .= webform_validation_parent_tree($pid, $components);
286
    $output .= $components[$pid]['form_key'] . '][';
287
  }
288
  return $output;
289
}
290

    
291
/**
292
 * Get array of formkeys for all components that have been assigned to a rule.
293
 *
294
 * @deprecated in webform_validation:7.x-1.14 and is removed from
295
 * webform_validation:7.x-2.0. No longer used.
296
 * @see https://www.drupal.org/project/webform_validation/issues/2841817
297
 */
298
function webform_validation_rule_get_formkeys($rule) {
299
  $formkeys = array();
300
  if (isset($rule['components'])) {
301
    foreach ($rule['components'] as $cid => $component) {
302
      $formkeys[] = $component['form_key'];
303
    }
304
  }
305
  return $formkeys;
306
}
307

    
308
/**
309
 * Prefix numeric array keys to avoid them being reindexed.
310
 *
311
 * Reindexing done in module_invoke_all().
312
 *
313
 * Opposite of webform_validation_unprefix_keys().
314
 */
315
function webform_validation_prefix_keys($arr) {
316
  $ret = array();
317
  foreach ($arr as $k => $v) {
318
    $ret['item_' . $k] = $v;
319
  }
320
  return $ret;
321
}
322

    
323
/**
324
 * Undo prefixing numeric array keys.
325
 *
326
 * Opposite of webform_validation_prefix_keys().
327
 */
328
function webform_validation_unprefix_keys($arr) {
329
  $ret = array();
330
  foreach ($arr as $k => $v) {
331
    $new_key = str_replace('item_', '', $k);
332
    $ret[$new_key] = $v;
333
  }
334
  return $ret;
335
}
336

    
337
/**
338
 * Theme the 'add rule' list.
339
 */
340
function theme_webform_validation_manage_add_rule($variables) {
341
  $nid = $variables['nid'];
342
  $output = '';
343
  $validators = webform_validation_get_validators();
344

    
345
  if ($validators) {
346
    $results = db_query('SELECT DISTINCT type FROM {webform_component} WHERE nid = :nid', array('nid' => $nid));
347
    $types = array();
348
    while ($item = $results->fetch()) {
349
      $types[] = $item->type;
350
    }
351

    
352
    $output = '<h3>' . t('Add a validation rule') . '</h3>';
353
    $output .= '<dl>';
354
    foreach ($validators as $validator_key => $validator_info) {
355
      $validator_types = webform_validation_valid_component_types($validator_key);
356
      $title = $validator_info['name'];
357
      if (array_intersect($types, $validator_types)) {
358
        $url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
359
        $title = l($title, $url, array('query' => drupal_get_destination()));
360
        $component_list_postfix = '';
361
      }
362
      else {
363
        $component_list_postfix = '; ' . t('none present in this form');
364
      }
365
      $item = '<dt>' . $title . '</dt>';
366
      $item .= '<dd>';
367
      $item .= $validator_info['description'];
368
      $item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_types) . $component_list_postfix)) . '</dd>';
369
      $output .= $item;
370
    }
371
    $output .= '</dl>';
372
  }
373
  return $output;
374
}
375

    
376
/**
377
 * Implements hook_webform_validation().
378
 */
379
function webform_validation_webform_validation($type, $op, $data) {
380
  if ($type == 'rule' && in_array($op, array('add', 'edit'))) {
381
    if (module_exists('i18n_string') && isset($data['error_message'])) {
382
      i18n_string_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
383
    }
384
  }
385
}
386

    
387
/**
388
 * Implements hook_node_insert().
389
 */
390
function webform_validation_node_insert($node) {
391
  if (module_exists('clone') && in_array($node->type, webform_variable_get('webform_node_types'))) {
392
    webform_validation_node_clone($node);
393
  }
394
}
395

    
396
/**
397
 * Implements hook_node_delete().
398
 */
399
function webform_validation_node_delete($node) {
400
  $rules = webform_validation_get_node_rules($node->nid);
401
  if ($rules) {
402
    foreach (array_keys($rules) as $ruleid) {
403
      webform_dynamic_delete_rule($ruleid);
404
    }
405
  }
406
}
407

    
408
/**
409
 * Adds support for node_clone module.
410
 */
411
function webform_validation_node_clone($node) {
412
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
413
    return;
414
  }
415
  if (isset($node->clone_from_original_nid)) {
416
    $original_nid = $node->clone_from_original_nid;
417
    // Get existing rules for original node.
418
    $rules = webform_validation_get_node_rules($original_nid);
419
    if ($rules) {
420
      foreach ($rules as $orig_ruleid => $rule) {
421
        unset($rule['ruleid']);
422
        $rule['action'] = 'add';
423
        // Attach existing rules to new node.
424
        $rule['nid'] = $node->nid;
425
        $rule['rule_components'] = $rule['components'];
426
        webform_validation_rule_save($rule);
427
      }
428
    }
429
  }
430
}
431

    
432
/**
433
 * Save a validation rule.
434
 *
435
 * Data comes from the admin form or nodeapi function in case of node clone.
436
 *
437
 * @param array $values
438
 *   An associative array containing:
439
 *   - action: "add" or "edit".
440
 *   - ruleid: ID of the rule to edit. Do not set for "add".
441
 *   - nid: Node ID of the Webform.
442
 *   - validator: Machine name of the validator used by this validation rule.
443
 *   - rulename: Human-readable name for this validation rule.
444
 *   - rule_components: An array in which the keys and the values are the cid's
445
 *     of the Webform components that this rule applies to.
446
 *
447
 * @return int
448
 *   The $ruleid of the rule added or edited.
449
 */
450
function webform_validation_rule_save(array $values) {
451
  if ($values['action'] === 'add') {
452
    $primary_keys = array();
453
  }
454
  elseif ($values['action'] === 'edit') {
455
    $primary_keys = array('ruleid');
456
  }
457
  else {
458
    return FALSE;
459
  }
460

    
461
  drupal_write_record('webform_validation_rule', $values, $primary_keys);
462

    
463
  // Delete existing component records for this ruleid.
464
  if ($values['action'] === 'edit') {
465
    db_delete('webform_validation_rule_components')
466
      ->condition('ruleid', $values['ruleid'])
467
      ->execute();
468
  }
469

    
470
  $components = array_filter($values['rule_components']);
471
  if ($values['ruleid'] && $components) {
472
    webform_validation_save_rule_components($values['ruleid'], $components);
473
    module_invoke_all('webform_validation', 'rule', $values['action'], $values);
474
  }
475

    
476
  return $values['ruleid'];
477
}
478

    
479
/**
480
 * Save components attached to a specific rule.
481
 *
482
 * @param int $ruleid
483
 *   The ruleid of the rule being saved.
484
 * @param array $components
485
 *   An array in which the keys are the cid's of the components attached to the
486
 *   rule.
487
 *
488
 * @return array
489
 *   An array of the return statuses for each query keyed by cid.
490
 */
491
function webform_validation_save_rule_components($ruleid, array $components) {
492
  $return_status = array();
493
  foreach ($components as $cid => $component) {
494
    $return_status[$cid] = db_merge('webform_validation_rule_components')
495
      ->key(array(
496
        'ruleid' => $ruleid,
497
        'cid' => $cid,
498
      ))
499
      ->fields(array(
500
        'ruleid' => $ruleid,
501
        'cid' => $cid,
502
      ))
503
      ->execute();
504
  }
505
  return $return_status;
506
}
507

    
508
/**
509
 * Given a webform node, get the component type based on a given component key.
510
 */
511
function _webform_validation_get_component_type($node, $component_key) {
512
  if ($node->webform['components']) {
513
    foreach ($node->webform['components'] as $component) {
514
      if ($component['form_key'] == $component_key) {
515
        return $component['type'];
516
      }
517
    }
518
  }
519
  return FALSE;
520
}
521

    
522
/**
523
 * Get all webform components that are defined as a group.
524
 */
525
function _webform_validation_get_group_types() {
526
  $types = array();
527
  foreach (webform_components() as $name => $component) {
528
    if (isset($component['features']['group']) && $component['features']['group']) {
529
      $types[] = $name;
530
    }
531
  }
532
  return $types;
533
}
534

    
535
/**
536
 * Implements hook_webform_validator_alter().
537
 */
538
function webform_validation_webform_validator_alter(&$validators) {
539
  // Add support for the Select (or Other) module.
540
  if (module_exists('select_or_other')) {
541
    // If this module exists, all select components can now except user input.
542
    // Thus we provide those components the same rules as a textfield.
543
    if ($validators) {
544
      foreach ($validators as $validator_name => $validator_info) {
545
        if (in_array('textfield', $validator_info['component_types'])) {
546
          $validators[$validator_name]['component_types'][] = 'select';
547
        }
548
        $validators[$validator_name]['component_types'] = array_unique($validators[$validator_name]['component_types']);
549
      }
550
    }
551
  }
552
}
553

    
554
/**
555
 * Implements hook_uuid_node_features_export_alter().
556
 */
557
function webform_validation_uuid_node_features_export_alter(&$data, $node, $module) {
558
  $nid = entity_get_id_by_uuid('node', array($node->uuid));
559
  $nid = reset($nid);
560
  if (webform_validation_get_node_rules($nid)) {
561
    $data['dependencies']['webform_validation'] = 'webform_validation';
562
  }
563
}
564

    
565
/**
566
 * Implements hook_uuid_node_features_export_render_alter().
567
 */
568
function webform_validation_uuid_node_features_export_render_alter(&$export, $node, $module) {
569
  if (!empty($node->webform)) {
570
    $rules = webform_validation_get_node_rules_assoc($node->nid);
571
    foreach ($rules as &$rule) {
572
      unset($rule['nid']);
573
      unset($rule['ruleid']);
574
    }
575
    $export->webform['validation'] = $rules;
576
  }
577
}
578

    
579
/**
580
 * Implements hook_uuid_entity_uuid_save().
581
 */
582
function webform_validation_entity_uuid_save($node, $entity_type) {
583
  if ($entity_type == 'node') {
584
    if (isset($node->webform['validation'])) {
585
      $rules = $node->webform['validation'];
586
      $orig_rules = webform_validation_get_node_rules_assoc($node->nid);
587
      // Delete obsolete rules.
588
      $delete = array_diff_key($orig_rules, $rules);
589
      foreach ($delete as $rule) {
590
        webform_dynamic_delete_rule($rule['ruleid']);
591
      }
592
      // Add new rules.
593
      $new = array_diff_key($rules, $orig_rules);
594
      foreach ($new as $rule) {
595
        $rule['action'] = 'add';
596
        $rule['nid'] = $node->nid;
597
        $rule['rule_components'] = $rule['components'];
598
        webform_validation_rule_save($rule);
599
      }
600
      // Update existing rules.
601
      $existing = array_diff_key($rules, $new + $delete);
602
      foreach ($existing as $name => $rule) {
603
        $orig_rule = $orig_rules[$name];
604
        $rule['nid'] = $orig_rule['nid'];
605
        $rule['ruleid'] = $orig_rule['ruleid'];
606
        if ($rule != $orig_rule) {
607
          $rule['action'] = 'edit';
608
          $rule['rule_components'] = $rule['components'];
609
          webform_validation_rule_save($rule);
610
        }
611
      }
612
    }
613
  }
614

    
615
}