Projet

Général

Profil

Paste
Télécharger (63,7 ko) Statistiques
| Branche: | Révision:

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

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
  unset($conditional); // Drop PHP reference.
35

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

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

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

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

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

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

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

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

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

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

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

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

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

    
239
  // Form validation will not rebuild the form, so we need to ensure
240
  // necessary JavaScript will still exist.
241
  _webform_conditional_expand_value_forms($node);
242
}
243

    
244
/**
245
 * Submit handler for webform_conditionals_form().
246
 */
247
function webform_conditionals_form_submit($form, &$form_state) {
248
  $node = $form['#node'];
249

    
250
  // Remove the new conditional placeholder.
251
  unset($form_state['values']['conditionals']['new']);
252

    
253
  $node->webform['conditionals'] = $form_state['values']['conditionals'];
254
  node_save($node);
255
  drupal_set_message(t('Conditionals for %title saved.', array('%title' => $node->title)));
256
}
257

    
258
/**
259
 * AJAX callback to render out adding a new condition.
260
 */
261
function webform_conditionals_ajax($form, $form_state) {
262
  $rgids = element_children($form['conditionals']);
263
  $new_rgid = max($rgids);
264
  $form['conditionals'][$new_rgid]['#ajax_added'] = TRUE;
265

    
266
  $commands = array('#type' => 'ajax');
267
  $commands['#commands'][] = ajax_command_before('.webform-conditional-new-row', drupal_render($form['conditionals'][$new_rgid]));
268
  $commands['#commands'][] = ajax_command_restripe('#webform-conditionals-table');
269
  return $commands;
270
}
271

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

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

    
306
  return $output;
307
}
308

    
309
/**
310
 * Theme an individual conditional row of webform_conditionals_form().
311
 */
312
function theme_webform_conditional_group_row($variables) {
313
  $element = $variables['element'];
314

    
315
  $element['weight']['#attributes']['class'] = array('webform-conditional-weight');
316
  $weight = drupal_render($element['weight']);
317
  $classes = array('draggable');
318
  if (!empty($element['#even_odd'])) {
319
    $classes[] = $element['#even_odd'];
320
  }
321
  if (!empty($element['#ajax_added'])) {
322
    $classes[] = 'ajax-new-content';
323
  }
324

    
325
  $output = '';
326
  $output .= '<tr class="' . implode(' ', $classes) . '">';
327
  $output .= '<td>' . drupal_render_children($element) . '</td>';
328
  $output .= '<td>' . $weight . '</td>';
329
  $output .= '</tr>';
330

    
331
  return $output;
332
}
333

    
334
/**
335
 * Form API #process function to expand a webform conditional element.
336
 */
337
function _webform_conditional_expand($element) {
338
  $element['#tree'] = TRUE;
339
  $element['#default_value'] += array(
340
    'andor' => 'and',
341
  );
342

    
343
  $wrapper_id = drupal_clean_css_identifier(implode('-', $element['#parents'])) . '-ajax';
344
  $element['#prefix'] = '<div id="' . $wrapper_id . '">';
345
  $element['#suffix'] = '</div>';
346
  $element['#wrapper_id'] = $wrapper_id;
347

    
348
  // Note: When rules or actions are added, the new rules are inserted into
349
  // $form_state['values']. So that FAPI can merge data from the post,
350
  // $form_state['input'] must be adjusted to. To make this easier, hidden
351
  // fields are added to the conditional_start and _end rules to ensure that
352
  // each rule is represented in the POST.
353

    
354
  $level = 0;
355
  $andor_stack[0] = array(
356
    'value' => $element['#default_value']['andor'],
357
    'parents' => array_merge($element['#parents'], array('andor')),
358
    'rid' => 0,
359
    'first' => TRUE,
360
  );
361

    
362
  $last_rid = -1;
363
  foreach ($element['#default_value']['rules'] as $rid => $conditional) {
364
    switch ($conditional['source_type']) {
365
      case 'conditional_start':
366
        $element['rules'][$rid] = array(
367
          '#level' => $level,
368
          'source_type' => array(
369
            '#type' => 'hidden',
370
            '#value' => 'conditional_start',
371
          ),
372
          // The andor operator is located in the first child, which is
373
          // guaranteed to exist. Therefore, don't add a 'value' element here.
374
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
375
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
376
          'remove' => _webform_conditional_remove_expand($element, $rid),
377
        );
378
        $andor_stack[++$level] = array(
379
          'value' => $conditional['operator'],
380
          'parents' => array_merge($element['#parents'], array('rules', $rid, 'operator')),
381
          'rid' => $rid,
382
          'first' => TRUE,
383
        );
384
        break;
385
      case 'conditional_end':
386
        --$level;
387
        $element['rules'][$rid] = array(
388
          '#level' => $level,
389
          'source_type' => array(
390
            '#type' => 'hidden',
391
            '#value' => 'conditional_end',
392
          ),
393
          'add_subconditional' => _webform_conditional_add_expand($element, $rid, TRUE),
394
          'add' => _webform_conditional_add_expand($element, $rid, FALSE),
395
          'remove' => _webform_conditional_remove_expand($element, $rid),
396
          'andor' => _webform_conditional_andor_expand($andor_stack[$level]),
397
        );
398
        // Remove the last nested and/or.
399
        unset($element['rules'][$last_rid]['andor']);
400
        break;
401
      case 'component':
402
        $element['rules'][$rid] = _webform_conditional_rule_expand($element, $rid, $conditional, $level, $andor_stack[$level]);
403
        break;
404
      default:
405
        drupal_set_message(t('Unexpected conditional rule source type found (rule id @rid). Contact the administrator.', array('@rid' => $rid)), 'error');
406
        // break;
407
    }
408
    $last_rid = $rid;
409
  }
410

    
411
  // Remove the last and/or.
412
  unset($element['rules'][$rid]['andor']);
413

    
414
  foreach ($element['#default_value']['actions'] as $aid => $action) {
415
    $element['actions'][$aid] = _webform_conditional_action_expand($element, $aid, $action);
416
  }
417

    
418
  return $element;
419
}
420

    
421

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

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

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

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

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

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

    
638
  foreach ($node->webform['components'] as $cid => $component) {
639
    if (webform_component_feature($component['type'], 'conditional')) {
640
      $data['sources'][$cid]['data_type'] = webform_component_property($component['type'], 'conditional_type');
641
    }
642
  }
643

    
644
  drupal_add_js(array('webform' => array('conditionalValues' => $data)), 'setting');
645
}
646

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

    
682
/**
683
 * Helper. Find the matching start or end of a given subconditional.
684
 *
685
 * @see _webform_conditional_find_end()
686
 */
687
function _webform_conditional_find_start($rules, $origin_rid, $target_delta_level = 0) {
688
  $rids = array_keys($rules);
689
  $offset = array_search($origin_rid, $rids);
690
  $delta_level = 0;
691
  foreach (array_reverse(array_slice($rules, 0, $offset + 1, TRUE), TRUE) as $rid => $conditional) {
692
    switch ($conditional['source_type']) {
693
      case 'conditional_end':
694
        $delta_level++;
695
        break;
696
      case 'conditional_start':
697
        $delta_level--;
698
        break;
699
    }
700
    if ($delta_level == $target_delta_level) {
701
      return $rid;
702
    }
703
  }
704
  // Mis-matched conditional_start / _end. Return -1.
705
  return -1;
706
}
707

    
708
/**
709
 * Submit handler for webform_conditional elements to add a new rule or action.
710
 */
711
function webform_conditional_element_add($form, &$form_state) {
712
  $button = $form_state['clicked_button'];
713
  $parents = $button['#parents'];
714
  $action = array_pop($parents);
715
  $rid = array_pop($parents);
716

    
717
  // Recurse through the form values until we find the Webform conditional rules
718
  // or actions. Save the conditional prior to descending to rules/actions.
719
  $parent_values = &$form_state['values'];
720
  $input_values = &$form_state['input'];
721
  foreach ($parents as $key) {
722
    if (array_key_exists($key, $parent_values)) {
723
      $conditional = $parent_values;
724
      $parent_values = &$parent_values[$key];
725
    }
726
    if (array_key_exists($key, $input_values)) {
727
      $input_values = &$input_values[$key];
728
    }
729
  }
730

    
731
  // Split the list of rules/actions in this conditional and inject into the
732
  // right spot.
733
  $rids = array_keys($parent_values);
734
  $offset = array_search($rid, $rids);
735
  $default_rule = isset($button['#subconditional'])
736
                    ? array(
737
                        'source' => NULL,
738
                        'source_type' => 'component',
739
                        'operator' => NULL,
740
                        'value' => NULL,
741
                      )
742
                    : array(
743
                        'target_type' => 'component',
744
                        'target' => NULL,
745
                        'invert' => NULL,
746
                        'action' => NULL,
747
                        'argument' => NULL,
748
                      );
749

    
750
  if (empty($button['#subconditional'])) {
751
    $new[0] = $parent_values[$rid]['source_type'] == 'component' ? $parent_values[$rid] : $default_rule;
752
  }
753
  else {
754
    // The default andor operator is opposite of current subconditional's
755
    // operatior.
756
    $parent_rid = _webform_conditional_find_start($parent_values, $rid, -1);
757
    $current_op = $parent_rid < 0 ? $conditional['andor'] : $parent_values[$parent_rid]['operator'];
758
    $current_op = $current_op == 'and' ? 'or' : 'and';
759
    $new = array(
760
      array('source_type' => 'conditional_start', 'operator' => $current_op) + $default_rule,
761
      $default_rule,
762
      $default_rule,
763
      array('source_type' => 'conditional_end') + $default_rule,
764
    );
765
  }
766

    
767
  // Update both $form_state['values'] and ['input] so that FAPI can merge
768
  // input values from the POST into the new form.
769
  $parent_values = array_merge(array_slice($parent_values, 0, $offset + 1), $new, array_slice($parent_values, $offset + 1));
770
  $input_values = array_merge(array_slice($input_values, 0, $offset + 1), $new, array_slice($input_values, $offset + 1));
771
  $form_state['rebuild'] = TRUE;
772
}
773

    
774
/**
775
 * Submit handler for webform_conditional elements to remove a rule or action.
776
 */
777
function webform_conditional_element_remove($form, &$form_state) {
778
  $button = $form_state['clicked_button'];
779
  $parents = $button['#parents'];
780
  $action = array_pop($parents);
781
  $rid = array_pop($parents);
782

    
783
  // Recurse through the form values until we find the root Webform conditional.
784
  $parent_values = &$form_state['values'];
785
  foreach ($parents as $key) {
786
    if (array_key_exists($key, $parent_values)) {
787
      $parent_values = &$parent_values[$key];
788
    }
789
  }
790
  switch ($parent_values[$rid]['source_type']) {
791
    case 'conditional_start':
792
      unset($parent_values[_webform_conditional_find_end($parent_values, $rid)]);
793
      break;
794
    case 'conditional_end':
795
      unset($parent_values[_webform_conditional_find_start($parent_values, $rid)]);
796
      break;
797
  }
798
  // Remove this rule or action from the list of conditionals.
799
  unset($parent_values[$rid]);
800

    
801
  $form_state['rebuild'] = TRUE;
802
}
803

    
804
/**
805
 * Helper. Delete any subconditionals which contain no rules.
806
 *
807
 * @param &array $conditional
808
 *   Conditional array containing the rules.
809
 * @return array
810
 *   Array of deleted subconditionals. Empty array if none were deleted.
811
 */
812
function webform_delete_empty_subconditionals(&$conditional) {
813
  $deleted = array();
814
  do {
815
    $empty_deleted = FALSE;
816
    $open_rid = NULL;
817
    foreach ($conditional['rules'] as $rid => $rule) {
818
      switch ($rule['source_type']) {
819
        case 'conditional_start':
820
          $open_rid = $rid;
821
          break;
822
        case 'conditional_end':
823
          if ($open_rid) {
824
            // A conditional_start rule was immediately followed by a
825
            // conditional_end rule. Delete them both. Repeat the check in case
826
            // the parent is now empty.
827
            $deleted[$open_rid] = $open_rid;
828
            $deleted[$rid] = $rid;
829
            unset($conditional['rules'][$open_rid], $conditional['rules'][$rid]);
830
            $open_rid = NULL;
831
            $empty_deleted = TRUE;
832
          }
833
          break;
834
        default:
835
          $open_rid = NULL;
836
          //break;
837
      }
838
    }
839
  } while ($empty_deleted);
840
  return $deleted;
841
}
842

    
843
/**
844
 * AJAX callback to render out adding a new condition.
845
 */
846
function webform_conditional_element_ajax($form, $form_state) {
847
  $button = $form_state['clicked_button'];
848
  $parents = $button['#parents'];
849

    
850
  // Trim down the parents to go back up to the level of this elements wrapper.
851
  array_pop($parents); // The button name (add/remove).
852
  array_pop($parents); // The rule ID.
853
  array_pop($parents); // The "rules" grouping.
854

    
855
  $element = $form;
856
  foreach ($parents as $key) {
857
    if (!isset($element[$key])) {
858
      // The entire conditional has been removed
859
      return '';
860
    }
861
    $element = $element[$key];
862
  }
863

    
864
  return drupal_render($element['conditional']);
865
}
866

    
867
/**
868
 * Theme the form for a conditional action.
869
 */
870
function theme_webform_conditional($variables) {
871
  $element = $variables['element'];
872

    
873
  $output = '';
874
  $output .= '<div class="webform-conditional">';
875
  $output .= '<span class="webform-conditional-if">' . t('If') . '</span>';
876

    
877
  foreach (element_children($element['rules']) as $rid) {
878
    $rule = &$element['rules'][$rid];
879
    switch ($rule['source_type']['#value']) {
880
      case 'conditional_start':
881
        $source_phrase = '<div class="webform-subconditional">' . t('(') . '</div>';
882
        break;
883
      case 'conditional_end':
884
        $source_phrase = '<div class="webform-subconditional">' . t(')') . '</div>';
885
        break;
886
      default:
887
        // Hide labels.
888
        $rule['source']['#title_display'] = 'none';
889
        $rule['operator']['#title_display'] = 'none';
890
        $rule['value']['#title_display'] = 'none';
891

    
892
        $source = '<div class="webform-conditional-source">' . drupal_render($rule['source']) . '</div>';
893
        $operator = '<div class="webform-conditional-operator">' . drupal_render($rule['operator']) . '</div>';
894
        $value = '<div class="webform-conditional-value">' . drupal_render($rule['value']) . '</div>';
895

    
896
        $source_phrase = t('!source !operator !value', array(
897
          '!source' => $source,
898
          '!operator' => $operator,
899
          '!value' => $value,
900
        ));
901
        // break
902
    }
903

    
904
    $output .= '<div class="webform-conditional-rule">';
905
    // Can't use theme('indentation') here because it causes the draghandle to
906
    // be located after the last indentation div.
907
    $output .= str_repeat('<div class="webform-indentation">&nbsp;</div>', $rule['#level']);
908
    $output .= drupal_render($rule['source_type']);
909
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
910
    $output .= $source_phrase;
911
    $output .= '</div>';
912

    
913
    if (isset($rule['andor'])) {
914
      $rule['andor']['#title_display'] = 'none';
915
      $output .= '<div class="webform-conditional-andor webform-container-inline">';
916
      $output .= drupal_render($rule['andor']);
917
      $output .= '</div>';
918
    }
919

    
920
    if (isset($rule['add']) || isset($rule['remove'])) {
921
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
922
      $output .= drupal_render($rule['add_subconditional']);
923
      $output .= drupal_render($rule['add']);
924
      $output .= drupal_render($rule['remove']);
925
      $output .= '</span>';
926
    }
927

    
928
    $output .= '</div>';
929
  }
930

    
931
  // Hide labels.
932
  foreach (element_children($element['actions']) as $aid) {
933
    // Hide labels.
934
    $element['actions'][$aid]['target']['#title_display'] = 'none';
935
    $element['actions'][$aid]['invert']['#title_display'] = 'none';
936
    $element['actions'][$aid]['action']['#title_display'] = 'none';
937
    $element['actions'][$aid]['argument']['#title_display'] = 'none';
938

    
939
    $target = '<div class="webform-conditional-target">' . drupal_render($element['actions'][$aid]['target']) . '</div>';
940
    $invert = '<div class="webform-conditional-invert">' . drupal_render($element['actions'][$aid]['invert']) . '</div>';
941
    $action = '<div class="webform-conditional-action">' . drupal_render($element['actions'][$aid]['action']) . '</div>';
942
    $argument = '<div class="webform-conditional-argument">' . drupal_render($element['actions'][$aid]['argument']) . '</div>';
943

    
944
    $target_phrase = t('then !target !invert !action !argument', array(
945
      '!target' => $target,
946
      '!invert' => $invert,
947
      '!action' => $action,
948
      '!argument' => $argument,
949
    ));
950

    
951
    $output .= '<div class="webform-conditional-action">';
952
    $output .= '<div class="webform-container-inline webform-conditional-condition">';
953
    $output .= $target_phrase;
954
    $output .= '</div>';
955

    
956
    if (isset($element['actions'][$aid]['add']) || isset($element['actions'][$aid]['remove'])) {
957
      $output .= '<span class="webform-conditional-operations webform-container-inline">';
958
      $output .= drupal_render($element['actions'][$aid]['add']);
959
      $output .= drupal_render($element['actions'][$aid]['remove']);
960
      $output .= '</span>';
961
    }
962

    
963
    $output .= '</div>';
964
  }
965

    
966
  $output .= '</div>';
967

    
968
  return $output;
969
}
970

    
971
/**
972
 * Return a list of all Webform conditional operators.
973
 */
974
function webform_conditional_operators() {
975
  static $operators;
976

    
977
  if (!isset($operators)) {
978
    $operators = module_invoke_all('webform_conditional_operator_info');
979
    drupal_alter('webform_conditional_operators', $operators);
980
  }
981

    
982
  return $operators;
983
}
984

    
985
/**
986
 * Return a nested list of all available operators, suitable for a select list.
987
 */
988
function webform_conditional_operators_list() {
989
  $options = array();
990
  $operators = webform_conditional_operators();
991

    
992
  foreach ($operators as $data_type => $type_operators) {
993
    $options[$data_type] = array();
994
    foreach ($type_operators as $operator => $operator_info) {
995
      $options[$data_type][$operator] = $operator_info['label'];
996
    }
997
  }
998

    
999
  return $options;
1000
}
1001

    
1002
/**
1003
 * Implements hook_webform_conditional_operator_info().
1004
 *
1005
 * Called from webform.module's webform_webform_conditional_operator_info().
1006
 */
1007
function _webform_conditional_operator_info() {
1008
  // General operators:
1009
  $operators['string']['equal'] = array(
1010
    'label' => t('is'),
1011
    'comparison callback' => 'webform_conditional_operator_string_equal',
1012
    'js comparison callback' => 'conditionalOperatorStringEqual',
1013
    // A form callback is not needed here, since we can use the default,
1014
    // non-JavaScript textfield for all text and numeric fields.
1015
    // 'form callback' => 'webform_conditional_operator_text',
1016
  );
1017
  $operators['string']['not_equal'] = array(
1018
    'label' => t('is not'),
1019
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
1020
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
1021
  );
1022
  $operators['string']['contains'] = array(
1023
    'label' => t('contains'),
1024
    'comparison callback' => 'webform_conditional_operator_string_contains',
1025
    'js comparison callback' => 'conditionalOperatorStringContains',
1026
  );
1027
  $operators['string']['does_not_contain'] = array(
1028
    'label' => t('does not contain'),
1029
    'comparison callback' => 'webform_conditional_operator_string_does_not_contain',
1030
    'js comparison callback' => 'conditionalOperatorStringDoesNotContain',
1031
  );
1032
  $operators['string']['begins_with'] = array(
1033
    'label' => t('begins with'),
1034
    'comparison callback' => 'webform_conditional_operator_string_begins_with',
1035
    'js comparison callback' => 'conditionalOperatorStringBeginsWith',
1036
  );
1037
  $operators['string']['ends_with'] = array(
1038
    'label' => t('ends with'),
1039
    'comparison callback' => 'webform_conditional_operator_string_ends_with',
1040
    'js comparison callback' => 'conditionalOperatorStringEndsWith',
1041
  );
1042
  $operators['string']['empty'] = array(
1043
    'label' => t('is blank'),
1044
    'comparison callback' => 'webform_conditional_operator_string_empty',
1045
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1046
    'form callback' => FALSE, // No value form at all.
1047
  );
1048
  $operators['string']['not_empty'] = array(
1049
    'label' => t('is not blank'),
1050
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1051
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1052
    'form callback' => FALSE, // No value form at all.
1053
  );
1054

    
1055
  // Numeric operators.
1056
  $operators['numeric']['equal'] = array(
1057
    'label' => t('is equal to'),
1058
    'comparison callback' => 'webform_conditional_operator_numeric_equal',
1059
    'js comparison callback' => 'conditionalOperatorNumericEqual',
1060
  );
1061
  $operators['numeric']['not_equal'] = array(
1062
    'label' => t('is not equal to'),
1063
    'comparison callback' => 'webform_conditional_operator_numeric_not_equal',
1064
    'js comparison callback' => 'conditionalOperatorNumericNotEqual',
1065
  );
1066
  $operators['numeric']['less_than'] = array(
1067
    'label' => t('is less than'),
1068
    'comparison callback' => 'webform_conditional_operator_numeric_less_than',
1069
    'js comparison callback' => 'conditionalOperatorNumericLessThan',
1070
  );
1071
  $operators['numeric']['less_than_equal'] = array(
1072
    'label' => t('is less than or equal'),
1073
    'comparison callback' => 'webform_conditional_operator_numeric_less_than_equal',
1074
    'js comparison callback' => 'conditionalOperatorNumericLessThanEqual',
1075
  );
1076
  $operators['numeric']['greater_than'] = array(
1077
    'label' => t('is greater than'),
1078
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than',
1079
    'js comparison callback' => 'conditionalOperatorNumericGreaterThan',
1080
  );
1081
  $operators['numeric']['greater_than_equal'] = array(
1082
    'label' => t('is greater than or equal'),
1083
    'comparison callback' => 'webform_conditional_operator_numeric_greater_than_equal',
1084
    'js comparison callback' => 'conditionalOperatorNumericGreaterThanEqual',
1085
  );
1086
  $operators['numeric']['empty'] = array(
1087
    'label' => t('is blank'),
1088
    'comparison callback' => 'webform_conditional_operator_string_empty',
1089
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1090
    'form callback' => FALSE, // No value form at all.
1091
  );
1092
  $operators['numeric']['not_empty'] = array(
1093
    'label' => t('is not blank'),
1094
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1095
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1096
    'form callback' => FALSE, // No value form at all.
1097
  );
1098

    
1099
  // Select operators.
1100
  $operators['select']['equal'] = array(
1101
    'label' => t('is'),
1102
    'comparison callback' => 'webform_conditional_operator_string_equal',
1103
    'js comparison callback' => 'conditionalOperatorStringEqual',
1104
    'form callback' => 'webform_conditional_form_select',
1105
  );
1106
  $operators['select']['not_equal'] = array(
1107
    'label' => t('is not'),
1108
    'comparison callback' => 'webform_conditional_operator_string_not_equal',
1109
    'js comparison callback' => 'conditionalOperatorStringNotEqual',
1110
    'form callback' => 'webform_conditional_form_select',
1111
  );
1112
  $operators['select']['less_than'] = array(
1113
    'label' => t('is before'),
1114
    'comparison callback' => 'webform_conditional_operator_select_less_than',
1115
    'js comparison callback' => 'conditionalOperatorSelectLessThan',
1116
    'form callback' => 'webform_conditional_form_select',
1117
  );
1118
  $operators['select']['less_than_equal'] = array(
1119
    'label' => t('is or is before'),
1120
    'comparison callback' => 'webform_conditional_operator_select_less_than_equal',
1121
    'js comparison callback' => 'conditionalOperatorSelectLessThanEqual',
1122
    'form callback' => 'webform_conditional_form_select',
1123
  );
1124
  $operators['select']['greater_than'] = array(
1125
    'label' => t('is after'),
1126
    'comparison callback' => 'webform_conditional_operator_select_greater_than',
1127
    'js comparison callback' => 'conditionalOperatorSelectGreaterThan',
1128
    'form callback' => 'webform_conditional_form_select',
1129
  );
1130
  $operators['select']['greater_than_equal'] = array(
1131
    'label' => t('is or is after'),
1132
    'comparison callback' => 'webform_conditional_operator_select_greater_than_equal',
1133
    'js comparison callback' => 'conditionalOperatorSelectGreaterThanEqual',
1134
    'form callback' => 'webform_conditional_form_select',
1135
  );
1136
  $operators['select']['empty'] = array(
1137
    'label' => t('is empty'),
1138
    'comparison callback' => 'webform_conditional_operator_string_empty',
1139
    'js comparison callback' => 'conditionalOperatorStringEmpty',
1140
    'form callback' => FALSE, // No value form at all.
1141
  );
1142
  $operators['select']['not_empty'] = array(
1143
    'label' => t('is not empty'),
1144
    'comparison callback' => 'webform_conditional_operator_string_not_empty',
1145
    'js comparison callback' => 'conditionalOperatorStringNotEmpty',
1146
    'form callback' => FALSE, // No value form at all.
1147
  );
1148

    
1149
  // Date operators:
1150
  $operators['date']['equal'] = array(
1151
    'label' => t('is on'),
1152
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
1153
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1154
    'js comparison callback' => 'conditionalOperatorDateEqual',
1155
    'form callback' => 'webform_conditional_form_date',
1156
  );
1157
  $operators['date']['not_equal'] = array(
1158
    'label' => t('is not on'),
1159
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
1160
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1161
    'js comparison callback' => 'conditionalOperatorDateNotEqual',
1162
    'form callback' => 'webform_conditional_form_date',
1163
  );
1164
  $operators['date']['before'] = array(
1165
    'label' => t('is before'),
1166
    'comparison callback' => 'webform_conditional_operator_datetime_before',
1167
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1168
    'js comparison callback' => 'conditionalOperatorDateBefore',
1169
    'form callback' => 'webform_conditional_form_date',
1170
  );
1171
  $operators['date']['before_equal'] = array(
1172
    'label' => t('is on or before'),
1173
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
1174
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1175
    'js comparison callback' => 'conditionalOperatorDateBeforeEqual',
1176
    'form callback' => 'webform_conditional_form_date',
1177
  );
1178
  $operators['date']['after'] = array(
1179
    'label' => t('is after'),
1180
    'comparison callback' => 'webform_conditional_operator_datetime_after',
1181
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1182
    'js comparison callback' => 'conditionalOperatorDateAfter',
1183
    'form callback' => 'webform_conditional_form_date',
1184
  );
1185
  $operators['date']['after_equal'] = array(
1186
    'label' => t('is on or after'),
1187
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
1188
    'comparison prepare js' => 'webform_conditional_prepare_date_js',
1189
    'js comparison callback' => 'conditionalOperatorDateAfterEqual',
1190
    'form callback' => 'webform_conditional_form_date',
1191
  );
1192

    
1193
  // Time operators:
1194
  $operators['time']['equal'] = array(
1195
    'label' => t('is at'),
1196
    'comparison callback' => 'webform_conditional_operator_datetime_equal',
1197
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1198
    'js comparison callback' => 'conditionalOperatorTimeEqual',
1199
    'form callback' => 'webform_conditional_form_time',
1200
  );
1201
  $operators['time']['not_equal'] = array(
1202
    'label' => t('is not at'),
1203
    'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
1204
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1205
    'js comparison callback' => 'conditionalOperatorTimeNotEqual',
1206
    'form callback' => 'webform_conditional_form_time',
1207
  );
1208
  $operators['time']['before'] = array(
1209
    'label' => t('is before'),
1210
    'comparison callback' => 'webform_conditional_operator_datetime_before',
1211
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1212
    'js comparison callback' => 'conditionalOperatorTimeBefore',
1213
    'form callback' => 'webform_conditional_form_time',
1214
  );
1215
  $operators['time']['before_equal'] = array(
1216
    'label' => t('is at or before'),
1217
    'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
1218
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1219
    'js comparison callback' => 'conditionalOperatorTimeBeforeEqual',
1220
    'form callback' => 'webform_conditional_form_time',
1221
  );
1222
  $operators['time']['after'] = array(
1223
    'label' => t('is after'),
1224
    'comparison callback' => 'webform_conditional_operator_datetime_after',
1225
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1226
    'js comparison callback' => 'conditionalOperatorTimeAfter',
1227
    'form callback' => 'webform_conditional_form_time',
1228
  );
1229
  $operators['time']['after_equal'] = array(
1230
    'label' => t('is at or after'),
1231
    'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
1232
    'comparison prepare js' => 'webform_conditional_prepare_time_js',
1233
    'js comparison callback' => 'conditionalOperatorTimeAfterEqual',
1234
    'form callback' => 'webform_conditional_form_time',
1235
  );
1236

    
1237
  return $operators;
1238
}
1239

    
1240
/**
1241
 * Form callback for select-type conditional fields.
1242
 *
1243
 * Unlike other built-in conditional value forms, the form callback for select
1244
 * types provides an array of forms, keyed by the $cid, which is the "source"
1245
 * for the condition.
1246
 */
1247
function webform_conditional_form_select($node) {
1248
  static $count = 0;
1249
  $forms = array();
1250
  webform_component_include('select');
1251
  foreach ($node->webform['components'] as $cid => $component) {
1252
    if (webform_component_property($component['type'], 'conditional_type') == 'select') {
1253
      // TODO: Use a pluggable mechanism for retrieving select list values.
1254
      $options = _webform_select_options($component);
1255
      $element = array(
1256
        '#type' => 'select',
1257
        '#multiple' => FALSE,
1258
        '#size' => NULL,
1259
        '#attributes' => array(),
1260
        '#id' => NULL,
1261
        '#name' => 'webform-conditional-select-' . $cid . '-' . $count,
1262
        '#options' => $options,
1263
        '#parents' => array(),
1264
      );
1265
      $forms[$cid] = drupal_render($element);
1266
    }
1267
  }
1268
  $count++;
1269
  return $forms;
1270
}
1271

    
1272
/**
1273
 * Form callback for date conditional fields.
1274
 */
1275
function webform_conditional_form_date($node) {
1276
  static $count = 0;
1277
  $element = array(
1278
    '#title' => NULL,
1279
    '#title_display' => 'none',
1280
    '#size' => 24,
1281
    '#attributes' => array('placeholder' => t('@format or valid date', array('@format' => webform_date_format('short')))),
1282
    '#type' => 'textfield',
1283
    '#name' => 'webform-conditional-date-' . $count++,
1284
  );
1285
  return drupal_render($element);
1286
}
1287

    
1288
/**
1289
 * Form callback for time conditional fields.
1290
 */
1291
function webform_conditional_form_time($node) {
1292
  static $count = 0;
1293
  $element = array(
1294
    '#title' => NULL,
1295
    '#title_display' => 'none',
1296
    '#size' => 24,
1297
    '#attributes' => array('placeholder' => t('HH:MMam or valid time')),
1298
    '#type' => 'textfield',
1299
    '#name' => 'webform-conditional-time-' . $count++,
1300
  );
1301
  return drupal_render($element);
1302
}
1303

    
1304
/**
1305
 * Load a conditional setting from the database.
1306
 */
1307
function webform_conditional_load($rgid, $nid) {
1308
  $node = node_load($nid);
1309

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

    
1312
  return $conditional;
1313
}
1314

    
1315
/**
1316
 * Insert a conditional rule group into the database.
1317
 */
1318
function webform_conditional_insert($conditional) {
1319
  drupal_write_record('webform_conditional', $conditional);
1320
  foreach ($conditional['rules'] as $rid => $rule) {
1321
    $rule['nid'] = $conditional['nid'];
1322
    $rule['rgid'] = $conditional['rgid'];
1323
    $rule['rid'] = $rid;
1324
    drupal_write_record('webform_conditional_rules', $rule);
1325
  }
1326
  foreach ($conditional['actions'] as $aid => $action) {
1327
    $action['nid'] = $conditional['nid'];
1328
    $action['rgid'] = $conditional['rgid'];
1329
    $action['aid'] = $aid;
1330
    drupal_write_record('webform_conditional_actions', $action);
1331
  }
1332
}
1333

    
1334
/**
1335
 * Update a conditional setting in the database.
1336
 */
1337
function webform_conditional_update($node, $conditional) {
1338
  webform_conditional_delete($node, $conditional);
1339
  webform_conditional_insert($conditional);
1340
}
1341

    
1342
/**
1343
 * Delete a conditional rule group.
1344
 */
1345
function webform_conditional_delete($node, $conditional) {
1346
  db_delete('webform_conditional')
1347
    ->condition('nid', $node->nid)
1348
    ->condition('rgid', $conditional['rgid'])
1349
    ->execute();
1350
  db_delete('webform_conditional_rules')
1351
    ->condition('nid', $node->nid)
1352
    ->condition('rgid', $conditional['rgid'])
1353
    ->execute();
1354
  db_delete('webform_conditional_actions')
1355
    ->condition('nid', $node->nid)
1356
    ->condition('rgid', $conditional['rgid'])
1357
    ->execute();
1358
}
1359

    
1360
/**
1361
 * Loop through all the conditional settings and add needed JavaScript settings.
1362
 *
1363
 * We do a bit of optimization for JavaScript before adding to the page as
1364
 * settings. We remove unnecessary data structures and provide a "source map"
1365
 * so that JavaScript can quickly determine if it needs to check rules when a
1366
 * field on the page has been modified.
1367
 *
1368
 * @param object $node
1369
 *   The loaded node object, containing the webform.
1370
 * @param array $submission_data
1371
 *   The cid-indexed array of existing submission values to be included for
1372
 *   sources outside of the current page.
1373
 * @param integer $page_num
1374
 *   The number of the page for which javascript settings should be generated.
1375
 * @return array
1376
 *   Array of settings to be send to the browser as javascript settings.
1377
 */
1378
function webform_conditional_prepare_javascript($node, $submission_data, $page_num) {
1379
  $settings = array(
1380
    'ruleGroups' => array(),
1381
    'sourceMap' => array(),
1382
    'values' => array(),
1383
  );
1384
  $operators = webform_conditional_operators();
1385
  $conditionals = $node->webform['conditionals'];
1386
  $components = $node->webform['components'];
1387
  $topological_order = webform_get_conditional_sorter($node)->getOrder();
1388
  foreach ($topological_order[$page_num] as $conditional_spec) {
1389
    $conditional = $conditionals[$conditional_spec['rgid']];
1390
    $rgid_key = 'rgid_' . $conditional['rgid'];
1391
    // Assemble the main conditional group settings.
1392

    
1393
    $settings['ruleGroups'][$rgid_key] = array(
1394
      'andor' => $conditional['andor'],
1395
    );
1396
    foreach ($conditional['actions'] as $action) {
1397
      if ($action['target_type'] == 'component') {
1398
        $target_component = $components[$action['target']];
1399
        $target_parents = webform_component_parent_keys($node, $target_component);
1400
        $aid_key = 'aid_' . $action['aid'];
1401
        $action_settings = array(
1402
          'target' => 'webform-component--' . str_replace('_', '-', implode('--', $target_parents)),
1403
          'invert' => (int)$action['invert'],
1404
          'action' => $action['action'],
1405
          'argument' => $components[$action['target']]['type'] == 'markup' ? filter_xss_admin($action['argument']) : $action['argument'],
1406
        );
1407
        $settings['ruleGroups'][$rgid_key]['actions'][$aid_key] = $action_settings;
1408
      }
1409
    }
1410
    // Add on the list of rules to the conditional group.
1411
    foreach ($conditional['rules'] as $rule) {
1412
      $rid_key = 'rid_' . $rule['rid'];
1413
      switch ($rule['source_type']) {
1414
        case 'component':
1415
          $source_component = $components[$rule['source']];
1416
          $source_parents = webform_component_parent_keys($node, $source_component);
1417
          $source_id = 'webform-component--' . str_replace('_', '-', implode('--', $source_parents));
1418

    
1419
          // If this source has a value set, add that as a setting.
1420
          // NULL or array(NULL) should be sent as an empty array to simplify the jQuery.
1421
          if (isset($submission_data[$source_component['cid']])) {
1422
            $source_value = $submission_data[$source_component['cid']];
1423
            $source_value = is_array($source_value) ? $source_value : array($source_value);
1424
            $settings['values'][$source_id] = $source_value === array(NULL) ? array() : $source_value;
1425
          }
1426

    
1427
          $conditional_type = webform_component_property($source_component['type'], 'conditional_type');
1428
          $operator_info = $operators[$conditional_type][$rule['operator']];
1429
          $rule_settings = array(
1430
            'source_type' => $rule['source_type'],
1431
            'source' => $source_id,
1432
            'value' => $rule['value'],
1433
            'callback' => $operator_info['js comparison callback'],
1434
          );
1435
          if (isset($operator_info['comparison prepare js'])) {
1436
            $callback = $operator_info['comparison prepare js'];
1437
            $rule_settings['value'] = $callback($rule['value']);
1438
          }
1439
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = $rule_settings;
1440
          $settings['sourceMap'][$source_id][$rgid_key] = $rgid_key;
1441
          break;
1442
        case 'conditional_start':
1443
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1444
            'source_type' => $rule['source_type'],
1445
            'andor' => $rule['operator'],
1446
          );
1447
          break;
1448
        case 'conditional_end':
1449
          $settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
1450
            'source_type' => $rule['source_type'],
1451
          );
1452
          break;
1453
      }
1454
    }
1455
  }
1456

    
1457
  return $settings;
1458
}
1459

    
1460
/**
1461
 * Determine whether a component type is capable of a given conditional action.
1462
 */
1463
function webform_conditional_action_able($component_type, $action) {
1464
  switch ($action) {
1465
    case 'show':
1466
      return TRUE;
1467
      // break;
1468
    case 'require':
1469
      return webform_component_feature($component_type, 'required');
1470
      // break;
1471
    default:
1472
      return webform_component_feature($component_type, "conditional_action_$action");
1473
      // break;
1474
  }
1475
}
1476

    
1477
/**
1478
 * Prepare a conditional value for adding as a JavaScript setting.
1479
 */
1480
function webform_conditional_prepare_date_js($rule_value) {
1481
  // Convert the time/date string to a UTC timestamp for comparison. Note that
1482
  // this means comparisons against immediate times (such as "now") may be
1483
  // slightly stale by the time the comparison executes. Timestamps are in
1484
  // milliseconds, as to match JavaScript's Date.toString() method.
1485
  $date = webform_strtodate('c', $rule_value, 'UTC');
1486
  return webform_strtotime($date);
1487
}
1488

    
1489
/**
1490
 * Prepare a conditional value for adding as a JavaScript setting.
1491
 */
1492
function webform_conditional_prepare_time_js($rule_value) {
1493
  $date = webform_conditional_prepare_date_js($rule_value);
1494
  $today = webform_strtodate('c', 'today', 'UTC');
1495
  $today = webform_strtotime($today);
1496
  return $date - $today;
1497
}
1498

    
1499
/**
1500
 * Conditional callback for string comparisons.
1501
 */
1502
function webform_conditional_operator_string_equal($input_values, $rule_value) {
1503
  foreach ($input_values as $value) {
1504
    // Checkbox values come in as 0 integers for unchecked boxes.
1505
    $value = ($value === 0) ? '' : $value;
1506
    if (strcasecmp($value, $rule_value) === 0) {
1507
      return TRUE;
1508
    }
1509
  }
1510
  return FALSE;
1511
}
1512

    
1513
/**
1514
 * Conditional callback for string comparisons.
1515
 */
1516
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
1517
  return !webform_conditional_operator_string_equal($input_values, $rule_value);
1518
}
1519

    
1520
/**
1521
 * Conditional callback for string comparisons.
1522
 */
1523
function webform_conditional_operator_string_contains($input_values, $rule_value) {
1524
  foreach ($input_values as $value) {
1525
    if (stripos($value, $rule_value) !== FALSE) {
1526
      return TRUE;
1527
    }
1528
  }
1529
  return FALSE;
1530
}
1531

    
1532
/**
1533
 * Conditional callback for string comparisons.
1534
 */
1535
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
1536
  return !webform_conditional_operator_string_contains($input_values, $rule_value);
1537
}
1538

    
1539
/**
1540
 * Conditional callback for string comparisons.
1541
 */
1542
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
1543
  foreach ($input_values as $value) {
1544
    if (stripos($value, $rule_value) === 0) {
1545
      return TRUE;
1546
    }
1547
  }
1548
  return FALSE;
1549
}
1550

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

    
1563
/**
1564
 * Conditional callback for checking for empty fields.
1565
 */
1566
function webform_conditional_operator_string_empty($input_values, $rule_value) {
1567
  $empty = TRUE;
1568
  foreach ($input_values as $value) {
1569
    if ($value !== '' && $value !== NULL && $value !== 0) {
1570
      $empty = FALSE;
1571
      break;
1572
    }
1573
  }
1574
  return $empty;
1575
}
1576

    
1577
/**
1578
 * Conditional callback for checking for empty fields.
1579
 */
1580
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
1581
  return !webform_conditional_operator_string_empty($input_values, $rule_value);
1582
}
1583

    
1584
/**
1585
 * Conditional callback for select comparisons.
1586
 */
1587
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
1588
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
1589
}
1590

    
1591
/**
1592
 * Conditional callback for select comparisons.
1593
 */
1594
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
1595
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1596
  return $comparison < 0 || $comparison === 0;
1597
}
1598

    
1599
/**
1600
 * Conditional callback for select comparisons.
1601
 */
1602
function webform_conditional_operator_select_greater_than($input_values, $rule_value, $component) {
1603
  return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) > 0;
1604
}
1605

    
1606
/**
1607
 * Conditional callback for select comparisons.
1608
 */
1609
function webform_conditional_operator_select_greater_than_equal($input_values, $rule_value, $component) {
1610
  $comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
1611
  return $comparison > 0 || $comparison === 0;
1612
}
1613

    
1614
/**
1615
 * Conditional callback for numeric comparisons.
1616
 */
1617
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
1618
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
1619
}
1620

    
1621
/**
1622
 * Conditional callback for numeric comparisons.
1623
 */
1624
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
1625
  return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
1626
}
1627

    
1628
/**
1629
 * Conditional callback for numeric comparisons.
1630
 */
1631
function webform_conditional_operator_numeric_less_than($input_values, $rule_value) {
1632
  return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) < 0;
1633
}
1634

    
1635
/**
1636
 * Conditional callback for numeric comparisons.
1637
 */
1638
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
1639
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1640
  return $comparison < 0 || $comparison === 0;
1641
}
1642

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

    
1650
/**
1651
 * Conditional callback for numeric comparisons.
1652
 */
1653
function webform_conditional_operator_numeric_greater_than_equal($input_values, $rule_value) {
1654
  $comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
1655
  return $comparison > 0 || $comparison === 0;
1656
}
1657

    
1658
/**
1659
 * Conditional callback for date and time comparisons.
1660
 */
1661
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
1662
  $input_values = webform_conditional_value_datetime($input_values);
1663
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
1664
}
1665

    
1666
/**
1667
 * Conditional callback for date and time comparisons.
1668
 */
1669
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
1670
  return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
1671
}
1672

    
1673
/**
1674
 * Conditional callback for date and time comparisons.
1675
 */
1676
function webform_conditional_operator_datetime_after($input_values, $rule_value) {
1677
  $input_values = webform_conditional_value_datetime($input_values);
1678
  return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) > webform_strtotime($rule_value);
1679
}
1680

    
1681
/**
1682
 * Conditional callback for date and time comparisons.
1683
 */
1684
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
1685
  return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
1686
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1687
}
1688

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

    
1697
/**
1698
 * Conditional callback for date and time comparisons.
1699
 */
1700
function webform_conditional_operator_datetime_before_equal($input_values, $rule_value) {
1701
  return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
1702
    webform_conditional_operator_datetime_equal($input_values, $rule_value);
1703
}
1704

    
1705
/**
1706
 * Utility function to convert incoming time and dates into strings.
1707
 */
1708
function webform_conditional_value_datetime($input_values) {
1709
  // Convert times into a string.
1710
  $input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
1711
  // Convert dates into a string.
1712
  $input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
1713
  return $input_values;
1714
}
1715

    
1716
/**
1717
 * Utility function to compare values of a select component.
1718
 * @param string $a
1719
 *   First select option key to compare
1720
 * @param string $b
1721
 *   Second select option key to compare
1722
 * @param array $options
1723
 *   Associative array where the $a and $b are within the keys
1724
 * @return integer based upon position of $a and $b in $options
1725
 *   -N if $a above (<) $b
1726
 *   0 if $a = $b
1727
 *   +N if $a is below (>) $b
1728
 */
1729
function webform_compare_select($a, $b, $options) {
1730
  // Select keys that are integer-like strings are numberic indices in PHP.
1731
  // Convert the array keys to an array of strings.
1732
  $options_array = array_map(function($i) {return (string) $i; }, array_keys($options));
1733
  $a_position = array_search($a, $options_array, TRUE);
1734
  $b_position = array_search($b, $options_array, TRUE);
1735
  return ($a_position === FALSE || $a_position === FALSE) ? NULL : $a_position - $b_position;
1736

    
1737
}