Projet

Général

Profil

Paste
Télécharger (16,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.module @ 81b16cc2

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_alter().
86
 */
87
function webform_validation_form_alter(&$form, &$form_state, $form_id) {
88
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {
89
    $form['#validate'][] = 'webform_validation_validate';
90
  }
91
}
92

    
93
/**
94
 * Implements hook_i18n_string_info().
95
 */
96
function webform_validation_i18n_string_info() {
97
  $groups = array();
98
  $groups['webform_validation'] = array(
99
    'title' => t('Webform Validation'),
100
    'description' => t('Translatable strings for webform validation translation'),
101
    // This group doesn't have strings with format.
102
    'format' => FALSE,
103
    // This group cannot list all strings.
104
    'list' => FALSE,
105
    'refresh callback' => 'webform_validation_i18n_string_refresh',
106
  );
107
  return $groups;
108
}
109

    
110
/**
111
 * Webform validation handler to validate against the given rules.
112
 */
113
function webform_validation_validate($form, &$form_state) {
114
  $static_error_messages = &drupal_static(__FUNCTION__, array());
115
  $page_count = 1;
116
  $nid = $form_state['values']['details']['nid'];
117
  $node = node_load($nid);
118
  $values = isset($form_state['values']['submitted']) ? $form_state['values']['submitted'] : NULL;
119
  $flat_values = _webform_client_form_submit_flatten($node, $values);
120
  $rules = webform_validation_get_node_rules($nid);
121
  $sid = empty($form_state['values']['details']['sid']) ? 0 : $form_state['values']['details']['sid'];
122

    
123
  // Get number of pages for this webform.
124
  if (isset($form_state['webform']['page_count'])) {
125
    $page_count = $form_state['webform']['page_count'];
126
  }
127
  elseif (isset($form_state['storage']['page_count'])) {
128
    $page_count = $form_state['storage']['page_count'];
129
  }
130

    
131
  // Filter out rules that don't apply to this step in the multistep form.
132
  if ($values && $page_count && $page_count > 1) {
133
    $validators = webform_validation_get_validators();
134
    foreach ($rules as $ruleid => $rule) {
135
      // Skip the rule if it does not have any components on the current page.
136
      if (!array_intersect_key($flat_values, $rule['components'])) {
137
        unset($rules[$ruleid]);
138
      }
139
      // For validators that require at least 2 components, skip the rule if any
140
      // of the components are on a page past the current page.
141
      elseif (isset($validators[$rule['validator']]['min_components']) && $validators[$rule['validator']]['min_components'] > 1) {
142
        foreach (array_keys($rule['components']) as $cid) {
143
          if ($node->webform['components'][$cid]['page_num'] > $form_state['webform']['page_num']) {
144
            unset($rules[$ruleid]);
145
            break;
146
          }
147
        }
148
      }
149
    }
150
  }
151

    
152
  if ($rules) {
153
    $component_definitions = webform_validation_prefix_keys($node->webform['components']);
154

    
155
    // Remove hidden components.
156
    if (defined('WebformConditionals::componentShown')) {
157
      // New conditionals system.
158
      $sorter = webform_get_conditional_sorter($node);
159
      // If the form was retrieved from the form cache, the conditionals may not
160
      // have been executed yet.
161
      if (!$sorter->isExecuted()) {
162
        $sorter->executeConditionals(array(), 0);
163
      }
164
      foreach ($component_definitions as $key => $component) {
165
        if ($sorter->componentVisibility($component['cid'], $component['page_num']) !== WebformConditionals::componentShown) {
166
          unset($flat_values[$key]);
167
        }
168
      }
169
    }
170
    else {
171
      // Old conditionals system removed in Webform 7.x-4.8.
172
      // Webform 7.x-3.x does not define WEBFORM_CONDITIONAL_INCLUDE.
173
      // Define if needed.
174
      if (!defined('WEBFORM_CONDITIONAL_INCLUDE')) {
175
        define('WEBFORM_CONDITIONAL_INCLUDE', 1);
176
      }
177
      foreach ($component_definitions as $key => $component) {
178
        // In Webform 7.x-3.x, _webform_client_form_rule_check() returns
179
        // boolean.
180
        // Cast to int so that the function behaves as it does in 7.x-4.x.
181
        if (isset($flat_values[$key]) && (int) _webform_client_form_rule_check($node, $component, 0, $form_state['values']['submitted']) !== WEBFORM_CONDITIONAL_INCLUDE) {
182
          unset($flat_values[$key]);
183
        }
184
      }
185
    }
186

    
187
    foreach ($rules as $rule) {
188
      // Create a list of components that need validation against this rule
189
      // (component id => user submitted value).
190
      $items = array();
191
      foreach ($rule['components'] as $cid => $component) {
192
        if (array_key_exists($cid, $flat_values)) {
193
          $items[$cid] = $flat_values[$cid];
194
        }
195
      }
196
      // Prefix array keys to avoid reindexing by the module_invoke_all function
197
      // call.
198
      $items = webform_validation_prefix_keys($items);
199
      $rule['sid'] = $sid;
200
      // Have the submitted values validated.
201
      $errors = module_invoke_all("webform_validation_validate", $rule['validator'], $items, $component_definitions, $rule);
202
      if ($errors) {
203
        $errors = webform_validation_unprefix_keys($errors);
204
        $components = webform_validation_unprefix_keys($component_definitions);
205
        foreach ($errors as $item_key => $error) {
206
          // Do not set error message if an identical message has already been
207
          // set.
208
          if (in_array($error, $static_error_messages, TRUE)) {
209
            continue;
210
          }
211
          $static_error_messages[] = $error;
212

    
213
          // Build the proper form element error key, taking into account
214
          // hierarchy.
215
          $error_key = 'submitted][' . webform_validation_parent_tree($item_key, $components) . $components[$item_key]['form_key'];
216
          if (is_array($error)) {
217
            foreach ($error as $sub_item_key => $sub_error) {
218
              form_set_error($error_key . '][' . $sub_item_key, $sub_error);
219
            }
220
          }
221
          else {
222
            // filter_xss() is run in _webform_validation_i18n_error_message().
223
            // @ignore security_form_set_error.
224
            form_set_error($error_key, $error);
225
          }
226
        }
227
      }
228
    }
229
  }
230
}
231

    
232
/**
233
 * Helper function to get all field keys (including fields in fieldsets).
234
 *
235
 * @deprecated No longer used and will be removed in 7.x-2.x.
236
 */
237
function webform_validation_get_field_keys($submitted, $node) {
238
  static $fields = array();
239
  foreach (element_children($submitted) as $child) {
240
    if (is_array($submitted[$child]) && element_children($submitted[$child])) {
241
      // Only keep searching recursively if it's a fieldset.
242
      $group_components = _webform_validation_get_group_types();
243
      if (in_array(_webform_validation_get_component_type($node, $child), $group_components)) {
244
        webform_validation_get_field_keys($submitted[$child], $node);
245
      }
246
      else {
247
        $fields[$child] = $child;
248
      }
249

    
250
    }
251
    else {
252
      $fields[$child] = $child;
253
    }
254
  }
255
  return $fields;
256
}
257

    
258
/**
259
 * Recursively add the parents for the element, to be used as first argument to form_set_error.
260
 */
261
function webform_validation_parent_tree($cid, $components) {
262
  $output = '';
263
  if ($pid = $components[$cid]['pid']) {
264
    $output .= webform_validation_parent_tree($pid, $components);
265
    $output .= $components[$pid]['form_key'] . '][';
266
  }
267
  return $output;
268
}
269

    
270
/**
271
 * Get array of formkeys for all components that have been assigned to a rule.
272
 *
273
 * @deprecated No longer used and will be removed in 7.x-2.x.
274
 */
275
function webform_validation_rule_get_formkeys($rule) {
276
  $formkeys = array();
277
  if (isset($rule['components'])) {
278
    foreach ($rule['components'] as $cid => $component) {
279
      $formkeys[] = $component['form_key'];
280
    }
281
  }
282
  return $formkeys;
283
}
284

    
285
/**
286
 * Prefix numeric array keys to avoid them being reindexed by module_invoke_all.
287
 */
288
function webform_validation_prefix_keys($arr) {
289
  $ret = array();
290
  foreach ($arr as $k => $v) {
291
    $ret['item_' . $k] = $v;
292
  }
293
  return $ret;
294
}
295

    
296
/**
297
 * Undo prefixing numeric array keys to avoid them being reindexed by module_invoke_all.
298
 */
299
function webform_validation_unprefix_keys($arr) {
300
  $ret = array();
301
  foreach ($arr as $k => $v) {
302
    $new_key = str_replace('item_', '', $k);
303
    $ret[$new_key] = $v;
304
  }
305
  return $ret;
306
}
307

    
308
/**
309
 * Theme the 'add rule' list.
310
 */
311
function theme_webform_validation_manage_add_rule($variables) {
312
  $nid = $variables['nid'];
313
  $output = '';
314
  $validators = webform_validation_get_validators();
315

    
316
  if ($validators) {
317
    $results = db_query('SELECT DISTINCT type FROM {webform_component} WHERE nid = :nid', array('nid' => $nid));
318
    $types = array();
319
    while ($item = $results->fetch()) {
320
      $types[] = $item->type;
321
    }
322

    
323
    $output = '<h3>' . t('Add a validation rule') . '</h3>';
324
    $output .= '<dl>';
325
    foreach ($validators as $validator_key => $validator_info) {
326
      $url = 'node/' . $nid . '/webform/validation/add/' . $validator_key;
327
      if (array_intersect($types, $validator_info['component_types'])) {
328
        $title = l($validator_info['name'], $url, array('query' => drupal_get_destination()));
329
        $component_list_postfix = '';
330
      }
331
      else {
332
        $title = $validator_info['name'];
333
        $component_list_postfix = '; ' . t('none present in this form');
334
      }
335
      $item = '<dt>' . $title . '</dt>';
336
      $item .= '<dd>';
337
      $item .= $validator_info['description'];
338
      $item .= ' ' . t('Works with: @component_types.', array('@component_types' => implode(', ', $validator_info['component_types']) . $component_list_postfix)) . '</dd>';
339
      $output .= $item;
340
    }
341
    $output .= '</dl>';
342
  }
343
  return $output;
344
}
345

    
346
/**
347
 * Implements hook_webform_validation().
348
 */
349
function webform_validation_webform_validation($type, $op, $data) {
350
  if ($type == 'rule' && in_array($op, array('add', 'edit'))) {
351
    if (module_exists('i18n_string') && isset($data['error_message'])) {
352
      i18n_string_update('webform_validation:error_message:' . $data['ruleid'] . ':message', $data['error_message']);
353
    }
354
  }
355
}
356

    
357
/**
358
 * Implements hook_node_insert().
359
 */
360
function webform_validation_node_insert($node) {
361
  if (module_exists('clone') && in_array($node->type, webform_variable_get('webform_node_types'))) {
362
    webform_validation_node_clone($node);
363
  }
364
}
365

    
366
/**
367
 * Implements hook_node_delete().
368
 */
369
function webform_validation_node_delete($node) {
370
  $rules = webform_validation_get_node_rules($node->nid);
371
  if ($rules) {
372
    foreach (array_keys($rules) as $ruleid) {
373
      webform_dynamic_delete_rule($ruleid);
374
    }
375
  }
376
}
377

    
378
/**
379
 * Adds support for node_clone module.
380
 */
381
function webform_validation_node_clone($node) {
382
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
383
    return;
384
  }
385
  if (isset($node->clone_from_original_nid)) {
386
    $original_nid = $node->clone_from_original_nid;
387
    // Get existing rules for original node.
388
    $rules = webform_validation_get_node_rules($original_nid);
389
    if ($rules) {
390
      foreach ($rules as $orig_ruleid => $rule) {
391
        unset($rule['ruleid']);
392
        $rule['action'] = 'add';
393
        // Attach existing rules to new node.
394
        $rule['nid'] = $node->nid;
395
        $rule['rule_components'] = $rule['components'];
396
        webform_validation_rule_save($rule);
397
      }
398
    }
399
  }
400
}
401

    
402
/**
403
 * Save a validation rule.
404
 *
405
 * Data comes from the admin form or nodeapi function in case of node clone.
406
 *
407
 * @param array $values
408
 *   An associative array containing:
409
 *   - action: "add" or "edit".
410
 *   - ruleid: ID of the rule to edit. Do not set for "add".
411
 *   - nid: Node ID of the Webform.
412
 *   - validator: Machine name of the validator used by this validation rule.
413
 *   - rulename: Human-readable name for this validation rule.
414
 *   - rule_components: An array in which the keys and the values are the cid's
415
 *     of the Webform components that this rule applies to.
416
 *
417
 * @return int
418
 *   The $ruleid of the rule added or edited.
419
 */
420
function webform_validation_rule_save(array $values) {
421
  if ($values['action'] === 'add') {
422
    $primary_keys = array();
423
  }
424
  elseif ($values['action'] === 'edit') {
425
    $primary_keys = array('ruleid');
426
  }
427
  else {
428
    return FALSE;
429
  }
430

    
431
  drupal_write_record('webform_validation_rule', $values, $primary_keys);
432

    
433
  // Delete existing component records for this ruleid.
434
  if ($values['action'] === 'edit') {
435
    db_delete('webform_validation_rule_components')
436
      ->condition('ruleid', $values['ruleid'])
437
      ->execute();
438
  }
439

    
440
  $components = array_filter($values['rule_components']);
441
  if ($values['ruleid'] && $components) {
442
    webform_validation_save_rule_components($values['ruleid'], $components);
443
    module_invoke_all('webform_validation', 'rule', $values['action'], $values);
444
  }
445

    
446
  return $values['ruleid'];
447
}
448

    
449
/**
450
 * Save components attached to a specific rule.
451
 *
452
 * @param int $ruleid
453
 *   The ruleid of the rule being saved.
454
 * @param array $components
455
 *   An array in which the keys are the cid's of the components attached to the
456
 *   rule.
457
 *
458
 * @return array
459
 *   An array of the return statuses for each query keyed by cid.
460
 */
461
function webform_validation_save_rule_components($ruleid, array $components) {
462
  $return_status = array();
463
  foreach ($components as $cid => $component) {
464
    $return_status[$cid] = db_merge('webform_validation_rule_components')
465
      ->key(array(
466
        'ruleid' => $ruleid,
467
        'cid' => $cid,
468
      ))
469
      ->fields(array(
470
        'ruleid' => $ruleid,
471
        'cid' => $cid,
472
      ))
473
      ->execute();
474
  }
475
  return $return_status;
476
}
477

    
478
/**
479
 * Given a webform node, get the component type based on a given component key.
480
 */
481
function _webform_validation_get_component_type($node, $component_key) {
482
  if ($node->webform['components']) {
483
    foreach ($node->webform['components'] as $component) {
484
      if ($component['form_key'] == $component_key) {
485
        return $component['type'];
486
      }
487
    }
488
  }
489
  return FALSE;
490
}
491

    
492
/**
493
 * Get all webform components that are defined as a group.
494
 */
495
function _webform_validation_get_group_types() {
496
  $types = array();
497
  foreach (webform_components() as $name => $component) {
498
    if (isset($component['features']['group']) && $component['features']['group']) {
499
      $types[] = $name;
500
    }
501
  }
502
  return $types;
503
}
504

    
505
/**
506
 * Implements hook_webform_validator_alter().
507
 */
508
function webform_validation_webform_validator_alter(&$validators) {
509
  // Add support for the Select (or Other) module.
510
  if (module_exists('select_or_other')) {
511
    // If this module exists, all select components can now except user input.
512
    // Thus we provide those components the same rules as a textfield.
513
    if ($validators) {
514
      foreach ($validators as $validator_name => $validator_info) {
515
        if (in_array('textfield', $validator_info['component_types'])) {
516
          $validators[$validator_name]['component_types'][] = 'select';
517
        }
518
        $validators[$validator_name]['component_types'] = array_unique($validators[$validator_name]['component_types']);
519
      }
520
    }
521
  }
522
}