Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform_validation / webform_validation.admin.inc @ 87dbc3bf

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
/**
35
 * Themable function to list and re-order the rules assigned to a webform
36
 */
37
function theme_webform_validation_manage_overview_form($variables) {
38
  $form = $variables['form'];
39
  $header = array(t('Rule name'), t('Validator'), t('Components'), t('Weight'), array(
40
    'data' => t('Operations'),
41
    'colspan' => 2,
42
  ));
43

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

    
83
/**
84
 * Form to list and reorder the rules assigned to a webform
85
 */
86
function webform_validation_manage_overview_form($form, &$form_state, $rules, $node) {
87
  $form = array();
88
  $form['#tree'] = TRUE;
89
  $validators = webform_validation_get_validators_selection();
90

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

    
134
  return $form;
135
}
136

    
137
/**
138
 * Submit function for rule overview form.
139
 */
140
function webform_validation_manage_overview_form_submit($form, $form_state) {
141
  //Save the rule weights.
142
  foreach ($form_state['values'] as $ruleid => $value) {
143
    if (is_numeric($ruleid)) {
144
      $update = db_update('webform_validation_rule')
145
        ->fields(array(
146
          'weight' => $value['weight'],
147
        ))
148
        ->condition('ruleid', $ruleid)
149
        ->execute();
150
    }
151
  }
152
  drupal_set_message(t('The order of the validation rules has been saved.'));
153
}
154

    
155
/**
156
 * Callback function to add or edit a validation rule
157
 */
158
function webform_validation_manage_rule($form, $form_state, $node, $action = 'add', $validator, $rule = NULL) {
159
  $form = array();
160
  $rule_validator = webform_validation_get_validator_info($validator);
161

    
162
  $form['rule'] = array(
163
    '#type' => 'fieldset',
164
    '#title' => ($action == 'edit') ? t('Edit rule') : t('Add rule'),
165
    '#collapsible' => FALSE,
166
    '#collapsed' => FALSE,
167
  );
168

    
169
  $form['rule']['validator'] = array(
170
    '#type' => 'hidden',
171
    '#value' => $validator,
172
  );
173

    
174
  $form['rule']['action'] = array(
175
    '#type' => 'hidden',
176
    '#value' => $action,
177
  );
178

    
179
  if ($action == 'edit' && $rule) {
180
    $form['rule']['ruleid'] = array(
181
      '#type' => 'hidden',
182
      '#value' => $rule['ruleid'],
183
    );
184

    
185
    $form['rule']['nid'] = array(
186
      '#type' => 'hidden',
187
      '#value' => $rule['nid'],
188
    );
189
  }
190
  else {
191
    $form['rule']['nid'] = array(
192
      '#type' => 'hidden',
193
      '#value' => $node->nid,
194
    );
195
  }
196

    
197
  $form['rule']['rulename'] = array(
198
    '#type' => 'textfield',
199
    '#title' => t('Rule name'),
200
    '#default_value' => (isset($rule['rulename'])) ? $rule['rulename'] : '',
201
    '#required' => TRUE,
202
    '#size' => 60,
203
    '#maxlength' => 255,
204
    '#weight' => 1,
205
  );
206

    
207
  $form['rule']['rule_components'] = array(
208
    '#type' => 'checkboxes',
209
    '#title' => t('Components'),
210
    '#weight' => 3,
211
    '#description' => t('Select the components to be validated by this validation rule'),
212
    '#options' => webform_validation_get_webform_components($node, $validator),
213
    '#default_value' => (isset($rule['components'])) ? array_keys($rule['components']) : array(),
214
  );
215

    
216
  if (isset($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
217
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
218
    $form['rule']['data'] = array(
219
      '#type' => 'textfield',
220
      '#title' => $rule_validator['custom_data']['label'],
221
      '#description' => $rule_validator['custom_data']['description'],
222
      '#required' => ($required !== FALSE) ? TRUE : FALSE,
223
      '#size' => 60,
224
      '#maxlength' => NULL,
225
      '#default_value' => $rule['data'],
226
      '#weight' => 4,
227
    );
228
  }
229

    
230
  if (isset($rule_validator['negatable'])) {
231
    $form['rule']['negate'] = array(
232
      '#type' => 'checkbox',
233
      '#title' => t('Negate rule'),
234
      '#description' => t('Validate the inverse of the rule.'),
235
      '#default_value' => $rule['negate'],
236
      '#weight' => 5,
237
    );
238
  }
239

    
240
  if (isset($rule_validator['custom_error'])) {
241
    $form['rule']['error_message'] = array(
242
      '#type' => 'textfield',
243
      '#title' => t('Custom error message'),
244
      '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
245
      '#required' => TRUE,
246
      '#size' => 60,
247
      '#maxlength' => 255,
248
      '#default_value' => $rule['error_message'],
249
      '#weight' => 5,
250
    );
251
  }
252

    
253
  $form['rule']['submit'] = array(
254
    '#type' => 'submit',
255
    '#value' => (isset($rule['ruleid'])) ? t('Save rule') : t('Add rule'),
256
    '#weight' => 25,
257
  );
258

    
259
  return $form;
260
}
261

    
262
/**
263
 * Validation handler to add / edit a rule
264
 */
265
function webform_validation_manage_rule_validate($form, &$form_state) {
266
  $values = $form_state['values'];
267
  if ($values['action'] == 'edit') {
268
    if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
269
      form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
270
    }
271
  }
272

    
273
  $rule_validator = webform_validation_get_validator_info($values['validator']);
274
  $selected_components = count(array_filter($values['rule_components']));
275
  // Check validator min_components and min_components property when they are equal.
276
  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']) {
277
      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'));
278
  }
279
  // check validator min_components property
280
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
281
      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'));
282
  }
283
  // check validator max_components property
284
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
285
      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'));
286
  }
287
}
288

    
289

    
290
/**
291
 * Submit handler to add / edit a rule
292
 */
293
function webform_validation_manage_rule_submit($form, &$form_state) {
294
  $values = $form_state['values'];
295
  webform_validation_rule_save($values);
296
}
297

    
298
/**
299
 * Get a list of components for a specific webform, filtered by the validator settings
300
 */
301
function webform_validation_get_webform_components($node, $validator) {
302
  $ret = array();
303
  $components = $node->webform['components'];
304
  $valid_components = webform_validation_valid_component_types($validator);
305
  if ($components) {
306
    foreach ($components as $cid => $component) {
307
      if (in_array($component['type'], $valid_components)) {
308
        $ret[$cid] = $component['name'];
309
      }
310
    }
311
  }
312
  return $ret;
313
}
314

    
315
/**
316
 * Confirmation form to delete a rule
317
 */
318
function webform_validation_delete_rule($form, &$form_state, $rule) {
319
  if (isset($rule['ruleid'])) {
320
    $form['ruleid'] = array(
321
      '#type' => 'value',
322
      '#value' => $rule['ruleid'],
323
    );
324
  }
325

    
326
  return confirm_form($form,
327
    t('Are you sure you want to delete the rule %name?', array('%name' => $rule['rulename'])),
328
    isset($_GET['destination']) ? $_GET['destination'] : $_GET['q'],
329
    t('This action cannot be undone.'),
330
    t('Delete'),
331
    t('Cancel')
332
  );
333
}
334

    
335
/**
336
 * Submit handler to delete a rule
337
 */
338
function webform_validation_delete_rule_submit($form, &$form_state) {
339
  $ruleid = $form_state['values']['ruleid'];
340
  module_invoke_all('webform_validation', 'rule', 'delete', $ruleid);
341
  webform_dynamic_delete_rule($ruleid);
342
}