Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform_validation / webform_validation.admin.inc @ 81b16cc2

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 = array();
14
  $output['webform_validation_manage_overview_form'] = drupal_get_form('webform_validation_manage_overview_form', $rules, $node);
15
  $output['webform_validation_manage_add_rule'] = array(
16
    '#theme' => 'webform_validation_manage_add_rule',
17
    '#nid' => $node->nid,
18
  );
19
  return $output;
20
}
21

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

    
33
/**
34
 * Themable function to list and re-order the rules assigned to a webform.
35
 */
36
function theme_webform_validation_manage_overview_form($variables) {
37
  $form = $variables['form'];
38
  $header = array(
39
    t('Rule name'),
40
    t('Validator'),
41
    t('Components'),
42
    t('Weight'), array(
43
      'data' => t('Operations'),
44
      'colspan' => 2,
45
    ),
46
  );
47

    
48
  $rows = array();
49
  foreach (element_children($form) as $rule) {
50
    $row = array();
51
    foreach (element_children($form[$rule]) as $item) {
52
      // Unset the titles of the form elements, since we are displaying them in
53
      // a table with a header.
54
      unset($form[$rule][$item]['#title']);
55
      // Add a class to the weight field.
56
      $form[$rule]['weight']['#attributes']['class'] = array('ruleid-weight');
57
      $row[] = array(
58
        'data' => drupal_render($form[$rule][$item]),
59
      );
60
    }
61
    if (count($row) > 1) {
62
      $rows[] = array(
63
        'data' => $row,
64
        'class' => array('draggable'),
65
      );
66
    }
67
    // Hide any fieldsets, since we are displaying the form in a table.
68
    if (isset($form[$rule]['#type']) && $form[$rule]['#type'] === 'fieldset') {
69
      hide($form[$rule]);
70
    }
71
  }
72
  $drag = TRUE;
73
  if (!$rows) {
74
    $drag = FALSE;
75
    $rows[][] = array(
76
      'data' => t('No validation rules available.'),
77
      'colspan' => 5,
78
    );
79
  }
80
  $output = theme('table', array(
81
    'header' => $header,
82
    'rows' => $rows,
83
    'attributes' => array(
84
      'id' => 'webform-validation-overview-form',
85
    ),
86
  ));
87
  $output .= drupal_render_children($form);
88
  if ($drag) {
89
    drupal_add_tabledrag('webform-validation-overview-form', 'order', 'sibling', 'ruleid-weight');
90
  }
91
  return $output;
92
}
93

    
94
/**
95
 * Form to list and reorder the rules assigned to a webform.
96
 */
97
function webform_validation_manage_overview_form($form, &$form_state, $rules, $node) {
98
  $form = array();
99
  $form['#tree'] = TRUE;
100
  $validators = webform_validation_get_validators_selection();
101

    
102
  foreach ($rules as $rule) {
103
    $component_info = webform_validation_rule_components_basic($rule['components']);
104
    $form[$rule['ruleid']] = array(
105
      '#type' => 'fieldset',
106
    );
107
    $form[$rule['ruleid']]['name'] = array(
108
      '#type' => 'item',
109
      '#title' => t('Name'),
110
      '#markup' => check_plain($rule['rulename']),
111
    );
112
    $form[$rule['ruleid']]['validator'] = array(
113
      '#type' => 'item',
114
      '#title' => t('Validator'),
115
      '#markup' => $validators[$rule['validator']],
116
    );
117
    $form[$rule['ruleid']]['components'] = array(
118
      '#type' => 'item',
119
      '#title' => t('Components'),
120
      '#markup' => theme('item_list', array('items' => $component_info)),
121
    );
122
    $form[$rule['ruleid']]['weight'] = array(
123
      '#type' => 'weight',
124
      '#title' => t('Weight'),
125
      '#default_value' => $rule['weight'],
126
      '#description' => t('Optional. By changing the order of validation rules, you can use code that relies on earlier validation functions being completed.'),
127
    );
128
    $form[$rule['ruleid']]['actions'] = array('#type' => 'actions');
129
    $form[$rule['ruleid']]['actions']['edit'] = array(
130
      '#type' => 'item',
131
      '#markup' => l(t('Edit'), 'node/' . $node->nid . '/webform/validation/edit/' . $rule['validator'] . '/' . $rule['ruleid'], array("query" => drupal_get_destination())),
132
    );
133
    $form[$rule['ruleid']]['actions']['delete'] = array(
134
      '#type' => 'item',
135
      '#markup' => l(t('Delete'), 'node/' . $node->nid . '/webform/validation/delete/' . $rule['ruleid'], array("query" => drupal_get_destination())),
136
    );
137
  }
138
  if (count($rules) > 1) {
139
    $form['submit'] = array(
140
      '#type' => 'submit',
141
      '#value' => t('Save rule order'),
142
    );
143
  }
144

    
145
  return $form;
146
}
147

    
148
/**
149
 * Submit function for rule overview form.
150
 */
151
function webform_validation_manage_overview_form_submit($form, $form_state) {
152
  // Save the rule weights.
153
  foreach ($form_state['values'] as $ruleid => $value) {
154
    if (is_numeric($ruleid)) {
155
      $update = db_update('webform_validation_rule')
156
          ->fields(array(
157
            'weight' => $value['weight'],
158
          ))
159
          ->condition('ruleid', $ruleid)
160
          ->execute();
161
    }
162
  }
163
  drupal_set_message(t('The order of the validation rules has been saved.'));
164
}
165

    
166
/**
167
 * Callback function to add or edit a validation rule.
168
 */
169
function webform_validation_manage_rule($form, $form_state, $node, $action, $validator, $rule = NULL) {
170
  $form = array();
171
  $rule_validator = webform_validation_get_validator_info($validator);
172

    
173
  $form['rule'] = array(
174
    '#type' => 'fieldset',
175
    '#title' => ($action == 'edit') ? t('Edit rule') : t('Add rule'),
176
    '#collapsible' => FALSE,
177
    '#collapsed' => FALSE,
178
  );
179

    
180
  $form['rule']['validator'] = array(
181
    '#type' => 'hidden',
182
    '#value' => $validator,
183
  );
184

    
185
  $form['rule']['action'] = array(
186
    '#type' => 'hidden',
187
    '#value' => $action,
188
  );
189

    
190
  if ($action == 'edit' && $rule) {
191
    $form['rule']['ruleid'] = array(
192
      '#type' => 'hidden',
193
      '#value' => $rule['ruleid'],
194
    );
195

    
196
    $form['rule']['nid'] = array(
197
      '#type' => 'hidden',
198
      '#value' => $rule['nid'],
199
    );
200
  }
201
  else {
202
    $form['rule']['nid'] = array(
203
      '#type' => 'hidden',
204
      '#value' => $node->nid,
205
    );
206
  }
207

    
208
  $form['rule']['rulename'] = array(
209
    '#type' => 'textfield',
210
    '#title' => t('Rule name'),
211
    '#default_value' => (isset($rule['rulename'])) ? $rule['rulename'] : '',
212
    '#required' => TRUE,
213
    '#size' => 60,
214
    '#maxlength' => 255,
215
    '#weight' => 1,
216
  );
217

    
218
  $form['rule']['rule_components'] = array(
219
    '#type' => 'checkboxes',
220
    '#title' => t('Components'),
221
    '#weight' => 3,
222
    '#description' => t('Select the components to be validated by this validation rule'),
223
    '#options' => webform_validation_get_webform_components($node, $validator),
224
    '#default_value' => (isset($rule['components'])) ? array_keys($rule['components']) : array(),
225
  );
226

    
227
  if (!empty($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
228
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
229
    $form['rule']['data'] = array(
230
      '#type' => 'textfield',
231
      '#title' => $rule_validator['custom_data']['label'],
232
      '#description' => $rule_validator['custom_data']['description'],
233
      '#required' => (bool) $required,
234
      '#size' => 60,
235
      '#maxlength' => NULL,
236
      '#default_value' => $rule['data'],
237
      '#weight' => 4,
238
    );
239
  }
240

    
241
  if (!empty($rule_validator['negatable'])) {
242
    $form['rule']['negate'] = array(
243
      '#type' => 'checkbox',
244
      '#title' => t('Negate rule'),
245
      '#description' => t('Validate the inverse of the rule.'),
246
      '#default_value' => $rule['negate'],
247
      '#weight' => 5,
248
    );
249
  }
250

    
251
  if (!empty($rule_validator['custom_error'])) {
252
    $form['rule']['error_message'] = array(
253
      '#type' => 'textfield',
254
      '#title' => t('Custom error message'),
255
      '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
256
      '#required' => TRUE,
257
      '#size' => 60,
258
      '#maxlength' => 2048,
259
      '#default_value' => $rule['error_message'],
260
      '#weight' => 5,
261
    );
262
  }
263

    
264
  $form['rule']['submit'] = array(
265
    '#type' => 'submit',
266
    '#value' => (isset($rule['ruleid'])) ? t('Save rule') : t('Add rule'),
267
    '#weight' => 25,
268
  );
269

    
270
  $destination = drupal_get_destination();
271
  $form['rule']['cancel'] = array(
272
    '#markup' => l(t('Cancel'), $destination['destination']),
273
    '#weight' => 26,
274
  );
275

    
276
  return $form;
277
}
278

    
279
/**
280
 * Validation handler to add / edit a rule.
281
 */
282
function webform_validation_manage_rule_validate($form, &$form_state) {
283
  $values = $form_state['values'];
284
  if ($values['action'] == 'edit') {
285
    if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
286
      form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
287
    }
288
  }
289

    
290
  $rule_validator = webform_validation_get_validator_info($values['validator']);
291
  $selected_components = count(array_filter($values['rule_components']));
292
  // Check validator min_components and min_components property when they are equal.
293
  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']) {
294
    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'));
295
  }
296
  // Check validator min_components property.
297
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
298
    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'));
299
  }
300
  // Check validator max_components property.
301
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
302
    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'));
303
  }
304
}
305

    
306
/**
307
 * Submit handler to add / edit a rule.
308
 */
309
function webform_validation_manage_rule_submit($form, &$form_state) {
310
  $values = $form_state['values'];
311
  webform_validation_rule_save($values);
312
}
313

    
314
/**
315
 * Get a list of components for a specific webform, filtered by the validator settings.
316
 */
317
function webform_validation_get_webform_components($node, $validator) {
318
  form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
319

    
320
  $ret = array();
321
  $components = $node->webform['components'];
322
  if ($components) {
323
    $valid_components = webform_validation_valid_component_types($validator);
324
    $component_names = webform_component_list($node, NULL, 'path');
325
    foreach ($components as $cid => $component) {
326
      if (in_array($component['type'], $valid_components)) {
327
        $ret[$cid] = $component_names[$cid];
328
      }
329
    }
330
  }
331
  return $ret;
332
}
333

    
334
/**
335
 * Confirmation form to delete a rule.
336
 */
337
function webform_validation_delete_rule($form, &$form_state, $rule) {
338
  if (isset($rule['ruleid'])) {
339
    $form['ruleid'] = array(
340
      '#type' => 'value',
341
      '#value' => $rule['ruleid'],
342
    );
343
  }
344

    
345
  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')
346
  );
347
}
348

    
349
/**
350
 * Submit handler to delete a rule.
351
 */
352
function webform_validation_delete_rule_submit($form, &$form_state) {
353
  $ruleid = $form_state['values']['ruleid'];
354
  module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
355
  webform_dynamic_delete_rule($ruleid);
356
}