1 |
a45e4bc1
|
Assos Assos
|
<?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 |
e9984459
|
Assos Assos
|
foreach ($conditionals as $rgid => &$conditional) {
|
29 |
|
|
webform_delete_empty_subconditionals($conditional);
|
30 |
a45e4bc1
|
Assos Assos
|
if (empty($conditional['rules']) || empty($conditional['actions'])) {
|
31 |
|
|
unset($conditionals[$rgid]);
|
32 |
|
|
}
|
33 |
|
|
}
|
34 |
e9984459
|
Assos Assos
|
unset($conditional); // Drop PHP reference.
|
35 |
a45e4bc1
|
Assos Assos
|
|
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 |
ba09eb79
|
Assos Assos
|
$components = $node->webform['components'];
|
62 |
a45e4bc1
|
Assos Assos
|
$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 |
e9984459
|
Assos Assos
|
'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 |
a45e4bc1
|
Assos Assos
|
),
|
88 |
|
|
);
|
89 |
ba09eb79
|
Assos Assos
|
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 |
a45e4bc1
|
Assos Assos
|
$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 |
e9984459
|
Assos Assos
|
// 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 |
a45e4bc1
|
Assos Assos
|
|
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 |
e9984459
|
Assos Assos
|
'source_type' => 'component',
|
167 |
a45e4bc1
|
Assos Assos
|
'source' => NULL,
|
168 |
|
|
'operator' => NULL,
|
169 |
|
|
'value' => NULL,
|
170 |
|
|
),
|
171 |
|
|
),
|
172 |
|
|
'andor' => 'and',
|
173 |
|
|
'actions' => array(
|
174 |
|
|
array(
|
175 |
e9984459
|
Assos Assos
|
'target_type' => 'component',
|
176 |
a45e4bc1
|
Assos Assos
|
'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 |
ba09eb79
|
Assos Assos
|
if (is_numeric($action_key)) {
|
208 |
|
|
$operation = $action['action']['#value'];
|
209 |
a45e4bc1
|
Assos Assos
|
$target_id = $action['target']['#value'];
|
210 |
ba09eb79
|
Assos Assos
|
if (isset($targets[$target_id][$operation])) {
|
211 |
a45e4bc1
|
Assos Assos
|
form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][target',
|
212 |
ba09eb79
|
Assos Assos
|
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 |
a45e4bc1
|
Assos Assos
|
}
|
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 |
ba09eb79
|
Assos Assos
|
$targets[$target_id][$operation] = $target_id;
|
226 |
a45e4bc1
|
Assos Assos
|
}
|
227 |
|
|
}
|
228 |
|
|
foreach ($conditional['rules'] as $rule_key => $rule) {
|
229 |
e9984459
|
Assos Assos
|
// 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 |
a45e4bc1
|
Assos Assos
|
form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][source',
|
232 |
ba09eb79
|
Assos Assos
|
t('The subject of the conditional cannot be the same as the component that is changed (%target).',
|
233 |
a45e4bc1
|
Assos Assos
|
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 |
ba09eb79
|
Assos Assos
|
_webform_conditional_expand_value_forms($node);
|
242 |
a45e4bc1
|
Assos Assos
|
}
|
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 |
e9984459
|
Assos Assos
|
$node->webform['conditionals'] = $form_state['values']['conditionals'];
|
254 |
a45e4bc1
|
Assos Assos
|
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> </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 |
e9984459
|
Assos Assos
|
$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 |
a45e4bc1
|
Assos Assos
|
$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 |
e9984459
|
Assos Assos
|
$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 |
a45e4bc1
|
Assos Assos
|
|
362 |
e9984459
|
Assos Assos
|
$last_rid = -1;
|
363 |
a45e4bc1
|
Assos Assos
|
foreach ($element['#default_value']['rules'] as $rid => $conditional) {
|
364 |
e9984459
|
Assos Assos
|
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 |
a45e4bc1
|
Assos Assos
|
|
421 |
e9984459
|
Assos Assos
|
|
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 |
a45e4bc1
|
Assos Assos
|
'#type' => 'select',
|
430 |
|
|
'#title' => t('And/or'),
|
431 |
|
|
'#options' => array(
|
432 |
|
|
'and' => t('and'),
|
433 |
|
|
'or' => t('or'),
|
434 |
|
|
),
|
435 |
e9984459
|
Assos Assos
|
'#parents' => $andor['parents'],
|
436 |
|
|
'#default_value' => $andor['value'],
|
437 |
|
|
'#attributes' => array('data-rid' => $andor['rid']),
|
438 |
a45e4bc1
|
Assos Assos
|
);
|
439 |
|
|
}
|
440 |
e9984459
|
Assos Assos
|
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 |
a45e4bc1
|
Assos Assos
|
|
451 |
e9984459
|
Assos Assos
|
/**
|
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 |
a45e4bc1
|
Assos Assos
|
|
472 |
e9984459
|
Assos Assos
|
/**
|
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 |
a45e4bc1
|
Assos Assos
|
'#type' => 'select',
|
537 |
|
|
'#title' => t('Target'),
|
538 |
|
|
'#options' => $element['#targets'],
|
539 |
e9984459
|
Assos Assos
|
'#default_value' => $action['target'],
|
540 |
|
|
),
|
541 |
|
|
'invert' => array(
|
542 |
a45e4bc1
|
Assos Assos
|
'#type' => 'select',
|
543 |
|
|
'#title' => t('Is/Isn\'t'),
|
544 |
|
|
'#options' => array(
|
545 |
|
|
'0' => t('is'),
|
546 |
|
|
'1' => t('isn\'t'),
|
547 |
|
|
),
|
548 |
e9984459
|
Assos Assos
|
'#default_value' => $action['invert'],
|
549 |
|
|
),
|
550 |
|
|
'action' => array(
|
551 |
a45e4bc1
|
Assos Assos
|
'#type' => 'select',
|
552 |
|
|
'#title' => t('Action'),
|
553 |
|
|
'#options' => $element['#actions'],
|
554 |
e9984459
|
Assos Assos
|
'#default_value' => $action['action'],
|
555 |
|
|
),
|
556 |
|
|
'argument' => array(
|
557 |
a45e4bc1
|
Assos Assos
|
'#type' => 'textfield',
|
558 |
|
|
'#title' => t('Argument'),
|
559 |
|
|
'#size' => 20,
|
560 |
ba09eb79
|
Assos Assos
|
'#maxlength' => NULL,
|
561 |
e9984459
|
Assos Assos
|
'#default_value' => $action['argument'],
|
562 |
|
|
),
|
563 |
|
|
'add' => array(
|
564 |
a45e4bc1
|
Assos Assos
|
'#type' => 'submit',
|
565 |
e9984459
|
Assos Assos
|
'#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 |
a45e4bc1
|
Assos Assos
|
'#ajax' => array(
|
570 |
|
|
'progress' => 'none',
|
571 |
|
|
'callback' => 'webform_conditional_element_ajax',
|
572 |
e9984459
|
Assos Assos
|
'wrapper' => $element['#wrapper_id'],
|
573 |
a45e4bc1
|
Assos Assos
|
'event' => 'click',
|
574 |
|
|
),
|
575 |
e9984459
|
Assos Assos
|
),
|
576 |
|
|
'remove' => array(
|
577 |
a45e4bc1
|
Assos Assos
|
'#type' => 'submit',
|
578 |
e9984459
|
Assos Assos
|
'#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 |
a45e4bc1
|
Assos Assos
|
'#ajax' => array(
|
583 |
|
|
'progress' => 'none',
|
584 |
|
|
'callback' => 'webform_conditional_element_ajax',
|
585 |
e9984459
|
Assos Assos
|
'wrapper' => $element['#wrapper_id'],
|
586 |
a45e4bc1
|
Assos Assos
|
'event' => 'click',
|
587 |
|
|
),
|
588 |
e9984459
|
Assos Assos
|
),
|
589 |
|
|
);
|
590 |
a45e4bc1
|
Assos Assos
|
}
|
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 |
ba09eb79
|
Assos Assos
|
* 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 |
a45e4bc1
|
Assos Assos
|
* - 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 |
ba09eb79
|
Assos Assos
|
*
|
619 |
|
|
* @param $node
|
620 |
|
|
* The Webform node for which these forms are being generated.
|
621 |
a45e4bc1
|
Assos Assos
|
*/
|
622 |
ba09eb79
|
Assos Assos
|
function _webform_conditional_expand_value_forms($node) {
|
623 |
a45e4bc1
|
Assos Assos
|
$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 |
ba09eb79
|
Assos Assos
|
if ($form_callback !== FALSE && !isset($data['forms'][$form_callback])) {
|
632 |
a45e4bc1
|
Assos Assos
|
$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 |
ba09eb79
|
Assos Assos
|
drupal_add_js(array('webform' => array('conditionalValues' => $data)), 'setting');
|
645 |
a45e4bc1
|
Assos Assos
|
}
|
646 |
|
|
|
647 |
|
|
/**
|
648 |
e9984459
|
Assos Assos
|
* 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 |
|
|
* @return integer
|
657 |
|
|
* The rid of the found rule, or -1 if none. Note that NULL is not used as a
|
658 |
|
|
* semaphore for "not found" because it casts to 0, which is a valid rule id.
|
659 |
|
|
*/
|
660 |
|
|
function _webform_conditional_find_end($rules, $origin_rid, $target_delta_level = 0) {
|
661 |
|
|
$rids = array_keys($rules);
|
662 |
|
|
$offset = array_search($origin_rid, $rids);
|
663 |
|
|
$delta_level = 0;
|
664 |
|
|
foreach (array_slice($rules, $offset, NULL, TRUE) as $rid => $conditional) {
|
665 |
|
|
switch ($conditional['source_type']) {
|
666 |
|
|
case 'conditional_start':
|
667 |
|
|
$delta_level++;
|
668 |
|
|
break;
|
669 |
|
|
case 'conditional_end':
|
670 |
|
|
$delta_level--;
|
671 |
|
|
break;
|
672 |
|
|
}
|
673 |
|
|
if ($delta_level == $target_delta_level) {
|
674 |
|
|
return $rid;
|
675 |
|
|
}
|
676 |
|
|
}
|
677 |
|
|
// Mis-matched conditional_start / _end. Return -1.
|
678 |
|
|
return -1;
|
679 |
|
|
}
|
680 |
|
|
|
681 |
|
|
/**
|
682 |
|
|
* Helper. Find the matching start or end of a given subconditional.
|
683 |
|
|
*
|
684 |
|
|
* @see _webform_conditional_find_end().
|
685 |
|
|
*/
|
686 |
|
|
function _webform_conditional_find_start($rules, $origin_rid, $target_delta_level = 0) {
|
687 |
|
|
$rids = array_keys($rules);
|
688 |
|
|
$offset = array_search($origin_rid, $rids);
|
689 |
|
|
$delta_level = 0;
|
690 |
|
|
foreach (array_reverse(array_slice($rules, 0, $offset + 1, TRUE), TRUE) as $rid => $conditional) {
|
691 |
|
|
switch ($conditional['source_type']) {
|
692 |
|
|
case 'conditional_end':
|
693 |
|
|
$delta_level++;
|
694 |
|
|
break;
|
695 |
|
|
case 'conditional_start':
|
696 |
|
|
$delta_level--;
|
697 |
|
|
break;
|
698 |
|
|
}
|
699 |
|
|
if ($delta_level == $target_delta_level) {
|
700 |
|
|
return $rid;
|
701 |
|
|
}
|
702 |
|
|
}
|
703 |
|
|
// Mis-matched conditional_start / _end. Return -1.
|
704 |
|
|
return -1;
|
705 |
|
|
}
|
706 |
|
|
|
707 |
|
|
/**
|
708 |
|
|
* Submit handler for webform_conditional elements to add a new rule or action.
|
709 |
a45e4bc1
|
Assos Assos
|
*/
|
710 |
|
|
function webform_conditional_element_add($form, &$form_state) {
|
711 |
|
|
$button = $form_state['clicked_button'];
|
712 |
|
|
$parents = $button['#parents'];
|
713 |
|
|
$action = array_pop($parents);
|
714 |
|
|
$rid = array_pop($parents);
|
715 |
|
|
|
716 |
e9984459
|
Assos Assos
|
// Recurse through the form values until we find the Webform conditional rules
|
717 |
|
|
// or actions. Save the conditional prior to descending to rules/actions.
|
718 |
a45e4bc1
|
Assos Assos
|
$parent_values = &$form_state['values'];
|
719 |
e9984459
|
Assos Assos
|
$input_values = &$form_state['input'];
|
720 |
a45e4bc1
|
Assos Assos
|
foreach ($parents as $key) {
|
721 |
|
|
if (array_key_exists($key, $parent_values)) {
|
722 |
e9984459
|
Assos Assos
|
$conditional = $parent_values;
|
723 |
a45e4bc1
|
Assos Assos
|
$parent_values = &$parent_values[$key];
|
724 |
|
|
}
|
725 |
e9984459
|
Assos Assos
|
if (array_key_exists($key, $input_values)) {
|
726 |
|
|
$input_values = &$input_values[$key];
|
727 |
|
|
}
|
728 |
a45e4bc1
|
Assos Assos
|
}
|
729 |
|
|
|
730 |
e9984459
|
Assos Assos
|
// Split the list of rules/actions in this conditional and inject into the
|
731 |
|
|
// right spot.
|
732 |
a45e4bc1
|
Assos Assos
|
$rids = array_keys($parent_values);
|
733 |
|
|
$offset = array_search($rid, $rids);
|
734 |
6a93dd76
|
Assos Assos
|
$default_rule = isset($button['#subconditional'])
|
735 |
|
|
? array(
|
736 |
|
|
'source' => NULL,
|
737 |
|
|
'source_type' => 'component',
|
738 |
|
|
'operator' => NULL,
|
739 |
|
|
'value' => NULL,
|
740 |
|
|
)
|
741 |
|
|
: array(
|
742 |
|
|
'target_type' => 'component',
|
743 |
|
|
'target' => NULL,
|
744 |
|
|
'invert' => NULL,
|
745 |
|
|
'action' => NULL,
|
746 |
|
|
'argument' => NULL,
|
747 |
|
|
);
|
748 |
|
|
|
749 |
e9984459
|
Assos Assos
|
if (empty($button['#subconditional'])) {
|
750 |
|
|
$new[0] = $parent_values[$rid]['source_type'] == 'component' ? $parent_values[$rid] : $default_rule;
|
751 |
|
|
}
|
752 |
|
|
else {
|
753 |
|
|
// The default andor operator is opposite of current subconditional's
|
754 |
|
|
// operatior.
|
755 |
|
|
$parent_rid = _webform_conditional_find_start($parent_values, $rid, -1);
|
756 |
|
|
$current_op = $parent_rid < 0 ? $conditional['andor'] : $parent_values[$parent_rid]['operator'];
|
757 |
|
|
$current_op = $current_op == 'and' ? 'or' : 'and';
|
758 |
|
|
$new = array(
|
759 |
|
|
array('source_type' => 'conditional_start', 'operator' => $current_op) + $default_rule,
|
760 |
|
|
$default_rule,
|
761 |
|
|
$default_rule,
|
762 |
|
|
array('source_type' => 'conditional_end') + $default_rule,
|
763 |
|
|
);
|
764 |
|
|
}
|
765 |
a45e4bc1
|
Assos Assos
|
|
766 |
e9984459
|
Assos Assos
|
// Update both $form_state['values'] and ['input] so that FAPI can merge
|
767 |
|
|
// input values from the POST into the new form.
|
768 |
|
|
$parent_values = array_merge(array_slice($parent_values, 0, $offset + 1), $new, array_slice($parent_values, $offset + 1));
|
769 |
|
|
$input_values = array_merge(array_slice($input_values, 0, $offset + 1), $new, array_slice($input_values, $offset + 1));
|
770 |
a45e4bc1
|
Assos Assos
|
$form_state['rebuild'] = TRUE;
|
771 |
|
|
}
|
772 |
|
|
|
773 |
|
|
/**
|
774 |
|
|
* Submit handler for webform_conditional elements to remove a rule or action.
|
775 |
|
|
*/
|
776 |
|
|
function webform_conditional_element_remove($form, &$form_state) {
|
777 |
|
|
$button = $form_state['clicked_button'];
|
778 |
|
|
$parents = $button['#parents'];
|
779 |
|
|
$action = array_pop($parents);
|
780 |
e9984459
|
Assos Assos
|
$rid = array_pop($parents);
|
781 |
a45e4bc1
|
Assos Assos
|
|
782 |
|
|
// Recurse through the form values until we find the root Webform conditional.
|
783 |
|
|
$parent_values = &$form_state['values'];
|
784 |
|
|
foreach ($parents as $key) {
|
785 |
|
|
if (array_key_exists($key, $parent_values)) {
|
786 |
|
|
$parent_values = &$parent_values[$key];
|
787 |
|
|
}
|
788 |
|
|
}
|
789 |
e9984459
|
Assos Assos
|
switch ($parent_values[$rid]['source_type']) {
|
790 |
|
|
case 'conditional_start':
|
791 |
|
|
unset($parent_values[_webform_conditional_find_end($parent_values, $rid)]);
|
792 |
|
|
break;
|
793 |
|
|
case 'conditional_end':
|
794 |
|
|
unset($parent_values[_webform_conditional_find_start($parent_values, $rid)]);
|
795 |
|
|
break;
|
796 |
|
|
}
|
797 |
a45e4bc1
|
Assos Assos
|
// Remove this rule or action from the list of conditionals.
|
798 |
e9984459
|
Assos Assos
|
unset($parent_values[$rid]);
|
799 |
a45e4bc1
|
Assos Assos
|
|
800 |
|
|
$form_state['rebuild'] = TRUE;
|
801 |
|
|
}
|
802 |
|
|
|
803 |
e9984459
|
Assos Assos
|
/**
|
804 |
|
|
* Helper. Delete any subconditionals which contain no rules.
|
805 |
|
|
*
|
806 |
|
|
* @param &array $conditional
|
807 |
|
|
* Conditional array containing the rules.
|
808 |
|
|
* @return array
|
809 |
|
|
* Array of deleted subconditionals. Empty array if none were deleted.
|
810 |
|
|
*/
|
811 |
|
|
function webform_delete_empty_subconditionals(&$conditional) {
|
812 |
|
|
$deleted = array();
|
813 |
|
|
do {
|
814 |
|
|
$empty_deleted = FALSE;
|
815 |
|
|
$open_rid = NULL;
|
816 |
|
|
foreach ($conditional['rules'] as $rid => $rule) {
|
817 |
|
|
switch ($rule['source_type']) {
|
818 |
|
|
case 'conditional_start':
|
819 |
|
|
$open_rid = $rid;
|
820 |
|
|
break;
|
821 |
|
|
case 'conditional_end':
|
822 |
|
|
if ($open_rid) {
|
823 |
|
|
// A conditional_start rule was immediately followed by a
|
824 |
|
|
// conditional_end rule. Delete them both. Repeat the check in case
|
825 |
|
|
// the parent is now empty.
|
826 |
|
|
$deleted[$open_rid] = $open_rid;
|
827 |
|
|
$deleted[$rid] = $rid;
|
828 |
|
|
unset($conditional['rules'][$open_rid], $conditional['rules'][$rid]);
|
829 |
|
|
$open_rid = NULL;
|
830 |
|
|
$empty_deleted = TRUE;
|
831 |
|
|
}
|
832 |
|
|
break;
|
833 |
|
|
default:
|
834 |
|
|
$open_rid = NULL;
|
835 |
|
|
//break;
|
836 |
|
|
}
|
837 |
|
|
}
|
838 |
|
|
} while ($empty_deleted);
|
839 |
|
|
return $deleted;
|
840 |
|
|
}
|
841 |
a45e4bc1
|
Assos Assos
|
|
842 |
|
|
/**
|
843 |
|
|
* AJAX callback to render out adding a new condition.
|
844 |
|
|
*/
|
845 |
|
|
function webform_conditional_element_ajax($form, $form_state) {
|
846 |
|
|
$button = $form_state['clicked_button'];
|
847 |
|
|
$parents = $button['#parents'];
|
848 |
|
|
|
849 |
|
|
// Trim down the parents to go back up to the level of this elements wrapper.
|
850 |
|
|
array_pop($parents); // The button name (add/remove).
|
851 |
|
|
array_pop($parents); // The rule ID.
|
852 |
|
|
array_pop($parents); // The "rules" grouping.
|
853 |
|
|
|
854 |
|
|
$element = $form;
|
855 |
|
|
foreach ($parents as $key) {
|
856 |
|
|
if (!isset($element[$key])) {
|
857 |
|
|
// The entire conditional has been removed
|
858 |
|
|
return '';
|
859 |
|
|
}
|
860 |
|
|
$element = $element[$key];
|
861 |
|
|
}
|
862 |
|
|
|
863 |
|
|
return drupal_render($element['conditional']);
|
864 |
|
|
}
|
865 |
|
|
|
866 |
|
|
/**
|
867 |
|
|
* Theme the form for a conditional action.
|
868 |
|
|
*/
|
869 |
|
|
function theme_webform_conditional($variables) {
|
870 |
|
|
$element = $variables['element'];
|
871 |
|
|
|
872 |
|
|
$output = '';
|
873 |
|
|
$output .= '<div class="webform-conditional">';
|
874 |
|
|
$output .= '<span class="webform-conditional-if">' . t('If') . '</span>';
|
875 |
|
|
|
876 |
|
|
foreach (element_children($element['rules']) as $rid) {
|
877 |
e9984459
|
Assos Assos
|
$rule = &$element['rules'][$rid];
|
878 |
|
|
switch ($rule['source_type']['#value']) {
|
879 |
|
|
case 'conditional_start':
|
880 |
|
|
$source_phrase = '<div class="webform-subconditional">' . t('(') . '</div>';
|
881 |
|
|
break;
|
882 |
|
|
case 'conditional_end':
|
883 |
|
|
$source_phrase = '<div class="webform-subconditional">' . t(')') . '</div>';
|
884 |
|
|
break;
|
885 |
|
|
default:
|
886 |
|
|
// Hide labels.
|
887 |
|
|
$rule['source']['#title_display'] = 'none';
|
888 |
|
|
$rule['operator']['#title_display'] = 'none';
|
889 |
|
|
$rule['value']['#title_display'] = 'none';
|
890 |
|
|
|
891 |
|
|
$source = '<div class="webform-conditional-source">' . drupal_render($rule['source']) . '</div>';
|
892 |
|
|
$operator = '<div class="webform-conditional-operator">' . drupal_render($rule['operator']) . '</div>';
|
893 |
|
|
$value = '<div class="webform-conditional-value">' . drupal_render($rule['value']) . '</div>';
|
894 |
|
|
|
895 |
|
|
$source_phrase = t('!source !operator !value', array(
|
896 |
|
|
'!source' => $source,
|
897 |
|
|
'!operator' => $operator,
|
898 |
|
|
'!value' => $value,
|
899 |
|
|
));
|
900 |
|
|
// break
|
901 |
|
|
}
|
902 |
a45e4bc1
|
Assos Assos
|
|
903 |
|
|
$output .= '<div class="webform-conditional-rule">';
|
904 |
e9984459
|
Assos Assos
|
// Can't use theme('indentation') here because it causes the draghandle to
|
905 |
|
|
// be located after the last indentation div.
|
906 |
|
|
$output .= str_repeat('<div class="webform-indentation"> </div>', $rule['#level']);
|
907 |
|
|
$output .= drupal_render($rule['source_type']);
|
908 |
a45e4bc1
|
Assos Assos
|
$output .= '<div class="webform-container-inline webform-conditional-condition">';
|
909 |
|
|
$output .= $source_phrase;
|
910 |
|
|
$output .= '</div>';
|
911 |
|
|
|
912 |
e9984459
|
Assos Assos
|
if (isset($rule['andor'])) {
|
913 |
|
|
$rule['andor']['#title_display'] = 'none';
|
914 |
a45e4bc1
|
Assos Assos
|
$output .= '<div class="webform-conditional-andor webform-container-inline">';
|
915 |
e9984459
|
Assos Assos
|
$output .= drupal_render($rule['andor']);
|
916 |
a45e4bc1
|
Assos Assos
|
$output .= '</div>';
|
917 |
|
|
}
|
918 |
|
|
|
919 |
e9984459
|
Assos Assos
|
if (isset($rule['add']) || isset($rule['remove'])) {
|
920 |
a45e4bc1
|
Assos Assos
|
$output .= '<span class="webform-conditional-operations webform-container-inline">';
|
921 |
e9984459
|
Assos Assos
|
$output .= drupal_render($rule['add_subconditional']);
|
922 |
|
|
$output .= drupal_render($rule['add']);
|
923 |
|
|
$output .= drupal_render($rule['remove']);
|
924 |
a45e4bc1
|
Assos Assos
|
$output .= '</span>';
|
925 |
|
|
}
|
926 |
|
|
|
927 |
|
|
$output .= '</div>';
|
928 |
|
|
}
|
929 |
|
|
|
930 |
|
|
// Hide labels.
|
931 |
|
|
foreach (element_children($element['actions']) as $aid) {
|
932 |
|
|
// Hide labels.
|
933 |
|
|
$element['actions'][$aid]['target']['#title_display'] = 'none';
|
934 |
|
|
$element['actions'][$aid]['invert']['#title_display'] = 'none';
|
935 |
|
|
$element['actions'][$aid]['action']['#title_display'] = 'none';
|
936 |
|
|
$element['actions'][$aid]['argument']['#title_display'] = 'none';
|
937 |
|
|
|
938 |
|
|
$target = '<div class="webform-conditional-target">' . drupal_render($element['actions'][$aid]['target']) . '</div>';
|
939 |
|
|
$invert = '<div class="webform-conditional-invert">' . drupal_render($element['actions'][$aid]['invert']) . '</div>';
|
940 |
|
|
$action = '<div class="webform-conditional-action">' . drupal_render($element['actions'][$aid]['action']) . '</div>';
|
941 |
|
|
$argument = '<div class="webform-conditional-argument">' . drupal_render($element['actions'][$aid]['argument']) . '</div>';
|
942 |
|
|
|
943 |
|
|
$target_phrase = t('then !target !invert !action !argument', array(
|
944 |
|
|
'!target' => $target,
|
945 |
|
|
'!invert' => $invert,
|
946 |
|
|
'!action' => $action,
|
947 |
|
|
'!argument' => $argument,
|
948 |
|
|
));
|
949 |
|
|
|
950 |
|
|
$output .= '<div class="webform-conditional-action">';
|
951 |
|
|
$output .= '<div class="webform-container-inline webform-conditional-condition">';
|
952 |
|
|
$output .= $target_phrase;
|
953 |
|
|
$output .= '</div>';
|
954 |
|
|
|
955 |
|
|
if (isset($element['actions'][$aid]['add']) || isset($element['actions'][$aid]['remove'])) {
|
956 |
|
|
$output .= '<span class="webform-conditional-operations webform-container-inline">';
|
957 |
|
|
$output .= drupal_render($element['actions'][$aid]['add']);
|
958 |
e9984459
|
Assos Assos
|
$output .= drupal_render($element['actions'][$aid]['remove']);
|
959 |
a45e4bc1
|
Assos Assos
|
$output .= '</span>';
|
960 |
|
|
}
|
961 |
|
|
|
962 |
|
|
$output .= '</div>';
|
963 |
|
|
}
|
964 |
|
|
|
965 |
|
|
$output .= '</div>';
|
966 |
|
|
|
967 |
|
|
return $output;
|
968 |
|
|
}
|
969 |
|
|
|
970 |
|
|
/**
|
971 |
|
|
* Return a list of all Webform conditional operators.
|
972 |
|
|
*/
|
973 |
|
|
function webform_conditional_operators() {
|
974 |
|
|
static $operators;
|
975 |
|
|
|
976 |
|
|
if (!isset($operators)) {
|
977 |
|
|
$operators = module_invoke_all('webform_conditional_operator_info');
|
978 |
|
|
drupal_alter('webform_conditional_operators', $operators);
|
979 |
|
|
}
|
980 |
|
|
|
981 |
|
|
return $operators;
|
982 |
|
|
}
|
983 |
|
|
|
984 |
|
|
/**
|
985 |
|
|
* Return a nested list of all available operators, suitable for a select list.
|
986 |
|
|
*/
|
987 |
|
|
function webform_conditional_operators_list() {
|
988 |
|
|
$options = array();
|
989 |
|
|
$operators = webform_conditional_operators();
|
990 |
|
|
|
991 |
|
|
foreach ($operators as $data_type => $type_operators) {
|
992 |
|
|
$options[$data_type] = array();
|
993 |
|
|
foreach ($type_operators as $operator => $operator_info) {
|
994 |
|
|
$options[$data_type][$operator] = $operator_info['label'];
|
995 |
|
|
}
|
996 |
|
|
}
|
997 |
|
|
|
998 |
|
|
return $options;
|
999 |
|
|
}
|
1000 |
|
|
|
1001 |
|
|
/**
|
1002 |
|
|
* Internal implementation of hook_webform_conditional_operator_info().
|
1003 |
|
|
*
|
1004 |
|
|
* Called from webform.module's webform_webform_conditional_operator_info().
|
1005 |
|
|
*/
|
1006 |
|
|
function _webform_conditional_operator_info() {
|
1007 |
|
|
// General operators:
|
1008 |
|
|
$operators['string']['equal'] = array(
|
1009 |
|
|
'label' => t('is'),
|
1010 |
|
|
'comparison callback' => 'webform_conditional_operator_string_equal',
|
1011 |
|
|
'js comparison callback' => 'conditionalOperatorStringEqual',
|
1012 |
|
|
// A form callback is not needed here, since we can use the default,
|
1013 |
|
|
// non-JavaScript textfield for all text and numeric fields.
|
1014 |
|
|
// 'form callback' => 'webform_conditional_operator_text',
|
1015 |
|
|
);
|
1016 |
|
|
$operators['string']['not_equal'] = array(
|
1017 |
|
|
'label' => t('is not'),
|
1018 |
|
|
'comparison callback' => 'webform_conditional_operator_string_not_equal',
|
1019 |
|
|
'js comparison callback' => 'conditionalOperatorStringNotEqual',
|
1020 |
|
|
);
|
1021 |
|
|
$operators['string']['contains'] = array(
|
1022 |
|
|
'label' => t('contains'),
|
1023 |
|
|
'comparison callback' => 'webform_conditional_operator_string_contains',
|
1024 |
|
|
'js comparison callback' => 'conditionalOperatorStringContains',
|
1025 |
|
|
);
|
1026 |
|
|
$operators['string']['does_not_contain'] = array(
|
1027 |
|
|
'label' => t('does not contain'),
|
1028 |
|
|
'comparison callback' => 'webform_conditional_operator_string_does_not_contain',
|
1029 |
|
|
'js comparison callback' => 'conditionalOperatorStringDoesNotContain',
|
1030 |
|
|
);
|
1031 |
|
|
$operators['string']['begins_with'] = array(
|
1032 |
|
|
'label' => t('begins with'),
|
1033 |
|
|
'comparison callback' => 'webform_conditional_operator_string_begins_with',
|
1034 |
|
|
'js comparison callback' => 'conditionalOperatorStringBeginsWith',
|
1035 |
|
|
);
|
1036 |
|
|
$operators['string']['ends_with'] = array(
|
1037 |
|
|
'label' => t('ends with'),
|
1038 |
|
|
'comparison callback' => 'webform_conditional_operator_string_ends_with',
|
1039 |
|
|
'js comparison callback' => 'conditionalOperatorStringEndsWith',
|
1040 |
|
|
);
|
1041 |
|
|
$operators['string']['empty'] = array(
|
1042 |
|
|
'label' => t('is blank'),
|
1043 |
|
|
'comparison callback' => 'webform_conditional_operator_string_empty',
|
1044 |
|
|
'js comparison callback' => 'conditionalOperatorStringEmpty',
|
1045 |
|
|
'form callback' => FALSE, // No value form at all.
|
1046 |
|
|
);
|
1047 |
|
|
$operators['string']['not_empty'] = array(
|
1048 |
|
|
'label' => t('is not blank'),
|
1049 |
|
|
'comparison callback' => 'webform_conditional_operator_string_not_empty',
|
1050 |
|
|
'js comparison callback' => 'conditionalOperatorStringNotEmpty',
|
1051 |
|
|
'form callback' => FALSE, // No value form at all.
|
1052 |
|
|
);
|
1053 |
|
|
|
1054 |
|
|
// Numeric operators.
|
1055 |
|
|
$operators['numeric']['equal'] = array(
|
1056 |
|
|
'label' => t('is equal to'),
|
1057 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_equal',
|
1058 |
|
|
'js comparison callback' => 'conditionalOperatorNumericEqual',
|
1059 |
|
|
);
|
1060 |
|
|
$operators['numeric']['not_equal'] = array(
|
1061 |
|
|
'label' => t('is not equal to'),
|
1062 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_not_equal',
|
1063 |
|
|
'js comparison callback' => 'conditionalOperatorNumericNotEqual',
|
1064 |
|
|
);
|
1065 |
|
|
$operators['numeric']['less_than'] = array(
|
1066 |
|
|
'label' => t('is less than'),
|
1067 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_less_than',
|
1068 |
|
|
'js comparison callback' => 'conditionalOperatorNumericLessThan',
|
1069 |
|
|
);
|
1070 |
|
|
$operators['numeric']['less_than_equal'] = array(
|
1071 |
|
|
'label' => t('is less than or equal'),
|
1072 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_less_than_equal',
|
1073 |
|
|
'js comparison callback' => 'conditionalOperatorNumericLessThanEqual',
|
1074 |
|
|
);
|
1075 |
|
|
$operators['numeric']['greater_than'] = array(
|
1076 |
|
|
'label' => t('is greater than'),
|
1077 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_greater_than',
|
1078 |
|
|
'js comparison callback' => 'conditionalOperatorNumericGreaterThan',
|
1079 |
|
|
);
|
1080 |
|
|
$operators['numeric']['greater_than_equal'] = array(
|
1081 |
|
|
'label' => t('is greater than or equal'),
|
1082 |
|
|
'comparison callback' => 'webform_conditional_operator_numeric_greater_than_equal',
|
1083 |
|
|
'js comparison callback' => 'conditionalOperatorNumericGreaterThanEqual',
|
1084 |
|
|
);
|
1085 |
|
|
$operators['numeric']['empty'] = array(
|
1086 |
|
|
'label' => t('is blank'),
|
1087 |
|
|
'comparison callback' => 'webform_conditional_operator_string_empty',
|
1088 |
|
|
'js comparison callback' => 'conditionalOperatorStringEmpty',
|
1089 |
|
|
'form callback' => FALSE, // No value form at all.
|
1090 |
|
|
);
|
1091 |
|
|
$operators['numeric']['not_empty'] = array(
|
1092 |
|
|
'label' => t('is not blank'),
|
1093 |
|
|
'comparison callback' => 'webform_conditional_operator_string_not_empty',
|
1094 |
|
|
'js comparison callback' => 'conditionalOperatorStringNotEmpty',
|
1095 |
|
|
'form callback' => FALSE, // No value form at all.
|
1096 |
|
|
);
|
1097 |
|
|
|
1098 |
|
|
// Select operators.
|
1099 |
|
|
$operators['select']['equal'] = array(
|
1100 |
|
|
'label' => t('is'),
|
1101 |
|
|
'comparison callback' => 'webform_conditional_operator_string_equal',
|
1102 |
|
|
'js comparison callback' => 'conditionalOperatorStringEqual',
|
1103 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1104 |
|
|
);
|
1105 |
|
|
$operators['select']['not_equal'] = array(
|
1106 |
|
|
'label' => t('is not'),
|
1107 |
|
|
'comparison callback' => 'webform_conditional_operator_string_not_equal',
|
1108 |
|
|
'js comparison callback' => 'conditionalOperatorStringNotEqual',
|
1109 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1110 |
|
|
);
|
1111 |
|
|
$operators['select']['less_than'] = array(
|
1112 |
|
|
'label' => t('is before'),
|
1113 |
|
|
'comparison callback' => 'webform_conditional_operator_select_less_than',
|
1114 |
|
|
'js comparison callback' => 'conditionalOperatorSelectLessThan',
|
1115 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1116 |
|
|
);
|
1117 |
|
|
$operators['select']['less_than_equal'] = array(
|
1118 |
|
|
'label' => t('is or is before'),
|
1119 |
|
|
'comparison callback' => 'webform_conditional_operator_select_less_than_equal',
|
1120 |
|
|
'js comparison callback' => 'conditionalOperatorSelectLessThanEqual',
|
1121 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1122 |
|
|
);
|
1123 |
|
|
$operators['select']['greater_than'] = array(
|
1124 |
|
|
'label' => t('is after'),
|
1125 |
|
|
'comparison callback' => 'webform_conditional_operator_select_greater_than',
|
1126 |
|
|
'js comparison callback' => 'conditionalOperatorSelectGreaterThan',
|
1127 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1128 |
|
|
);
|
1129 |
|
|
$operators['select']['greater_than_equal'] = array(
|
1130 |
|
|
'label' => t('is or is after'),
|
1131 |
|
|
'comparison callback' => 'webform_conditional_operator_select_greater_than_equal',
|
1132 |
|
|
'js comparison callback' => 'conditionalOperatorSelectGreaterThanEqual',
|
1133 |
|
|
'form callback' => 'webform_conditional_form_select',
|
1134 |
|
|
);
|
1135 |
|
|
$operators['select']['empty'] = array(
|
1136 |
|
|
'label' => t('is empty'),
|
1137 |
|
|
'comparison callback' => 'webform_conditional_operator_string_empty',
|
1138 |
|
|
'js comparison callback' => 'conditionalOperatorStringEmpty',
|
1139 |
|
|
'form callback' => FALSE, // No value form at all.
|
1140 |
|
|
);
|
1141 |
|
|
$operators['select']['not_empty'] = array(
|
1142 |
|
|
'label' => t('is not empty'),
|
1143 |
|
|
'comparison callback' => 'webform_conditional_operator_string_not_empty',
|
1144 |
|
|
'js comparison callback' => 'conditionalOperatorStringNotEmpty',
|
1145 |
|
|
'form callback' => FALSE, // No value form at all.
|
1146 |
|
|
);
|
1147 |
|
|
|
1148 |
|
|
// Date operators:
|
1149 |
|
|
$operators['date']['equal'] = array(
|
1150 |
|
|
'label' => t('is on'),
|
1151 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_equal',
|
1152 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1153 |
|
|
'js comparison callback' => 'conditionalOperatorDateEqual',
|
1154 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1155 |
|
|
);
|
1156 |
|
|
$operators['date']['not_equal'] = array(
|
1157 |
|
|
'label' => t('is not on'),
|
1158 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
|
1159 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1160 |
|
|
'js comparison callback' => 'conditionalOperatorDateNotEqual',
|
1161 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1162 |
|
|
);
|
1163 |
|
|
$operators['date']['before'] = array(
|
1164 |
|
|
'label' => t('is before'),
|
1165 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_before',
|
1166 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1167 |
|
|
'js comparison callback' => 'conditionalOperatorDateBefore',
|
1168 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1169 |
|
|
);
|
1170 |
|
|
$operators['date']['before_equal'] = array(
|
1171 |
|
|
'label' => t('is on or before'),
|
1172 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
|
1173 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1174 |
|
|
'js comparison callback' => 'conditionalOperatorDateBeforeEqual',
|
1175 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1176 |
|
|
);
|
1177 |
|
|
$operators['date']['after'] = array(
|
1178 |
|
|
'label' => t('is after'),
|
1179 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_after',
|
1180 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1181 |
|
|
'js comparison callback' => 'conditionalOperatorDateAfter',
|
1182 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1183 |
|
|
);
|
1184 |
|
|
$operators['date']['after_equal'] = array(
|
1185 |
|
|
'label' => t('is on or after'),
|
1186 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
|
1187 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_date_js',
|
1188 |
|
|
'js comparison callback' => 'conditionalOperatorDateAfterEqual',
|
1189 |
|
|
'form callback' => 'webform_conditional_form_date',
|
1190 |
|
|
);
|
1191 |
|
|
|
1192 |
|
|
// Time operators:
|
1193 |
|
|
$operators['time']['equal'] = array(
|
1194 |
|
|
'label' => t('is at'),
|
1195 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_equal',
|
1196 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1197 |
|
|
'js comparison callback' => 'conditionalOperatorTimeEqual',
|
1198 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1199 |
|
|
);
|
1200 |
|
|
$operators['time']['not_equal'] = array(
|
1201 |
|
|
'label' => t('is not at'),
|
1202 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_not_equal',
|
1203 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1204 |
|
|
'js comparison callback' => 'conditionalOperatorTimeNotEqual',
|
1205 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1206 |
|
|
);
|
1207 |
|
|
$operators['time']['before'] = array(
|
1208 |
|
|
'label' => t('is before'),
|
1209 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_before',
|
1210 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1211 |
|
|
'js comparison callback' => 'conditionalOperatorTimeBefore',
|
1212 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1213 |
|
|
);
|
1214 |
|
|
$operators['time']['before_equal'] = array(
|
1215 |
|
|
'label' => t('is at or before'),
|
1216 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_before_equal',
|
1217 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1218 |
|
|
'js comparison callback' => 'conditionalOperatorTimeBeforeEqual',
|
1219 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1220 |
|
|
);
|
1221 |
|
|
$operators['time']['after'] = array(
|
1222 |
|
|
'label' => t('is after'),
|
1223 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_after',
|
1224 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1225 |
|
|
'js comparison callback' => 'conditionalOperatorTimeAfter',
|
1226 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1227 |
|
|
);
|
1228 |
|
|
$operators['time']['after_equal'] = array(
|
1229 |
|
|
'label' => t('is at or after'),
|
1230 |
|
|
'comparison callback' => 'webform_conditional_operator_datetime_after_equal',
|
1231 |
|
|
'comparison prepare js' => 'webform_conditional_prepare_time_js',
|
1232 |
|
|
'js comparison callback' => 'conditionalOperatorTimeAfterEqual',
|
1233 |
|
|
'form callback' => 'webform_conditional_form_time',
|
1234 |
|
|
);
|
1235 |
|
|
|
1236 |
|
|
return $operators;
|
1237 |
|
|
}
|
1238 |
|
|
|
1239 |
|
|
/**
|
1240 |
|
|
* Form callback for select-type conditional fields.
|
1241 |
|
|
*
|
1242 |
|
|
* Unlike other built-in conditional value forms, the form callback for select
|
1243 |
|
|
* types provides an array of forms, keyed by the $cid, which is the "source"
|
1244 |
|
|
* for the condition.
|
1245 |
|
|
*/
|
1246 |
|
|
function webform_conditional_form_select($node) {
|
1247 |
|
|
static $count = 0;
|
1248 |
|
|
$forms = array();
|
1249 |
|
|
webform_component_include('select');
|
1250 |
|
|
foreach ($node->webform['components'] as $cid => $component) {
|
1251 |
|
|
if (webform_component_property($component['type'], 'conditional_type') == 'select') {
|
1252 |
|
|
// TODO: Use a pluggable mechanism for retrieving select list values.
|
1253 |
|
|
$options = _webform_select_options($component);
|
1254 |
|
|
$element = array(
|
1255 |
|
|
'#type' => 'select',
|
1256 |
|
|
'#multiple' => FALSE,
|
1257 |
|
|
'#size' => NULL,
|
1258 |
|
|
'#attributes' => array(),
|
1259 |
|
|
'#id' => NULL,
|
1260 |
|
|
'#name' => 'webform-conditional-select-' . $cid . '-' . $count,
|
1261 |
|
|
'#options' => $options,
|
1262 |
|
|
'#parents' => array(),
|
1263 |
|
|
);
|
1264 |
|
|
$forms[$cid] = drupal_render($element);
|
1265 |
|
|
}
|
1266 |
|
|
}
|
1267 |
|
|
$count++;
|
1268 |
|
|
return $forms;
|
1269 |
|
|
}
|
1270 |
|
|
|
1271 |
|
|
/**
|
1272 |
|
|
* Form callback for date conditional fields.
|
1273 |
|
|
*/
|
1274 |
|
|
function webform_conditional_form_date($node) {
|
1275 |
|
|
static $count = 0;
|
1276 |
|
|
$element = array(
|
1277 |
|
|
'#title' => NULL,
|
1278 |
|
|
'#title_display' => 'none',
|
1279 |
|
|
'#size' => 24,
|
1280 |
|
|
'#attributes' => array('placeholder' => t('@format or valid date', array('@format' => webform_date_format('short')))),
|
1281 |
|
|
'#type' => 'textfield',
|
1282 |
|
|
'#name' => 'webform-conditional-date-' . $count++,
|
1283 |
|
|
);
|
1284 |
|
|
return drupal_render($element);
|
1285 |
|
|
}
|
1286 |
|
|
|
1287 |
|
|
/**
|
1288 |
|
|
* Form callback for time conditional fields.
|
1289 |
|
|
*/
|
1290 |
|
|
function webform_conditional_form_time($node) {
|
1291 |
|
|
static $count = 0;
|
1292 |
|
|
$element = array(
|
1293 |
|
|
'#title' => NULL,
|
1294 |
|
|
'#title_display' => 'none',
|
1295 |
|
|
'#size' => 24,
|
1296 |
|
|
'#attributes' => array('placeholder' => t('HH:MMam or valid time')),
|
1297 |
|
|
'#type' => 'textfield',
|
1298 |
|
|
'#name' => 'webform-conditional-time-' . $count++,
|
1299 |
|
|
);
|
1300 |
|
|
return drupal_render($element);
|
1301 |
|
|
}
|
1302 |
|
|
|
1303 |
|
|
/**
|
1304 |
|
|
* Load a conditional setting from the database.
|
1305 |
|
|
*/
|
1306 |
|
|
function webform_conditional_load($rgid, $nid) {
|
1307 |
|
|
$node = node_load($nid);
|
1308 |
|
|
|
1309 |
|
|
$conditional = isset($node->webform['conditionals'][$rgid]) ? $node->webform['conditionals'][$rgid] : FALSE;
|
1310 |
|
|
|
1311 |
|
|
return $conditional;
|
1312 |
|
|
}
|
1313 |
|
|
|
1314 |
|
|
/**
|
1315 |
|
|
* Insert a conditional rule group into the database.
|
1316 |
|
|
*/
|
1317 |
|
|
function webform_conditional_insert($conditional) {
|
1318 |
|
|
drupal_write_record('webform_conditional', $conditional);
|
1319 |
|
|
foreach ($conditional['rules'] as $rid => $rule) {
|
1320 |
|
|
$rule['nid'] = $conditional['nid'];
|
1321 |
|
|
$rule['rgid'] = $conditional['rgid'];
|
1322 |
|
|
$rule['rid'] = $rid;
|
1323 |
|
|
drupal_write_record('webform_conditional_rules', $rule);
|
1324 |
|
|
}
|
1325 |
|
|
foreach ($conditional['actions'] as $aid => $action) {
|
1326 |
|
|
$action['nid'] = $conditional['nid'];
|
1327 |
|
|
$action['rgid'] = $conditional['rgid'];
|
1328 |
|
|
$action['aid'] = $aid;
|
1329 |
|
|
drupal_write_record('webform_conditional_actions', $action);
|
1330 |
|
|
}
|
1331 |
|
|
}
|
1332 |
|
|
|
1333 |
|
|
/**
|
1334 |
|
|
* Update a conditional setting in the database.
|
1335 |
|
|
*/
|
1336 |
|
|
function webform_conditional_update($node, $conditional) {
|
1337 |
|
|
webform_conditional_delete($node, $conditional);
|
1338 |
|
|
webform_conditional_insert($conditional);
|
1339 |
|
|
}
|
1340 |
|
|
|
1341 |
|
|
/**
|
1342 |
|
|
* Delete a conditional rule group.
|
1343 |
|
|
*/
|
1344 |
|
|
function webform_conditional_delete($node, $conditional) {
|
1345 |
|
|
db_delete('webform_conditional')
|
1346 |
|
|
->condition('nid', $node->nid)
|
1347 |
|
|
->condition('rgid', $conditional['rgid'])
|
1348 |
|
|
->execute();
|
1349 |
|
|
db_delete('webform_conditional_rules')
|
1350 |
|
|
->condition('nid', $node->nid)
|
1351 |
|
|
->condition('rgid', $conditional['rgid'])
|
1352 |
|
|
->execute();
|
1353 |
|
|
db_delete('webform_conditional_actions')
|
1354 |
|
|
->condition('nid', $node->nid)
|
1355 |
|
|
->condition('rgid', $conditional['rgid'])
|
1356 |
|
|
->execute();
|
1357 |
|
|
}
|
1358 |
|
|
|
1359 |
|
|
/**
|
1360 |
|
|
* Loop through all the conditional settings and add needed JavaScript settings.
|
1361 |
|
|
*
|
1362 |
|
|
* We do a bit of optimization for JavaScript before adding to the page as
|
1363 |
|
|
* settings. We remove unnecessary data structures and provide a "source map"
|
1364 |
|
|
* so that JavaScript can quickly determine if it needs to check rules when a
|
1365 |
|
|
* field on the page has been modified.
|
1366 |
|
|
*
|
1367 |
|
|
* @param object $node
|
1368 |
|
|
* The loaded node object, containing the webform.
|
1369 |
|
|
* @param array $submission_data
|
1370 |
|
|
* The cid-indexed array of existing submission values to be included for
|
1371 |
|
|
* sources outside of the current page.
|
1372 |
|
|
* @param integer $page_num
|
1373 |
|
|
* The number of the page for which javascript settings should be generated.
|
1374 |
|
|
* @return array
|
1375 |
|
|
* Array of settings to be send to the browser as javascript settings.
|
1376 |
|
|
*/
|
1377 |
|
|
function webform_conditional_prepare_javascript($node, $submission_data, $page_num) {
|
1378 |
|
|
$settings = array(
|
1379 |
|
|
'ruleGroups' => array(),
|
1380 |
|
|
'sourceMap' => array(),
|
1381 |
|
|
'values' => array(),
|
1382 |
|
|
);
|
1383 |
|
|
$operators = webform_conditional_operators();
|
1384 |
|
|
$conditionals = $node->webform['conditionals'];
|
1385 |
|
|
$components = $node->webform['components'];
|
1386 |
|
|
$topological_order = webform_get_conditional_sorter($node)->getOrder();
|
1387 |
|
|
foreach ($topological_order[$page_num] as $conditional_spec) {
|
1388 |
|
|
$conditional = $conditionals[$conditional_spec['rgid']];
|
1389 |
|
|
$rgid_key = 'rgid_' . $conditional['rgid'];
|
1390 |
|
|
// Assemble the main conditional group settings.
|
1391 |
|
|
|
1392 |
|
|
$settings['ruleGroups'][$rgid_key] = array(
|
1393 |
|
|
'andor' => $conditional['andor'],
|
1394 |
|
|
);
|
1395 |
|
|
foreach ($conditional['actions'] as $action) {
|
1396 |
|
|
if ($action['target_type'] == 'component') {
|
1397 |
|
|
$target_component = $components[$action['target']];
|
1398 |
|
|
$target_parents = webform_component_parent_keys($node, $target_component);
|
1399 |
|
|
$aid_key = 'aid_' . $action['aid'];
|
1400 |
|
|
$action_settings = array(
|
1401 |
|
|
'target' => 'webform-component--' . str_replace('_', '-', implode('--', $target_parents)),
|
1402 |
|
|
'invert' => (int)$action['invert'],
|
1403 |
|
|
'action' => $action['action'],
|
1404 |
|
|
'argument' => $components[$action['target']]['type'] == 'markup' ? filter_xss_admin($action['argument']) : $action['argument'],
|
1405 |
|
|
);
|
1406 |
|
|
$settings['ruleGroups'][$rgid_key]['actions'][$aid_key] = $action_settings;
|
1407 |
|
|
}
|
1408 |
|
|
}
|
1409 |
|
|
// Add on the list of rules to the conditional group.
|
1410 |
|
|
foreach ($conditional['rules'] as $rule) {
|
1411 |
e9984459
|
Assos Assos
|
$rid_key = 'rid_' . $rule['rid'];
|
1412 |
|
|
switch ($rule['source_type']) {
|
1413 |
|
|
case 'component':
|
1414 |
|
|
$source_component = $components[$rule['source']];
|
1415 |
|
|
$source_parents = webform_component_parent_keys($node, $source_component);
|
1416 |
|
|
$source_id = 'webform-component--' . str_replace('_', '-', implode('--', $source_parents));
|
1417 |
|
|
|
1418 |
|
|
// If this source has a value set, add that as a setting.
|
1419 |
|
|
// NULL or array(NULL) should be sent as an empty array to simplify the jQuery.
|
1420 |
|
|
if (isset($submission_data[$source_component['cid']])) {
|
1421 |
|
|
$source_value = $submission_data[$source_component['cid']];
|
1422 |
|
|
$source_value = is_array($source_value) ? $source_value : array($source_value);
|
1423 |
|
|
$settings['values'][$source_id] = $source_value === array(NULL) ? array() : $source_value;
|
1424 |
|
|
}
|
1425 |
a45e4bc1
|
Assos Assos
|
|
1426 |
e9984459
|
Assos Assos
|
$conditional_type = webform_component_property($source_component['type'], 'conditional_type');
|
1427 |
|
|
$operator_info = $operators[$conditional_type][$rule['operator']];
|
1428 |
|
|
$rule_settings = array(
|
1429 |
|
|
'source_type' => $rule['source_type'],
|
1430 |
|
|
'source' => $source_id,
|
1431 |
|
|
'value' => $rule['value'],
|
1432 |
|
|
'callback' => $operator_info['js comparison callback'],
|
1433 |
|
|
);
|
1434 |
|
|
if (isset($operator_info['comparison prepare js'])) {
|
1435 |
|
|
$callback = $operator_info['comparison prepare js'];
|
1436 |
|
|
$rule_settings['value'] = $callback($rule['value']);
|
1437 |
|
|
}
|
1438 |
|
|
$settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = $rule_settings;
|
1439 |
|
|
$settings['sourceMap'][$source_id][$rgid_key] = $rgid_key;
|
1440 |
|
|
break;
|
1441 |
|
|
case 'conditional_start':
|
1442 |
|
|
$settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
|
1443 |
|
|
'source_type' => $rule['source_type'],
|
1444 |
|
|
'andor' => $rule['operator'],
|
1445 |
|
|
);
|
1446 |
|
|
break;
|
1447 |
|
|
case 'conditional_end':
|
1448 |
|
|
$settings['ruleGroups'][$rgid_key]['rules'][$rid_key] = array(
|
1449 |
|
|
'source_type' => $rule['source_type'],
|
1450 |
|
|
);
|
1451 |
|
|
break;
|
1452 |
a45e4bc1
|
Assos Assos
|
}
|
1453 |
|
|
}
|
1454 |
|
|
}
|
1455 |
|
|
|
1456 |
|
|
return $settings;
|
1457 |
|
|
}
|
1458 |
|
|
|
1459 |
|
|
/**
|
1460 |
|
|
* Determine whether a component type is capable of a given conditional action.
|
1461 |
|
|
*/
|
1462 |
|
|
function webform_conditional_action_able($component_type, $action) {
|
1463 |
|
|
switch ($action) {
|
1464 |
|
|
case 'show':
|
1465 |
|
|
return TRUE;
|
1466 |
|
|
// break;
|
1467 |
|
|
case 'require':
|
1468 |
|
|
return webform_component_feature($component_type, 'required');
|
1469 |
|
|
// break;
|
1470 |
|
|
default:
|
1471 |
|
|
return webform_component_feature($component_type, "conditional_action_$action");
|
1472 |
|
|
// break;
|
1473 |
|
|
}
|
1474 |
|
|
}
|
1475 |
|
|
|
1476 |
|
|
/**
|
1477 |
|
|
* Prepare a conditional value for adding as a JavaScript setting.
|
1478 |
|
|
*/
|
1479 |
|
|
function webform_conditional_prepare_date_js($rule_value) {
|
1480 |
|
|
// Convert the time/date string to a UTC timestamp for comparison. Note that
|
1481 |
|
|
// this means comparisons against immediate times (such as "now") may be
|
1482 |
|
|
// slightly stale by the time the comparison executes. Timestamps are in
|
1483 |
|
|
// milliseconds, as to match JavaScript's Date.toString() method.
|
1484 |
|
|
$date = webform_strtodate('c', $rule_value, 'UTC');
|
1485 |
|
|
return webform_strtotime($date);
|
1486 |
|
|
}
|
1487 |
|
|
|
1488 |
|
|
/**
|
1489 |
|
|
* Prepare a conditional value for adding as a JavaScript setting.
|
1490 |
|
|
*/
|
1491 |
|
|
function webform_conditional_prepare_time_js($rule_value) {
|
1492 |
|
|
$date = webform_conditional_prepare_date_js($rule_value);
|
1493 |
|
|
$today = webform_strtodate('c', 'today', 'UTC');
|
1494 |
|
|
$today = webform_strtotime($today);
|
1495 |
|
|
return $date - $today;
|
1496 |
|
|
}
|
1497 |
|
|
|
1498 |
|
|
/**
|
1499 |
|
|
* Conditional callback for string comparisons.
|
1500 |
|
|
*/
|
1501 |
|
|
function webform_conditional_operator_string_equal($input_values, $rule_value) {
|
1502 |
|
|
foreach ($input_values as $value) {
|
1503 |
|
|
// Checkbox values come in as 0 integers for unchecked boxes.
|
1504 |
|
|
$value = ($value === 0) ? '' : $value;
|
1505 |
|
|
if (strcasecmp($value, $rule_value) === 0) {
|
1506 |
|
|
return TRUE;
|
1507 |
|
|
}
|
1508 |
|
|
}
|
1509 |
|
|
return FALSE;
|
1510 |
|
|
}
|
1511 |
|
|
|
1512 |
|
|
/**
|
1513 |
|
|
* Conditional callback for string comparisons.
|
1514 |
|
|
*/
|
1515 |
|
|
function webform_conditional_operator_string_not_equal($input_values, $rule_value) {
|
1516 |
|
|
return !webform_conditional_operator_string_equal($input_values, $rule_value);
|
1517 |
|
|
}
|
1518 |
|
|
|
1519 |
|
|
/**
|
1520 |
|
|
* Conditional callback for string comparisons.
|
1521 |
|
|
*/
|
1522 |
|
|
function webform_conditional_operator_string_contains($input_values, $rule_value) {
|
1523 |
|
|
foreach ($input_values as $value) {
|
1524 |
|
|
if (stripos($value, $rule_value) !== FALSE) {
|
1525 |
|
|
return TRUE;
|
1526 |
|
|
}
|
1527 |
|
|
}
|
1528 |
|
|
return FALSE;
|
1529 |
|
|
}
|
1530 |
|
|
|
1531 |
|
|
/**
|
1532 |
|
|
* Conditional callback for string comparisons.
|
1533 |
|
|
*/
|
1534 |
|
|
function webform_conditional_operator_string_does_not_contain($input_values, $rule_value) {
|
1535 |
|
|
return !webform_conditional_operator_string_contains($input_values, $rule_value);
|
1536 |
|
|
}
|
1537 |
|
|
|
1538 |
|
|
/**
|
1539 |
|
|
* Conditional callback for string comparisons.
|
1540 |
|
|
*/
|
1541 |
|
|
function webform_conditional_operator_string_begins_with($input_values, $rule_value) {
|
1542 |
|
|
foreach ($input_values as $value) {
|
1543 |
|
|
if (stripos($value, $rule_value) === 0) {
|
1544 |
|
|
return TRUE;
|
1545 |
|
|
}
|
1546 |
|
|
}
|
1547 |
|
|
return FALSE;
|
1548 |
|
|
}
|
1549 |
|
|
|
1550 |
|
|
/**
|
1551 |
|
|
* Conditional callback for string comparisons.
|
1552 |
|
|
*/
|
1553 |
|
|
function webform_conditional_operator_string_ends_with($input_values, $rule_value) {
|
1554 |
|
|
foreach ($input_values as $value) {
|
1555 |
|
|
if (strripos($value, $rule_value) === strlen($value) - strlen($rule_value)) {
|
1556 |
|
|
return TRUE;
|
1557 |
|
|
}
|
1558 |
|
|
}
|
1559 |
|
|
return FALSE;
|
1560 |
|
|
}
|
1561 |
|
|
|
1562 |
|
|
/**
|
1563 |
|
|
* Conditional callback for checking for empty fields.
|
1564 |
|
|
*/
|
1565 |
|
|
function webform_conditional_operator_string_empty($input_values, $rule_value) {
|
1566 |
|
|
$empty = TRUE;
|
1567 |
|
|
foreach ($input_values as $value) {
|
1568 |
|
|
if ($value !== '' && $value !== NULL && $value !== 0) {
|
1569 |
|
|
$empty = FALSE;
|
1570 |
|
|
break;
|
1571 |
|
|
}
|
1572 |
|
|
}
|
1573 |
|
|
return $empty;
|
1574 |
|
|
}
|
1575 |
|
|
|
1576 |
|
|
/**
|
1577 |
|
|
* Conditional callback for checking for empty fields.
|
1578 |
|
|
*/
|
1579 |
|
|
function webform_conditional_operator_string_not_empty($input_values, $rule_value) {
|
1580 |
|
|
return !webform_conditional_operator_string_empty($input_values, $rule_value);
|
1581 |
|
|
}
|
1582 |
|
|
|
1583 |
|
|
/**
|
1584 |
|
|
* Conditional callback for select comparisons.
|
1585 |
|
|
*/
|
1586 |
|
|
function webform_conditional_operator_select_less_than($input_values, $rule_value, $component) {
|
1587 |
|
|
return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) < 0;
|
1588 |
|
|
}
|
1589 |
|
|
|
1590 |
|
|
/**
|
1591 |
|
|
* Conditional callback for select comparisons.
|
1592 |
|
|
*/
|
1593 |
|
|
function webform_conditional_operator_select_less_than_equal($input_values, $rule_value, $component) {
|
1594 |
|
|
$comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
|
1595 |
|
|
return $comparison < 0 || $comparison === 0;
|
1596 |
|
|
}
|
1597 |
|
|
|
1598 |
|
|
/**
|
1599 |
|
|
* Conditional callback for select comparisons.
|
1600 |
|
|
*/
|
1601 |
|
|
function webform_conditional_operator_select_greater_than($input_values, $rule_value, $component) {
|
1602 |
|
|
return empty($input_values) ? FALSE : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE)) > 0;
|
1603 |
|
|
}
|
1604 |
|
|
|
1605 |
|
|
/**
|
1606 |
|
|
* Conditional callback for select comparisons.
|
1607 |
|
|
*/
|
1608 |
|
|
function webform_conditional_operator_select_greater_than_equal($input_values, $rule_value, $component) {
|
1609 |
|
|
$comparison = empty($input_values) ? NULL : webform_compare_select($input_values[0], $rule_value, _webform_select_options($component, TRUE));
|
1610 |
|
|
return $comparison > 0 || $comparison === 0;
|
1611 |
|
|
}
|
1612 |
|
|
|
1613 |
|
|
/**
|
1614 |
|
|
* Conditional callback for numeric comparisons.
|
1615 |
|
|
*/
|
1616 |
|
|
function webform_conditional_operator_numeric_equal($input_values, $rule_value) {
|
1617 |
|
|
return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) === 0;
|
1618 |
|
|
}
|
1619 |
|
|
|
1620 |
|
|
/**
|
1621 |
|
|
* Conditional callback for numeric comparisons.
|
1622 |
|
|
*/
|
1623 |
|
|
function webform_conditional_operator_numeric_not_equal($input_values, $rule_value) {
|
1624 |
|
|
return !webform_conditional_operator_numeric_equal($input_values, $rule_value);
|
1625 |
|
|
}
|
1626 |
|
|
|
1627 |
|
|
/**
|
1628 |
|
|
* Conditional callback for numeric comparisons.
|
1629 |
|
|
*/
|
1630 |
|
|
function webform_conditional_operator_numeric_less_than($input_values, $rule_value) {
|
1631 |
|
|
return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) < 0;
|
1632 |
|
|
}
|
1633 |
|
|
|
1634 |
|
|
/**
|
1635 |
|
|
* Conditional callback for numeric comparisons.
|
1636 |
|
|
*/
|
1637 |
|
|
function webform_conditional_operator_numeric_less_than_equal($input_values, $rule_value) {
|
1638 |
|
|
$comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
|
1639 |
|
|
return $comparison < 0 || $comparison === 0;
|
1640 |
|
|
}
|
1641 |
|
|
|
1642 |
|
|
/**
|
1643 |
|
|
* Conditional callback for numeric comparisons.
|
1644 |
|
|
*/
|
1645 |
|
|
function webform_conditional_operator_numeric_greater_than($input_values, $rule_value) {
|
1646 |
|
|
return empty($input_values) ? FALSE : webform_compare_floats($input_values[0], $rule_value) > 0;
|
1647 |
|
|
}
|
1648 |
|
|
|
1649 |
|
|
/**
|
1650 |
|
|
* Conditional callback for numeric comparisons.
|
1651 |
|
|
*/
|
1652 |
|
|
function webform_conditional_operator_numeric_greater_than_equal($input_values, $rule_value) {
|
1653 |
|
|
$comparison = empty($input_values) ? NULL : webform_compare_floats($input_values[0], $rule_value);
|
1654 |
|
|
return $comparison > 0 || $comparison === 0;
|
1655 |
|
|
}
|
1656 |
|
|
|
1657 |
|
|
/**
|
1658 |
|
|
* Conditional callback for date and time comparisons.
|
1659 |
|
|
*/
|
1660 |
|
|
function webform_conditional_operator_datetime_equal($input_values, $rule_value) {
|
1661 |
|
|
$input_values = webform_conditional_value_datetime($input_values);
|
1662 |
|
|
return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) === webform_strtotime($rule_value);
|
1663 |
|
|
}
|
1664 |
|
|
|
1665 |
|
|
/**
|
1666 |
|
|
* Conditional callback for date and time comparisons.
|
1667 |
|
|
*/
|
1668 |
|
|
function webform_conditional_operator_datetime_not_equal($input_values, $rule_value) {
|
1669 |
|
|
return !webform_conditional_operator_datetime_equal($input_values, $rule_value);
|
1670 |
|
|
}
|
1671 |
|
|
|
1672 |
|
|
/**
|
1673 |
|
|
* Conditional callback for date and time comparisons.
|
1674 |
|
|
*/
|
1675 |
|
|
function webform_conditional_operator_datetime_after($input_values, $rule_value) {
|
1676 |
|
|
$input_values = webform_conditional_value_datetime($input_values);
|
1677 |
|
|
return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) > webform_strtotime($rule_value);
|
1678 |
|
|
}
|
1679 |
|
|
|
1680 |
|
|
/**
|
1681 |
|
|
* Conditional callback for date and time comparisons.
|
1682 |
|
|
*/
|
1683 |
|
|
function webform_conditional_operator_datetime_after_equal($input_values, $rule_value) {
|
1684 |
|
|
return webform_conditional_operator_datetime_after($input_values, $rule_value) ||
|
1685 |
|
|
webform_conditional_operator_datetime_equal($input_values, $rule_value);
|
1686 |
|
|
}
|
1687 |
|
|
|
1688 |
|
|
/**
|
1689 |
|
|
* Conditional callback for date and time comparisons.
|
1690 |
|
|
*/
|
1691 |
|
|
function webform_conditional_operator_datetime_before($input_values, $rule_value) {
|
1692 |
|
|
$input_values = webform_conditional_value_datetime($input_values);
|
1693 |
|
|
return empty($input_values) ? FALSE : webform_strtotime($input_values[0]) < webform_strtotime($rule_value);
|
1694 |
|
|
}
|
1695 |
|
|
|
1696 |
|
|
/**
|
1697 |
|
|
* Conditional callback for date and time comparisons.
|
1698 |
|
|
*/
|
1699 |
|
|
function webform_conditional_operator_datetime_before_equal($input_values, $rule_value) {
|
1700 |
|
|
return webform_conditional_operator_datetime_before($input_values, $rule_value) ||
|
1701 |
|
|
webform_conditional_operator_datetime_equal($input_values, $rule_value);
|
1702 |
|
|
}
|
1703 |
|
|
|
1704 |
|
|
/**
|
1705 |
|
|
* Utility function to convert incoming time and dates into strings.
|
1706 |
|
|
*/
|
1707 |
|
|
function webform_conditional_value_datetime($input_values) {
|
1708 |
|
|
// Convert times into a string.
|
1709 |
|
|
$input_values = isset($input_values['hour']) ? array(webform_date_string(webform_time_convert($input_values, '24-hour'), 'time')) : $input_values;
|
1710 |
|
|
// Convert dates into a string.
|
1711 |
|
|
$input_values = isset($input_values['month']) ? array(webform_date_string($input_values, 'date')) : $input_values;
|
1712 |
|
|
return $input_values;
|
1713 |
|
|
}
|
1714 |
|
|
|
1715 |
|
|
/**
|
1716 |
|
|
* Utility function to compare values of a select component.
|
1717 |
|
|
* @param string $a
|
1718 |
|
|
* First select option key to compare
|
1719 |
|
|
* @param string $b
|
1720 |
|
|
* Second select option key to compare
|
1721 |
|
|
* @param array $options
|
1722 |
|
|
* Associative array where the $a and $b are within the keys
|
1723 |
|
|
* @return integer based upon position of $a and $b in $options
|
1724 |
|
|
* -N if $a above (<) $b
|
1725 |
|
|
* 0 if $a = $b
|
1726 |
|
|
* +N if $a is below (>) $b
|
1727 |
|
|
*/
|
1728 |
|
|
function webform_compare_select($a, $b, $options) {
|
1729 |
|
|
// Select keys that are integer-like strings are numberic indices in PHP.
|
1730 |
|
|
// Convert the array keys to an array of strings.
|
1731 |
|
|
$options_array = array_map(function($i) {return (string) $i; }, array_keys($options));
|
1732 |
|
|
$a_position = array_search($a, $options_array, TRUE);
|
1733 |
|
|
$b_position = array_search($b, $options_array, TRUE);
|
1734 |
|
|
return ($a_position === FALSE || $a_position === FALSE) ? NULL : $a_position - $b_position;
|
1735 |
|
|
|
1736 |
|
|
} |