Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / includes / webform.conditionals.inc @ cb17e347

1
<?php
2

    
3
/**
4
 * @file
5
 * Form elements and menu callbacks to provide conditional handling in Webform.
6
 */
7

    
8
/**
9
 * Form builder; Provide the form for adding conditionals to a webform node.
10
 */
11
function webform_conditionals_form($form, &$form_state, $node) {
12
  form_load_include($form_state, 'inc', 'webform', $name = 'includes/webform.components');
13
  form_load_include($form_state, 'inc', 'webform', $name = 'includes/webform.conditionals');
14

    
15
  // Add JavaScript settings to the page needed for conditional elements.
16
  _webform_conditional_expand_value_forms($node);
17

    
18
  if (isset($form_state['values']['conditionals'])) {
19
    // Remove the "new" conditional that always comes in.
20
    unset($form_state['values']['conditionals']['new']);
21

    
22
    $conditionals = $form_state['values']['conditionals'];
23
  }
24
  else {
25
    $conditionals = $node->webform['conditionals'];
26
  }
27
  // Empty out any conditionals that have no rules or actions.
28
  foreach ($conditionals as $rgid => $conditional) {
29
    if (empty($conditional['rules']) || empty($conditional['actions'])) {
30
      unset($conditionals[$rgid]);
31
    }
32
  }
33

    
34
  // Check the current topological sort order for the conditionals and report any errors,
35
  // but only for actual form submissions and not for ajax-related form builds, such as
36
  // adding or removing a condtion or conditional group.
37
  if (empty($form_state['triggering_element']['#ajax'])) {
38
    $node->webform['conditionals'] = $conditionals;
39
    webform_get_conditional_sorter($node)->reportErrors($conditionals);
40
  }
41

    
42
  $form['#tree'] = TRUE;
43
  $form['#node'] = $node;
44

    
45
  $form['#attached']['library'][] = array('webform', 'admin');
46
  $form['#attached']['css'][] = drupal_get_path('module', 'webform') . '/css/webform.css';
47

    
48
  // Wrappers used for AJAX addition/removal.
49
  $form['conditionals']['#theme'] = 'webform_conditional_groups';
50
  $form['conditionals']['#prefix'] = '<div id="webform-conditionals-ajax">';
51
  $form['conditionals']['#suffix'] = '</div>';
52

    
53
  // Keep track of the max conditional count to use as the range for weights.
54
  $form_state['conditional_count'] = isset($form_state['conditional_count']) ? $form_state['conditional_count'] : 1;
55
  $form_state['conditional_count'] = count($conditionals) > $form_state['conditional_count'] ? count($conditionals) : $form_state['conditional_count'];
56

    
57
  $source_list = webform_component_list($node, 'conditional', 'path', TRUE);
58
  $target_list = webform_component_list($node, TRUE, 'path', TRUE);
59
  $delta = $form_state['conditional_count'];
60
  $weight = -$delta - 1;
61
  $index = 0;
62
  foreach ($conditionals as $rgid => $conditional_group) {
63
    $weight = $conditional_group['weight'];
64
    $form['conditionals'][$rgid] = array(
65
      '#theme' => 'webform_conditional_group_row',
66
      '#even_odd' => ++$index % 2 ? 'odd' : 'even',
67
      '#weight' => $weight,
68
    );
69
    $form['conditionals'][$rgid]['conditional'] = array(
70
      '#type' => 'webform_conditional',
71
      '#default_value' => $conditional_group,
72
      '#nid' => $node->nid,
73
      '#sources' => $source_list,
74
      '#actions' => array(
75
        'show' => t('shown'),
76
        'require' => t('required'),
77
        'set' => t('set to'),
78
      ),
79
      '#targets' => $target_list,
80
      '#parents' => array('conditionals', $rgid),
81
    );
82
    $form['conditionals'][$rgid]['weight'] = array(
83
      '#type' => 'weight',
84
      '#title' => t('Weight for rule group !rgid', array('!rgid' => $rgid)),
85
      '#title_display' => 'invisible',
86
      '#default_value' => $weight,
87
      '#delta' => $delta,
88
    );
89
  }
90

    
91
  $form['conditionals']['new']['#weight'] = $weight + 1;
92
  $form['conditionals']['new']['weight'] = array(
93
    '#type' => 'weight',
94
    '#title' => t('Weight for new rule group'),
95
    '#title_display' => 'invisible',
96
    '#default_value' => $weight + 1,
97
    '#delta' => $delta,
98
  );
99
  $form['conditionals']['new']['new'] = array(
100
    '#type' => 'submit',
101
    '#value' => t('+'),
102
    '#submit' => array('webform_conditionals_form_add'),
103
    '#ajax' => array(
104
      'progress' => 'none',
105
      'effect' => 'fade',
106
      'callback' => 'webform_conditionals_ajax',
107
    ),
108
  );
109

    
110
  $form['actions'] = array(
111
    '#type' => 'actions',
112
    '#tree' => FALSE,
113
  );
114
  $form['actions']['submit'] = array(
115
    '#type' => 'submit',
116
    '#value' => t('Save conditions'),
117
    '#validate' => array('webform_conditionals_form_validate'),
118
    '#submit' => array('webform_conditionals_form_submit'),
119
  );
120

    
121
  // Estimate if the form is too long for PHP max_input_vars and detect whether a previous submission was truncated.
122
  // The estimate will be accurate because the form elements for this page are well known. Ajax use of this
123
  // page will not generate user-visible errors, so a preflight may be the only indication to the user that
124
  // the page is too long.
125
  webform_input_vars_check($form, $form_state, 'conditionals', '');
126
  return $form;
127
}
128

    
129
/**
130
 * Submit handler for webform_conditionals_form(). Add an additional choice.
131
 */
132
function webform_conditionals_form_add($form, &$form_state) {
133
  // Build a default new conditional.
134
  unset($form_state['values']['conditionals']['new']);
135
  $weight = count($form_state['values']['conditionals']) > 10 ? -count($form_state['values']['conditionals']) : -10;
136
  foreach ($form_state['values']['conditionals'] as $key => $conditional) {
137
    $weight = max($weight, $conditional['weight']);
138
  }
139

    
140
  // Add the conditional to form state and rebuild the form.
141
  $form_state['values']['conditionals'][] = array(
142
    'rules' => array(
143
      array(
144
        'source' => NULL,
145
        'operator' => NULL,
146
        'value' => NULL,
147
      ),
148
    ),
149
    'andor' => 'and',
150
    'actions' => array(
151
      array(
152
        'target' => NULL,
153
        'invert' => NULL,
154
        'action' => NULL,
155
        'argument' => NULL,
156
      ),
157
    ),
158
    'weight' => $weight + 1,
159
  );
160
  $form_state['rebuild'] = TRUE;
161
}
162

    
163
/**
164
 * Validate handler for webform_conditionals_form().
165
 *
166
 * Prohibit the source and target of a conditional rule from being the same.
167
 */
168
function webform_conditionals_form_validate($form, &$form_state) {
169
  // Skip validation unless this is saving the form.
170
  $button_key = end($form_state['triggering_element']['#array_parents']);
171
  if ($button_key !== 'submit') {
172
    return;
173
  }
174

    
175
  $node = $form['#node'];
176
  $components = $node->webform['components'];
177
  $component_options = webform_component_options();
178
  foreach ($form_state['complete form']['conditionals'] as $conditional_key => $element) {
179
    if (substr($conditional_key, 0, 1) !== '#' && $conditional_key !== 'new') {
180
      $conditional = $element['conditional'];
181
      $targets = array();
182
      foreach ($conditional['actions'] as $action_key => $action) {
183
        if (substr($action_key, 0, 1) !== '#') {
184
          $target_id = $action['target']['#value'];
185
          if (isset($targets[$target_id])) {
186
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][target',
187
                           t('A conditional cannot show or hide a component more than once. (%target).',
188
                             array('%target' => $components[$action['target']['#value']]['name'])));
189
          }
190
          $component_type = $node->webform['components'][$action['target']['#value']]['type'];
191
          if (!webform_conditional_action_able($component_type, $action['action']['#value'])) {
192
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][action',
193
                           t('A component of type %type can\'t be %action. (%target)',
194
                             array(
195
                                '%action' => $action['action']['#options'][$action['action']['#value']],
196
                                '%type' => $component_options[$component_type],
197
                                '%target' => $components[$action['target']['#value']]['name'])));
198
          }
199
          $targets[$target_id] = $target_id;
200
        }
201
      }
202
      foreach ($conditional['rules'] as $rule_key => $rule) {
203
        if (substr($rule_key, 0, 1) !== '#' && in_array($rule['source']['#value'], $targets)) {
204
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][source',
205
                         t('The subject of the conditional cannot be the same as the component that is shown or hidden (%target).',
206
                         array('%target' => $components[$rule['source']['#value']]['name'])));
207
        }
208
      }
209
    }
210
  }
211

    
212
  // Form validation will not rebuild the form, so we need to ensure
213
  // necessary JavaScript will still exist.
214
  _webform_conditional_expand_value_forms($form['#node']);
215
}
216

    
217
/**
218
 * Submit handler for webform_conditionals_form().
219
 */
220
function webform_conditionals_form_submit($form, &$form_state) {
221
  $node = $form['#node'];
222

    
223
  // Remove the new conditional placeholder.
224
  unset($form_state['values']['conditionals']['new']);
225

    
226
  $conditionals = array();
227

    
228
  // Fill in missing properties for each value so that it can save properly.
229
  // TODO: Remove hard-coded source and target type.
230
  foreach ($form_state['values']['conditionals'] as $rgid => $conditional) {
231
    $conditional['rgid'] = $rgid;
232
    foreach ($conditional['rules'] as $rid => $rule) {
233
      $conditional['rules'][$rid]['source_type'] = 'component';
234
    }
235
    foreach ($conditional['actions'] as $aid => $action) {
236
      $conditional['actions'][$aid]['target_type'] = 'component';
237
    }
238
    $conditionals[$rgid] = $conditional;
239
  }
240

    
241
  $node->webform['conditionals'] = $conditionals;
242
  node_save($node);
243
  drupal_set_message(t('Conditionals for %title saved.', array('%title' => $node->title)));
244
}
245

    
246
/**
247
 * AJAX callback to render out adding a new condition.
248
 */
249
function webform_conditionals_ajax($form, $form_state) {
250
  $rgids = element_children($form['conditionals']);
251
  $new_rgid = max($rgids);
252
  $form['conditionals'][$new_rgid]['#ajax_added'] = TRUE;
253

    
254
  $commands = array('#type' => 'ajax');
255
  $commands['#commands'][] = ajax_command_before('.webform-conditional-new-row', drupal_render($form['conditionals'][$new_rgid]));
256
  $commands['#commands'][] = ajax_command_restripe('#webform-conditionals-table');
257
  return $commands;
258
}
259

    
260
/**
261
 * Theme the $form['conditionals'] of webform_conditionals_form().
262
 */
263
function theme_webform_conditional_groups($variables) {
264
  $element = $variables['element'];
265
  drupal_add_tabledrag('webform-conditionals-table', 'order', 'sibling', 'webform-conditional-weight');
266
  drupal_add_js('Drupal.theme.prototype.tableDragChangedMarker = function() { return ""; }', 'inline');
267
  drupal_add_js('Drupal.theme.prototype.tableDragChangedWarning = function() { return "<span>&nbsp;</span>"; }', 'inline');
268

    
269
  $output = '<table id="webform-conditionals-table"><tbody>';
270
  $element_children = element_children($element, TRUE);
271
  $element_count = count($element_children);
272
  foreach ($element_children as $index => $key) {
273
    if ($key === 'new') {
274
      $data = '';
275
      if ($element_count === 1) {
276
        $data = t('There are no conditional actions on this form.') . ' ';
277
      }
278
      $even_odd = ($index + 1) % 2 ? 'odd' : 'even';
279
      $element[$key]['weight']['#attributes']['class'] = array('webform-conditional-weight');
280
      $data = '<div class="webform-conditional-new">' . $data . t('Add a new condition:') . ' ' . drupal_render($element[$key]['new']) . '</div>';
281
      $output .= '<tr class="webform-conditional-new-row ' . $even_odd . '">';
282
      $output .= '<td>' . $data . '</td>';
283
      $output .= '<td>' . drupal_render($element[$key]['weight']) . '</td>';
284
      $output .= '</tr>';
285
    }
286
    else {
287
      $output .= drupal_render($element[$key]);
288
    }
289
  }
290
  $output .= '</tbody></table>';
291
  $output .= drupal_render_children($element);
292

    
293
  return $output;
294
}
295

    
296
/**
297
 * Theme an individual conditional row of webform_conditionals_form().
298
 */
299
function theme_webform_conditional_group_row($variables) {
300
  $element = $variables['element'];
301

    
302
  $element['weight']['#attributes']['class'] = array('webform-conditional-weight');
303
  $weight = drupal_render($element['weight']);
304
  $classes = array('draggable');
305
  if (!empty($element['#even_odd'])) {
306
    $classes[] = $element['#even_odd'];
307
  }
308
  if (!empty($element['#ajax_added'])) {
309
    $classes[] = 'ajax-new-content';
310
  }
311

    
312
  $output = '';
313
  $output .= '<tr class="' . implode(' ', $classes) . '">';
314
  $output .= '<td>' . drupal_render_children($element) . '</td>';
315
  $output .= '<td>' . $weight . '</td>';
316
  $output .= '</tr>';
317

    
318
  return $output;
319
}
320

    
321
/**
322
 * Form API #process function to expand a webform conditional element.
323
 */
324
function _webform_conditional_expand($element) {
325
  $element['#tree'] = TRUE;
326
  $element['#default_value'] += array(
327
    'andor' => 'and',
328
  );
329

    
330
  $wrapper_id = drupal_clean_css_identifier(implode('-', $element['#parents'])) . '-ajax';
331
  $element['#prefix'] = '<div id="' . $wrapper_id . '">';
332
  $element['#suffix'] = '</div>';
333

    
334
  foreach ($element['#default_value']['rules'] as $rid => $conditional) {
335
    $element['rules'][$rid]['source'] = array(
336
      '#type' => 'select',
337
      '#title' => t('Source'),
338
      '#options' => $element['#sources'],
339
      '#default_value' => $element['#default_value']['rules'][$rid]['source'],
340
    );
341
    $element['rules'][$rid]['operator'] = array(
342
      '#type' => 'select',
343
      '#title' => t('Operator'),
344
      '#options' => webform_conditional_operators_list(),
345
      '#default_value' => $element['#default_value']['rules'][$rid]['operator'],
346
    );
347
    $element['rules'][$rid]['value'] = array(
348
      '#type' => 'textfield',
349
      '#title' => t('Value'),
350
      '#size' => 20,
351
      '#default_value' => $element['#default_value']['rules'][$rid]['value'],
352
    );
353
    $element['rules'][$rid]['remove'] = array(
354
      '#type' => 'submit',
355
      '#value' => t('-'),
356
      '#submit' => array('webform_conditional_element_remove'),
357
      '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . '_remove',
358
      '#attributes' => array('class' => array('webform-conditional-rule-remove')),
359
      '#ajax' => array(
360
        'progress' => 'none',
361
        'callback' => 'webform_conditional_element_ajax',
362
        'wrapper' => $wrapper_id,
363
        'event' => 'click',
364
      ),
365
    );
366
    $element['rules'][$rid]['add'] = array(
367
      '#type' => 'submit',
368
      '#value' => t('+'),
369
      '#submit' => array('webform_conditional_element_add'),
370
      '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . '_add',
371
      '#attributes' => array('class' => array('webform-conditional-rule-add')),
372
      '#ajax' => array(
373
        'progress' => 'none',
374
        'callback' => 'webform_conditional_element_ajax',
375
        'wrapper' => $wrapper_id,
376
        'event' => 'click',
377
      ),
378
    );
379

    
380
    // The and/or selector is shown for every rule, even though the whole
381
    // conditional group shares a single and/or property. They are made to match
382
    // via JavaScript (though they all share the same "name" attribute so only
383
    // a single value is ever submitted via POST).
384
    $element['rules'][$rid]['andor'] = array(
385
      '#type' => 'select',
386
      '#title' => t('And/or'),
387
      '#options' => array(
388
        'and' => t('and'),
389
        'or' => t('or'),
390
      ),
391
      '#parents' => array_merge($element['#parents'], array('andor')),
392
      '#default_value' => $element['#default_value']['andor'],
393
    );
394
  }
395

    
396
  // Remove the last and/or.
397
  unset($element['rules'][$rid]['andor']);
398

    
399
  foreach ($element['#default_value']['actions'] as $aid => $action) {
400
    $element['actions'][$aid]['target'] = array(
401
      '#type' => 'select',
402
      '#title' => t('Target'),
403
      '#options' => $element['#targets'],
404
      '#default_value' => $element['#default_value']['actions'][$aid]['target'],
405
    );
406
    $element['actions'][$aid]['invert'] = array(
407
      '#type' => 'select',
408
      '#title' => t('Is/Isn\'t'),
409
      '#options' => array(
410
        '0' => t('is'),
411
        '1' => t('isn\'t'),
412
      ),
413
      '#default_value' => $element['#default_value']['actions'][$aid]['invert'],
414
    );
415
    $element['actions'][$aid]['action'] = array(
416
      '#type' => 'select',
417
      '#title' => t('Action'),
418
      '#options' => $element['#actions'],
419
      '#default_value' => $element['#default_value']['actions'][$aid]['action'],
420
    );
421
    $element['actions'][$aid]['argument'] = array(
422
      '#type' => 'textfield',
423
      '#title' => t('Argument'),
424
      '#size' => 20,
425
      '#default_value' => $element['#default_value']['actions'][$aid]['argument'],
426
    );
427
    $element['actions'][$aid]['remove'] = array(
428
      '#type' => 'submit',
429
      '#value' => t('-'),
430
      '#submit' => array('webform_conditional_element_remove'),
431
      '#name' => implode('_', $element['#parents']) . '_actions_' . $aid . '_remove',
432
      '#attributes' => array('class' => array('webform-conditional-action-remove')),
433
      '#ajax' => array(
434
        'progress' => 'none',
435
        'callback' => 'webform_conditional_element_ajax',
436
        'wrapper' => $wrapper_id,
437
        'event' => 'click',
438
      ),
439
    );
440
    $element['actions'][$aid]['add'] = array(
441
      '#type' => 'submit',
442
      '#value' => t('+'),
443
      '#submit' => array('webform_conditional_element_add'),
444
      '#name' => implode('_', $element['#parents']) . '_actions_' . $aid . '_add',
445
      '#attributes' => array('class' => array('webform-conditional-action-add')),
446
      '#ajax' => array(
447
        'progress' => 'none',
448
        'callback' => 'webform_conditional_element_ajax',
449
        'wrapper' => $wrapper_id,
450
        'event' => 'click',
451
      ),
452
    );
453
  }
454

    
455
  return $element;
456
}
457

    
458
/**
459
 * Expand out all the value forms that could potentially be used.
460
 *
461
 * These forms are added to the page via JavaScript and swapped in only when
462
 * needed. Because the user may change the source and operator at any time,
463
 * all these forms need to be generated ahead of time and swapped in. This
464
 * could have been done via AJAX, but having all forms available makes for a
465
 * faster user experience.
466
 *
467
 * @param $node
468
 *   The Webform node for which these forms are being generated.
469
 * @return
470
 *   An array settings suitable for adding to the page via JavaScript. This
471
 *   array contains the following keys:
472
 *   - operators: An array containing a map of data types, operators, and form
473
 *     keys. This array is structured as follows:
474
 *     @code
475
 *   - sources[$source_key] = array(
476
 *       'data_type' => $data_type,
477
 *     );
478
 *     $operators[$data_type][$operator] = array(
479
 *       'form' => $form_key,
480
 *     );
481
 *     @endcode
482
 *   - forms[$form_key]: A string representing an HTML form for an operator.
483
 *   - forms[$form_key][$source]: Or instead of a single form for all components,
484
 *     if each component requires its own form, key each component by its source
485
 *     value (currently always the component ID).
486
 */
487
function _webform_conditional_expand_value_forms($node, $add_to_page = TRUE) {
488
  static $value_forms_added;
489

    
490
  $operators = webform_conditional_operators();
491
  $data = array();
492
  foreach ($operators as $data_type => $operator_info) {
493
    foreach ($operator_info as $operator => $data_operator_info) {
494
      $data['operators'][$data_type][$operator]['form'] = 'default';
495
      if (isset($data_operator_info['form callback'])) {
496
        $form_callback = $data_operator_info['form callback'];
497
        $data['operators'][$data_type][$operator]['form'] = $form_callback;
498
        if ($form_callback !== FALSE && !isset($value_forms_added[$form_callback])) {
499
          $data['forms'][$form_callback] = $form_callback($node);
500
        }
501
      }
502
    }
503
  }
504

    
505
  foreach ($node->webform['components'] as $cid => $component) {
506
    if (webform_component_feature($component['type'], 'conditional')) {
507
      $data['sources'][$cid]['data_type'] = webform_component_property($component['type'], 'conditional_type');
508
    }
509
  }
510

    
511
  if (!isset($value_forms_added) && $add_to_page) {
512
    $value_forms_added = TRUE;
513
    drupal_add_js(array('webform' => array('conditionalValues' => $data)), 'setting');
514
  }
515

    
516
  return $data;
517
}
518

    
519
/**
520
 * Submit handler for webform_conditional elements to add a new rule.
521
 */
522
function webform_conditional_element_add($form, &$form_state) {
523
  $button = $form_state['clicked_button'];
524
  $parents = $button['#parents'];
525
  $action = array_pop($parents);
526
  $rid = array_pop($parents);
527

    
528
  // Recurse through the form values until we find the root Webform conditional.
529
  $parent_values = &$form_state['values'];
530
  foreach ($parents as $key) {
531
    if (array_key_exists($key, $parent_values)) {
532
      $parent_values = &$parent_values[$key];
533
    }
534
  }
535

    
536
  // Split the list of rules in this conditional and inject into the right spot.
537
  $rids = array_keys($parent_values);
538
  $offset = array_search($rid, $rids);
539
  $first = array_slice($parent_values, 0, $offset + 1);
540
  $second = array_slice($parent_values, $offset + 1);
541
  $new[0] = $parent_values[$rid];
542

    
543
  $parent_values = array_merge($first, $new, $second);
544

    
545
  $form_state['rebuild'] = TRUE;
546
}
547

    
548
/**
549
 * Submit handler for webform_conditional elements to remove a rule or action.
550
 */
551
function webform_conditional_element_remove($form, &$form_state) {
552
  $button = $form_state['clicked_button'];
553
  $parents = $button['#parents'];
554
  $action = array_pop($parents);
555
  $current_id = array_pop($parents);
556

    
557
  // Recurse through the form values until we find the root Webform conditional.
558
  $parent_values = &$form_state['values'];
559
  foreach ($parents as $key) {
560
    if (array_key_exists($key, $parent_values)) {
561
      $parent_values = &$parent_values[$key];
562
    }
563
  }
564

    
565
  // Remove this rule or action from the list of conditionals.
566
  unset($parent_values[$current_id]);
567

    
568
  $form_state['rebuild'] = TRUE;
569
}
570

    
571

    
572
/**
573
 * AJAX callback to render out adding a new condition.
574
 */
575
function webform_conditional_element_ajax($form, $form_state) {
576
  $button = $form_state['clicked_button'];
577
  $parents = $button['#parents'];
578

    
579
  // Trim down the parents to go back up to the level of this elements wrapper.
580
  array_pop($parents); // The button name (add/remove).
581
  array_pop($parents); // The rule ID.
582
  array_pop($parents); // The "rules" grouping.
583

    
584
  $element = $form;
585
  foreach ($parents as $key) {
586
    if (!isset($element[$key])) {
587
      // The entire conditional has been removed
588
      return '';
589
    }
590
    $element = $element[$key];
591
  }
592

    
593
  return drupal_render($element['conditional']);
594
}
595

    
596
/**
597
 * Theme the form for a conditional action.
598
 */
599
function theme_webform_conditional($variables) {
600
  $element = $variables['element'];
601

    
602
  $output = '';
603
  $output .= '<div class="webform-conditional">';
604
  $output .= '<span class="webform-conditional-if">' . t('If') . '</span>';
605

    
606
  foreach (element_children($element['rules']) as $rid) {
607
    // Hide labels.
608
    $element['rules'][$rid]['source']['#title_display'] = 'none';
609
    $element['rules'][$rid]['operator']['#title_display'] = 'none';
610
    $element['rules'][$rid]['value']['#title_display'] = 'none';
611
    $element['rules'][$rid]['andor']['#title_display'] = 'none';
612

    
613
    $source = '<div class="webform-conditional-source">' . drupal_render($element['rules'][$rid]['source']) . '</div>';
614
    $operator = '<div class="webform-conditional-operator">' . drupal_render($element['rules'][$rid]['operator']) . '</div>';
615
    $value = '<div class="webform-conditional-value">' . drupal_render($element['rules'][$rid]['value']) . '</div>';
616

    
617
    $source_phrase = t('!source !operator !value', array(
618
      '!source' => $source,
619
      '!operator' => $operator,
620
      '!value' => $value,
621
    ));
622

    
623
    $output .= '<div class="webform-conditional-rule">';
624
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
625
    $output .= $source_phrase;
626
    $output .= '</div>';
627

    
628
    if (isset($element['rules'][$rid]['andor'])) {
629
      $output .= '<div class="webform-conditional-andor webform-container-inline">';
630
      $output .= drupal_render($element['rules'][$rid]['andor']);
631
      $output .= '</div>';
632
    }
633

    
634
    if (isset($element['rules'][$rid]['add']) || isset($element['rules'][$rid]['remove'])) {
635
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
636
      $output .= drupal_render($element['rules'][$rid]['remove']);
637
      $output .= drupal_render($element['rules'][$rid]['add']);
638
      $output .= '</span>';
639
    }
640

    
641
    $output .= '</div>';
642
  }
643

    
644
  // Hide labels.
645
  foreach (element_children($element['actions']) as $aid) {
646
    // Hide labels.
647
    $element['actions'][$aid]['target']['#title_display'] = 'none';
648
    $element['actions'][$aid]['invert']['#title_display'] = 'none';
649
    $element['actions'][$aid]['action']['#title_display'] = 'none';
650
    $element['actions'][$aid]['argument']['#title_display'] = 'none';
651

    
652
    $target = '<div class="webform-conditional-target">' . drupal_render($element['actions'][$aid]['target']) . '</div>';
653
    $invert = '<div class="webform-conditional-invert">' . drupal_render($element['actions'][$aid]['invert']) . '</div>';
654
    $action = '<div class="webform-conditional-action">' . drupal_render($element['actions'][$aid]['action']) . '</div>';
655
    $argument = '<div class="webform-conditional-argument">' . drupal_render($element['actions'][$aid]['argument']) . '</div>';
656

    
657
    $target_phrase = t('then !target !invert !action !argument', array(
658
      '!target' => $target,
659
      '!invert' => $invert,
660
      '!action' => $action,
661
      '!argument' => $argument,
662
    ));
663

    
664
    $output .= '<div class="webform-conditional-action">';
665
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
666
    $output .= $target_phrase;
667
    $output .= '</div>';
668

    
669
    if (isset($element['actions'][$aid]['add']) || isset($element['actions'][$aid]['remove'])) {
670
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
671
      $output .= drupal_render($element['actions'][$aid]['remove']);
672
      $output .= drupal_render($element['actions'][$aid]['add']);
673
      $output .= '</span>';
674
    }
675

    
676
    $output .= '</div>';
677
  }
678

    
679
  $output .= '</div>';
680

    
681
  return $output;
682
}
683

    
684
/**
685
 * Return a list of all Webform conditional operators.
686
 */
687
function webform_conditional_operators() {
688
  static $operators;
689

    
690
  if (!isset($operators)) {
691
    $operators = module_invoke_all('webform_conditional_operator_info');
692
    drupal_alter('webform_conditional_operators', $operators);
693
  }
694

    
695
  return $operators;
696
}
697

    
698
/**
699
 * Return a nested list of all available operators, suitable for a select list.
700
 */
701
function webform_conditional_operators_list() {
702
  $options = array();
703
  $operators = webform_conditional_operators();
704

    
705
  foreach ($operators as $data_type => $type_operators) {
706
    $options[$data_type] = array();
707
    foreach ($type_operators as $operator => $operator_info) {
708
      $options[$data_type][$operator] = $operator_info['label'];
709
    }
710
  }
711

    
712
  return $options;
713
}
714

    
715
/**
716
 * Internal implementation of hook_webform_conditional_operator_info().
717
 *
718
 * Called from webform.module's webform_webform_conditional_operator_info().
719
 */
720
function _webform_conditional_operator_info() {
721
  // General operators:
722
  $operators['string']['equal'] = array(
723
    'label' => t('is'),
724
    'comparison callback' => 'webform_conditional_operator_string_equal',
725
    'js comparison callback' => 'conditionalOperatorStringEqual',
726
    // A form callback is not needed here, since we can use the default,
727
    // non-JavaScript textfield for all text and numeric fields.
728
    // 'form callback' => 'webform_conditional_operator_text',
729
  );
730
  $operators['string']['not_equal'] = array(
731
    'label' => t('is not'),
732
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
733
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
734
  );
735
  $operators['string']['contains'] = array(
736
    'label' => t('contains'),
737
    'comparison callback' => 'webform_conditional_operator_string_contains',
738
    'js comparison callback' => 'conditionalOperatorStringContains',
739
  );
740
  $operators['string']['does_not_contain'] = array(
741
    'label' => t('does not contain'),
742
    'comparison callback' => 'webform_conditional_operator_string_does_not_contain',
743
    'js comparison callback' => 'conditionalOperatorStringDoesNotContain',
744
  );
745
  $operators['string']['begins_with'] = array(
746
    'label' => t('begins with'),
747
    'comparison callback' => 'webform_conditional_operator_string_begins_with',
748
    'js comparison callback' => 'conditionalOperatorStringBeginsWith',
749
  );
750
  $operators['string']['ends_with'] = array(
751
    'label' => t('ends with'),
752
    'comparison callback' => 'webform_conditional_operator_string_ends_with',
753
    'js comparison callback' => 'conditionalOperatorStringEndsWith',
754
  );
755
  $operators['string']['empty'] = array(
756
    'label' => t('is blank'),
757
    'comparison callback' => 'webform_conditional_operator_string_empty',
758
    'js comparison callback' => 'conditionalOperatorStringEmpty',
759
    'form callback' => FALSE, // No value form at all.
760
  );
761
  $operators['string']['not_empty'] = array(
762
    'label' => t('is not blank'),
763
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
764
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
765
    'form callback' => FALSE, // No value form at all.
766
  );
767

    
768
  // Numeric operators.
769
  $operators['numeric']['equal'] = array(
770
    'label' => t('is equal to'),
771
    'comparison callback' => 'webform_conditional_operator_numeric_equal',
772
    'js comparison callback' => 'conditionalOperatorNumericEqual',
773
  );
774
  $operators['numeric']['not_equal'] = array(
775
    'label' => t('is not equal to'),
776
    'comparison callback' => 'webform_conditional_operator_numeric_not_equal',
777
    'js comparison callback' => 'conditionalOperatorNumericNotEqual',
778
  );
779
  $operators['numeric']['less_than'] = array(
780
    'label' => t('is less than'),
781
    'comparison callback' => 'webform_conditional_operator_numeric_less_than',
782
    'js comparison callback' => 'conditionalOperatorNumericLessThan',
783
  );
784
  $operators['numeric']['less_than_equal'] = array(
785
    'label' => t('is less than or equal'),
786
    'comparison callback' => 'webform_conditional_operator_numeric_less_than_equal',
787
    'js comparison callback' => 'conditionalOperatorNumericLessThanEqual',
788
  );
789
  $operators['numeric']['greater_than'] = array(
790
    'label' => t('is greater than'),
791
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than',
792
    'js comparison callback' => 'conditionalOperatorNumericGreaterThan',
793
  );
794
  $operators['numeric']['greater_than_equal'] = array(
795
    'label' => t('is greater than or equal'),
796
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than_equal',
797
    'js comparison callback' => 'conditionalOperatorNumericGreaterThanEqual',
798
  );
799
  $operators['numeric']['empty'] = array(
800
    'label' => t('is blank'),
801
    'comparison callback' => 'webform_conditional_operator_string_empty',
802
    'js comparison callback' => 'conditionalOperatorStringEmpty',
803
    'form callback' => FALSE, // No value form at all.
804
  );
805
  $operators['numeric']['not_empty'] = array(
806
    'label' => t('is not blank'),
807
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
808
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
809
    'form callback' => FALSE, // No value form at all.
810
  );
811

    
812
  // Select operators.
813
  $operators['select']['equal'] = array(
814
    'label' => t('is'),
815
    'comparison callback' => 'webform_conditional_operator_string_equal',
816
    'js comparison callback' => 'conditionalOperatorStringEqual',
817
    'form callback' => 'webform_conditional_form_select',
818
  );
819
  $operators['select']['not_equal'] = array(
820
    'label' => t('is not'),
821
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
822
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
823
    'form callback' => 'webform_conditional_form_select',
824
  );
825
  $operators['select']['less_than'] = array(
826
    'label' => t('is before'),
827
    'comparison callback' => 'webform_conditional_operator_select_less_than',
828
    'js comparison callback' => 'conditionalOperatorSelectLessThan',
829
    'form callback' => 'webform_conditional_form_select',
830
  );
831
  $operators['select']['less_than_equal'] = array(
832
    'label' => t('is or is before'),
833
    'comparison callback' => 'webform_conditional_operator_select_less_than_equal',
834
    'js comparison callback' => 'conditionalOperatorSelectLessThanEqual',
835
    'form callback' => 'webform_conditional_form_select',
836
  );
837
  $operators['select']['greater_than'] = array(
838
    'label' => t('is after'),
839
    'comparison callback' => 'webform_conditional_operator_select_greater_than',
840
    'js comparison callback' => 'conditionalOperatorSelectGreaterThan',
841
    'form callback' => 'webform_conditional_form_select',
842
  );
843
  $operators['select']['greater_than_equal'] = array(
844
    'label' => t('is or is after'),
845
    'comparison callback' => 'webform_conditional_operator_select_greater_than_equal',
846
    'js comparison callback' => 'conditionalOperatorSelectGreaterThanEqual',
847
    'form callback' => 'webform_conditional_form_select',
848
  );
849
  $operators['select']['empty'] = array(
850
    'label' => t('is empty'),
851
    'comparison callback' => 'webform_conditional_operator_string_empty',
852
    'js comparison callback' => 'conditionalOperatorStringEmpty',
853
    'form callback' => FALSE, // No value form at all.
854
  );
855
  $operators['select']['not_empty'] = array(
856
    'label' => t('is not empty'),
857
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
858
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
859
    'form callback' => FALSE, // No value form at all.
860
  );
861

    
862
  // Date operators:
863
  $operators['date']['equal'] = array(
864
    'label' => t('is on'),
865
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
866
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
867
    'js comparison callback' => 'conditionalOperatorDateEqual',
868
    'form callback' => 'webform_conditional_form_date',
869
  );
870
  $operators['date']['not_equal'] = array(
871
    'label' => t('is not on'),
872
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
873
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
874
    'js comparison callback' => 'conditionalOperatorDateNotEqual',
875
    'form callback' => 'webform_conditional_form_date',
876
  );
877
  $operators['date']['before'] = array(
878
    'label' => t('is before'),
879
    'comparison callback' => 'webform_conditional_operator_datetime_before',
880
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
881
    'js comparison callback' => 'conditionalOperatorDateBefore',
882
    'form callback' => 'webform_conditional_form_date',
883
  );
884
  $operators['date']['before_equal'] = array(
885
    'label' => t('is on or before'),
886
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
887
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
888
    'js comparison callback' => 'conditionalOperatorDateBeforeEqual',
889
    'form callback' => 'webform_conditional_form_date',
890
  );
891
  $operators['date']['after'] = array(
892
    'label' => t('is after'),
893
    'comparison callback' => 'webform_conditional_operator_datetime_after',
894
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
895
    'js comparison callback' => 'conditionalOperatorDateAfter',
896
    'form callback' => 'webform_conditional_form_date',
897
  );
898
  $operators['date']['after_equal'] = array(
899
    'label' => t('is on or after'),
900
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
901
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
902
    'js comparison callback' => 'conditionalOperatorDateAfterEqual',
903
    'form callback' => 'webform_conditional_form_date',
904
  );
905

    
906
  // Time operators:
907
  $operators['time']['equal'] = array(
908
    'label' => t('is at'),
909
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
910
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
911
    'js comparison callback' => 'conditionalOperatorTimeEqual',
912
    'form callback' => 'webform_conditional_form_time',
913
  );
914
  $operators['time']['not_equal'] = array(
915
    'label' => t('is not at'),
916
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
917
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
918
    'js comparison callback' => 'conditionalOperatorTimeNotEqual',
919
    'form callback' => 'webform_conditional_form_time',
920
  );
921
  $operators['time']['before'] = array(
922
    'label' => t('is before'),
923
    'comparison callback' => 'webform_conditional_operator_datetime_before',
924
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
925
    'js comparison callback' => 'conditionalOperatorTimeBefore',
926
    'form callback' => 'webform_conditional_form_time',
927
  );
928
  $operators['time']['before_equal'] = array(
929
    'label' => t('is at or before'),
930
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
931
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
932
    'js comparison callback' => 'conditionalOperatorTimeBeforeEqual',
933
    'form callback' => 'webform_conditional_form_time',
934
  );
935
  $operators['time']['after'] = array(
936
    'label' => t('is after'),
937
    'comparison callback' => 'webform_conditional_operator_datetime_after',
938
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
939
    'js comparison callback' => 'conditionalOperatorTimeAfter',
940
    'form callback' => 'webform_conditional_form_time',
941
  );
942
  $operators['time']['after_equal'] = array(
943
    'label' => t('is at or after'),
944
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
945
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
946
    'js comparison callback' => 'conditionalOperatorTimeAfterEqual',
947
    'form callback' => 'webform_conditional_form_time',
948
  );
949

    
950
  return $operators;
951
}
952

    
953
/**
954
 * Form callback for select-type conditional fields.
955
 *
956
 * Unlike other built-in conditional value forms, the form callback for select
957
 * types provides an array of forms, keyed by the $cid, which is the "source"
958
 * for the condition.
959
 */
960
function webform_conditional_form_select($node) {
961
  static $count = 0;
962
  $forms = array();
963
  webform_component_include('select');
964
  foreach ($node->webform['components'] as $cid => $component) {
965
    if (webform_component_property($component['type'], 'conditional_type') == 'select') {
966
      // TODO: Use a pluggable mechanism for retrieving select list values.
967
      $options = _webform_select_options($component);
968
      $element = array(
969
        '#type' => 'select',
970
        '#multiple' => FALSE,
971
        '#size' => NULL,
972
        '#attributes' => array(),
973
        '#id' => NULL,
974
        '#name' => 'webform-conditional-select-' . $cid . '-' . $count,
975
        '#options' => $options,
976
        '#parents' => array(),
977
      );
978
      $forms[$cid] = drupal_render($element);
979
    }
980
  }
981
  $count++;
982
  return $forms;
983
}
984

    
985
/**
986
 * Form callback for date conditional fields.
987
 */
988
function webform_conditional_form_date($node) {
989
  static $count = 0;
990
  $element = array(
991
    '#title' => NULL,
992
    '#title_display' => 'none',
993
    '#size' => 24,
994
    '#attributes' => array('placeholder' => t('@format or valid date', array('@format' => webform_date_format('short')))),
995
    '#type' => 'textfield',
996
    '#name' => 'webform-conditional-date-' . $count++,
997
  );
998
  return drupal_render($element);
999
}
1000

    
1001
/**
1002
 * Form callback for time conditional fields.
1003
 */
1004
function webform_conditional_form_time($node) {
1005
  static $count = 0;
1006
  $element = array(
1007
    '#title' => NULL,
1008
    '#title_display' => 'none',
1009
    '#size' => 24,
1010
    '#attributes' => array('placeholder' => t('HH:MMam or valid time')),
1011
    '#type' => 'textfield',
1012
    '#name' => 'webform-conditional-time-' . $count++,
1013
  );
1014
  return drupal_render($element);
1015
}
1016

    
1017
/**
1018
 * Load a conditional setting from the database.
1019
 */
1020
function webform_conditional_load($rgid, $nid) {
1021
  $node = node_load($nid);
1022

    
1023
  $conditional = isset($node->webform['conditionals'][$rgid]) ? $node->webform['conditionals'][$rgid] : FALSE;
1024

    
1025
  return $conditional;
1026
}
1027

    
1028
/**
1029
 * Insert a conditional rule group into the database.
1030
 */
1031
function webform_conditional_insert($conditional) {
1032
  drupal_write_record('webform_conditional', $conditional);
1033
  foreach ($conditional['rules'] as $rid => $rule) {
1034
    $rule['nid'] = $conditional['nid'];
1035
    $rule['rgid'] = $conditional['rgid'];
1036
    $rule['rid'] = $rid;
1037
    drupal_write_record('webform_conditional_rules', $rule);
1038
  }
1039
  foreach ($conditional['actions'] as $aid => $action) {
1040
    $action['nid'] = $conditional['nid'];
1041
    $action['rgid'] = $conditional['rgid'];
1042
    $action['aid'] = $aid;
1043
    drupal_write_record('webform_conditional_actions', $action);
1044
  }
1045
}
1046

    
1047
/**
1048
 * Update a conditional setting in the database.
1049
 */
1050
function webform_conditional_update($node, $conditional) {
1051
  webform_conditional_delete($node, $conditional);
1052
  webform_conditional_insert($conditional);
1053
}
1054

    
1055
/**
1056
 * Delete a conditional rule group.
1057
 */
1058
function webform_conditional_delete($node, $conditional) {
1059
  db_delete('webform_conditional')
1060
    ->condition('nid', $node->nid)
1061
    ->condition('rgid', $conditional['rgid'])
1062
    ->execute();
1063
  db_delete('webform_conditional_rules')
1064
    ->condition('nid', $node->nid)
1065
    ->condition('rgid', $conditional['rgid'])
1066
    ->execute();
1067
  db_delete('webform_conditional_actions')
1068
    ->condition('nid', $node->nid)
1069
    ->condition('rgid', $conditional['rgid'])
1070
    ->execute();
1071
}
1072

    
1073
/**
1074
 * Loop through all the conditional settings and add needed JavaScript settings.
1075
 *
1076
 * We do a bit of optimization for JavaScript before adding to the page as
1077
 * settings. We remove unnecessary data structures and provide a "source map"
1078
 * so that JavaScript can quickly determine if it needs to check rules when a
1079
 * field on the page has been modified.
1080
 *
1081
 * @param object $node
1082
 *   The loaded node object, containing the webform.
1083
 * @param array $submission_data
1084
 *   The cid-indexed array of existing submission values to be included for
1085
 *   sources outside of the current page.
1086
 * @param integer $page_num
1087
 *   The number of the page for which javascript settings should be generated.
1088
 * @return array
1089
 *   Array of settings to be send to the browser as javascript settings.
1090
 */
1091
function webform_conditional_prepare_javascript($node, $submission_data, $page_num) {
1092
  $settings = array(
1093
    'ruleGroups' => array(),
1094
    'sourceMap' => array(),
1095
    'values' => array(),
1096
  );
1097
  $operators = webform_conditional_operators();
1098
  $conditionals = $node->webform['conditionals'];
1099
  $components = $node->webform['components'];
1100
  $topological_order = webform_get_conditional_sorter($node)->getOrder();
1101
  foreach ($topological_order[$page_num] as $conditional_spec) {
1102
    $conditional = $conditionals[$conditional_spec['rgid']];
1103
    $rgid_key = 'rgid_' . $conditional['rgid'];
1104
    // Assemble the main conditional group settings.
1105

    
1106
    $settings['ruleGroups'][$rgid_key] = array(
1107
      'andor' => $conditional['andor'],
1108
    );
1109
    foreach ($conditional['actions'] as $action) {
1110
      if ($action['target_type'] == 'component') {
1111
        $target_component = $components[$action['target']];
1112
        $target_parents = webform_component_parent_keys($node, $target_component);
1113
        $aid_key = 'aid_' . $action['aid'];
1114
        $action_settings = array(
1115
          'target' => 'webform-component--' . str_replace('_', '-', implode('--', $target_parents)),
1116
          'invert' => (int)$action['invert'],
1117
          'action' => $action['action'],
1118
          'argument' => $components[$action['target']]['type'] == 'markup' ? filter_xss_admin($action['argument']) : $action['argument'],
1119
        );
1120
        $settings['ruleGroups'][$rgid_key]['actions'][$aid_key] = $action_settings;
1121
      }
1122
    }
1123
    // Add on the list of rules to the conditional group.
1124
    foreach ($conditional['rules'] as $rule) {
1125
      if ($rule['source_type'] == 'component') {
1126
        $source_component = $components[$rule['source']];
1127
        $source_parents = webform_component_parent_keys($node, $source_component);
1128
        $source_id = 'webform-component--' . str_replace('_', '-', implode('--', $source_parents));
1129
        $rid_key = 'rid_' . $rule['rid'];
1130

    
1131
        // If this source has a value set, add that as a setting.
1132
        // NULL or array(NULL) should be sent as an empty array to simplify the jQuery.
1133
        if (isset($submission_data[$source_component['cid']])) {
1134
          $source_value = $submission_data[$source_component['cid']];
1135
          $source_value = is_array($source_value) ? $source_value : array($source_value);
1136
          $settings['values'][$source_id] = $source_value === array(NULL) ? array() : $source_value;
1137
        }
1138

    
1139
        $conditional_type = webform_component_property($source_component['type'], 'conditional_type');
1140
        $operator_info = $operators[$conditional_type][$rule['operator']];
1141
        $rule_settings = array(
1142
          'source' => $source_id,
1143
          'value' => $rule['value'],
1144
          'callback' => $operator_info['js comparison callback'],
1145
        );
1146
        if (isset($operator_info['comparison prepare js'])) {
1147
          $callback = $operator_info['comparison prepare js'];
1148
          $rule_settings['value'] = $callback($rule['value']);
1149
        }
1150
        $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = $rule_settings;
1151
        $settings['sourceMap'][$source_id][$rgid_key] = $rgid_key;
1152
      }
1153
    }
1154
  }
1155

    
1156
  return $settings;
1157
}
1158

    
1159
/**
1160
 * Determine whether a component type is capable of a given conditional action.
1161
 */
1162
function webform_conditional_action_able($component_type, $action) {
1163
  switch ($action) {
1164
    case 'show':
1165
      return TRUE;
1166
      // break;
1167
    case 'require':
1168
      return webform_component_feature($component_type, 'required');
1169
      // break;
1170
    default:
1171
      return webform_component_feature($component_type, "conditional_action_$action");
1172
      // break;
1173
  }
1174
}
1175

    
1176
/**
1177
 * Prepare a conditional value for adding as a JavaScript setting.
1178
 */
1179
function webform_conditional_prepare_date_js($rule_value) {
1180
  // Convert the time/date string to a UTC timestamp for comparison. Note that
1181
  // this means comparisons against immediate times (such as "now") may be
1182
  // slightly stale by the time the comparison executes. Timestamps are in
1183
  // milliseconds, as to match JavaScript's Date.toString() method.
1184
  $date = webform_strtodate('c', $rule_value, 'UTC');
1185
  return webform_strtotime($date);
1186
}
1187

    
1188
/**
1189
 * Prepare a conditional value for adding as a JavaScript setting.
1190
 */
1191
function webform_conditional_prepare_time_js($rule_value) {
1192
  $date = webform_conditional_prepare_date_js($rule_value);
1193
  $today = webform_strtodate('c', 'today', 'UTC');
1194
  $today = webform_strtotime($today);
1195
  return $date - $today;
1196
}
1197

    
1198
/**
1199
 * Conditional callback for string comparisons.
1200
 */
1201
function webform_conditional_operator_string_equal($input_values, $rule_value) {
1202
  foreach ($input_values as $value) {
1203
    // Checkbox values come in as 0 integers for unchecked boxes.
1204
    $value = ($value === 0) ? '' : $value;
1205
    if (strcasecmp($value, $rule_value) === 0) {
1206
      return TRUE;
1207
    }
1208
  }
1209
  return FALSE;
1210
}
1211

    
1212
/**
1213
 * Conditional callback for string comparisons.
1214
 */
1215
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
1216
  return !webform_conditional_operator_string_equal($input_values, $rule_value);
1217
}
1218

    
1219
/**
1220
 * Conditional callback for string comparisons.
1221
 */
1222
function webform_conditional_operator_string_contains($input_values, $rule_value) {
1223
  foreach ($input_values as $value) {
1224
    if (stripos($value, $rule_value) !== FALSE) {
1225
      return TRUE;
1226
    }
1227
  }
1228
  return FALSE;
1229
}
1230

    
1231
/**
1232
 * Conditional callback for string comparisons.
1233
 */
1234
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
1235
  return !webform_conditional_operator_string_contains($input_values, $rule_value);
1236
}
1237

    
1238
/**
1239
 * Conditional callback for string comparisons.
1240
 */
1241
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
1242
  foreach ($input_values as $value) {
1243
    if (stripos($value, $rule_value) === 0) {
1244
      return TRUE;
1245
    }
1246
  }
1247
  return FALSE;
1248
}
1249

    
1250
/**
1251
 * Conditional callback for string comparisons.
1252
 */
1253
function webform_conditional_operator_string_ends_with($input_values, $rule_value) {
1254
  foreach ($input_values as $value) {
1255
    if (strripos($value, $rule_value) === strlen($value) - strlen($rule_value)) {
1256
      return TRUE;
1257
    }
1258
  }
1259
  return FALSE;
1260
}
1261

    
1262
/**
1263
 * Conditional callback for checking for empty fields.
1264
 */
1265
function webform_conditional_operator_string_empty($input_values, $rule_value) {
1266
  $empty = TRUE;
1267
  foreach ($input_values as $value) {
1268
    if ($value !== '' && $value !== NULL && $value !== 0) {
1269
      $empty = FALSE;
1270
      break;
1271
    }
1272
  }
1273
  return $empty;
1274
}
1275

    
1276
/**
1277
 * Conditional callback for checking for empty fields.
1278
 */
1279
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
1280
  return !webform_conditional_operator_string_empty($input_values, $rule_value);
1281
}
1282

    
1283
/**
1284
 * Conditional callback for select comparisons.
1285
 */
1286
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
1287
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
1288
}
1289

    
1290
/**
1291
 * Conditional callback for select comparisons.
1292
 */
1293
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
1294
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1295
  return $comparison < 0 || $comparison === 0;
1296
}
1297

    
1298
/**
1299
 * Conditional callback for select comparisons.
1300
 */
1301
function webform_conditional_operator_select_greater_than($input_values, $rule_value, $component) {
1302
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) > 0;
1303
}
1304

    
1305
/**
1306
 * Conditional callback for select comparisons.
1307
 */
1308
function webform_conditional_operator_select_greater_than_equal($input_values, $rule_value, $component) {
1309
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1310
  return $comparison > 0 || $comparison === 0;
1311
}
1312

    
1313
/**
1314
 * Conditional callback for numeric comparisons.
1315
 */
1316
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
1317
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
1318
}
1319

    
1320
/**
1321
 * Conditional callback for numeric comparisons.
1322
 */
1323
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
1324
  return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
1325
}
1326

    
1327
/**
1328
 * Conditional callback for numeric comparisons.
1329
 */
1330
function webform_conditional_operator_numeric_less_than($input_values, $rule_value) {
1331
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) < 0;
1332
}
1333

    
1334
/**
1335
 * Conditional callback for numeric comparisons.
1336
 */
1337
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
1338
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1339
  return $comparison < 0 || $comparison === 0;
1340
}
1341

    
1342
/**
1343
 * Conditional callback for numeric comparisons.
1344
 */
1345
function webform_conditional_operator_numeric_greater_than($input_values, $rule_value) {
1346
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) > 0;
1347
}
1348

    
1349
/**
1350
 * Conditional callback for numeric comparisons.
1351
 */
1352
function webform_conditional_operator_numeric_greater_than_equal($input_values, $rule_value) {
1353
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1354
  return $comparison > 0 || $comparison === 0;
1355
}
1356

    
1357
/**
1358
 * Conditional callback for date and time comparisons.
1359
 */
1360
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
1361
  $input_values = webform_conditional_value_datetime($input_values);
1362
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
1363
}
1364

    
1365
/**
1366
 * Conditional callback for date and time comparisons.
1367
 */
1368
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
1369
  return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
1370
}
1371

    
1372
/**
1373
 * Conditional callback for date and time comparisons.
1374
 */
1375
function webform_conditional_operator_datetime_after($input_values, $rule_value) {
1376
  $input_values = webform_conditional_value_datetime($input_values);
1377
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) > webform_strtotime($rule_value);
1378
}
1379

    
1380
/**
1381
 * Conditional callback for date and time comparisons.
1382
 */
1383
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
1384
  return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
1385
         webform_conditional_operator_datetime_equal($input_values, $rule_value);
1386
}
1387

    
1388
/**
1389
 * Conditional callback for date and time comparisons.
1390
 */
1391
function webform_conditional_operator_datetime_before($input_values, $rule_value) {
1392
  $input_values = webform_conditional_value_datetime($input_values);
1393
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) < webform_strtotime($rule_value);
1394
}
1395

    
1396
/**
1397
 * Conditional callback for date and time comparisons.
1398
 */
1399
function webform_conditional_operator_datetime_before_equal($input_values, $rule_value) {
1400
  return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
1401
         webform_conditional_operator_datetime_equal($input_values, $rule_value);
1402
}
1403

    
1404
/**
1405
 * Utility function to convert incoming time and dates into strings.
1406
 */
1407
function webform_conditional_value_datetime($input_values) {
1408
  // Convert times into a string.
1409
  $input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
1410
  // Convert dates into a string.
1411
  $input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
1412
  return $input_values;
1413
}
1414

    
1415
/**
1416
 * Utility function to compare values of a select component.
1417
 * @param string $a
1418
 *   First select option key to compare
1419
 * @param string $b
1420
 *   Second select option key to compare
1421
 * @param array $options
1422
 *   Associative array where the $a and $b are within the keys
1423
 * @return integer based upon position of $a and $b in $options
1424
 *   -N if $a above (<) $b
1425
 *   0 if $a = $b
1426
 *   +N if $a is below (>) $b
1427
 */
1428
function webform_compare_select($a, $b, $options) {
1429
  // Select keys that are integer-like strings are numberic indices in PHP.
1430
  // Convert the array keys to an array of strings.
1431
  $options_array = array_map(function($i) {return (string) $i; }, array_keys($options));
1432
  $a_position = array_search($a, $options_array, TRUE);
1433
  $b_position = array_search($b, $options_array, TRUE);
1434
  return ($a_position === FALSE || $a_position === FALSE) ? NULL : $a_position - $b_position;
1435

    
1436
}