Project

General

Profile

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

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

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
    webform_delete_empty_subconditionals($conditional);
30
    if (empty($conditional['rules']) || empty($conditional['actions'])) {
31
      unset($conditionals[$rgid]);
32
    }
33
  }
34
  // Drop PHP reference.
35
  unset($conditional);
36

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

    
45
  $form['#tree'] = TRUE;
46
  $form['#node'] = $node;
47

    
48
  $form['#attached']['library'][] = array('webform', 'admin');
49
  $form['#attached']['css'][] = drupal_get_path('module', 'webform') . '/css/webform.css';
50

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

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

    
60
  $source_list = webform_component_list($node, 'conditional', 'path', TRUE);
61
  $target_list = webform_component_list($node, TRUE, 'path', TRUE);
62
  $components = $node->webform['components'];
63
  $delta = $form_state['conditional_count'];
64
  $weight = -$delta - 1;
65
  $index = 0;
66
  foreach ($conditionals as $rgid => $conditional_group) {
67
    $weight = $conditional_group['weight'];
68
    $form['conditionals'][$rgid] = array(
69
      '#theme' => 'webform_conditional_group_row',
70
      '#even_odd' => ++$index % 2 ? 'odd' : 'even',
71
      '#weight' => $weight,
72
      'rgid' => array(
73
        '#type' => 'value',
74
        '#value' => $rgid,
75
      ),
76
      'conditional' => array(
77
        '#type' => 'webform_conditional',
78
        '#default_value' => $conditional_group,
79
        '#nid' => $node->nid,
80
        '#sources' => $source_list,
81
        '#actions' => array(
82
          'show' => t('shown'),
83
          'require' => t('required'),
84
          'set' => t('set to'),
85
        ),
86
        '#targets' => $target_list,
87
        '#parents' => array('conditionals', $rgid),
88
      ),
89
    );
90
    foreach ($conditional_group['actions'] as $action) {
91
      $cid = $action['target'];
92
      if ($action['action'] == 'require' && !$components[$cid]['required']) {
93
        drupal_set_message(t('Component %title must be configured as Required for Webform to conditionally change its required status. <a href="!url">Configure %title.</a>',
94
          array(
95
            '%title' => $components[$cid]['name'],
96
            '!url' => url("node/{$node->nid}/webform/components/$cid", array('query' => array('destination' => "node/{$node->nid}/webform/conditionals"))),
97
          )
98
          ), 'error');
99
      }
100
    }
101
    $form['conditionals'][$rgid]['weight'] = array(
102
      '#type' => 'weight',
103
      '#title' => t('Weight for rule group !rgid', array('!rgid' => $rgid)),
104
      '#title_display' => 'invisible',
105
      '#default_value' => $weight,
106
      '#delta' => $delta,
107
    );
108
  }
109

    
110
  $form['conditionals']['new']['#weight'] = $weight + 1;
111
  $form['conditionals']['new']['weight'] = array(
112
    '#type' => 'weight',
113
    '#title' => t('Weight for new rule group'),
114
    '#title_display' => 'invisible',
115
    '#default_value' => $weight + 1,
116
    '#delta' => $delta,
117
  );
118
  $form['conditionals']['new']['new'] = array(
119
    '#type' => 'submit',
120
    '#value' => t('+'),
121
    '#submit' => array('webform_conditionals_form_add'),
122
    '#ajax' => array(
123
      'progress' => 'none',
124
      'effect' => 'fade',
125
      'callback' => 'webform_conditionals_ajax',
126
    ),
127
  );
128
  // Create dummy remove button for form alignment only.
129
  $form['conditionals']['new']['remove'] = array(
130
    '#type' => 'submit',
131
    '#value' => t('-'),
132
    '#disabled' => TRUE,
133
  );
134

    
135
  $form['actions'] = array(
136
    '#type' => 'actions',
137
    '#tree' => FALSE,
138
  );
139
  $form['actions']['submit'] = array(
140
    '#type' => 'submit',
141
    '#value' => t('Save conditions'),
142
    '#validate' => array('webform_conditionals_form_validate'),
143
    '#submit' => array('webform_conditionals_form_submit'),
144
  );
145

    
146
  // Estimate if the form is too long for PHP max_input_vars and detect whether
147
  // a previous submission was truncated. The estimate will be accurate because
148
  // the form elements for this page are well known. Ajax use of this page will
149
  // not generate user-visible errors, so a preflight may be the only indication
150
  // to the user that the page is too long.
151
  webform_input_vars_check($form, $form_state, 'conditionals', '');
152
  return $form;
153
}
154

    
155
/**
156
 * Submit handler for webform_conditionals_form(). Add an additional choice.
157
 */
158
function webform_conditionals_form_add($form, &$form_state) {
159
  // Build a default new conditional.
160
  unset($form_state['values']['conditionals']['new']);
161
  $weight = count($form_state['values']['conditionals']) > 10 ? -count($form_state['values']['conditionals']) : -10;
162
  foreach ($form_state['values']['conditionals'] as $key => $conditional) {
163
    $weight = max($weight, $conditional['weight']);
164
  }
165

    
166
  // Add the conditional to form state and rebuild the form.
167
  $form_state['values']['conditionals'][] = array(
168
    'rules' => array(
169
      array(
170
        'source_type' => 'component',
171
        'source' => NULL,
172
        'operator' => NULL,
173
        'value' => NULL,
174
      ),
175
    ),
176
    'andor' => 'and',
177
    'actions' => array(
178
      array(
179
        'target_type' => 'component',
180
        'target' => NULL,
181
        'invert' => NULL,
182
        'action' => NULL,
183
        'argument' => NULL,
184
      ),
185
    ),
186
    'weight' => $weight + 1,
187
  );
188
  $form_state['rebuild'] = TRUE;
189
}
190

    
191
/**
192
 * Validate handler for webform_conditionals_form().
193
 *
194
 * Prohibit the source and target of a conditional rule from being the same.
195
 */
196
function webform_conditionals_form_validate($form, &$form_state) {
197
  // Skip validation unless this is saving the form.
198
  $button_key = end($form_state['triggering_element']['#array_parents']);
199
  if ($button_key !== 'submit') {
200
    return;
201
  }
202

    
203
  $node = $form['#node'];
204
  $components = $node->webform['components'];
205
  $component_options = webform_component_options();
206
  foreach ($form_state['complete form']['conditionals'] as $conditional_key => $element) {
207
    if (substr($conditional_key, 0, 1) !== '#' && $conditional_key !== 'new') {
208
      $conditional = $element['conditional'];
209
      $targets = array();
210
      foreach ($conditional['actions'] as $action_key => $action) {
211
        if (is_numeric($action_key)) {
212
          $operation = $action['action']['#value'];
213
          $target_id = $action['target']['#value'];
214
          if (isset($targets[$target_id][$operation])) {
215
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][target',
216
              t('A operation %op cannot be made for a component more than once. (%target).',
217
              array(
218
                '%op' => $action['action']['#options'][$operation],
219
                '%target' => $components[$action['target']['#value']]['name'],
220
              )));
221
          }
222
          $component_type = $node->webform['components'][$action['target']['#value']]['type'];
223
          if (!webform_conditional_action_able($component_type, $action['action']['#value'])) {
224
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][action',
225
              t("A component of type %type can't be %action. (%target)",
226
              array(
227
                '%action' => $action['action']['#options'][$action['action']['#value']],
228
                '%type' => $component_options[$component_type],
229
                '%target' => $components[$action['target']['#value']]['name'],
230
              )));
231
          }
232
          $targets[$target_id][$operation] = $target_id;
233
        }
234
      }
235
      foreach ($conditional['rules'] as $rule_key => $rule) {
236
        if (!is_numeric($rule_key)) {
237
          continue;
238
        }
239
        $source_cid = isset($rule['source']['#value']) ? $rule['source']['#value'] : NULL;
240
        // Validate component rules, but not conditional_start/end rules.
241
        if ($source_cid && $rule['source_type']['#value'] == 'component' && isset($targets[$source_cid])) {
242
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][source',
243
            t('The subject of the conditional cannot be the same as the component that is changed (%target).',
244
            array('%target' => $components[$source_cid]['name'])));
245
        }
246
        if ($source_cid && $components[$source_cid]['type'] === 'date' && strtotime($rule['value']['#value']) === FALSE) {
247
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][value', t('The conditional comparison value must be a valid date.'));
248
        }
249
      }
250
    }
251
  }
252

    
253
  // Form validation will not rebuild the form, so we need to ensure
254
  // necessary JavaScript will still exist.
255
  _webform_conditional_expand_value_forms($node);
256
}
257

    
258
/**
259
 * Submit handler for webform_conditionals_form().
260
 */
261
function webform_conditionals_form_submit($form, &$form_state) {
262
  $node = $form['#node'];
263
  // Get a fresh copy of the node so that we are only saving these changes.
264
  // Otherwise, changes to the Webform on another tab will be overwritten.
265
  $node = node_load($node->nid, NULL, TRUE);
266

    
267
  // Remove the new conditional placeholder.
268
  unset($form_state['values']['conditionals']['new']);
269

    
270
  $node->webform['conditionals'] = $form_state['values']['conditionals'];
271
  node_save($node);
272
  drupal_set_message(t('Conditionals for %title saved.', array('%title' => $node->title)));
273
}
274

    
275
/**
276
 * AJAX callback to render out adding a new condition.
277
 */
278
function webform_conditionals_ajax($form, $form_state) {
279
  $rgids = element_children($form['conditionals']);
280
  $new_rgid = max($rgids);
281
  $form['conditionals'][$new_rgid]['#ajax_added'] = TRUE;
282

    
283
  $commands = array('#type' => 'ajax');
284
  $commands['#commands'][] = ajax_command_before('.webform-conditional-new-row', drupal_render($form['conditionals'][$new_rgid]));
285
  $commands['#commands'][] = ajax_command_restripe('#webform-conditionals-table');
286
  return $commands;
287
}
288

    
289
/**
290
 * Theme the $form['conditionals'] of webform_conditionals_form().
291
 */
292
function theme_webform_conditional_groups($variables) {
293
  $element = $variables['element'];
294
  drupal_add_tabledrag('webform-conditionals-table', 'order', 'sibling', 'webform-conditional-weight');
295
  drupal_add_js('Drupal.theme.prototype.tableDragChangedMarker = function() { return ""; }', 'inline');
296
  drupal_add_js('Drupal.theme.prototype.tableDragChangedWarning = function() { return "<span>&nbsp;</span>"; }', 'inline');
297

    
298
  $output = '<table id="webform-conditionals-table"><tbody>';
299
  $element_children = element_children($element, TRUE);
300
  $element_count = count($element_children);
301
  foreach ($element_children as $index => $key) {
302
    if ($key === 'new') {
303
      $even_odd = ($index + 1) % 2 ? 'odd' : 'even';
304
      $element[$key]['weight']['#attributes']['class'] = array('webform-conditional-weight');
305
      $data = '<div class="webform-conditional-new">';
306
      if ($element_count === 1) {
307
        $data .= t('There are no conditional actions on this form.') . ' ';
308
      }
309
      $data .= t('Add a new condition:') . ' ' . drupal_render($element[$key]['new']) . drupal_render($element[$key]['remove']);
310
      $data .= '</div>';
311
      $output .= '<tr class="webform-conditional-new-row ' . $even_odd . '">';
312
      $output .= '<td>' . $data . '</td>';
313
      $output .= '<td>' . drupal_render($element[$key]['weight']) . '</td>';
314
      $output .= '</tr>';
315
    }
316
    else {
317
      $output .= drupal_render($element[$key]);
318
    }
319
  }
320
  $output .= '</tbody></table>';
321
  $output .= drupal_render_children($element);
322

    
323
  return $output;
324
}
325

    
326
/**
327
 * Theme an individual conditional row of webform_conditionals_form().
328
 */
329
function theme_webform_conditional_group_row($variables) {
330
  $element = $variables['element'];
331

    
332
  $element['weight']['#attributes']['class'] = array('webform-conditional-weight');
333
  $weight = drupal_render($element['weight']);
334
  $classes = array('draggable');
335
  if (!empty($element['#even_odd'])) {
336
    $classes[] = $element['#even_odd'];
337
  }
338
  if (!empty($element['#ajax_added'])) {
339
    $classes[] = 'ajax-new-content';
340
  }
341

    
342
  $output = '';
343
  $output .= '<tr class="' . implode(' ', $classes) . '">';
344
  $output .= '<td>' . drupal_render_children($element) . '</td>';
345
  $output .= '<td>' . $weight . '</td>';
346
  $output .= '</tr>';
347

    
348
  return $output;
349
}
350

    
351
/**
352
 * Form API #process function to expand a webform conditional element.
353
 */
354
function _webform_conditional_expand($element) {
355
  $default_operator = 'and';
356

    
357
  $element['#tree'] = TRUE;
358
  $element['#default_value'] += array(
359
    'andor' => $default_operator,
360
  );
361

    
362
  $wrapper_id = drupal_clean_css_identifier(implode('-', $element['#parents'])) . '-ajax';
363
  $element['#prefix'] = '<div id="' . $wrapper_id . '">';
364
  $element['#suffix'] = '</div>';
365
  $element['#wrapper_id'] = $wrapper_id;
366

    
367
  // Note: When rules or actions are added, the new rules are inserted into
368
  // $form_state['values']. So that FAPI can merge data from the post,
369
  // $form_state['input'] must be adjusted to. To make this easier, hidden
370
  // fields are added to the conditional_start and _end rules to ensure that
371
  // each rule is represented in the POST.
372
  $level = 0;
373
  $andor_stack[0] = array(
374
    'value' => $element['#default_value']['andor'],
375
    'parents' => array_merge($element['#parents'], array('andor')),
376
    'rid' => 0,
377
    'first' => TRUE,
378
  );
379

    
380
  $last_rid = -1;
381
  foreach ($element['#default_value']['rules'] as $rid => $conditional) {
382
    switch ($conditional['source_type']) {
383
      case 'conditional_start':
384
        $element['rules'][$rid] = array(
385
          '#level' => $level,
386
          'source_type' => array(
387
            '#type' => 'hidden',
388
            '#value' => 'conditional_start',
389
          ),
390
          // The andor operator is located in the first child, which is
391
          // guaranteed to exist. Therefore, don't add a 'value' element here.
392
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
393
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
394
          'remove' => _webform_conditional_remove_expand($element, $rid),
395
        );
396
        $andor_stack[++$level] = array(
397
          'value' => isset($conditional['operator']) ? $conditional['operator'] : $default_operator,
398
          'parents' => array_merge($element['#parents'], array('rules', $rid, 'operator')),
399
          'rid' => $rid,
400
          'first' => TRUE,
401
        );
402
        break;
403

    
404
      case 'conditional_end':
405
        --$level;
406
        $element['rules'][$rid] = array(
407
          '#level' => $level,
408
          'source_type' => array(
409
            '#type' => 'hidden',
410
            '#value' => 'conditional_end',
411
          ),
412
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
413
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
414
          'remove' => _webform_conditional_remove_expand($element, $rid),
415
          'andor' => _webform_conditional_andor_expand($andor_stack[$level]),
416
        );
417
        // Remove the last nested and/or.
418
        unset($element['rules'][$last_rid]['andor']);
419
        break;
420

    
421
      case 'component':
422
        $element['rules'][$rid] = _webform_conditional_rule_expand($element, $rid, $conditional, $level, $andor_stack[$level]);
423
        break;
424

    
425
      default:
426
        drupal_set_message(t('Unexpected conditional rule source type found (rule id @rid). Contact the administrator.', array('@rid' => $rid)), 'error');
427
    }
428
    $last_rid = $rid;
429
  }
430

    
431
  // Remove the last and/or.
432
  unset($element['rules'][$rid]['andor']);
433

    
434
  foreach ($element['#default_value']['actions'] as $aid => $action) {
435
    $element['actions'][$aid] = _webform_conditional_action_expand($element, $aid, $action);
436
  }
437

    
438
  return $element;
439
}
440

    
441
/**
442
 * Helper. Generate the and/or select or static text.
443
 */
444
function _webform_conditional_andor_expand(&$andor) {
445
  if ($andor['first']) {
446
    $andor['first'] = FALSE;
447
    return array(
448
      '#type' => 'select',
449
      '#title' => t('And/or'),
450
      '#options' => array(
451
        'and' => t('and'),
452
        'or' => t('or'),
453
      ),
454
      '#parents' => $andor['parents'],
455
      '#default_value' => $andor['value'],
456
      '#attributes' => array('data-rid' => $andor['rid']),
457
    );
458
  }
459
  else {
460
    return array(
461
      '#type' => 'container',
462
      '#attributes' => array('class' => array('webform-andor'), 'data-rid' => $andor['rid']),
463
      'andor_text' => array(
464
        '#markup' => $andor['value'] == 'or' ? t('or') : t('and'),
465
      ),
466
    );
467
  }
468
}
469

    
470
/**
471
 * Helper. Generate the add_subconditional (+) or add + button.
472
 */
473
function _webform_conditional_add_expand($element, $rid, $subconditional) {
474
  return array(
475
    '#type' => 'submit',
476
    '#value' => $subconditional ? t('(+)') : t('+'),
477
    '#submit' => array('webform_conditional_element_add'),
478
    '#subconditional' => $subconditional,
479
    '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . ($subconditional ? '_add_subconditional' : '_add'),
480
    '#attributes' => array('class' => array('webform-conditional-rule-add')),
481
    '#ajax' => array(
482
      'progress' => 'none',
483
      'callback' => 'webform_conditional_element_ajax',
484
      'wrapper' => $element['#wrapper_id'],
485
      'event' => 'click',
486
    ),
487
  );
488
}
489

    
490
/**
491
 * Helper. Generate the add_subconditional (+), add + or remove - button.
492
 */
493
function _webform_conditional_remove_expand($element, $rid) {
494
  return array(
495
    '#type' => 'submit',
496
    '#value' => t('-'),
497
    '#submit' => array('webform_conditional_element_remove'),
498
    '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . '_remove',
499
    '#attributes' => array('class' => array('webform-conditional-rule-remove')),
500
    '#ajax' => array(
501
      'progress' => 'none',
502
      'callback' => 'webform_conditional_element_ajax',
503
      'wrapper' => $element['#wrapper_id'],
504
      'event' => 'click',
505
    ),
506
  );
507
}
508

    
509
/**
510
 * Helper. Generate form elements for one rule.
511
 */
512
function _webform_conditional_rule_expand($element, $rid, $conditional, $level, &$andor) {
513
  return array(
514
    '#level' => $level,
515
    'source_type' => array(
516
      '#type' => 'value',
517
      '#value' => $conditional['source_type'],
518
    ),
519
    'source' => array(
520
      '#type' => 'select',
521
      '#title' => t('Source'),
522
      '#options' => $element['#sources'],
523
      '#default_value' => $conditional['source'],
524
    ),
525
    'operator' => array(
526
      '#type' => 'select',
527
      '#title' => t('Operator'),
528
      '#options' => webform_conditional_operators_list(),
529
      '#default_value' => $conditional['operator'],
530
    ),
531
    'value' => array(
532
      '#type' => 'textfield',
533
      '#title' => t('Value'),
534
      '#size' => 20,
535
      '#default_value' => $conditional['value'],
536
    ),
537
    'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
538
    'add' => _webform_conditional_add_expand($element, $rid, FALSE),
539
    'remove' => _webform_conditional_remove_expand($element, $rid),
540
    'andor' => _webform_conditional_andor_expand($andor),
541
  );
542
}
543

    
544
/**
545
 * Helper. Generate form elements for one action.
546
 */
547
function _webform_conditional_action_expand($element, $aid, $action) {
548
  return array(
549
    'target_type' => array(
550
      '#type' => 'value',
551
      '#value' => $action['target_type'],
552
    ),
553
    'target' => array(
554
      '#type' => 'select',
555
      '#title' => t('Target'),
556
      '#options' => $element['#targets'],
557
      '#default_value' => $action['target'],
558
    ),
559
    'invert' => array(
560
      '#type' => 'select',
561
      '#title' => t("Is/Isn't"),
562
      '#options' => array(
563
        '0' => t('is'),
564
        '1' => t("isn't"),
565
      ),
566
      '#default_value' => $action['invert'],
567
    ),
568
    'action' => array(
569
      '#type' => 'select',
570
      '#title' => t('Action'),
571
      '#options' => $element['#actions'],
572
      '#default_value' => $action['action'],
573
    ),
574
    'argument' => array(
575
      '#type' => 'textfield',
576
      '#title' => t('Argument'),
577
      '#size' => 20,
578
      '#maxlength' => NULL,
579
      '#default_value' => $action['argument'],
580
    ),
581
    'add' => array(
582
      '#type' => 'submit',
583
      '#value' => t('+'),
584
      '#submit' => array('webform_conditional_element_add'),
585
      '#name' => implode('_', $element['#parents']) . '_actions_' . $aid . '_add',
586
      '#attributes' => array('class' => array('webform-conditional-action-add')),
587
      '#ajax' => array(
588
        'progress' => 'none',
589
        'callback' => 'webform_conditional_element_ajax',
590
        'wrapper' => $element['#wrapper_id'],
591
        'event' => 'click',
592
      ),
593
    ),
594
    'remove' => array(
595
      '#type' => 'submit',
596
      '#value' => t('-'),
597
      '#submit' => array('webform_conditional_element_remove'),
598
      '#name' => implode('_', $element['#parents']) . '_actions_' . $aid . '_remove',
599
      '#attributes' => array('class' => array('webform-conditional-action-remove')),
600
      '#ajax' => array(
601
        'progress' => 'none',
602
        'callback' => 'webform_conditional_element_ajax',
603
        'wrapper' => $element['#wrapper_id'],
604
        'event' => 'click',
605
      ),
606
    ),
607
  );
608
}
609

    
610
/**
611
 * Expand out all the value forms that could potentially be used.
612
 *
613
 * These forms are added to the page via JavaScript and swapped in only when
614
 * needed. Because the user may change the source and operator at any time,
615
 * all these forms need to be generated ahead of time and swapped in. This
616
 * could have been done via AJAX, but having all forms available makes for a
617
 * faster user experience.
618
 *
619
 * Added to the JavaScript settings is conditionalValues which contains
620
 * an array settings suitable for adding to the page via JavaScript. This
621
 * array contains the following keys:
622
 *   - operators: An array containing a map of data types, operators, and form
623
 *     keys. This array is structured as follows:
624
 * @code
625
 *   - sources[$source_key] = array(
626
 *       'data_type' => $data_type,
627
 *     );
628
 *     $operators[$data_type][$operator] = array(
629
 *       'form' => $form_key,
630
 *     );
631
 * @endcode
632
 *   - forms[$form_key]: A string representing an HTML form for an operator.
633
 *   - forms[$form_key][$source]: Or instead of a single form for all
634
 *     components, if each component requires its own form, key each component
635
 *     by its source value (currently always the component ID).
636
 *
637
 * @param object $node
638
 *   The Webform node for which these forms are being generated.
639
 */
640
function _webform_conditional_expand_value_forms($node) {
641
  $operators = webform_conditional_operators();
642
  $data = array();
643
  foreach ($operators as $data_type => $operator_info) {
644
    foreach ($operator_info as $operator => $data_operator_info) {
645
      $data['operators'][$data_type][$operator]['form'] = 'default';
646
      if (isset($data_operator_info['form callback'])) {
647
        $form_callback = $data_operator_info['form callback'];
648
        $data['operators'][$data_type][$operator]['form'] = $form_callback;
649
        if ($form_callback !== FALSE && !isset($data['forms'][$form_callback])) {
650
          $data['forms'][$form_callback] = $form_callback($node);
651
        }
652
      }
653
    }
654
  }
655

    
656
  foreach ($node->webform['components'] as $cid => $component) {
657
    if (webform_component_feature($component['type'], 'conditional')) {
658
      $data['sources'][$cid]['data_type'] = webform_component_property($component['type'], 'conditional_type');
659
    }
660
  }
661

    
662
  drupal_add_js(array('webform' => array('conditionalValues' => $data)), 'setting');
663
}
664

    
665
/**
666
 * Helper. Find the matching end of a given subconditional.
667
 *
668
 * @param array $rules
669
 *   Array of conditional rules to be searched.
670
 * @param int $origin_rid
671
 *   The starting rule id for the search.
672
 * @param int $target_delta_level
673
 *   The level that is sought. 0 for current left. -1 for parent.
674
 *
675
 * @return int
676
 *   The rid of the found rule, or -1 if none. Note that NULL is not used as a
677
 *   semaphore for "not found" because it casts to 0, which is a valid rule id.
678
 */
679
function _webform_conditional_find_end(array $rules, $origin_rid, $target_delta_level = 0) {
680
  $rids = array_keys($rules);
681
  $offset = array_search($origin_rid, $rids);
682
  $delta_level = 0;
683
  foreach (array_slice($rules, $offset, NULL, TRUE) as $rid => $conditional) {
684
    switch ($conditional['source_type']) {
685
      case 'conditional_start':
686
        $delta_level++;
687
        break;
688

    
689
      case 'conditional_end':
690
        $delta_level--;
691
        break;
692
    }
693
    if ($delta_level == $target_delta_level) {
694
      return $rid;
695
    }
696
  }
697
  // Mis-matched conditional_start / _end. Return -1.
698
  return -1;
699
}
700

    
701
/**
702
 * Helper. Find the matching start or end of a given subconditional.
703
 *
704
 * @see _webform_conditional_find_end()
705
 */
706
function _webform_conditional_find_start($rules, $origin_rid, $target_delta_level = 0) {
707
  $rids = array_keys($rules);
708
  $offset = array_search($origin_rid, $rids);
709
  $delta_level = 0;
710
  foreach (array_reverse(array_slice($rules, 0, $offset + 1, TRUE), TRUE) as $rid => $conditional) {
711
    switch ($conditional['source_type']) {
712
      case 'conditional_end':
713
        $delta_level++;
714
        break;
715

    
716
      case 'conditional_start':
717
        $delta_level--;
718
        break;
719
    }
720
    if ($delta_level == $target_delta_level) {
721
      return $rid;
722
    }
723
  }
724
  // Mis-matched conditional_start / _end. Return -1.
725
  return -1;
726
}
727

    
728
/**
729
 * Submit handler for webform_conditional elements to add a new rule or action.
730
 */
731
function webform_conditional_element_add($form, &$form_state) {
732
  $button = $form_state['clicked_button'];
733
  $parents = $button['#parents'];
734
  array_pop($parents);
735
  $rid = array_pop($parents);
736

    
737
  // Recurse through the form values until we find the Webform conditional rules
738
  // or actions. Save the conditional prior to descending to rules/actions.
739
  $parent_values = &$form_state['values'];
740
  $input_values = &$form_state['input'];
741
  foreach ($parents as $key) {
742
    if (array_key_exists($key, $parent_values)) {
743
      $conditional = $parent_values;
744
      $parent_values = &$parent_values[$key];
745
    }
746
    if (array_key_exists($key, $input_values)) {
747
      $input_values = &$input_values[$key];
748
    }
749
  }
750

    
751
  // Split the list of rules/actions in this conditional and inject into the
752
  // right spot.
753
  $rids = array_keys($parent_values);
754
  $offset = array_search($rid, $rids);
755
  $default_rule = isset($button['#subconditional'])
756
                    ? array(
757
                      'source' => NULL,
758
                      'source_type' => 'component',
759
                      'operator' => NULL,
760
                      'value' => NULL,
761
                    )
762
                    : array(
763
                      'target_type' => 'component',
764
                      'target' => NULL,
765
                      'invert' => NULL,
766
                      'action' => NULL,
767
                      'argument' => NULL,
768
                    );
769

    
770
  if (empty($button['#subconditional'])) {
771
    $new[0] = (isset($parent_values[$rid]['source_type']) && $parent_values[$rid]['source_type'] == 'component') ? $parent_values[$rid] : $default_rule;
772
  }
773
  else {
774
    // The default andor operator is opposite of current subconditional's
775
    // operatior.
776
    $parent_rid = _webform_conditional_find_start($parent_values, $rid, -1);
777
    $current_op = $parent_rid < 0 ? $conditional['andor'] : $parent_values[$parent_rid]['operator'];
778
    $current_op = $current_op == 'and' ? 'or' : 'and';
779
    $new = array(
780
      array('source_type' => 'conditional_start', 'operator' => $current_op) + $default_rule,
781
      $default_rule,
782
      $default_rule,
783
      array('source_type' => 'conditional_end') + $default_rule,
784
    );
785
  }
786

    
787
  // Update both $form_state['values'] and ['input] so that FAPI can merge
788
  // input values from the POST into the new form.
789
  $parent_values = array_merge(array_slice($parent_values, 0, $offset + 1), $new, array_slice($parent_values, $offset + 1));
790
  $input_values = array_merge(array_slice($input_values, 0, $offset + 1), $new, array_slice($input_values, $offset + 1));
791
  $form_state['rebuild'] = TRUE;
792
}
793

    
794
/**
795
 * Submit handler for webform_conditional elements to remove a rule or action.
796
 */
797
function webform_conditional_element_remove($form, &$form_state) {
798
  $button = $form_state['clicked_button'];
799
  $parents = $button['#parents'];
800
  $action = array_pop($parents);
801
  $rid = array_pop($parents);
802

    
803
  // Recurse through the form values until we find the root Webform conditional.
804
  $parent_values = &$form_state['values'];
805
  foreach ($parents as $key) {
806
    if (array_key_exists($key, $parent_values)) {
807
      $parent_values = &$parent_values[$key];
808
    }
809
  }
810
  switch ($parent_values[$rid]['source_type']) {
811
    case 'conditional_start':
812
      unset($parent_values[_webform_conditional_find_end($parent_values, $rid)]);
813
      break;
814

    
815
    case 'conditional_end':
816
      unset($parent_values[_webform_conditional_find_start($parent_values, $rid)]);
817
      break;
818
  }
819
  // Remove this rule or action from the list of conditionals.
820
  unset($parent_values[$rid]);
821

    
822
  $form_state['rebuild'] = TRUE;
823
}
824

    
825
/**
826
 * Helper. Delete any subconditionals which contain no rules.
827
 *
828
 * @param array $conditional
829
 *   Conditional array containing the rules.
830
 *
831
 * @return array
832
 *   Array of deleted subconditionals. Empty array if none were deleted.
833
 */
834
function webform_delete_empty_subconditionals(array &$conditional) {
835
  $deleted = array();
836
  do {
837
    $empty_deleted = FALSE;
838
    $open_rid = NULL;
839
    foreach ($conditional['rules'] as $rid => $rule) {
840
      switch ($rule['source_type']) {
841
        case 'conditional_start':
842
          $open_rid = $rid;
843
          break;
844

    
845
        case 'conditional_end':
846
          if ($open_rid) {
847
            // A conditional_start rule was immediately followed by a
848
            // conditional_end rule. Delete them both. Repeat the check in case
849
            // the parent is now empty.
850
            $deleted[$open_rid] = $open_rid;
851
            $deleted[$rid] = $rid;
852
            unset($conditional['rules'][$open_rid], $conditional['rules'][$rid]);
853
            $open_rid = NULL;
854
            $empty_deleted = TRUE;
855
          }
856
          break;
857

    
858
        default:
859
          $open_rid = NULL;
860
      }
861
    }
862
  } while ($empty_deleted);
863
  return $deleted;
864
}
865

    
866
/**
867
 * AJAX callback to render out adding a new condition.
868
 */
869
function webform_conditional_element_ajax($form, $form_state) {
870
  $button = $form_state['clicked_button'];
871
  $parents = $button['#parents'];
872

    
873
  // Trim down the parents to go back up to the level of this elements wrapper.
874
  // The button name (add/remove).
875
  array_pop($parents);
876
  // The rule ID.
877
  array_pop($parents);
878
  // The "rules" grouping.
879
  array_pop($parents);
880

    
881
  $element = $form;
882
  foreach ($parents as $key) {
883
    if (!isset($element[$key])) {
884
      // The entire conditional has been removed.
885
      return '';
886
    }
887
    $element = $element[$key];
888
  }
889

    
890
  return drupal_render($element['conditional']);
891
}
892

    
893
/**
894
 * Theme the form for a conditional action.
895
 */
896
function theme_webform_conditional($variables) {
897
  $element = $variables['element'];
898

    
899
  $output = '';
900
  $output .= '<div class="webform-conditional">';
901
  $output .= '<span class="webform-conditional-if">' . t('If') . '</span>';
902

    
903
  foreach (element_children($element['rules']) as $rid) {
904
    $rule = &$element['rules'][$rid];
905
    switch ($rule['source_type']['#value']) {
906
      case 'conditional_start':
907
        $source_phrase = '<div class="webform-subconditional">' . t('(') . '</div>';
908
        break;
909

    
910
      case 'conditional_end':
911
        $source_phrase = '<div class="webform-subconditional">' . t(')') . '</div>';
912
        break;
913

    
914
      default:
915
        // Hide labels.
916
        $rule['source']['#title_display'] = 'invisible';
917
        $rule['operator']['#title_display'] = 'invisible';
918
        $rule['value']['#title_display'] = 'invisible';
919

    
920
        $source = '<div class="webform-conditional-source">' . drupal_render($rule['source']) . '</div>';
921
        $operator = '<div class="webform-conditional-operator">' . drupal_render($rule['operator']) . '</div>';
922
        $value = '<div class="webform-conditional-value">' . drupal_render($rule['value']) . '</div>';
923

    
924
        $source_phrase = t('!source !operator !value', array(
925
          '!source' => $source,
926
          '!operator' => $operator,
927
          '!value' => $value,
928
        ));
929
    }
930

    
931
    $output .= '<div class="webform-conditional-rule">';
932
    // Can't use theme('indentation') here because it causes the draghandle to
933
    // be located after the last indentation div.
934
    $output .= str_repeat('<div class="webform-indentation">&nbsp;</div>', $rule['#level']);
935
    $output .= drupal_render($rule['source_type']);
936
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
937
    $output .= $source_phrase;
938
    $output .= '</div>';
939

    
940
    if (isset($rule['andor'])) {
941
      $rule['andor']['#title_display'] = 'invisible';
942
      $output .= '<div class="webform-conditional-andor webform-container-inline">';
943
      $output .= drupal_render($rule['andor']);
944
      $output .= '</div>';
945
    }
946

    
947
    if (isset($rule['add']) || isset($rule['remove'])) {
948
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
949
      $output .= drupal_render($rule['add_subconditional']);
950
      $output .= drupal_render($rule['add']);
951
      $output .= drupal_render($rule['remove']);
952
      $output .= '</span>';
953
    }
954

    
955
    $output .= '</div>';
956
  }
957

    
958
  // Hide labels.
959
  foreach (element_children($element['actions']) as $aid) {
960
    // Hide labels.
961
    $element['actions'][$aid]['target']['#title_display'] = 'invisible';
962
    $element['actions'][$aid]['invert']['#title_display'] = 'invisible';
963
    $element['actions'][$aid]['action']['#title_display'] = 'invisible';
964
    $element['actions'][$aid]['argument']['#title_display'] = 'invisible';
965

    
966
    $target = '<div class="webform-conditional-target">' . drupal_render($element['actions'][$aid]['target']) . '</div>';
967
    $invert = '<div class="webform-conditional-invert">' . drupal_render($element['actions'][$aid]['invert']) . '</div>';
968
    $action = '<div class="webform-conditional-action">' . drupal_render($element['actions'][$aid]['action']) . '</div>';
969
    $argument = '<div class="webform-conditional-argument">' . drupal_render($element['actions'][$aid]['argument']) . '</div>';
970

    
971
    $target_phrase = t('then !target !invert !action !argument', array(
972
      '!target' => $target,
973
      '!invert' => $invert,
974
      '!action' => $action,
975
      '!argument' => $argument,
976
    ));
977

    
978
    $output .= '<div class="webform-conditional-action">';
979
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
980
    $output .= $target_phrase;
981
    $output .= '</div>';
982

    
983
    if (isset($element['actions'][$aid]['add']) || isset($element['actions'][$aid]['remove'])) {
984
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
985
      $output .= drupal_render($element['actions'][$aid]['add']);
986
      $output .= drupal_render($element['actions'][$aid]['remove']);
987
      $output .= '</span>';
988
    }
989

    
990
    $output .= '</div>';
991
  }
992

    
993
  $output .= '</div>';
994

    
995
  return $output;
996
}
997

    
998
/**
999
 * Return a list of all Webform conditional operators.
1000
 */
1001
function webform_conditional_operators() {
1002
  static $operators;
1003

    
1004
  if (!isset($operators)) {
1005
    $operators = module_invoke_all('webform_conditional_operator_info');
1006
    drupal_alter('webform_conditional_operators', $operators);
1007
  }
1008

    
1009
  return $operators;
1010
}
1011

    
1012
/**
1013
 * Return a nested list of all available operators, suitable for a select list.
1014
 */
1015
function webform_conditional_operators_list() {
1016
  $options = array();
1017
  $operators = webform_conditional_operators();
1018

    
1019
  foreach ($operators as $data_type => $type_operators) {
1020
    $options[$data_type] = array();
1021
    foreach ($type_operators as $operator => $operator_info) {
1022
      $options[$data_type][$operator] = $operator_info['label'];
1023
    }
1024
  }
1025

    
1026
  return $options;
1027
}
1028

    
1029
/**
1030
 * Implements hook_webform_conditional_operator_info().
1031
 *
1032
 * Called from webform.module's webform_webform_conditional_operator_info().
1033
 */
1034
function _webform_conditional_operator_info() {
1035
  // General operators:
1036
  $operators['string']['equal'] = array(
1037
    'label' => t('is'),
1038
    'comparison callback' => 'webform_conditional_operator_string_equal',
1039
    'js comparison callback' => 'conditionalOperatorStringEqual',
1040
    // A form callback is not needed here, since we can use the default,
1041
    // non-JavaScript textfield for all text and numeric fields.
1042
    // @code
1043
    // 'form callback' => 'webform_conditional_operator_text',
1044
    // @endcode
1045
  );
1046
  $operators['string']['not_equal'] = array(
1047
    'label' => t('is not'),
1048
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
1049
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
1050
  );
1051
  $operators['string']['contains'] = array(
1052
    'label' => t('contains'),
1053
    'comparison callback' => 'webform_conditional_operator_string_contains',
1054
    'js comparison callback' => 'conditionalOperatorStringContains',
1055
  );
1056
  $operators['string']['does_not_contain'] = array(
1057
    'label' => t('does not contain'),
1058
    'comparison callback' => 'webform_conditional_operator_string_does_not_contain',
1059
    'js comparison callback' => 'conditionalOperatorStringDoesNotContain',
1060
  );
1061
  $operators['string']['begins_with'] = array(
1062
    'label' => t('begins with'),
1063
    'comparison callback' => 'webform_conditional_operator_string_begins_with',
1064
    'js comparison callback' => 'conditionalOperatorStringBeginsWith',
1065
  );
1066
  $operators['string']['ends_with'] = array(
1067
    'label' => t('ends with'),
1068
    'comparison callback' => 'webform_conditional_operator_string_ends_with',
1069
    'js comparison callback' => 'conditionalOperatorStringEndsWith',
1070
  );
1071
  $operators['string']['empty'] = array(
1072
    'label' => t('is blank'),
1073
    'comparison callback' => 'webform_conditional_operator_string_empty',
1074
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1075
    // No value form at all.
1076
    'form callback' => FALSE,
1077
  );
1078
  $operators['string']['not_empty'] = array(
1079
    'label' => t('is not blank'),
1080
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1081
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1082
    // No value form at all.
1083
    'form callback' => FALSE,
1084
  );
1085

    
1086
  // Numeric operators.
1087
  $operators['numeric']['equal'] = array(
1088
    'label' => t('is equal to'),
1089
    'comparison callback' => 'webform_conditional_operator_numeric_equal',
1090
    'js comparison callback' => 'conditionalOperatorNumericEqual',
1091
  );
1092
  $operators['numeric']['not_equal'] = array(
1093
    'label' => t('is not equal to'),
1094
    'comparison callback' => 'webform_conditional_operator_numeric_not_equal',
1095
    'js comparison callback' => 'conditionalOperatorNumericNotEqual',
1096
  );
1097
  $operators['numeric']['less_than'] = array(
1098
    'label' => t('is less than'),
1099
    'comparison callback' => 'webform_conditional_operator_numeric_less_than',
1100
    'js comparison callback' => 'conditionalOperatorNumericLessThan',
1101
  );
1102
  $operators['numeric']['less_than_equal'] = array(
1103
    'label' => t('is less than or equal'),
1104
    'comparison callback' => 'webform_conditional_operator_numeric_less_than_equal',
1105
    'js comparison callback' => 'conditionalOperatorNumericLessThanEqual',
1106
  );
1107
  $operators['numeric']['greater_than'] = array(
1108
    'label' => t('is greater than'),
1109
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than',
1110
    'js comparison callback' => 'conditionalOperatorNumericGreaterThan',
1111
  );
1112
  $operators['numeric']['greater_than_equal'] = array(
1113
    'label' => t('is greater than or equal'),
1114
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than_equal',
1115
    'js comparison callback' => 'conditionalOperatorNumericGreaterThanEqual',
1116
  );
1117
  $operators['numeric']['empty'] = array(
1118
    'label' => t('is blank'),
1119
    'comparison callback' => 'webform_conditional_operator_string_empty',
1120
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1121
    // No value form at all.
1122
    'form callback' => FALSE,
1123
  );
1124
  $operators['numeric']['not_empty'] = array(
1125
    'label' => t('is not blank'),
1126
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1127
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1128
    // No value form at all.
1129
    'form callback' => FALSE,
1130
  );
1131

    
1132
  // Select operators.
1133
  $operators['select']['equal'] = array(
1134
    'label' => t('is'),
1135
    'comparison callback' => 'webform_conditional_operator_string_equal',
1136
    'js comparison callback' => 'conditionalOperatorStringEqual',
1137
    'form callback' => 'webform_conditional_form_select',
1138
  );
1139
  $operators['select']['not_equal'] = array(
1140
    'label' => t('is not'),
1141
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
1142
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
1143
    'form callback' => 'webform_conditional_form_select',
1144
  );
1145
  $operators['select']['less_than'] = array(
1146
    'label' => t('is before'),
1147
    'comparison callback' => 'webform_conditional_operator_select_less_than',
1148
    'js comparison callback' => 'conditionalOperatorSelectLessThan',
1149
    'form callback' => 'webform_conditional_form_select',
1150
  );
1151
  $operators['select']['less_than_equal'] = array(
1152
    'label' => t('is or is before'),
1153
    'comparison callback' => 'webform_conditional_operator_select_less_than_equal',
1154
    'js comparison callback' => 'conditionalOperatorSelectLessThanEqual',
1155
    'form callback' => 'webform_conditional_form_select',
1156
  );
1157
  $operators['select']['greater_than'] = array(
1158
    'label' => t('is after'),
1159
    'comparison callback' => 'webform_conditional_operator_select_greater_than',
1160
    'js comparison callback' => 'conditionalOperatorSelectGreaterThan',
1161
    'form callback' => 'webform_conditional_form_select',
1162
  );
1163
  $operators['select']['greater_than_equal'] = array(
1164
    'label' => t('is or is after'),
1165
    'comparison callback' => 'webform_conditional_operator_select_greater_than_equal',
1166
    'js comparison callback' => 'conditionalOperatorSelectGreaterThanEqual',
1167
    'form callback' => 'webform_conditional_form_select',
1168
  );
1169
  $operators['select']['empty'] = array(
1170
    'label' => t('is empty'),
1171
    'comparison callback' => 'webform_conditional_operator_string_empty',
1172
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1173
    // No value form at all.
1174
    'form callback' => FALSE,
1175
  );
1176
  $operators['select']['not_empty'] = array(
1177
    'label' => t('is not empty'),
1178
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1179
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1180
    // No value form at all.
1181
    'form callback' => FALSE,
1182
  );
1183

    
1184
  // Date operators:
1185
  $operators['date']['equal'] = array(
1186
    'label' => t('is on'),
1187
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
1188
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1189
    'js comparison callback' => 'conditionalOperatorDateEqual',
1190
    'form callback' => 'webform_conditional_form_date',
1191
  );
1192
  $operators['date']['not_equal'] = array(
1193
    'label' => t('is not on'),
1194
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
1195
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1196
    'js comparison callback' => 'conditionalOperatorDateNotEqual',
1197
    'form callback' => 'webform_conditional_form_date',
1198
  );
1199
  $operators['date']['before'] = array(
1200
    'label' => t('is before'),
1201
    'comparison callback' => 'webform_conditional_operator_datetime_before',
1202
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1203
    'js comparison callback' => 'conditionalOperatorDateBefore',
1204
    'form callback' => 'webform_conditional_form_date',
1205
  );
1206
  $operators['date']['before_equal'] = array(
1207
    'label' => t('is on or before'),
1208
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
1209
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1210
    'js comparison callback' => 'conditionalOperatorDateBeforeEqual',
1211
    'form callback' => 'webform_conditional_form_date',
1212
  );
1213
  $operators['date']['after'] = array(
1214
    'label' => t('is after'),
1215
    'comparison callback' => 'webform_conditional_operator_datetime_after',
1216
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1217
    'js comparison callback' => 'conditionalOperatorDateAfter',
1218
    'form callback' => 'webform_conditional_form_date',
1219
  );
1220
  $operators['date']['after_equal'] = array(
1221
    'label' => t('is on or after'),
1222
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
1223
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1224
    'js comparison callback' => 'conditionalOperatorDateAfterEqual',
1225
    'form callback' => 'webform_conditional_form_date',
1226
  );
1227

    
1228
  // Time operators:
1229
  $operators['time']['equal'] = array(
1230
    'label' => t('is at'),
1231
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
1232
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1233
    'js comparison callback' => 'conditionalOperatorTimeEqual',
1234
    'form callback' => 'webform_conditional_form_time',
1235
  );
1236
  $operators['time']['not_equal'] = array(
1237
    'label' => t('is not at'),
1238
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
1239
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1240
    'js comparison callback' => 'conditionalOperatorTimeNotEqual',
1241
    'form callback' => 'webform_conditional_form_time',
1242
  );
1243
  $operators['time']['before'] = array(
1244
    'label' => t('is before'),
1245
    'comparison callback' => 'webform_conditional_operator_datetime_before',
1246
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1247
    'js comparison callback' => 'conditionalOperatorTimeBefore',
1248
    'form callback' => 'webform_conditional_form_time',
1249
  );
1250
  $operators['time']['before_equal'] = array(
1251
    'label' => t('is at or before'),
1252
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
1253
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1254
    'js comparison callback' => 'conditionalOperatorTimeBeforeEqual',
1255
    'form callback' => 'webform_conditional_form_time',
1256
  );
1257
  $operators['time']['after'] = array(
1258
    'label' => t('is after'),
1259
    'comparison callback' => 'webform_conditional_operator_datetime_after',
1260
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1261
    'js comparison callback' => 'conditionalOperatorTimeAfter',
1262
    'form callback' => 'webform_conditional_form_time',
1263
  );
1264
  $operators['time']['after_equal'] = array(
1265
    'label' => t('is at or after'),
1266
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
1267
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1268
    'js comparison callback' => 'conditionalOperatorTimeAfterEqual',
1269
    'form callback' => 'webform_conditional_form_time',
1270
  );
1271

    
1272
  return $operators;
1273
}
1274

    
1275
/**
1276
 * Form callback for select-type conditional fields.
1277
 *
1278
 * Unlike other built-in conditional value forms, the form callback for select
1279
 * types provides an array of forms, keyed by the $cid, which is the "source"
1280
 * for the condition.
1281
 */
1282
function webform_conditional_form_select($node) {
1283
  static $count = 0;
1284
  $forms = array();
1285
  webform_component_include('select');
1286
  foreach ($node->webform['components'] as $cid => $component) {
1287
    if (webform_component_property($component['type'], 'conditional_type') == 'select') {
1288
      // @todo: Use a pluggable mechanism for retrieving select list values.
1289
      $options = _webform_select_options($component);
1290
      $element = array(
1291
        '#type' => 'select',
1292
        '#multiple' => FALSE,
1293
        '#size' => NULL,
1294
        '#attributes' => array(),
1295
        '#id' => NULL,
1296
        '#name' => 'webform-conditional-select-' . $cid . '-' . $count,
1297
        '#options' => $options,
1298
        '#parents' => array(),
1299
      );
1300
      $forms[$cid] = drupal_render($element);
1301
    }
1302
  }
1303
  $count++;
1304
  return $forms;
1305
}
1306

    
1307
/**
1308
 * Form callback for date conditional fields.
1309
 */
1310
function webform_conditional_form_date($node) {
1311
  static $count = 0;
1312
  $element = array(
1313
    '#title' => NULL,
1314
    '#title_display' => 'invisible',
1315
    '#size' => 24,
1316
    '#attributes' => array('placeholder' => t('@format or valid date', array('@format' => webform_date_format('short')))),
1317
    '#type' => 'textfield',
1318
    '#name' => 'webform-conditional-date-' . $count++,
1319
  );
1320
  return drupal_render($element);
1321
}
1322

    
1323
/**
1324
 * Form callback for time conditional fields.
1325
 */
1326
function webform_conditional_form_time($node) {
1327
  static $count = 0;
1328
  $element = array(
1329
    '#title' => NULL,
1330
    '#title_display' => 'invisible',
1331
    '#size' => 24,
1332
    '#attributes' => array('placeholder' => t('HH:MMam or valid time')),
1333
    '#type' => 'textfield',
1334
    '#name' => 'webform-conditional-time-' . $count++,
1335
  );
1336
  return drupal_render($element);
1337
}
1338

    
1339
/**
1340
 * Load a conditional setting from the database.
1341
 */
1342
function webform_conditional_load($rgid, $nid) {
1343
  $node = node_load($nid);
1344

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

    
1347
  return $conditional;
1348
}
1349

    
1350
/**
1351
 * Insert a conditional rule group into the database.
1352
 */
1353
function webform_conditional_insert($conditional) {
1354
  $transaction = db_transaction();
1355
  drupal_write_record('webform_conditional', $conditional);
1356
  foreach ($conditional['rules'] as $rid => $rule) {
1357
    $rule['nid'] = $conditional['nid'];
1358
    $rule['rgid'] = $conditional['rgid'];
1359
    $rule['rid'] = $rid;
1360
    drupal_write_record('webform_conditional_rules', $rule);
1361
  }
1362
  foreach ($conditional['actions'] as $aid => $action) {
1363
    $action['nid'] = $conditional['nid'];
1364
    $action['rgid'] = $conditional['rgid'];
1365
    $action['aid'] = $aid;
1366
    drupal_write_record('webform_conditional_actions', $action);
1367
  }
1368
}
1369

    
1370
/**
1371
 * Update a conditional setting in the database.
1372
 */
1373
function webform_conditional_update($node, $conditional) {
1374
  $transaction = db_transaction();
1375
  webform_conditional_delete($node, $conditional);
1376
  webform_conditional_insert($conditional);
1377
}
1378

    
1379
/**
1380
 * Delete a conditional rule group.
1381
 */
1382
function webform_conditional_delete($node, $conditional) {
1383
  $transaction = db_transaction();
1384
  db_delete('webform_conditional')
1385
    ->condition('nid', $node->nid)
1386
    ->condition('rgid', $conditional['rgid'])
1387
    ->execute();
1388
  db_delete('webform_conditional_rules')
1389
    ->condition('nid', $node->nid)
1390
    ->condition('rgid', $conditional['rgid'])
1391
    ->execute();
1392
  db_delete('webform_conditional_actions')
1393
    ->condition('nid', $node->nid)
1394
    ->condition('rgid', $conditional['rgid'])
1395
    ->execute();
1396
}
1397

    
1398
/**
1399
 * Loop through all the conditional settings and add needed JavaScript settings.
1400
 *
1401
 * We do a bit of optimization for JavaScript before adding to the page as
1402
 * settings. We remove unnecessary data structures and provide a "source map"
1403
 * so that JavaScript can quickly determine if it needs to check rules when a
1404
 * field on the page has been modified.
1405
 *
1406
 * @param object $node
1407
 *   The loaded node object, containing the webform.
1408
 * @param array $submission_data
1409
 *   The cid-indexed array of existing submission values to be included for
1410
 *   sources outside of the current page.
1411
 * @param int $page_num
1412
 *   The number of the page for which javascript settings should be generated.
1413
 *
1414
 * @return array
1415
 *   Array of settings to be send to the browser as javascript settings.
1416
 */
1417
function webform_conditional_prepare_javascript($node, array $submission_data, $page_num) {
1418
  $settings = array(
1419
    'ruleGroups' => array(),
1420
    'sourceMap' => array(),
1421
    'values' => array(),
1422
  );
1423
  $operators = webform_conditional_operators();
1424
  $conditionals = $node->webform['conditionals'];
1425
  $components = $node->webform['components'];
1426
  $topological_order = webform_get_conditional_sorter($node)->getOrder();
1427
  foreach ($topological_order[$page_num] as $conditional_spec) {
1428
    $conditional = $conditionals[$conditional_spec['rgid']];
1429
    $rgid_key = 'rgid_' . $conditional['rgid'];
1430
    // Assemble the main conditional group settings.
1431
    $settings['ruleGroups'][$rgid_key] = array(
1432
      'andor' => $conditional['andor'],
1433
    );
1434
    foreach ($conditional['actions'] as $action) {
1435
      if ($action['target_type'] == 'component') {
1436
        $target_component = $components[$action['target']];
1437
        $target_parents = webform_component_parent_keys($node, $target_component);
1438
        $aid_key = 'aid_' . $action['aid'];
1439
        $action_settings = array(
1440
          'target' => 'webform-component--' . str_replace('_', '-', implode('--', $target_parents)),
1441
          'invert' => (int) $action['invert'],
1442
          'action' => $action['action'],
1443
          'argument' => $components[$action['target']]['type'] == 'markup' ? filter_xss_admin($action['argument']) : $action['argument'],
1444
        );
1445
        $settings['ruleGroups'][$rgid_key]['actions'][$aid_key] = $action_settings;
1446
      }
1447
    }
1448
    // Add on the list of rules to the conditional group.
1449
    foreach ($conditional['rules'] as $rule) {
1450
      $rid_key = 'rid_' . $rule['rid'];
1451
      switch ($rule['source_type']) {
1452
        case 'component':
1453
          $source_component = $components[$rule['source']];
1454
          $source_parents = webform_component_parent_keys($node, $source_component);
1455
          $source_id = 'webform-component--' . str_replace('_', '-', implode('--', $source_parents));
1456

    
1457
          // If this source has a value set, add that as a setting. NULL or
1458
          // array(NULL) should be sent as an empty array to simplify the
1459
          // jQuery.
1460
          if (isset($submission_data[$source_component['cid']])) {
1461
            $source_value = $submission_data[$source_component['cid']];
1462
            $source_value = is_array($source_value) ? $source_value : array($source_value);
1463
            $settings['values'][$source_id] = $source_value === array(NULL) ? array() : $source_value;
1464
          }
1465

    
1466
          $conditional_type = webform_component_property($source_component['type'], 'conditional_type');
1467
          $operator_info = $operators[$conditional_type][$rule['operator']];
1468
          $rule_settings = array(
1469
            'source_type' => $rule['source_type'],
1470
            'source' => $source_id,
1471
            'value' => $rule['value'],
1472
            'callback' => $operator_info['js comparison callback'],
1473
          );
1474
          if (isset($operator_info['comparison prepare js'])) {
1475
            $callback = $operator_info['comparison prepare js'];
1476
            $rule_settings['value'] = $callback($rule['value']);
1477
          }
1478
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = $rule_settings;
1479
          $settings['sourceMap'][$source_id][$rgid_key] = $rgid_key;
1480
          break;
1481

    
1482
        case 'conditional_start':
1483
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1484
            'source_type' => $rule['source_type'],
1485
            'andor' => $rule['operator'],
1486
          );
1487
          break;
1488

    
1489
        case 'conditional_end':
1490
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1491
            'source_type' => $rule['source_type'],
1492
          );
1493
          break;
1494
      }
1495
    }
1496
  }
1497

    
1498
  return $settings;
1499
}
1500

    
1501
/**
1502
 * Determine whether a component type is capable of a given conditional action.
1503
 */
1504
function webform_conditional_action_able($component_type, $action) {
1505
  switch ($action) {
1506
    case 'show':
1507
      return TRUE;
1508

    
1509
    case 'require':
1510
      return webform_component_feature($component_type, 'required');
1511

    
1512
    default:
1513
      return webform_component_feature($component_type, "conditional_action_$action");
1514
  }
1515
}
1516

    
1517
/**
1518
 * Prepare a conditional value for adding as a JavaScript setting.
1519
 */
1520
function webform_conditional_prepare_date_js($rule_value) {
1521
  // Convert the time/date string to a UTC timestamp for comparison. Note that
1522
  // this means comparisons against immediate times (such as "now") may be
1523
  // slightly stale by the time the comparison executes. Timestamps are in
1524
  // milliseconds, as to match JavaScript's Date.toString() method.
1525
  $date = webform_strtodate('c', $rule_value, 'UTC');
1526
  return webform_strtotime($date);
1527
}
1528

    
1529
/**
1530
 * Prepare a conditional value for adding as a JavaScript setting.
1531
 */
1532
function webform_conditional_prepare_time_js($rule_value) {
1533
  $date = webform_conditional_prepare_date_js($rule_value);
1534
  $today = webform_strtodate('c', 'today', 'UTC');
1535
  $today = webform_strtotime($today);
1536
  return $date - $today;
1537
}
1538

    
1539
/**
1540
 * Conditional callback for string comparisons.
1541
 */
1542
function webform_conditional_operator_string_equal($input_values, $rule_value) {
1543
  foreach ($input_values as $value) {
1544
    // Checkbox values come in as 0 integers for unchecked boxes.
1545
    $value = ($value === 0) ? '' : $value;
1546
    if (strcasecmp($value, $rule_value) === 0) {
1547
      return TRUE;
1548
    }
1549
  }
1550
  return FALSE;
1551
}
1552

    
1553
/**
1554
 * Conditional callback for string comparisons.
1555
 */
1556
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
1557
  return !webform_conditional_operator_string_equal($input_values, $rule_value);
1558
}
1559

    
1560
/**
1561
 * Conditional callback for string comparisons.
1562
 */
1563
function webform_conditional_operator_string_contains($input_values, $rule_value) {
1564
  foreach ($input_values as $value) {
1565
    if (stripos($value, $rule_value) !== FALSE) {
1566
      return TRUE;
1567
    }
1568
  }
1569
  return FALSE;
1570
}
1571

    
1572
/**
1573
 * Conditional callback for string comparisons.
1574
 */
1575
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
1576
  return !webform_conditional_operator_string_contains($input_values, $rule_value);
1577
}
1578

    
1579
/**
1580
 * Conditional callback for string comparisons.
1581
 */
1582
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
1583
  foreach ($input_values as $value) {
1584
    if (stripos($value, $rule_value) === 0) {
1585
      return TRUE;
1586
    }
1587
  }
1588
  return FALSE;
1589
}
1590

    
1591
/**
1592
 * Conditional callback for string comparisons.
1593
 */
1594
function webform_conditional_operator_string_ends_with($input_values, $rule_value) {
1595
  foreach ($input_values as $value) {
1596
    if (strripos($value, $rule_value) === strlen($value) - strlen($rule_value)) {
1597
      return TRUE;
1598
    }
1599
  }
1600
  return FALSE;
1601
}
1602

    
1603
/**
1604
 * Conditional callback for checking for empty fields.
1605
 */
1606
function webform_conditional_operator_string_empty($input_values, $rule_value) {
1607
  $empty = TRUE;
1608
  foreach ($input_values as $value) {
1609
    if ($value !== '' && $value !== NULL && $value !== 0) {
1610
      $empty = FALSE;
1611
      break;
1612
    }
1613
  }
1614
  return $empty;
1615
}
1616

    
1617
/**
1618
 * Conditional callback for checking for empty fields.
1619
 */
1620
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
1621
  return !webform_conditional_operator_string_empty($input_values, $rule_value);
1622
}
1623

    
1624
/**
1625
 * Conditional callback for select comparisons.
1626
 */
1627
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
1628
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
1629
}
1630

    
1631
/**
1632
 * Conditional callback for select comparisons.
1633
 */
1634
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
1635
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1636
  return $comparison < 0 || $comparison === 0;
1637
}
1638

    
1639
/**
1640
 * Conditional callback for select comparisons.
1641
 */
1642
function webform_conditional_operator_select_greater_than($input_values, $rule_value, $component) {
1643
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) > 0;
1644
}
1645

    
1646
/**
1647
 * Conditional callback for select comparisons.
1648
 */
1649
function webform_conditional_operator_select_greater_than_equal($input_values, $rule_value, $component) {
1650
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1651
  return $comparison > 0 || $comparison === 0;
1652
}
1653

    
1654
/**
1655
 * Conditional callback for numeric comparisons.
1656
 */
1657
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
1658
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
1659
}
1660

    
1661
/**
1662
 * Conditional callback for numeric comparisons.
1663
 */
1664
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
1665
  return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
1666
}
1667

    
1668
/**
1669
 * Conditional callback for numeric comparisons.
1670
 */
1671
function webform_conditional_operator_numeric_less_than($input_values, $rule_value) {
1672
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) < 0;
1673
}
1674

    
1675
/**
1676
 * Conditional callback for numeric comparisons.
1677
 */
1678
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
1679
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1680
  return $comparison < 0 || $comparison === 0;
1681
}
1682

    
1683
/**
1684
 * Conditional callback for numeric comparisons.
1685
 */
1686
function webform_conditional_operator_numeric_greater_than($input_values, $rule_value) {
1687
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) > 0;
1688
}
1689

    
1690
/**
1691
 * Conditional callback for numeric comparisons.
1692
 */
1693
function webform_conditional_operator_numeric_greater_than_equal($input_values, $rule_value) {
1694
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1695
  return $comparison > 0 || $comparison === 0;
1696
}
1697

    
1698
/**
1699
 * Conditional callback for date and time comparisons.
1700
 */
1701
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
1702
  $input_values = webform_conditional_value_datetime($input_values);
1703
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
1704
}
1705

    
1706
/**
1707
 * Conditional callback for date and time comparisons.
1708
 */
1709
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
1710
  return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
1711
}
1712

    
1713
/**
1714
 * Conditional callback for date and time comparisons.
1715
 */
1716
function webform_conditional_operator_datetime_after($input_values, $rule_value) {
1717
  $input_values = webform_conditional_value_datetime($input_values);
1718
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) > webform_strtotime($rule_value);
1719
}
1720

    
1721
/**
1722
 * Conditional callback for date and time comparisons.
1723
 */
1724
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
1725
  return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
1726
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1727
}
1728

    
1729
/**
1730
 * Conditional callback for date and time comparisons.
1731
 */
1732
function webform_conditional_operator_datetime_before($input_values, $rule_value) {
1733
  $input_values = webform_conditional_value_datetime($input_values);
1734
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) < webform_strtotime($rule_value);
1735
}
1736

    
1737
/**
1738
 * Conditional callback for date and time comparisons.
1739
 */
1740
function webform_conditional_operator_datetime_before_equal($input_values, $rule_value) {
1741
  return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
1742
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1743
}
1744

    
1745
/**
1746
 * Utility function to convert incoming time and dates into strings.
1747
 */
1748
function webform_conditional_value_datetime($input_values) {
1749
  // Convert times into a string.
1750
  $input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
1751
  // Convert dates into a string.
1752
  $input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
1753
  return $input_values;
1754
}
1755

    
1756
/**
1757
 * Utility function to compare values of a select component.
1758
 *
1759
 * @param string $a
1760
 *   First select option key to compare.
1761
 * @param string $b
1762
 *   Second select option key to compare.
1763
 * @param array $options
1764
 *   Associative array where the $a and $b are within the keys.
1765
 *
1766
 * @return int|null
1767
 *   Based upon position of $a and $b in $options:
1768
 *   -N if $a above (<) $b
1769
 *   0 if $a = $b
1770
 *   +N if $a is below (>) $b
1771
 */
1772
function webform_compare_select($a, $b, array $options) {
1773
  // Select keys that are integer-like strings are numeric indices in PHP.
1774
  // Convert the array keys to an array of strings.
1775
  $options_array = array_map(function ($i) {
1776
    return (string) $i;
1777
  }, array_keys($options));
1778
  $a_position = array_search($a, $options_array, TRUE);
1779
  $b_position = array_search($b, $options_array, TRUE);
1780
  return ($a_position === FALSE || $b_position === FALSE) ? NULL : $a_position - $b_position;
1781
}