Project

General

Profile

Paste
Download (12.3 KB) Statistics
| Branch: | Revision:

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

1
<?php
2

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

    
8
/**
9
 * Menu callback function.
10
 *
11
 * Shows an overview of the existing validation rules and the option to add a
12
 * rule.
13
 */
14
function webform_validation_manage($node) {
15
  $rules = webform_validation_get_webform_rules($node);
16
  $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
  return $output;
23
}
24

    
25
/**
26
 * Get the list of rules associated with the webform.
27
 */
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
 * Themable function to list and re-order the rules assigned to a webform.
38
 */
39
function theme_webform_validation_manage_overview_form($variables) {
40
  $form = $variables['form'];
41
  $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

    
51
  $rows = array();
52
  foreach (element_children($form) as $rule) {
53
    $row = array();
54
    foreach (element_children($form[$rule]) as $item) {
55
      // Unset the titles of the form elements, since we are displaying them in
56
      // a table with a header.
57
      unset($form[$rule][$item]['#title']);
58
      // Add a class to the weight field.
59
      $form[$rule]['weight']['#attributes']['class'] = array('ruleid-weight');
60
      $row[] = array(
61
        'data' => drupal_render($form[$rule][$item]),
62
      );
63
    }
64
    if (count($row) > 1) {
65
      $rows[] = array(
66
        'data' => $row,
67
        'class' => array('draggable'),
68
      );
69
    }
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
    }
74
  }
75
  $drag = TRUE;
76
  if (!$rows) {
77
    $drag = FALSE;
78
    $rows[][] = array(
79
      'data' => t('No validation rules available.'),
80
      'colspan' => 5,
81
    );
82
  }
83
  $output = theme('table', array(
84
    'header' => $header,
85
    'rows' => $rows,
86
    'attributes' => array(
87
      'id' => 'webform-validation-overview-form',
88
    ),
89
  ));
90
  $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
 * Form to list and reorder the rules assigned to a webform.
99
 */
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

    
105
  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
  // Save the rule weights.
156
  foreach ($form_state['values'] as $ruleid => $value) {
157
    if (is_numeric($ruleid)) {
158
      $update = db_update('webform_validation_rule')
159
        ->fields(array(
160
          'weight' => $value['weight'],
161
        ))
162
        ->condition('ruleid', $ruleid)
163
        ->execute();
164
    }
165
  }
166
  drupal_set_message(t('The order of the validation rules has been saved.'));
167
}
168

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

    
176
  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

    
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
  $form['rule']['type'] = array(
213
    '#type' => 'item',
214
    '#title' => t('Rule type'),
215
    '#markup' => $rule_validator['name'],
216
  );
217

    
218
  $form['rule']['rulename'] = array(
219
    '#type' => 'textfield',
220
    '#title' => t('Rule name'),
221
    '#default_value' => isset($rule['rulename']) ? $rule['rulename'] : NULL,
222
    '#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
    '#default_value' => isset($rule['components']) ? array_keys($rule['components']) : array(),
235
  );
236

    
237
  if (!empty($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
238
    $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
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
245
    $form['rule']['data'] = array(
246
      '#type' => $type,
247
      '#title' => $rule_validator['custom_data']['label'],
248
      '#description' => $rule_validator['custom_data']['description'],
249
      '#required' => (bool) $required,
250
      '#size' => 60,
251
      '#maxlength' => NULL,
252
      '#default_value' => $rule['data'],
253
      '#weight' => 4,
254
    );
255
  }
256

    
257
  if (!empty($rule_validator['negatable'])) {
258
    $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
  if (!empty($rule_validator['custom_error'])) {
268
    $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
      '#maxlength' => 2048,
275
      '#default_value' => $rule['error_message'],
276
      '#weight' => 5,
277
    );
278
  }
279

    
280
  $form['rule']['submit'] = array(
281
    '#type' => 'submit',
282
    '#value' => isset($rule['ruleid']) ? t('Save rule') : t('Add rule'),
283
    '#weight' => 25,
284
  );
285

    
286
  $destination = drupal_get_destination();
287
  $form['rule']['cancel'] = array(
288
    '#markup' => l(t('Cancel'), $destination['destination']),
289
    '#weight' => 26,
290
  );
291

    
292
  return $form;
293
}
294

    
295
/**
296
 * Validation handler to add / edit a rule.
297
 */
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

    
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
  $selected_components = count(array_filter($values['rule_components']));
314
  // Check validator min_components and min_components property when they are
315
  // equal.
316
  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
    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
  }
319
  // Check validator min_components property.
320
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
321
    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
  }
323
  // Check validator max_components property.
324
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
325
    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
  }
327
}
328

    
329
/**
330
 * Submit handler to add / edit a rule.
331
 */
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
 * Get a filtered list of components for a specific webform.
339
 *
340
 * List is filtered by the validator settings.
341
 */
342
function webform_validation_get_webform_components($node, $validator) {
343
  form_load_include($form_state, 'inc', 'webform', 'includes/webform.components');
344

    
345
  $ret = array();
346
  $components = $node->webform['components'];
347
  if ($components) {
348
    $valid_components = webform_validation_valid_component_types($validator);
349
    $component_names = webform_component_list($node, NULL, 'path');
350
    foreach ($components as $cid => $component) {
351
      if (in_array($component['type'], $valid_components)) {
352
        $ret[$cid] = $component_names[$cid];
353
      }
354
    }
355
  }
356
  return $ret;
357
}
358

    
359
/**
360
 * Confirmation form to delete a rule.
361
 */
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
  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
  );
372
}
373

    
374
/**
375
 * Submit handler to delete a rule.
376
 */
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
  webform_dynamic_delete_rule($ruleid);
381
}