Project

General

Profile

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

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

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 any errors,
38
  // but only for actual form submissions and not for ajax-related form builds, such as
39
  // 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 a previous submission was truncated.
147
  // The estimate will be accurate because the form elements for this page are well known. Ajax use of this
148
  // page will not generate user-visible errors, so a preflight may be the only indication to the user that
149
  // the page is too long.
150
  webform_input_vars_check($form, $form_state, 'conditionals', '');
151
  return $form;
152
}
153

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

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

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

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

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

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

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

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

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

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

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

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

    
312
  return $output;
313
}
314

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

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

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

    
337
  return $output;
338
}
339

    
340
/**
341
 * Form API #process function to expand a webform conditional element.
342
 */
343
function _webform_conditional_expand($element) {
344
  $element['#tree'] = TRUE;
345
  $element['#default_value'] += array(
346
    'andor' => 'and',
347
  );
348

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

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

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

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

    
408
      case 'component':
409
        $element['rules'][$rid] = _webform_conditional_rule_expand($element, $rid, $conditional, $level, $andor_stack[$level]);
410
        break;
411

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

    
418
  // Remove the last and/or.
419
  unset($element['rules'][$rid]['andor']);
420

    
421
  foreach ($element['#default_value']['actions'] as $aid => $action) {
422
    $element['actions'][$aid] = _webform_conditional_action_expand($element, $aid, $action);
423
  }
424

    
425
  return $element;
426
}
427

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
810
  $form_state['rebuild'] = TRUE;
811
}
812

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

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

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

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

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

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

    
878
  return drupal_render($element['conditional']);
879
}
880

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

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

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

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

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

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

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

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

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

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

    
943
    $output .= '</div>';
944
  }
945

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

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

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

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

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

    
978
    $output .= '</div>';
979
  }
980

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

    
983
  return $output;
984
}
985

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

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

    
997
  return $operators;
998
}
999

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

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

    
1014
  return $options;
1015
}
1016

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

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

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

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

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

    
1260
  return $operators;
1261
}
1262

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

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

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

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

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

    
1335
  return $conditional;
1336
}
1337

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

    
1357
/**
1358
 * Update a conditional setting in the database.
1359
 */
1360
function webform_conditional_update($node, $conditional) {
1361
  webform_conditional_delete($node, $conditional);
1362
  webform_conditional_insert($conditional);
1363
}
1364

    
1365
/**
1366
 * Delete a conditional rule group.
1367
 */
1368
function webform_conditional_delete($node, $conditional) {
1369
  db_delete('webform_conditional')
1370
    ->condition('nid', $node->nid)
1371
    ->condition('rgid', $conditional['rgid'])
1372
    ->execute();
1373
  db_delete('webform_conditional_rules')
1374
    ->condition('nid', $node->nid)
1375
    ->condition('rgid', $conditional['rgid'])
1376
    ->execute();
1377
  db_delete('webform_conditional_actions')
1378
    ->condition('nid', $node->nid)
1379
    ->condition('rgid', $conditional['rgid'])
1380
    ->execute();
1381
}
1382

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

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

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

    
1466
        case 'conditional_start':
1467
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1468
            'source_type' => $rule['source_type'],
1469
            'andor' => $rule['operator'],
1470
          );
1471
          break;
1472

    
1473
        case 'conditional_end':
1474
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1475
            'source_type' => $rule['source_type'],
1476
          );
1477
          break;
1478
      }
1479
    }
1480
  }
1481

    
1482
  return $settings;
1483
}
1484

    
1485
/**
1486
 * Determine whether a component type is capable of a given conditional action.
1487
 */
1488
function webform_conditional_action_able($component_type, $action) {
1489
  switch ($action) {
1490
    case 'show':
1491
      return TRUE;
1492

    
1493
    case 'require':
1494
      return webform_component_feature($component_type, 'required');
1495

    
1496
    default:
1497
      return webform_component_feature($component_type, "conditional_action_$action");
1498
  }
1499
}
1500

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

    
1513
/**
1514
 * Prepare a conditional value for adding as a JavaScript setting.
1515
 */
1516
function webform_conditional_prepare_time_js($rule_value) {
1517
  $date = webform_conditional_prepare_date_js($rule_value);
1518
  $today = webform_strtodate('c', 'today', 'UTC');
1519
  $today = webform_strtotime($today);
1520
  return $date - $today;
1521
}
1522

    
1523
/**
1524
 * Conditional callback for string comparisons.
1525
 */
1526
function webform_conditional_operator_string_equal($input_values, $rule_value) {
1527
  foreach ($input_values as $value) {
1528
    // Checkbox values come in as 0 integers for unchecked boxes.
1529
    $value = ($value === 0) ? '' : $value;
1530
    if (strcasecmp($value, $rule_value) === 0) {
1531
      return TRUE;
1532
    }
1533
  }
1534
  return FALSE;
1535
}
1536

    
1537
/**
1538
 * Conditional callback for string comparisons.
1539
 */
1540
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
1541
  return !webform_conditional_operator_string_equal($input_values, $rule_value);
1542
}
1543

    
1544
/**
1545
 * Conditional callback for string comparisons.
1546
 */
1547
function webform_conditional_operator_string_contains($input_values, $rule_value) {
1548
  foreach ($input_values as $value) {
1549
    if (stripos($value, $rule_value) !== FALSE) {
1550
      return TRUE;
1551
    }
1552
  }
1553
  return FALSE;
1554
}
1555

    
1556
/**
1557
 * Conditional callback for string comparisons.
1558
 */
1559
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
1560
  return !webform_conditional_operator_string_contains($input_values, $rule_value);
1561
}
1562

    
1563
/**
1564
 * Conditional callback for string comparisons.
1565
 */
1566
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
1567
  foreach ($input_values as $value) {
1568
    if (stripos($value, $rule_value) === 0) {
1569
      return TRUE;
1570
    }
1571
  }
1572
  return FALSE;
1573
}
1574

    
1575
/**
1576
 * Conditional callback for string comparisons.
1577
 */
1578
function webform_conditional_operator_string_ends_with($input_values, $rule_value) {
1579
  foreach ($input_values as $value) {
1580
    if (strripos($value, $rule_value) === strlen($value) - strlen($rule_value)) {
1581
      return TRUE;
1582
    }
1583
  }
1584
  return FALSE;
1585
}
1586

    
1587
/**
1588
 * Conditional callback for checking for empty fields.
1589
 */
1590
function webform_conditional_operator_string_empty($input_values, $rule_value) {
1591
  $empty = TRUE;
1592
  foreach ($input_values as $value) {
1593
    if ($value !== '' && $value !== NULL && $value !== 0) {
1594
      $empty = FALSE;
1595
      break;
1596
    }
1597
  }
1598
  return $empty;
1599
}
1600

    
1601
/**
1602
 * Conditional callback for checking for empty fields.
1603
 */
1604
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
1605
  return !webform_conditional_operator_string_empty($input_values, $rule_value);
1606
}
1607

    
1608
/**
1609
 * Conditional callback for select comparisons.
1610
 */
1611
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
1612
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
1613
}
1614

    
1615
/**
1616
 * Conditional callback for select comparisons.
1617
 */
1618
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
1619
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1620
  return $comparison < 0 || $comparison === 0;
1621
}
1622

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

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

    
1638
/**
1639
 * Conditional callback for numeric comparisons.
1640
 */
1641
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
1642
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
1643
}
1644

    
1645
/**
1646
 * Conditional callback for numeric comparisons.
1647
 */
1648
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
1649
  return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
1650
}
1651

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

    
1659
/**
1660
 * Conditional callback for numeric comparisons.
1661
 */
1662
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
1663
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1664
  return $comparison < 0 || $comparison === 0;
1665
}
1666

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

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

    
1682
/**
1683
 * Conditional callback for date and time comparisons.
1684
 */
1685
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
1686
  $input_values = webform_conditional_value_datetime($input_values);
1687
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
1688
}
1689

    
1690
/**
1691
 * Conditional callback for date and time comparisons.
1692
 */
1693
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
1694
  return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
1695
}
1696

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

    
1705
/**
1706
 * Conditional callback for date and time comparisons.
1707
 */
1708
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
1709
  return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
1710
    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_before($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_before_equal($input_values, $rule_value) {
1725
  return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
1726
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1727
}
1728

    
1729
/**
1730
 * Utility function to convert incoming time and dates into strings.
1731
 */
1732
function webform_conditional_value_datetime($input_values) {
1733
  // Convert times into a string.
1734
  $input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
1735
  // Convert dates into a string.
1736
  $input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
1737
  return $input_values;
1738
}
1739

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