Projet

Général

Profil

Paste
Télécharger (8,45 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.admin.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Manages validation rules administration UI
6
 */
7

    
8
/**
9
 * Menu callback function to show an overview of the existing validation rules, and the option to add a rule
10
 */
11
function webform_validation_manage($node) {
12
  $rules = webform_validation_get_webform_rules($node);
13
  $output = '';
14
  $output .= theme('webform_validation_manage_overview', array('rules' => $rules, 'node' => $node));
15
  $output .= theme('webform_validation_manage_add_rule', array('nid' => $node->nid));
16
  return $output;
17
}
18

    
19
/**
20
 * Get the list of rules associated with the webform
21
 */
22
function webform_validation_get_webform_rules($node) {
23
  if (in_array($node->type, webform_variable_get('webform_node_types'))) {
24
    $webform_nid = $node->nid;
25
    $rules = webform_validation_get_node_rules($node->nid);
26
  }
27
  return $rules;
28
}
29

    
30
/**
31
 * Themable function to list the rules assigned to a webform
32
 */
33
function theme_webform_validation_manage_overview($variables) {
34
  $rules = $variables['rules'];
35
  $node = $variables['node'];
36

    
37
  $header = array(t('Rule name'), t('Validator'), t('Components'), array(
38
      'data' => t('Operations'),
39
      'colspan' => 2,
40
    ));
41
  $validators = webform_validation_get_validators_selection();
42
  if (!empty($rules)) {
43
    foreach ($rules as $rule) {
44
      $component_info = webform_validation_rule_components_basic($rule['components']);
45
      $row = array();
46
      $row[] = array(
47
        'data' => check_plain($rule['rulename']),
48
      );
49
      $row[] = array(
50
        'data' => $validators[$rule['validator']],
51
      );
52
      $row[] = array(
53
        'data' => theme('item_list', array('items' => $component_info)),
54
      );
55
      $row[] = array(
56
        'data' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
57
      );
58
      $row[] = array(
59
        'data' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
60
      );
61
      $rows[] = $row;
62
    }
63
  }
64
  else {
65
    $rows[][] = array(
66
      'data' => t('No validation rules available.'),
67
      'colspan' => 5,
68
    );
69
  }
70

    
71
  return theme('table', array('header' => $header, 'rows' => $rows));
72
}
73

    
74
/**
75
 * Callback function to add or edit a validation rule
76
 */
77
function webform_validation_manage_rule($form, $form_state, $node, $action = 'add', $validator, $rule = NULL) {
78
  $form = array();
79
  $rule_validator = webform_validation_get_validator_info($validator);
80

    
81
  $form['rule'] = array(
82
    '#type' => 'fieldset',
83
    '#title' => ($action == 'edit') ? t('Edit rule') : t('Add rule'),
84
    '#collapsible' => FALSE,
85
    '#collapsed' => FALSE,
86
  );
87

    
88
  $form['rule']['validator'] = array(
89
    '#type' => 'hidden',
90
    '#value' => $validator,
91
  );
92

    
93
  $form['rule']['action'] = array(
94
    '#type' => 'hidden',
95
    '#value' => $action,
96
  );
97

    
98
  if ($action == 'edit' && $rule) {
99
    $form['rule']['ruleid'] = array(
100
      '#type' => 'hidden',
101
      '#value' => $rule['ruleid'],
102
    );
103

    
104
    $form['rule']['nid'] = array(
105
      '#type' => 'hidden',
106
      '#value' => $rule['nid'],
107
    );
108
  }
109
  else {
110
    $form['rule']['nid'] = array(
111
      '#type' => 'hidden',
112
      '#value' => $node->nid,
113
    );
114
  }
115

    
116
  $form['rule']['rulename'] = array(
117
    '#type' => 'textfield',
118
    '#title' => t('Rule name'),
119
    '#default_value' => (isset($rule['rulename'])) ? $rule['rulename'] : '',
120
    '#required' => TRUE,
121
    '#size' => 60,
122
    '#maxlength' => 255,
123
    '#weight' => 1,
124
  );
125

    
126
  $form['rule']['rule_components'] = array(
127
    '#type' => 'checkboxes',
128
    '#title' => t('Components'),
129
    '#weight' => 3,
130
    '#description' => t('Select the components to be validated by this validation rule'),
131
    '#options' => webform_validation_get_webform_components($node, $validator),
132
    '#default_value' => (isset($rule['components'])) ? array_keys($rule['components']) : array(),
133
  );
134

    
135
  if (isset($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
136
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
137
    $form['rule']['data'] = array(
138
      '#type' => 'textfield',
139
      '#title' => $rule_validator['custom_data']['label'],
140
      '#description' => $rule_validator['custom_data']['description'],
141
      '#required' => ($required !== FALSE) ? TRUE : FALSE,
142
      '#size' => 60,
143
      '#maxlength' => NULL,
144
      '#default_value' => $rule['data'],
145
      '#weight' => 4,
146
    );
147
  }
148

    
149
  if (isset($rule_validator['negatable'])) {
150
    $form['rule']['negate'] = array(
151
      '#type' => 'checkbox',
152
      '#title' => t('Negate rule'),
153
      '#description' => t('Validate the inverse of the rule.'),
154
      '#default_value' => $rule['negate'],
155
      '#weight' => 5,
156
    );
157
  }
158

    
159
  if (isset($rule_validator['custom_error'])) {
160
    $form['rule']['error_message'] = array(
161
      '#type' => 'textfield',
162
      '#title' => t('Custom error message'),
163
      '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
164
      '#required' => TRUE,
165
      '#size' => 60,
166
      '#maxlength' => 255,
167
      '#default_value' => $rule['error_message'],
168
      '#weight' => 5,
169
    );
170
  }
171

    
172
  $form['rule']['submit'] = array(
173
    '#type' => 'submit',
174
    '#value' => (isset($rule['ruleid'])) ? t('Save rule') : t('Add rule'),
175
    '#weight' => 25,
176
  );
177

    
178
  return $form;
179
}
180

    
181
/**
182
 * Validation handler to add / edit a rule
183
 */
184
function webform_validation_manage_rule_validate($form, &$form_state) {
185
  $values = $form_state['values'];
186
  if ($values['action'] == 'edit') {
187
    if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
188
      form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
189
    }
190
  }
191

    
192
  $rule_validator = webform_validation_get_validator_info($values['validator']);
193
  $selected_components = count(array_filter($values['rule_components']));
194
  // Check validator min_components and min_components property when they are equal.
195
  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']) {
196
      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'));
197
  }
198
  // check validator min_components property
199
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
200
      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'));
201
  }
202
  // check validator max_components property
203
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
204
      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'));
205
  }
206
}
207

    
208

    
209
/**
210
 * Submit handler to add / edit a rule
211
 */
212
function webform_validation_manage_rule_submit($form, &$form_state) {
213
  $values = $form_state['values'];
214
  webform_validation_rule_save($values);
215
}
216

    
217
/**
218
 * Get a list of components for a specific webform, filtered by the validator settings
219
 */
220
function webform_validation_get_webform_components($node, $validator) {
221
  $ret = array();
222
  $components = $node->webform['components'];
223
  $valid_components = webform_validation_valid_component_types($validator);
224
  if ($components) {
225
    foreach ($components as $cid => $component) {
226
      if (in_array($component['type'], $valid_components)) {
227
        $ret[$cid] = $component['name'];
228
      }
229
    }
230
  }
231
  return $ret;
232
}
233

    
234
/**
235
 * Confirmation form to delete a rule
236
 */
237
function webform_validation_delete_rule($form, &$form_state, $rule) {
238
  if (isset($rule['ruleid'])) {
239
    $form['ruleid'] = array(
240
      '#type' => 'value',
241
      '#value' => $rule['ruleid'],
242
    );
243
  }
244

    
245
  return confirm_form($form,
246
    t('Are you sure you want to delete the rule %name?', array('%name' => $rule['rulename'])),
247
    isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'],
248
    t('This action cannot be undone.'),
249
    t('Delete'),
250
    t('Cancel')
251
  );
252
}
253

    
254
/**
255
 * Submit handler to delete a rule
256
 */
257
function webform_validation_delete_rule_submit($form, &$form_state) {
258
  $ruleid = $form_state['values']['ruleid'];
259
  webform_dynamic_delete_rule($ruleid);
260
  module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
261
}