Projet

Général

Profil

Paste
Télécharger (12,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / webform_validation.admin.inc @ 65389548

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5 81b16cc2 Assos Assos
 * Manages validation rules administration UI.
6 85ad3d82 Assos Assos
 */
7
8
/**
9 76bdcd04 Assos Assos
 * Menu callback function.
10
 *
11
 * Shows an overview of the existing validation rules and the option to add a
12
 * rule.
13 85ad3d82 Assos Assos
 */
14
function webform_validation_manage($node) {
15
  $rules = webform_validation_get_webform_rules($node);
16 a2baadd1 Assos Assos
  $output = array();
17
  $output['webform_validation_manage_overview_form'] = drupal_get_form('webform_validation_manage_overview_form', $rules, $node);
18
  $output['webform_validation_manage_add_rule'] = array(
19
    '#theme' => 'webform_validation_manage_add_rule',
20
    '#nid' => $node->nid,
21
  );
22 85ad3d82 Assos Assos
  return $output;
23
}
24
25
/**
26 81b16cc2 Assos Assos
 * Get the list of rules associated with the webform.
27 85ad3d82 Assos Assos
 */
28
function webform_validation_get_webform_rules($node) {
29
  if (in_array($node->type, webform_variable_get('webform_node_types'))) {
30
    $webform_nid = $node->nid;
31
    $rules = webform_validation_get_node_rules($node->nid);
32
  }
33
  return $rules;
34
}
35
36
/**
37 81b16cc2 Assos Assos
 * Themable function to list and re-order the rules assigned to a webform.
38 85ad3d82 Assos Assos
 */
39 a2baadd1 Assos Assos
function theme_webform_validation_manage_overview_form($variables) {
40
  $form = $variables['form'];
41 81b16cc2 Assos Assos
  $header = array(
42
    t('Rule name'),
43
    t('Validator'),
44
    t('Components'),
45
    t('Weight'), array(
46
      'data' => t('Operations'),
47
      'colspan' => 2,
48
    ),
49
  );
50 85ad3d82 Assos Assos
51 a2baadd1 Assos Assos
  $rows = array();
52
  foreach (element_children($form) as $rule) {
53
    $row = array();
54
    foreach (element_children($form[$rule]) as $item) {
55 81b16cc2 Assos Assos
      // Unset the titles of the form elements, since we are displaying them in
56
      // a table with a header.
57 a2baadd1 Assos Assos
      unset($form[$rule][$item]['#title']);
58 81b16cc2 Assos Assos
      // Add a class to the weight field.
59 a2baadd1 Assos Assos
      $form[$rule]['weight']['#attributes']['class'] = array('ruleid-weight');
60 85ad3d82 Assos Assos
      $row[] = array(
61 a2baadd1 Assos Assos
        'data' => drupal_render($form[$rule][$item]),
62 85ad3d82 Assos Assos
      );
63 a2baadd1 Assos Assos
    }
64
    if (count($row) > 1) {
65
      $rows[] = array(
66
        'data' => $row,
67
        'class' => array('draggable'),
68 85ad3d82 Assos Assos
      );
69 a2baadd1 Assos Assos
    }
70
    // Hide any fieldsets, since we are displaying the form in a table.
71
    if (isset($form[$rule]['#type']) && $form[$rule]['#type'] === 'fieldset') {
72
      hide($form[$rule]);
73 85ad3d82 Assos Assos
    }
74
  }
75 a2baadd1 Assos Assos
  $drag = TRUE;
76
  if (!$rows) {
77
    $drag = FALSE;
78 85ad3d82 Assos Assos
    $rows[][] = array(
79
      'data' => t('No validation rules available.'),
80
      'colspan' => 5,
81
    );
82
  }
83 81b16cc2 Assos Assos
  $output = theme('table', array(
84
    'header' => $header,
85
    'rows' => $rows,
86
    'attributes' => array(
87
      'id' => 'webform-validation-overview-form',
88
    ),
89
  ));
90 a2baadd1 Assos Assos
  $output .= drupal_render_children($form);
91
  if ($drag) {
92
    drupal_add_tabledrag('webform-validation-overview-form', 'order', 'sibling', 'ruleid-weight');
93
  }
94
  return $output;
95
}
96
97
/**
98 81b16cc2 Assos Assos
 * Form to list and reorder the rules assigned to a webform.
99 a2baadd1 Assos Assos
 */
100
function webform_validation_manage_overview_form($form, &$form_state, $rules, $node) {
101
  $form = array();
102
  $form['#tree'] = TRUE;
103
  $validators = webform_validation_get_validators_selection();
104 85ad3d82 Assos Assos
105 a2baadd1 Assos Assos
  foreach ($rules as $rule) {
106
    $component_info = webform_validation_rule_components_basic($rule['components']);
107
    $form[$rule['ruleid']] = array(
108
      '#type' => 'fieldset',
109
    );
110
    $form[$rule['ruleid']]['name'] = array(
111
      '#type' => 'item',
112
      '#title' => t('Name'),
113
      '#markup' => check_plain($rule['rulename']),
114
    );
115
    $form[$rule['ruleid']]['validator'] = array(
116
      '#type' => 'item',
117
      '#title' => t('Validator'),
118
      '#markup' => $validators[$rule['validator']],
119
    );
120
    $form[$rule['ruleid']]['components'] = array(
121
      '#type' => 'item',
122
      '#title' => t('Components'),
123
      '#markup' => theme('item_list', array('items' => $component_info)),
124
    );
125
    $form[$rule['ruleid']]['weight'] = array(
126
      '#type' => 'weight',
127
      '#title' => t('Weight'),
128
      '#default_value' => $rule['weight'],
129
      '#description' => t('Optional. By changing the order of validation rules, you can use code that relies on earlier validation functions being completed.'),
130
    );
131
    $form[$rule['ruleid']]['actions'] = array('#type' => 'actions');
132
    $form[$rule['ruleid']]['actions']['edit'] = array(
133
      '#type' => 'item',
134
      '#markup' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
135
    );
136
    $form[$rule['ruleid']]['actions']['delete'] = array(
137
      '#type' => 'item',
138
      '#markup' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
139
    );
140
  }
141
  if (count($rules) > 1) {
142
    $form['submit'] = array(
143
      '#type' => 'submit',
144
      '#value' => t('Save rule order'),
145
    );
146
  }
147
148
  return $form;
149
}
150
151
/**
152
 * Submit function for rule overview form.
153
 */
154
function webform_validation_manage_overview_form_submit($form, $form_state) {
155 81b16cc2 Assos Assos
  // Save the rule weights.
156 a2baadd1 Assos Assos
  foreach ($form_state['values'] as $ruleid => $value) {
157
    if (is_numeric($ruleid)) {
158
      $update = db_update('webform_validation_rule')
159 76bdcd04 Assos Assos
        ->fields(array(
160
          'weight' => $value['weight'],
161
        ))
162
        ->condition('ruleid', $ruleid)
163
        ->execute();
164 a2baadd1 Assos Assos
    }
165
  }
166
  drupal_set_message(t('The order of the validation rules has been saved.'));
167 85ad3d82 Assos Assos
}
168
169
/**
170 81b16cc2 Assos Assos
 * Callback function to add or edit a validation rule.
171 85ad3d82 Assos Assos
 */
172 81b16cc2 Assos Assos
function webform_validation_manage_rule($form, $form_state, $node, $action, $validator, $rule = NULL) {
173 85ad3d82 Assos Assos
  $form = array();
174
  $rule_validator = webform_validation_get_validator_info($validator);
175
176 65389548 Assos Assos
  if ($action === 'edit') {
177
    $title = t('Edit validation rule %title', array('%title' => $rule['rulename']));
178
  }
179
  else {
180
    $title = t('Add validation rule');
181
  }
182
  drupal_set_title($title, PASS_THROUGH);
183 85ad3d82 Assos Assos
184
  $form['rule']['validator'] = array(
185
    '#type' => 'hidden',
186
    '#value' => $validator,
187
  );
188
189
  $form['rule']['action'] = array(
190
    '#type' => 'hidden',
191
    '#value' => $action,
192
  );
193
194
  if ($action == 'edit' && $rule) {
195
    $form['rule']['ruleid'] = array(
196
      '#type' => 'hidden',
197
      '#value' => $rule['ruleid'],
198
    );
199
200
    $form['rule']['nid'] = array(
201
      '#type' => 'hidden',
202
      '#value' => $rule['nid'],
203
    );
204
  }
205
  else {
206
    $form['rule']['nid'] = array(
207
      '#type' => 'hidden',
208
      '#value' => $node->nid,
209
    );
210
  }
211
212 65389548 Assos Assos
  $form['rule']['type'] = array(
213
    '#type' => 'item',
214
    '#title' => t('Rule type'),
215
    '#markup' => $rule_validator['name'],
216
  );
217
218 85ad3d82 Assos Assos
  $form['rule']['rulename'] = array(
219
    '#type' => 'textfield',
220
    '#title' => t('Rule name'),
221 65389548 Assos Assos
    '#default_value' => isset($rule['rulename']) ? $rule['rulename'] : NULL,
222 85ad3d82 Assos Assos
    '#required' => TRUE,
223
    '#size' => 60,
224
    '#maxlength' => 255,
225
    '#weight' => 1,
226
  );
227
228
  $form['rule']['rule_components'] = array(
229
    '#type' => 'checkboxes',
230
    '#title' => t('Components'),
231
    '#weight' => 3,
232
    '#description' => t('Select the components to be validated by this validation rule'),
233
    '#options' => webform_validation_get_webform_components($node, $validator),
234 65389548 Assos Assos
    '#default_value' => isset($rule['components']) ? array_keys($rule['components']) : array(),
235 85ad3d82 Assos Assos
  );
236
237 4f315dab Assos Assos
  if (!empty($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
238 76bdcd04 Assos Assos
    $valid_types = array('textfield', 'textarea');
239
    $type = isset($rule_validator['custom_data']['type']) ? $rule_validator['custom_data']['type'] : 'textfield';
240
    if (!in_array($type, $valid_types)) {
241
      $type = 'textfield';
242
    }
243
244 85ad3d82 Assos Assos
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
245
    $form['rule']['data'] = array(
246 76bdcd04 Assos Assos
      '#type' => $type,
247 85ad3d82 Assos Assos
      '#title' => $rule_validator['custom_data']['label'],
248
      '#description' => $rule_validator['custom_data']['description'],
249 4f315dab Assos Assos
      '#required' => (bool) $required,
250 85ad3d82 Assos Assos
      '#size' => 60,
251
      '#maxlength' => NULL,
252
      '#default_value' => $rule['data'],
253
      '#weight' => 4,
254
    );
255
  }
256
257 4f315dab Assos Assos
  if (!empty($rule_validator['negatable'])) {
258 85ad3d82 Assos Assos
    $form['rule']['negate'] = array(
259
      '#type' => 'checkbox',
260
      '#title' => t('Negate rule'),
261
      '#description' => t('Validate the inverse of the rule.'),
262
      '#default_value' => $rule['negate'],
263
      '#weight' => 5,
264
    );
265
  }
266
267 4f315dab Assos Assos
  if (!empty($rule_validator['custom_error'])) {
268 85ad3d82 Assos Assos
    $form['rule']['error_message'] = array(
269
      '#type' => 'textfield',
270
      '#title' => t('Custom error message'),
271
      '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
272
      '#required' => TRUE,
273
      '#size' => 60,
274 4f315dab Assos Assos
      '#maxlength' => 2048,
275 85ad3d82 Assos Assos
      '#default_value' => $rule['error_message'],
276
      '#weight' => 5,
277
    );
278
  }
279
280
  $form['rule']['submit'] = array(
281
    '#type' => 'submit',
282 65389548 Assos Assos
    '#value' => isset($rule['ruleid']) ? t('Save rule') : t('Add rule'),
283 85ad3d82 Assos Assos
    '#weight' => 25,
284
  );
285
286 e4c061ad Assos Assos
  $destination = drupal_get_destination();
287
  $form['rule']['cancel'] = array(
288
    '#markup' => l(t('Cancel'), $destination['destination']),
289
    '#weight' => 26,
290
  );
291
292 85ad3d82 Assos Assos
  return $form;
293
}
294
295
/**
296 81b16cc2 Assos Assos
 * Validation handler to add / edit a rule.
297 85ad3d82 Assos Assos
 */
298
function webform_validation_manage_rule_validate($form, &$form_state) {
299
  $values = $form_state['values'];
300
  if ($values['action'] == 'edit') {
301
    if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
302
      form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
303
    }
304
  }
305
306
  $rule_validator = webform_validation_get_validator_info($values['validator']);
307 65389548 Assos Assos
308
  // Validate custom data.
309
  if ($values['data'] && !empty($rule_validator['custom_data']['validate_regex']) && !preg_match($rule_validator['custom_data']['validate_regex'], $values['data'])) {
310
    form_set_error('data', $rule_validator['custom_data']['label'] . ' ' . t('is invalid.'));
311
  }
312
313 85ad3d82 Assos Assos
  $selected_components = count(array_filter($values['rule_components']));
314 76bdcd04 Assos Assos
  // Check validator min_components and min_components property when they are
315
  // equal.
316 85ad3d82 Assos Assos
  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']) {
317 65389548 Assos Assos
    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.'));
318 85ad3d82 Assos Assos
  }
319 81b16cc2 Assos Assos
  // Check validator min_components property.
320 85ad3d82 Assos Assos
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
321 65389548 Assos Assos
    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.'));
322 85ad3d82 Assos Assos
  }
323 81b16cc2 Assos Assos
  // Check validator max_components property.
324 85ad3d82 Assos Assos
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
325 65389548 Assos Assos
    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.'));
326 85ad3d82 Assos Assos
  }
327
}
328
329
/**
330 81b16cc2 Assos Assos
 * Submit handler to add / edit a rule.
331 85ad3d82 Assos Assos
 */
332
function webform_validation_manage_rule_submit($form, &$form_state) {
333
  $values = $form_state['values'];
334
  webform_validation_rule_save($values);
335
}
336
337
/**
338 76bdcd04 Assos Assos
 * Get a filtered list of components for a specific webform.
339
 *
340
 * List is filtered by the validator settings.
341 85ad3d82 Assos Assos
 */
342
function webform_validation_get_webform_components($node, $validator) {
343 81b16cc2 Assos Assos
  form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
344
345 85ad3d82 Assos Assos
  $ret = array();
346
  $components = $node->webform['components'];
347
  if ($components) {
348 a2bb1a14 Assos Assos
    $valid_components = webform_validation_valid_component_types($validator);
349
    $component_names = webform_component_list($node, NULL, 'path');
350 85ad3d82 Assos Assos
    foreach ($components as $cid => $component) {
351
      if (in_array($component['type'], $valid_components)) {
352 a2bb1a14 Assos Assos
        $ret[$cid] = $component_names[$cid];
353 85ad3d82 Assos Assos
      }
354
    }
355
  }
356
  return $ret;
357
}
358
359
/**
360 81b16cc2 Assos Assos
 * Confirmation form to delete a rule.
361 85ad3d82 Assos Assos
 */
362
function webform_validation_delete_rule($form, &$form_state, $rule) {
363
  if (isset($rule['ruleid'])) {
364
    $form['ruleid'] = array(
365
      '#type' => 'value',
366
      '#value' => $rule['ruleid'],
367
    );
368
  }
369
370 81b16cc2 Assos Assos
  return confirm_form($form, t('Are you sure you want to delete the rule %name?', array('%name' => $rule['rulename'])), isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'], t('This action cannot be undone.'), t('Delete'), t('Cancel')
371 85ad3d82 Assos Assos
  );
372
}
373
374
/**
375 81b16cc2 Assos Assos
 * Submit handler to delete a rule.
376 85ad3d82 Assos Assos
 */
377
function webform_validation_delete_rule_submit($form, &$form_state) {
378
  $ruleid = $form_state['values']['ruleid'];
379
  module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
380 a2baadd1 Assos Assos
  webform_dynamic_delete_rule($ruleid);
381 85ad3d82 Assos Assos
}