Project

General

Profile

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

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

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
        // Validate component rules, but not conditional_start/end rules.
237
        if (is_numeric($rule_key) && $rule['source_type']['#value'] == 'component' && isset($targets[$rule['source']['#value']])) {
238
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][source',
239
            t('The subject of the conditional cannot be the same as the component that is changed (%target).',
240
            array('%target' => $components[$rule['source']['#value']]['name'])));
241
        }
242
      }
243
    }
244
  }
245

    
246
  // Form validation will not rebuild the form, so we need to ensure
247
  // necessary JavaScript will still exist.
248
  _webform_conditional_expand_value_forms($node);
249
}
250

    
251
/**
252
 * Submit handler for webform_conditionals_form().
253
 */
254
function webform_conditionals_form_submit($form, &$form_state) {
255
  $node = $form['#node'];
256

    
257
  // Remove the new conditional placeholder.
258
  unset($form_state['values']['conditionals']['new']);
259

    
260
  $node->webform['conditionals'] = $form_state['values']['conditionals'];
261
  node_save($node);
262
  drupal_set_message(t('Conditionals for %title saved.', array('%title' => $node->title)));
263
}
264

    
265
/**
266
 * AJAX callback to render out adding a new condition.
267
 */
268
function webform_conditionals_ajax($form, $form_state) {
269
  $rgids = element_children($form['conditionals']);
270
  $new_rgid = max($rgids);
271
  $form['conditionals'][$new_rgid]['#ajax_added'] = TRUE;
272

    
273
  $commands = array('#type' => 'ajax');
274
  $commands['#commands'][] = ajax_command_before('.webform-conditional-new-row', drupal_render($form['conditionals'][$new_rgid]));
275
  $commands['#commands'][] = ajax_command_restripe('#webform-conditionals-table');
276
  return $commands;
277
}
278

    
279
/**
280
 * Theme the $form['conditionals'] of webform_conditionals_form().
281
 */
282
function theme_webform_conditional_groups($variables) {
283
  $element = $variables['element'];
284
  drupal_add_tabledrag('webform-conditionals-table', 'order', 'sibling', 'webform-conditional-weight');
285
  drupal_add_js('Drupal.theme.prototype.tableDragChangedMarker = function() { return ""; }', 'inline');
286
  drupal_add_js('Drupal.theme.prototype.tableDragChangedWarning = function() { return "<span>&nbsp;</span>"; }', 'inline');
287

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

    
313
  return $output;
314
}
315

    
316
/**
317
 * Theme an individual conditional row of webform_conditionals_form().
318
 */
319
function theme_webform_conditional_group_row($variables) {
320
  $element = $variables['element'];
321

    
322
  $element['weight']['#attributes']['class'] = array('webform-conditional-weight');
323
  $weight = drupal_render($element['weight']);
324
  $classes = array('draggable');
325
  if (!empty($element['#even_odd'])) {
326
    $classes[] = $element['#even_odd'];
327
  }
328
  if (!empty($element['#ajax_added'])) {
329
    $classes[] = 'ajax-new-content';
330
  }
331

    
332
  $output = '';
333
  $output .= '<tr class="' . implode(' ', $classes) . '">';
334
  $output .= '<td>' . drupal_render_children($element) . '</td>';
335
  $output .= '<td>' . $weight . '</td>';
336
  $output .= '</tr>';
337

    
338
  return $output;
339
}
340

    
341
/**
342
 * Form API #process function to expand a webform conditional element.
343
 */
344
function _webform_conditional_expand($element) {
345
  $default_operator = 'and';
346

    
347
  $element['#tree'] = TRUE;
348
  $element['#default_value'] += array(
349
    'andor' => $default_operator,
350
  );
351

    
352
  $wrapper_id = drupal_clean_css_identifier(implode('-', $element['#parents'])) . '-ajax';
353
  $element['#prefix'] = '<div id="' . $wrapper_id . '">';
354
  $element['#suffix'] = '</div>';
355
  $element['#wrapper_id'] = $wrapper_id;
356

    
357
  // Note: When rules or actions are added, the new rules are inserted into
358
  // $form_state['values']. So that FAPI can merge data from the post,
359
  // $form_state['input'] must be adjusted to. To make this easier, hidden
360
  // fields are added to the conditional_start and _end rules to ensure that
361
  // each rule is represented in the POST.
362
  $level = 0;
363
  $andor_stack[0] = array(
364
    'value' => $element['#default_value']['andor'],
365
    'parents' => array_merge($element['#parents'], array('andor')),
366
    'rid' => 0,
367
    'first' => TRUE,
368
  );
369

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

    
394
      case 'conditional_end':
395
        --$level;
396
        $element['rules'][$rid] = array(
397
          '#level' => $level,
398
          'source_type' => array(
399
            '#type' => 'hidden',
400
            '#value' => 'conditional_end',
401
          ),
402
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
403
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
404
          'remove' => _webform_conditional_remove_expand($element, $rid),
405
          'andor' => _webform_conditional_andor_expand($andor_stack[$level]),
406
        );
407
        // Remove the last nested and/or.
408
        unset($element['rules'][$last_rid]['andor']);
409
        break;
410

    
411
      case 'component':
412
        $element['rules'][$rid] = _webform_conditional_rule_expand($element, $rid, $conditional, $level, $andor_stack[$level]);
413
        break;
414

    
415
      default:
416
        drupal_set_message(t('Unexpected conditional rule source type found (rule id @rid). Contact the administrator.', array('@rid' => $rid)), 'error');
417
    }
418
    $last_rid = $rid;
419
  }
420

    
421
  // Remove the last and/or.
422
  unset($element['rules'][$rid]['andor']);
423

    
424
  foreach ($element['#default_value']['actions'] as $aid => $action) {
425
    $element['actions'][$aid] = _webform_conditional_action_expand($element, $aid, $action);
426
  }
427

    
428
  return $element;
429
}
430

    
431
/**
432
 * Helper. Generate the and/or select or static text.
433
 */
434
function _webform_conditional_andor_expand(&$andor) {
435
  if ($andor['first']) {
436
    $andor['first'] = FALSE;
437
    return array(
438
      '#type' => 'select',
439
      '#title' => t('And/or'),
440
      '#options' => array(
441
        'and' => t('and'),
442
        'or' => t('or'),
443
      ),
444
      '#parents' => $andor['parents'],
445
      '#default_value' => $andor['value'],
446
      '#attributes' => array('data-rid' => $andor['rid']),
447
    );
448
  }
449
  else {
450
    return array(
451
      '#type' => 'container',
452
      '#attributes' => array('class' => array('webform-andor'), 'data-rid' => $andor['rid']),
453
      'andor_text' => array(
454
        '#markup' => $andor['value'] == 'or' ? t('or') : t('and'),
455
      ),
456
    );
457
  }
458
}
459

    
460
/**
461
 * Helper. Generate the add_subconditional (+) or add + button.
462
 */
463
function _webform_conditional_add_expand($element, $rid, $subconditional) {
464
  return array(
465
    '#type' => 'submit',
466
    '#value' => $subconditional ? t('(+)') : t('+'),
467
    '#submit' => array('webform_conditional_element_add'),
468
    '#subconditional' => $subconditional,
469
    '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . ($subconditional ? '_add_subconditional' : '_add'),
470
    '#attributes' => array('class' => array('webform-conditional-rule-add')),
471
    '#ajax' => array(
472
      'progress' => 'none',
473
      'callback' => 'webform_conditional_element_ajax',
474
      'wrapper' => $element['#wrapper_id'],
475
      'event' => 'click',
476
    ),
477
  );
478
}
479

    
480
/**
481
 * Helper. Generate the add_subconditional (+), add + or remove - button.
482
 */
483
function _webform_conditional_remove_expand($element, $rid) {
484
  return array(
485
    '#type' => 'submit',
486
    '#value' => t('-'),
487
    '#submit' => array('webform_conditional_element_remove'),
488
    '#name' => implode('_', $element['#parents']) . '_rules_' . $rid . '_remove',
489
    '#attributes' => array('class' => array('webform-conditional-rule-remove')),
490
    '#ajax' => array(
491
      'progress' => 'none',
492
      'callback' => 'webform_conditional_element_ajax',
493
      'wrapper' => $element['#wrapper_id'],
494
      'event' => 'click',
495
    ),
496
  );
497
}
498

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

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

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

    
646
  foreach ($node->webform['components'] as $cid => $component) {
647
    if (webform_component_feature($component['type'], 'conditional')) {
648
      $data['sources'][$cid]['data_type'] = webform_component_property($component['type'], 'conditional_type');
649
    }
650
  }
651

    
652
  drupal_add_js(array('webform' => array('conditionalValues' => $data)), 'setting');
653
}
654

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

    
679
      case 'conditional_end':
680
        $delta_level--;
681
        break;
682
    }
683
    if ($delta_level == $target_delta_level) {
684
      return $rid;
685
    }
686
  }
687
  // Mis-matched conditional_start / _end. Return -1.
688
  return -1;
689
}
690

    
691
/**
692
 * Helper. Find the matching start or end of a given subconditional.
693
 *
694
 * @see _webform_conditional_find_end()
695
 */
696
function _webform_conditional_find_start($rules, $origin_rid, $target_delta_level = 0) {
697
  $rids = array_keys($rules);
698
  $offset = array_search($origin_rid, $rids);
699
  $delta_level = 0;
700
  foreach (array_reverse(array_slice($rules, 0, $offset + 1, TRUE), TRUE) as $rid => $conditional) {
701
    switch ($conditional['source_type']) {
702
      case 'conditional_end':
703
        $delta_level++;
704
        break;
705

    
706
      case 'conditional_start':
707
        $delta_level--;
708
        break;
709
    }
710
    if ($delta_level == $target_delta_level) {
711
      return $rid;
712
    }
713
  }
714
  // Mis-matched conditional_start / _end. Return -1.
715
  return -1;
716
}
717

    
718
/**
719
 * Submit handler for webform_conditional elements to add a new rule or action.
720
 */
721
function webform_conditional_element_add($form, &$form_state) {
722
  $button = $form_state['clicked_button'];
723
  $parents = $button['#parents'];
724
  array_pop($parents);
725
  $rid = array_pop($parents);
726

    
727
  // Recurse through the form values until we find the Webform conditional rules
728
  // or actions. Save the conditional prior to descending to rules/actions.
729
  $parent_values = &$form_state['values'];
730
  $input_values = &$form_state['input'];
731
  foreach ($parents as $key) {
732
    if (array_key_exists($key, $parent_values)) {
733
      $conditional = $parent_values;
734
      $parent_values = &$parent_values[$key];
735
    }
736
    if (array_key_exists($key, $input_values)) {
737
      $input_values = &$input_values[$key];
738
    }
739
  }
740

    
741
  // Split the list of rules/actions in this conditional and inject into the
742
  // right spot.
743
  $rids = array_keys($parent_values);
744
  $offset = array_search($rid, $rids);
745
  $default_rule = isset($button['#subconditional'])
746
                    ? array(
747
                      'source' => NULL,
748
                      'source_type' => 'component',
749
                      'operator' => NULL,
750
                      'value' => NULL,
751
                    )
752
                    : array(
753
                      'target_type' => 'component',
754
                      'target' => NULL,
755
                      'invert' => NULL,
756
                      'action' => NULL,
757
                      'argument' => NULL,
758
                    );
759

    
760
  if (empty($button['#subconditional'])) {
761
    $new[0] = (isset($parent_values[$rid]['source_type']) && $parent_values[$rid]['source_type'] == 'component') ? $parent_values[$rid] : $default_rule;
762
  }
763
  else {
764
    // The default andor operator is opposite of current subconditional's
765
    // operatior.
766
    $parent_rid = _webform_conditional_find_start($parent_values, $rid, -1);
767
    $current_op = $parent_rid < 0 ? $conditional['andor'] : $parent_values[$parent_rid]['operator'];
768
    $current_op = $current_op == 'and' ? 'or' : 'and';
769
    $new = array(
770
      array('source_type' => 'conditional_start', 'operator' => $current_op) + $default_rule,
771
      $default_rule,
772
      $default_rule,
773
      array('source_type' => 'conditional_end') + $default_rule,
774
    );
775
  }
776

    
777
  // Update both $form_state['values'] and ['input] so that FAPI can merge
778
  // input values from the POST into the new form.
779
  $parent_values = array_merge(array_slice($parent_values, 0, $offset + 1), $new, array_slice($parent_values, $offset + 1));
780
  $input_values = array_merge(array_slice($input_values, 0, $offset + 1), $new, array_slice($input_values, $offset + 1));
781
  $form_state['rebuild'] = TRUE;
782
}
783

    
784
/**
785
 * Submit handler for webform_conditional elements to remove a rule or action.
786
 */
787
function webform_conditional_element_remove($form, &$form_state) {
788
  $button = $form_state['clicked_button'];
789
  $parents = $button['#parents'];
790
  $action = array_pop($parents);
791
  $rid = array_pop($parents);
792

    
793
  // Recurse through the form values until we find the root Webform conditional.
794
  $parent_values = &$form_state['values'];
795
  foreach ($parents as $key) {
796
    if (array_key_exists($key, $parent_values)) {
797
      $parent_values = &$parent_values[$key];
798
    }
799
  }
800
  switch ($parent_values[$rid]['source_type']) {
801
    case 'conditional_start':
802
      unset($parent_values[_webform_conditional_find_end($parent_values, $rid)]);
803
      break;
804

    
805
    case 'conditional_end':
806
      unset($parent_values[_webform_conditional_find_start($parent_values, $rid)]);
807
      break;
808
  }
809
  // Remove this rule or action from the list of conditionals.
810
  unset($parent_values[$rid]);
811

    
812
  $form_state['rebuild'] = TRUE;
813
}
814

    
815
/**
816
 * Helper. Delete any subconditionals which contain no rules.
817
 *
818
 * @param array $conditional
819
 *   Conditional array containing the rules.
820
 *
821
 * @return array
822
 *   Array of deleted subconditionals. Empty array if none were deleted.
823
 */
824
function webform_delete_empty_subconditionals(array &$conditional) {
825
  $deleted = array();
826
  do {
827
    $empty_deleted = FALSE;
828
    $open_rid = NULL;
829
    foreach ($conditional['rules'] as $rid => $rule) {
830
      switch ($rule['source_type']) {
831
        case 'conditional_start':
832
          $open_rid = $rid;
833
          break;
834

    
835
        case 'conditional_end':
836
          if ($open_rid) {
837
            // A conditional_start rule was immediately followed by a
838
            // conditional_end rule. Delete them both. Repeat the check in case
839
            // the parent is now empty.
840
            $deleted[$open_rid] = $open_rid;
841
            $deleted[$rid] = $rid;
842
            unset($conditional['rules'][$open_rid], $conditional['rules'][$rid]);
843
            $open_rid = NULL;
844
            $empty_deleted = TRUE;
845
          }
846
          break;
847

    
848
        default:
849
          $open_rid = NULL;
850
      }
851
    }
852
  } while ($empty_deleted);
853
  return $deleted;
854
}
855

    
856
/**
857
 * AJAX callback to render out adding a new condition.
858
 */
859
function webform_conditional_element_ajax($form, $form_state) {
860
  $button = $form_state['clicked_button'];
861
  $parents = $button['#parents'];
862

    
863
  // Trim down the parents to go back up to the level of this elements wrapper.
864
  // The button name (add/remove).
865
  array_pop($parents);
866
  // The rule ID.
867
  array_pop($parents);
868
  // The "rules" grouping.
869
  array_pop($parents);
870

    
871
  $element = $form;
872
  foreach ($parents as $key) {
873
    if (!isset($element[$key])) {
874
      // The entire conditional has been removed.
875
      return '';
876
    }
877
    $element = $element[$key];
878
  }
879

    
880
  return drupal_render($element['conditional']);
881
}
882

    
883
/**
884
 * Theme the form for a conditional action.
885
 */
886
function theme_webform_conditional($variables) {
887
  $element = $variables['element'];
888

    
889
  $output = '';
890
  $output .= '<div class="webform-conditional">';
891
  $output .= '<span class="webform-conditional-if">' . t('If') . '</span>';
892

    
893
  foreach (element_children($element['rules']) as $rid) {
894
    $rule = &$element['rules'][$rid];
895
    switch ($rule['source_type']['#value']) {
896
      case 'conditional_start':
897
        $source_phrase = '<div class="webform-subconditional">' . t('(') . '</div>';
898
        break;
899

    
900
      case 'conditional_end':
901
        $source_phrase = '<div class="webform-subconditional">' . t(')') . '</div>';
902
        break;
903

    
904
      default:
905
        // Hide labels.
906
        $rule['source']['#title_display'] = 'invisible';
907
        $rule['operator']['#title_display'] = 'invisible';
908
        $rule['value']['#title_display'] = 'invisible';
909

    
910
        $source = '<div class="webform-conditional-source">' . drupal_render($rule['source']) . '</div>';
911
        $operator = '<div class="webform-conditional-operator">' . drupal_render($rule['operator']) . '</div>';
912
        $value = '<div class="webform-conditional-value">' . drupal_render($rule['value']) . '</div>';
913

    
914
        $source_phrase = t('!source !operator !value', array(
915
          '!source' => $source,
916
          '!operator' => $operator,
917
          '!value' => $value,
918
        ));
919
    }
920

    
921
    $output .= '<div class="webform-conditional-rule">';
922
    // Can't use theme('indentation') here because it causes the draghandle to
923
    // be located after the last indentation div.
924
    $output .= str_repeat('<div class="webform-indentation">&nbsp;</div>', $rule['#level']);
925
    $output .= drupal_render($rule['source_type']);
926
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
927
    $output .= $source_phrase;
928
    $output .= '</div>';
929

    
930
    if (isset($rule['andor'])) {
931
      $rule['andor']['#title_display'] = 'invisible';
932
      $output .= '<div class="webform-conditional-andor webform-container-inline">';
933
      $output .= drupal_render($rule['andor']);
934
      $output .= '</div>';
935
    }
936

    
937
    if (isset($rule['add']) || isset($rule['remove'])) {
938
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
939
      $output .= drupal_render($rule['add_subconditional']);
940
      $output .= drupal_render($rule['add']);
941
      $output .= drupal_render($rule['remove']);
942
      $output .= '</span>';
943
    }
944

    
945
    $output .= '</div>';
946
  }
947

    
948
  // Hide labels.
949
  foreach (element_children($element['actions']) as $aid) {
950
    // Hide labels.
951
    $element['actions'][$aid]['target']['#title_display'] = 'invisible';
952
    $element['actions'][$aid]['invert']['#title_display'] = 'invisible';
953
    $element['actions'][$aid]['action']['#title_display'] = 'invisible';
954
    $element['actions'][$aid]['argument']['#title_display'] = 'invisible';
955

    
956
    $target = '<div class="webform-conditional-target">' . drupal_render($element['actions'][$aid]['target']) . '</div>';
957
    $invert = '<div class="webform-conditional-invert">' . drupal_render($element['actions'][$aid]['invert']) . '</div>';
958
    $action = '<div class="webform-conditional-action">' . drupal_render($element['actions'][$aid]['action']) . '</div>';
959
    $argument = '<div class="webform-conditional-argument">' . drupal_render($element['actions'][$aid]['argument']) . '</div>';
960

    
961
    $target_phrase = t('then !target !invert !action !argument', array(
962
      '!target' => $target,
963
      '!invert' => $invert,
964
      '!action' => $action,
965
      '!argument' => $argument,
966
    ));
967

    
968
    $output .= '<div class="webform-conditional-action">';
969
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
970
    $output .= $target_phrase;
971
    $output .= '</div>';
972

    
973
    if (isset($element['actions'][$aid]['add']) || isset($element['actions'][$aid]['remove'])) {
974
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
975
      $output .= drupal_render($element['actions'][$aid]['add']);
976
      $output .= drupal_render($element['actions'][$aid]['remove']);
977
      $output .= '</span>';
978
    }
979

    
980
    $output .= '</div>';
981
  }
982

    
983
  $output .= '</div>';
984

    
985
  return $output;
986
}
987

    
988
/**
989
 * Return a list of all Webform conditional operators.
990
 */
991
function webform_conditional_operators() {
992
  static $operators;
993

    
994
  if (!isset($operators)) {
995
    $operators = module_invoke_all('webform_conditional_operator_info');
996
    drupal_alter('webform_conditional_operators', $operators);
997
  }
998

    
999
  return $operators;
1000
}
1001

    
1002
/**
1003
 * Return a nested list of all available operators, suitable for a select list.
1004
 */
1005
function webform_conditional_operators_list() {
1006
  $options = array();
1007
  $operators = webform_conditional_operators();
1008

    
1009
  foreach ($operators as $data_type => $type_operators) {
1010
    $options[$data_type] = array();
1011
    foreach ($type_operators as $operator => $operator_info) {
1012
      $options[$data_type][$operator] = $operator_info['label'];
1013
    }
1014
  }
1015

    
1016
  return $options;
1017
}
1018

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

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

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

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

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

    
1262
  return $operators;
1263
}
1264

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

    
1297
/**
1298
 * Form callback for date conditional fields.
1299
 */
1300
function webform_conditional_form_date($node) {
1301
  static $count = 0;
1302
  $element = array(
1303
    '#title' => NULL,
1304
    '#title_display' => 'invisible',
1305
    '#size' => 24,
1306
    '#attributes' => array('placeholder' => t('@format or valid date', array('@format' => webform_date_format('short')))),
1307
    '#type' => 'textfield',
1308
    '#name' => 'webform-conditional-date-' . $count++,
1309
  );
1310
  return drupal_render($element);
1311
}
1312

    
1313
/**
1314
 * Form callback for time conditional fields.
1315
 */
1316
function webform_conditional_form_time($node) {
1317
  static $count = 0;
1318
  $element = array(
1319
    '#title' => NULL,
1320
    '#title_display' => 'invisible',
1321
    '#size' => 24,
1322
    '#attributes' => array('placeholder' => t('HH:MMam or valid time')),
1323
    '#type' => 'textfield',
1324
    '#name' => 'webform-conditional-time-' . $count++,
1325
  );
1326
  return drupal_render($element);
1327
}
1328

    
1329
/**
1330
 * Load a conditional setting from the database.
1331
 */
1332
function webform_conditional_load($rgid, $nid) {
1333
  $node = node_load($nid);
1334

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

    
1337
  return $conditional;
1338
}
1339

    
1340
/**
1341
 * Insert a conditional rule group into the database.
1342
 */
1343
function webform_conditional_insert($conditional) {
1344
  $transaction = db_transaction();
1345
  drupal_write_record('webform_conditional', $conditional);
1346
  foreach ($conditional['rules'] as $rid => $rule) {
1347
    $rule['nid'] = $conditional['nid'];
1348
    $rule['rgid'] = $conditional['rgid'];
1349
    $rule['rid'] = $rid;
1350
    drupal_write_record('webform_conditional_rules', $rule);
1351
  }
1352
  foreach ($conditional['actions'] as $aid => $action) {
1353
    $action['nid'] = $conditional['nid'];
1354
    $action['rgid'] = $conditional['rgid'];
1355
    $action['aid'] = $aid;
1356
    drupal_write_record('webform_conditional_actions', $action);
1357
  }
1358
}
1359

    
1360
/**
1361
 * Update a conditional setting in the database.
1362
 */
1363
function webform_conditional_update($node, $conditional) {
1364
  $transaction = db_transaction();
1365
  webform_conditional_delete($node, $conditional);
1366
  webform_conditional_insert($conditional);
1367
}
1368

    
1369
/**
1370
 * Delete a conditional rule group.
1371
 */
1372
function webform_conditional_delete($node, $conditional) {
1373
  $transaction = db_transaction();
1374
  db_delete('webform_conditional')
1375
    ->condition('nid', $node->nid)
1376
    ->condition('rgid', $conditional['rgid'])
1377
    ->execute();
1378
  db_delete('webform_conditional_rules')
1379
    ->condition('nid', $node->nid)
1380
    ->condition('rgid', $conditional['rgid'])
1381
    ->execute();
1382
  db_delete('webform_conditional_actions')
1383
    ->condition('nid', $node->nid)
1384
    ->condition('rgid', $conditional['rgid'])
1385
    ->execute();
1386
}
1387

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

    
1447
          // If this source has a value set, add that as a setting. NULL or
1448
          // array(NULL) should be sent as an empty array to simplify the
1449
          // jQuery.
1450
          if (isset($submission_data[$source_component['cid']])) {
1451
            $source_value = $submission_data[$source_component['cid']];
1452
            $source_value = is_array($source_value) ? $source_value : array($source_value);
1453
            $settings['values'][$source_id] = $source_value === array(NULL) ? array() : $source_value;
1454
          }
1455

    
1456
          $conditional_type = webform_component_property($source_component['type'], 'conditional_type');
1457
          $operator_info = $operators[$conditional_type][$rule['operator']];
1458
          $rule_settings = array(
1459
            'source_type' => $rule['source_type'],
1460
            'source' => $source_id,
1461
            'value' => $rule['value'],
1462
            'callback' => $operator_info['js comparison callback'],
1463
          );
1464
          if (isset($operator_info['comparison prepare js'])) {
1465
            $callback = $operator_info['comparison prepare js'];
1466
            $rule_settings['value'] = $callback($rule['value']);
1467
          }
1468
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = $rule_settings;
1469
          $settings['sourceMap'][$source_id][$rgid_key] = $rgid_key;
1470
          break;
1471

    
1472
        case 'conditional_start':
1473
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1474
            'source_type' => $rule['source_type'],
1475
            'andor' => $rule['operator'],
1476
          );
1477
          break;
1478

    
1479
        case 'conditional_end':
1480
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1481
            'source_type' => $rule['source_type'],
1482
          );
1483
          break;
1484
      }
1485
    }
1486
  }
1487

    
1488
  return $settings;
1489
}
1490

    
1491
/**
1492
 * Determine whether a component type is capable of a given conditional action.
1493
 */
1494
function webform_conditional_action_able($component_type, $action) {
1495
  switch ($action) {
1496
    case 'show':
1497
      return TRUE;
1498

    
1499
    case 'require':
1500
      return webform_component_feature($component_type, 'required');
1501

    
1502
    default:
1503
      return webform_component_feature($component_type, "conditional_action_$action");
1504
  }
1505
}
1506

    
1507
/**
1508
 * Prepare a conditional value for adding as a JavaScript setting.
1509
 */
1510
function webform_conditional_prepare_date_js($rule_value) {
1511
  // Convert the time/date string to a UTC timestamp for comparison. Note that
1512
  // this means comparisons against immediate times (such as "now") may be
1513
  // slightly stale by the time the comparison executes. Timestamps are in
1514
  // milliseconds, as to match JavaScript's Date.toString() method.
1515
  $date = webform_strtodate('c', $rule_value, 'UTC');
1516
  return webform_strtotime($date);
1517
}
1518

    
1519
/**
1520
 * Prepare a conditional value for adding as a JavaScript setting.
1521
 */
1522
function webform_conditional_prepare_time_js($rule_value) {
1523
  $date = webform_conditional_prepare_date_js($rule_value);
1524
  $today = webform_strtodate('c', 'today', 'UTC');
1525
  $today = webform_strtotime($today);
1526
  return $date - $today;
1527
}
1528

    
1529
/**
1530
 * Conditional callback for string comparisons.
1531
 */
1532
function webform_conditional_operator_string_equal($input_values, $rule_value) {
1533
  foreach ($input_values as $value) {
1534
    // Checkbox values come in as 0 integers for unchecked boxes.
1535
    $value = ($value === 0) ? '' : $value;
1536
    if (strcasecmp($value, $rule_value) === 0) {
1537
      return TRUE;
1538
    }
1539
  }
1540
  return FALSE;
1541
}
1542

    
1543
/**
1544
 * Conditional callback for string comparisons.
1545
 */
1546
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
1547
  return !webform_conditional_operator_string_equal($input_values, $rule_value);
1548
}
1549

    
1550
/**
1551
 * Conditional callback for string comparisons.
1552
 */
1553
function webform_conditional_operator_string_contains($input_values, $rule_value) {
1554
  foreach ($input_values as $value) {
1555
    if (stripos($value, $rule_value) !== FALSE) {
1556
      return TRUE;
1557
    }
1558
  }
1559
  return FALSE;
1560
}
1561

    
1562
/**
1563
 * Conditional callback for string comparisons.
1564
 */
1565
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
1566
  return !webform_conditional_operator_string_contains($input_values, $rule_value);
1567
}
1568

    
1569
/**
1570
 * Conditional callback for string comparisons.
1571
 */
1572
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
1573
  foreach ($input_values as $value) {
1574
    if (stripos($value, $rule_value) === 0) {
1575
      return TRUE;
1576
    }
1577
  }
1578
  return FALSE;
1579
}
1580

    
1581
/**
1582
 * Conditional callback for string comparisons.
1583
 */
1584
function webform_conditional_operator_string_ends_with($input_values, $rule_value) {
1585
  foreach ($input_values as $value) {
1586
    if (strripos($value, $rule_value) === strlen($value) - strlen($rule_value)) {
1587
      return TRUE;
1588
    }
1589
  }
1590
  return FALSE;
1591
}
1592

    
1593
/**
1594
 * Conditional callback for checking for empty fields.
1595
 */
1596
function webform_conditional_operator_string_empty($input_values, $rule_value) {
1597
  $empty = TRUE;
1598
  foreach ($input_values as $value) {
1599
    if ($value !== '' && $value !== NULL && $value !== 0) {
1600
      $empty = FALSE;
1601
      break;
1602
    }
1603
  }
1604
  return $empty;
1605
}
1606

    
1607
/**
1608
 * Conditional callback for checking for empty fields.
1609
 */
1610
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
1611
  return !webform_conditional_operator_string_empty($input_values, $rule_value);
1612
}
1613

    
1614
/**
1615
 * Conditional callback for select comparisons.
1616
 */
1617
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
1618
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
1619
}
1620

    
1621
/**
1622
 * Conditional callback for select comparisons.
1623
 */
1624
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
1625
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1626
  return $comparison < 0 || $comparison === 0;
1627
}
1628

    
1629
/**
1630
 * Conditional callback for select comparisons.
1631
 */
1632
function webform_conditional_operator_select_greater_than($input_values, $rule_value, $component) {
1633
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) > 0;
1634
}
1635

    
1636
/**
1637
 * Conditional callback for select comparisons.
1638
 */
1639
function webform_conditional_operator_select_greater_than_equal($input_values, $rule_value, $component) {
1640
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1641
  return $comparison > 0 || $comparison === 0;
1642
}
1643

    
1644
/**
1645
 * Conditional callback for numeric comparisons.
1646
 */
1647
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
1648
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
1649
}
1650

    
1651
/**
1652
 * Conditional callback for numeric comparisons.
1653
 */
1654
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
1655
  return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
1656
}
1657

    
1658
/**
1659
 * Conditional callback for numeric comparisons.
1660
 */
1661
function webform_conditional_operator_numeric_less_than($input_values, $rule_value) {
1662
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) < 0;
1663
}
1664

    
1665
/**
1666
 * Conditional callback for numeric comparisons.
1667
 */
1668
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
1669
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1670
  return $comparison < 0 || $comparison === 0;
1671
}
1672

    
1673
/**
1674
 * Conditional callback for numeric comparisons.
1675
 */
1676
function webform_conditional_operator_numeric_greater_than($input_values, $rule_value) {
1677
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) > 0;
1678
}
1679

    
1680
/**
1681
 * Conditional callback for numeric comparisons.
1682
 */
1683
function webform_conditional_operator_numeric_greater_than_equal($input_values, $rule_value) {
1684
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1685
  return $comparison > 0 || $comparison === 0;
1686
}
1687

    
1688
/**
1689
 * Conditional callback for date and time comparisons.
1690
 */
1691
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
1692
  $input_values = webform_conditional_value_datetime($input_values);
1693
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
1694
}
1695

    
1696
/**
1697
 * Conditional callback for date and time comparisons.
1698
 */
1699
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
1700
  return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
1701
}
1702

    
1703
/**
1704
 * Conditional callback for date and time comparisons.
1705
 */
1706
function webform_conditional_operator_datetime_after($input_values, $rule_value) {
1707
  $input_values = webform_conditional_value_datetime($input_values);
1708
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) > webform_strtotime($rule_value);
1709
}
1710

    
1711
/**
1712
 * Conditional callback for date and time comparisons.
1713
 */
1714
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
1715
  return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
1716
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1717
}
1718

    
1719
/**
1720
 * Conditional callback for date and time comparisons.
1721
 */
1722
function webform_conditional_operator_datetime_before($input_values, $rule_value) {
1723
  $input_values = webform_conditional_value_datetime($input_values);
1724
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) < webform_strtotime($rule_value);
1725
}
1726

    
1727
/**
1728
 * Conditional callback for date and time comparisons.
1729
 */
1730
function webform_conditional_operator_datetime_before_equal($input_values, $rule_value) {
1731
  return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
1732
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1733
}
1734

    
1735
/**
1736
 * Utility function to convert incoming time and dates into strings.
1737
 */
1738
function webform_conditional_value_datetime($input_values) {
1739
  // Convert times into a string.
1740
  $input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
1741
  // Convert dates into a string.
1742
  $input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
1743
  return $input_values;
1744
}
1745

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