1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Webform module multiple select component.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements _webform_defaults_component().
|
10
|
*/
|
11
|
function _webform_defaults_select() {
|
12
|
return array(
|
13
|
'name' => '',
|
14
|
'form_key' => NULL,
|
15
|
'required' => 0,
|
16
|
'pid' => 0,
|
17
|
'weight' => 0,
|
18
|
'value' => '',
|
19
|
'extra' => array(
|
20
|
'items' => '',
|
21
|
'multiple' => NULL,
|
22
|
'aslist' => NULL,
|
23
|
'empty_option' => '',
|
24
|
'optrand' => 0,
|
25
|
'other_option' => NULL,
|
26
|
'other_text' => t('Other...'),
|
27
|
'title_display' => 0,
|
28
|
'description' => '',
|
29
|
'description_above' => FALSE,
|
30
|
'custom_keys' => FALSE,
|
31
|
'options_source' => '',
|
32
|
'private' => FALSE,
|
33
|
'analysis' => TRUE,
|
34
|
),
|
35
|
);
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Implements _webform_theme_component().
|
40
|
*/
|
41
|
function _webform_theme_select() {
|
42
|
return array(
|
43
|
'webform_display_select' => array(
|
44
|
'render element' => 'element',
|
45
|
'file' => 'components/select.inc',
|
46
|
),
|
47
|
);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Implements _webform_edit_component().
|
52
|
*/
|
53
|
function _webform_edit_select($component) {
|
54
|
$form = array(
|
55
|
'#attached' => array(
|
56
|
'js' => array(
|
57
|
drupal_get_path('module', 'webform') . '/js/select-admin.js' => array('preprocess' => FALSE),
|
58
|
array('data' => array('webform' => array('selectOptionsUrl' => url('webform/ajax/options/' . $component['nid']))), 'type' => 'setting'),
|
59
|
),
|
60
|
),
|
61
|
);
|
62
|
|
63
|
// Default component if nested under a grid.
|
64
|
if (!isset($component['cid']) && $component['pid'] &&
|
65
|
($node = node_load($component['nid'])) && ($parent = $node->webform['components'][$component['pid']]) &&
|
66
|
$parent['type'] == 'grid') {
|
67
|
$component['value'] = $parent['value'];
|
68
|
$component['extra']['items'] = $parent['extra']['options'];
|
69
|
$component['required'] = $parent['required'];
|
70
|
}
|
71
|
|
72
|
$other = array();
|
73
|
if ($info = _webform_select_options_info()) {
|
74
|
$options = array('' => t('None'));
|
75
|
foreach ($info as $name => $source) {
|
76
|
$options[$name] = $source['title'];
|
77
|
}
|
78
|
|
79
|
$other['options_source'] = array(
|
80
|
'#title' => t('Load a pre-built option list'),
|
81
|
'#type' => 'select',
|
82
|
'#options' => $options,
|
83
|
'#default_value' => $component['extra']['options_source'],
|
84
|
'#description' => t('Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.'),
|
85
|
'#parents' => array('extra', 'options_source'),
|
86
|
'#weight' => 5,
|
87
|
);
|
88
|
}
|
89
|
|
90
|
if (module_exists('select_or_other')) {
|
91
|
$other['other_option'] = array(
|
92
|
'#type' => 'checkbox',
|
93
|
'#title' => t('Allow "Other..." option'),
|
94
|
'#default_value' => $component['extra']['other_option'],
|
95
|
'#description' => t('Check this option if you want to allow users to enter an option not on the list.'),
|
96
|
'#parents' => array('extra', 'other_option'),
|
97
|
'#attributes' => array('class' => array('other-option-checkbox')),
|
98
|
'#weight' => 2,
|
99
|
);
|
100
|
$other['other_text'] = array(
|
101
|
'#type' => 'textfield',
|
102
|
'#title' => t('Text for "Other..." option'),
|
103
|
'#default_value' => $component['extra']['other_text'],
|
104
|
'#description' => t('If allowing other options, enter text to be used for other-enabling option.'),
|
105
|
'#parents' => array('extra', 'other_text'),
|
106
|
'#weight' => 3,
|
107
|
'#states' => array(
|
108
|
'visible' => array(
|
109
|
':input.other-option-checkbox' => array('checked' => TRUE),
|
110
|
),
|
111
|
),
|
112
|
);
|
113
|
}
|
114
|
|
115
|
if (module_exists('options_element')) {
|
116
|
$options = _webform_select_options($component, FALSE, FALSE);
|
117
|
|
118
|
$form['items'] = array(
|
119
|
'#type' => 'fieldset',
|
120
|
'#title' => t('Options'),
|
121
|
'#collapsible' => TRUE,
|
122
|
'#attributes' => array('class' => array('webform-options-element')),
|
123
|
'#element_validate' => array('_webform_edit_validate_options'),
|
124
|
'#weight' => 2,
|
125
|
);
|
126
|
|
127
|
$form['items']['options'] = array(
|
128
|
'#type' => 'options',
|
129
|
'#limit' => 500,
|
130
|
'#optgroups' => TRUE,
|
131
|
'#multiple' => $component['extra']['multiple'],
|
132
|
'#multiple_toggle' => t('Multiple'),
|
133
|
'#default_value' => $component['value'],
|
134
|
'#options' => $options,
|
135
|
'#options_readonly' => !empty($component['extra']['options_source']),
|
136
|
'#key_type' => 'mixed',
|
137
|
'#key_type_toggle' => t('Customize keys (Advanced)'),
|
138
|
'#key_type_toggled' => $component['extra']['custom_keys'],
|
139
|
'#default_value_pattern' => '^%.+\[.+\]$',
|
140
|
'#weight' => 1,
|
141
|
);
|
142
|
|
143
|
$form['items']['options']['option_settings'] = $other;
|
144
|
}
|
145
|
else {
|
146
|
$form['extra']['items'] = array(
|
147
|
'#type' => 'textarea',
|
148
|
'#title' => t('Options'),
|
149
|
'#default_value' => $component['extra']['items'],
|
150
|
'#description' => t('<strong>Key-value pairs MUST be specified as "safe_key|Some readable option"</strong>. Use of only alphanumeric characters and underscores is recommended in keys. One option per line. Option groups may be specified with <Group Name>. <> can be used to insert items at the root of the menu after specifying a group.') . ' ' . theme('webform_token_help'),
|
151
|
'#cols' => 60,
|
152
|
'#rows' => 5,
|
153
|
'#weight' => 0,
|
154
|
'#required' => TRUE,
|
155
|
'#wysiwyg' => FALSE,
|
156
|
'#element_validate' => array('_webform_edit_validate_select'),
|
157
|
);
|
158
|
|
159
|
if (!empty($component['extra']['options_source'])) {
|
160
|
$form['extra']['items']['#attributes'] = array('readonly' => 'readonly');
|
161
|
}
|
162
|
|
163
|
$form['extra'] = array_merge($form['extra'], $other);
|
164
|
$form['value'] = array(
|
165
|
'#type' => 'textfield',
|
166
|
'#title' => t('Default value'),
|
167
|
'#default_value' => $component['value'],
|
168
|
'#description' => t('The default value of the field identified by its key. For multiple selects use commas to separate multiple defaults.') . ' ' . theme('webform_token_help'),
|
169
|
'#size' => 60,
|
170
|
'#maxlength' => 1024,
|
171
|
'#weight' => 0,
|
172
|
);
|
173
|
$form['extra']['multiple'] = array(
|
174
|
'#type' => 'checkbox',
|
175
|
'#title' => t('Multiple'),
|
176
|
'#default_value' => $component['extra']['multiple'],
|
177
|
'#description' => t('Check this option if the user should be allowed to choose multiple values.'),
|
178
|
'#weight' => 0,
|
179
|
);
|
180
|
}
|
181
|
|
182
|
$form['display']['aslist'] = array(
|
183
|
'#type' => 'checkbox',
|
184
|
'#title' => t('Listbox'),
|
185
|
'#default_value' => $component['extra']['aslist'],
|
186
|
'#description' => t('Check this option if you want the select component to be displayed as a select list box instead of radio buttons or checkboxes. Option groups (nested options) are only supported with listbox components.'),
|
187
|
'#parents' => array('extra', 'aslist'),
|
188
|
);
|
189
|
$form['display']['empty_option'] = array(
|
190
|
'#type' => 'textfield',
|
191
|
'#title' => t('Empty option'),
|
192
|
'#default_value' => $component['extra']['empty_option'],
|
193
|
'#size' => 60,
|
194
|
'#maxlength' => 255,
|
195
|
'#description' => t('The list item to show when no default is provided. Leave blank for "- None -" or "- Select -".'),
|
196
|
'#parents' => array('extra', 'empty_option'),
|
197
|
'#states' => array(
|
198
|
'visible' => array(
|
199
|
':input[name="extra[aslist]"]' => array('checked' => TRUE),
|
200
|
':input[name="extra[multiple]"]' => array('checked' => FALSE),
|
201
|
':input[name="value"]' => array('filled' => FALSE),
|
202
|
),
|
203
|
),
|
204
|
);
|
205
|
$form['display']['optrand'] = array(
|
206
|
'#type' => 'checkbox',
|
207
|
'#title' => t('Randomize options'),
|
208
|
'#default_value' => $component['extra']['optrand'],
|
209
|
'#description' => t('Randomizes the order of the options when they are displayed in the form.'),
|
210
|
'#parents' => array('extra', 'optrand'),
|
211
|
);
|
212
|
|
213
|
return $form;
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Element validation callback. Ensure keys are not duplicated.
|
218
|
*/
|
219
|
function _webform_edit_validate_select($element, &$form_state) {
|
220
|
// Check for duplicate key values to prevent unexpected data loss. Require
|
221
|
// all options to include a safe_key.
|
222
|
if (!empty($element['#value'])) {
|
223
|
$lines = explode("\n", trim($element['#value']));
|
224
|
$existing_keys = array();
|
225
|
$duplicate_keys = array();
|
226
|
$missing_keys = array();
|
227
|
$long_keys = array();
|
228
|
$group = '';
|
229
|
foreach ($lines as $line) {
|
230
|
$matches = array();
|
231
|
$line = trim($line);
|
232
|
if (preg_match('/^\<([^>]*)\>$/', $line, $matches)) {
|
233
|
$group = $matches[1];
|
234
|
$key = NULL; // No need to store group names.
|
235
|
}
|
236
|
elseif (preg_match('/^([^|]*)\|(.*)$/', $line, $matches)) {
|
237
|
$key = $matches[1];
|
238
|
if (strlen($key) > 128) {
|
239
|
$long_keys[] = $key;
|
240
|
}
|
241
|
}
|
242
|
else {
|
243
|
$missing_keys[] = $line;
|
244
|
}
|
245
|
|
246
|
if (isset($key)) {
|
247
|
if (isset($existing_keys[$group][$key])) {
|
248
|
$duplicate_keys[$key] = $key;
|
249
|
}
|
250
|
else {
|
251
|
$existing_keys[$group][$key] = $key;
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
|
256
|
if (!empty($missing_keys)) {
|
257
|
form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
|
258
|
}
|
259
|
|
260
|
if (!empty($long_keys)) {
|
261
|
form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
|
262
|
}
|
263
|
|
264
|
if (!empty($duplicate_keys)) {
|
265
|
form_error($element, t('Options within the select list must be unique. The following keys have been used multiple times:') . theme('item_list', array('items' => $duplicate_keys)));
|
266
|
}
|
267
|
|
268
|
// Set the listbox option if needed.
|
269
|
if (empty($missing_keys) && empty($long_keys) && empty($duplicate_keys)) {
|
270
|
$options = _webform_select_options_from_text($element['#value']);
|
271
|
_webform_edit_validate_set_aslist($options, $form_state);
|
272
|
}
|
273
|
}
|
274
|
|
275
|
return TRUE;
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* Set the appropriate webform values when using the options element module.
|
280
|
*/
|
281
|
function _webform_edit_validate_options($element, &$form_state) {
|
282
|
$key = end($element['#parents']);
|
283
|
$element_options = $form_state['values'][$key]['options'];
|
284
|
unset($form_state['values'][$key]);
|
285
|
|
286
|
$form_state['values']['extra'][$key] = form_options_to_text($element_options['options'], 'custom');
|
287
|
|
288
|
// Options saved for select components.
|
289
|
if ($key == 'items') {
|
290
|
$form_state['values']['extra']['multiple'] = $element_options['multiple'];
|
291
|
$form_state['values']['extra']['custom_keys'] = $element_options['custom_keys'];
|
292
|
$form_state['values']['value'] = is_array($element_options['default_value']) ? implode(', ', $element_options['default_value']) : $element_options['default_value'];
|
293
|
|
294
|
// Set the listbox option if needed.
|
295
|
_webform_edit_validate_set_aslist($element_options['options'], $form_state);
|
296
|
}
|
297
|
// Options saved for grid components.
|
298
|
else {
|
299
|
$form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
|
300
|
// There is only one 'value', but grids have two options widgets, one for questions and one for options.
|
301
|
// Only options have a default 'value'. Note that multiple selection is now allowed for grid options.
|
302
|
if ($key == 'options') {
|
303
|
$form_state['values']['value'] = $element_options['default_value'];
|
304
|
}
|
305
|
}
|
306
|
}
|
307
|
|
308
|
/**
|
309
|
* Ensure "aslist" is used for option groups. Called from options validations.
|
310
|
*/
|
311
|
function _webform_edit_validate_set_aslist($options, &$form_state) {
|
312
|
if (empty($form_state['values']['extra']['aslist']) && !empty($options)) {
|
313
|
foreach ($options as $option) {
|
314
|
if (is_array($option)) {
|
315
|
$form_state['values']['extra']['aslist'] = 1;
|
316
|
drupal_set_message(t('The component %name has automatically been set to display as a listbox in order to support option groups.', array('%name' => $form_state['values']['name'])), 'warning');
|
317
|
break;
|
318
|
}
|
319
|
}
|
320
|
}
|
321
|
}
|
322
|
|
323
|
/**
|
324
|
* Implements _webform_render_component().
|
325
|
*/
|
326
|
function _webform_render_select($component, $value = NULL, $filter = TRUE, $submission = NULL) {
|
327
|
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
|
328
|
|
329
|
$element = array(
|
330
|
'#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
|
331
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
332
|
'#required' => $component['required'],
|
333
|
'#weight' => $component['weight'],
|
334
|
'#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
|
335
|
'#theme_wrappers' => array('webform_element'),
|
336
|
'#translatable' => array('title', 'description', 'options'),
|
337
|
);
|
338
|
|
339
|
// Prevent double-wrapping of radios and checkboxes.
|
340
|
if (!$component['extra']['aslist']) {
|
341
|
$element['#pre_render'] = array();
|
342
|
}
|
343
|
|
344
|
// Convert the user-entered options list into an array.
|
345
|
$default_value = $filter ? webform_replace_tokens($component['value'], $node) : $component['value'];
|
346
|
$options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
|
347
|
if (empty($options)) {
|
348
|
// Make element inaccessible if there are no options as there is no point in showing it.
|
349
|
$element['#access'] = FALSE;
|
350
|
}
|
351
|
|
352
|
if ($component['extra']['optrand']) {
|
353
|
_webform_shuffle_options($options);
|
354
|
}
|
355
|
|
356
|
// Add HTML5 required attribute, if needed and possible (not working on more than one checkboxes).
|
357
|
if ($component['required'] && ($component['extra']['aslist'] || !$component['extra']['multiple'] || count($options) == 1)) {
|
358
|
$element['#attributes']['required'] = 'required';
|
359
|
}
|
360
|
|
361
|
// Add default options if using a select list with no default. This trigger's
|
362
|
// Drupal 7's adding of the option for us. See @form_process_select().
|
363
|
if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
|
364
|
$element['#empty_value'] = '';
|
365
|
if (strlen($component['extra']['empty_option'])) {
|
366
|
$element['#empty_option'] = $component['extra']['empty_option'];
|
367
|
$element['#translatable'][] = 'empty_option';
|
368
|
}
|
369
|
}
|
370
|
|
371
|
// Set the component options.
|
372
|
$element['#options'] = $options;
|
373
|
|
374
|
// Use the component's default value if the component is currently empty.
|
375
|
if (!isset($value)) {
|
376
|
// The default for multiple selects is a comma-delimited list, without white-space or empty entries.
|
377
|
$value = $component['extra']['multiple'] ? array_filter(array_map('trim', explode(',', $default_value)), 'strlen') : $default_value;
|
378
|
}
|
379
|
|
380
|
// Convert all values into an array; component may now be single but was previously multiple, or vice-versa
|
381
|
$value = (array)$value;
|
382
|
|
383
|
// Set the default value. Note: "No choice" is stored as an empty string,
|
384
|
// which will match a 0 key for radios; NULL is used to avoid unintentional
|
385
|
// defaulting to the 0 option.
|
386
|
if ($component['extra']['multiple']) {
|
387
|
// Set the value as an array.
|
388
|
$element['#default_value'] = array();
|
389
|
foreach ($value as $option_value) {
|
390
|
$element['#default_value'][] = $option_value === '' ? NULL : $option_value;
|
391
|
}
|
392
|
}
|
393
|
else {
|
394
|
// Set the value as a single string.
|
395
|
$option_value = reset($value);
|
396
|
$element['#default_value'] = $option_value === '' ? NULL : $option_value;
|
397
|
}
|
398
|
|
399
|
if ($component['extra']['other_option'] && module_exists('select_or_other')) {
|
400
|
// Set display as a select_or_other element:
|
401
|
$element['#type'] = 'select_or_other';
|
402
|
$element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
|
403
|
$element['#translatable'][] = 'other';
|
404
|
$element['#other_title'] = $element['#title'] . ' ' . $element['#other'];
|
405
|
$element['#other_title_display'] = 'invisible';
|
406
|
$element['#other_unknown_defaults'] = 'other';
|
407
|
$element['#other_delimiter'] = ', ';
|
408
|
// Merge in Webform's #process function for Select or other.
|
409
|
$element['#process'] = array_merge(element_info_property('select_or_other', '#process'), array('webform_expand_select_or_other'));
|
410
|
// Inherit select_or_other settings or set defaults.
|
411
|
$element['#disabled'] = isset($component['extra']['#disabled']) ? $component['extra']['#disabled'] : element_info_property('select_or_other', 'disabled');
|
412
|
$element['#size'] = isset($component['extra']['#size']) ? $component['extra']['#size'] : element_info_property('select_or_other', 'size');
|
413
|
|
414
|
if ($component['extra']['multiple']) {
|
415
|
$element['#multiple'] = TRUE;
|
416
|
$element['#select_type'] = 'checkboxes';
|
417
|
}
|
418
|
else {
|
419
|
$element['#multiple'] = FALSE;
|
420
|
$element['#select_type'] = 'radios';
|
421
|
}
|
422
|
if ($component['extra']['aslist']) {
|
423
|
$element['#select_type'] = 'select';
|
424
|
}
|
425
|
}
|
426
|
elseif ($component['extra']['aslist']) {
|
427
|
// Set display as a select list:
|
428
|
$element['#type'] = 'select';
|
429
|
if ($component['extra']['multiple']) {
|
430
|
$element['#size'] = 4;
|
431
|
$element['#multiple'] = TRUE;
|
432
|
}
|
433
|
}
|
434
|
else {
|
435
|
if ($component['extra']['multiple']) {
|
436
|
// Set display as a checkbox set.
|
437
|
$element['#type'] = 'checkboxes';
|
438
|
$element['#theme_wrappers'] = array_merge(array('checkboxes'), $element['#theme_wrappers']);
|
439
|
$element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array('webform_expand_select_ids'));
|
440
|
}
|
441
|
else {
|
442
|
// Set display as a radio set.
|
443
|
$element['#type'] = 'radios';
|
444
|
$element['#theme_wrappers'] = array_merge(array('radios'), $element['#theme_wrappers']);
|
445
|
$element['#process'] = array_merge(element_info_property('radios', '#process'), array('webform_expand_select_ids'));
|
446
|
}
|
447
|
}
|
448
|
|
449
|
return $element;
|
450
|
}
|
451
|
|
452
|
/**
|
453
|
* Process function to ensure select_or_other elements validate properly.
|
454
|
*/
|
455
|
function webform_expand_select_or_other($element) {
|
456
|
// Disable validation for back-button and save draft.
|
457
|
$element['select']['#validated'] = TRUE;
|
458
|
$element['select']['#webform_validated'] = FALSE;
|
459
|
|
460
|
$element['other']['#validated'] = TRUE;
|
461
|
$element['other']['#webform_validated'] = FALSE;
|
462
|
|
463
|
// The Drupal FAPI does not support #title_display inline so we need to move
|
464
|
// to a supported value here to be compatible with select_or_other.
|
465
|
$element['select']['#title_display'] = $element['#title_display'] === 'inline' ? 'before' : $element['#title_display'];
|
466
|
|
467
|
// If the default value contains "select_or_other" (the key of the select
|
468
|
// element for the "other..." choice), discard it and set the "other" value.
|
469
|
if (is_array($element['#default_value']) && in_array('select_or_other', $element['#default_value'])) {
|
470
|
$key = array_search('select_or_other', $element['#default_value']);
|
471
|
unset($element['#default_value'][$key]);
|
472
|
$element['#default_value'] = array_values($element['#default_value']);
|
473
|
$element['other']['#default_value'] = implode(', ', $element['#default_value']);
|
474
|
}
|
475
|
|
476
|
// Sanitize the options in Select or other check boxes and radio buttons.
|
477
|
if ($element['#select_type'] == 'checkboxes' || $element['#select_type'] == 'radios') {
|
478
|
$element['select']['#process'] = array_merge(element_info_property($element['#select_type'], '#process'), array('webform_expand_select_ids'));
|
479
|
}
|
480
|
|
481
|
return $element;
|
482
|
}
|
483
|
|
484
|
/**
|
485
|
* FAPI process function to rename IDs attached to checkboxes and radios.
|
486
|
*/
|
487
|
function webform_expand_select_ids($element) {
|
488
|
$id = $element['#id'] = str_replace('_', '-', _webform_safe_name(strip_tags($element['#id'])));
|
489
|
$delta = 0;
|
490
|
foreach (element_children($element) as $key) {
|
491
|
$delta++;
|
492
|
// Convert the #id for each child to a safe name, regardless of key.
|
493
|
$element[$key]['#id'] = $id . '-' . $delta;
|
494
|
|
495
|
// Prevent scripts or CSS in the labels for each checkbox or radio.
|
496
|
$element[$key]['#title'] = isset($element[$key]['#title']) ? webform_filter_xss($element[$key]['#title']) : '';
|
497
|
}
|
498
|
return $element;
|
499
|
}
|
500
|
|
501
|
/**
|
502
|
* Implements _webform_display_component().
|
503
|
*/
|
504
|
function _webform_display_select($component, $value, $format = 'html', $submission = array()) {
|
505
|
// Sort values by numeric key. These may be in alphabetic order from the database query,
|
506
|
// which is not numeric order for keys '10' and higher.
|
507
|
$value = (array) $value;
|
508
|
ksort($value, SORT_NUMERIC);
|
509
|
return array(
|
510
|
'#title' => $component['name'],
|
511
|
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
|
512
|
'#weight' => $component['weight'],
|
513
|
'#multiple' => $component['extra']['multiple'],
|
514
|
'#theme' => 'webform_display_select',
|
515
|
'#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
|
516
|
'#format' => $format,
|
517
|
'#options' => _webform_select_options($component, !$component['extra']['aslist']),
|
518
|
'#value' => $value,
|
519
|
'#translatable' => array('title', 'options'),
|
520
|
);
|
521
|
}
|
522
|
|
523
|
/**
|
524
|
* Implements _webform_submit_component().
|
525
|
*
|
526
|
* Handle select or other... modifications and convert FAPI 0/1 values into
|
527
|
* something saveable.
|
528
|
*/
|
529
|
function _webform_submit_select($component, $value) {
|
530
|
if (module_exists('select_or_other') && $component['extra']['other_option'] && is_array($value) && count($value) == 2 && isset($value['select'])) {
|
531
|
if (is_array($value['select']) && isset($value['select']['select_or_other'])) {
|
532
|
unset($value['select']['select_or_other']);
|
533
|
$value['select'][] = $value['other'];
|
534
|
}
|
535
|
elseif ($value['select'] == 'select_or_other') {
|
536
|
$value['select'] = $value['other'];
|
537
|
}
|
538
|
$value = $value['select'];
|
539
|
}
|
540
|
|
541
|
// Build a list of all valid keys expected to be submitted.
|
542
|
$options = _webform_select_options($component, TRUE);
|
543
|
|
544
|
$return = NULL;
|
545
|
if (is_array($value)) {
|
546
|
$return = array();
|
547
|
foreach ($value as $key => $option_value) {
|
548
|
// Handle options that are specified options.
|
549
|
if ($option_value !== '' && isset($options[$option_value])) {
|
550
|
// Checkboxes submit an integer value of 0 when unchecked. A checkbox
|
551
|
// with a value of '0' is valid, so we can't use empty() here.
|
552
|
if ($option_value === 0 && !$component['extra']['aslist'] && $component['extra']['multiple']) {
|
553
|
// Don't save unchecked values
|
554
|
}
|
555
|
else {
|
556
|
$return[] = $option_value;
|
557
|
}
|
558
|
}
|
559
|
// Handle options that are added through the "other" field. Specifically
|
560
|
// exclude the "select_or_other" value, which is added by the select list.
|
561
|
elseif ($component['extra']['other_option'] && module_exists('select_or_other') && $option_value != 'select_or_other') {
|
562
|
$return[] = $option_value;
|
563
|
}
|
564
|
}
|
565
|
|
566
|
// If no elements are selected, then save an empty string to indicate that this components should not be defaulted again
|
567
|
if (!$return) {
|
568
|
$return = array('');
|
569
|
}
|
570
|
}
|
571
|
elseif (is_string($value)) {
|
572
|
$return = $value;
|
573
|
}
|
574
|
|
575
|
return $return;
|
576
|
}
|
577
|
|
578
|
/**
|
579
|
* Format the text output for this component.
|
580
|
*/
|
581
|
function theme_webform_display_select($variables) {
|
582
|
$element = $variables['element'];
|
583
|
|
584
|
// Flatten the list of options so we can get values easily. These options
|
585
|
// may be translated by hook_webform_display_component_alter().
|
586
|
$options = array();
|
587
|
foreach ($element['#options'] as $key => $value) {
|
588
|
if (is_array($value)) {
|
589
|
foreach ($value as $subkey => $subvalue) {
|
590
|
$options[$subkey] = $subvalue;
|
591
|
}
|
592
|
}
|
593
|
else {
|
594
|
$options[$key] = $value;
|
595
|
}
|
596
|
}
|
597
|
|
598
|
$items = array();
|
599
|
if ($element['#multiple']) {
|
600
|
foreach ((array) $element['#value'] as $option_value) {
|
601
|
if ($option_value !== '') {
|
602
|
// Administer provided values.
|
603
|
if (isset($options[$option_value])) {
|
604
|
$items[] = $element['#format'] == 'html' ? webform_filter_xss($options[$option_value]) : $options[$option_value];
|
605
|
}
|
606
|
// User-specified in the "other" field.
|
607
|
else {
|
608
|
$items[] = $element['#format'] == 'html' ? check_plain($option_value) : $option_value;
|
609
|
}
|
610
|
}
|
611
|
}
|
612
|
}
|
613
|
else {
|
614
|
if (isset($element['#value'][0]) && $element['#value'][0] !== '') {
|
615
|
// Administer provided values.
|
616
|
if (isset($options[$element['#value'][0]])) {
|
617
|
$items[] = $element['#format'] == 'html' ? webform_filter_xss($options[$element['#value'][0]]) : $options[$element['#value'][0]];
|
618
|
}
|
619
|
// User-specified in the "other" field.
|
620
|
else {
|
621
|
$items[] = $element['#format'] == 'html' ? check_plain($element['#value'][0]) : $element['#value'][0];
|
622
|
}
|
623
|
}
|
624
|
}
|
625
|
|
626
|
if ($element['#format'] == 'html') {
|
627
|
$output = count($items) > 1 ? theme('item_list', array('items' => $items)) : (isset($items[0]) ? $items[0] : ' ');
|
628
|
}
|
629
|
else {
|
630
|
if (count($items) > 1) {
|
631
|
foreach ($items as $key => $item) {
|
632
|
$items[$key] = ' - ' . $item;
|
633
|
}
|
634
|
$output = implode("\n", $items);
|
635
|
}
|
636
|
else {
|
637
|
$output = isset($items[0]) ? $items[0] : ' ';
|
638
|
}
|
639
|
}
|
640
|
|
641
|
return $output;
|
642
|
}
|
643
|
|
644
|
/**
|
645
|
* Implements _webform_analysis_component().
|
646
|
*/
|
647
|
function _webform_analysis_select($component, $sids = array(), $single = FALSE, $join = NULL) {
|
648
|
$options = _webform_select_options($component, TRUE);
|
649
|
|
650
|
// Create a generic query for the component.
|
651
|
$query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
|
652
|
->condition('wsd.nid', $component['nid'])
|
653
|
->condition('wsd.cid', $component['cid'])
|
654
|
->condition('wsd.data', '', '<>');
|
655
|
|
656
|
if ($sids) {
|
657
|
$query->condition('wsd.sid', $sids, 'IN');
|
658
|
}
|
659
|
|
660
|
if ($join) {
|
661
|
$query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
|
662
|
}
|
663
|
|
664
|
// Clone the query for later use, if needed.
|
665
|
if ($component['extra']['other_option']) {
|
666
|
$count_query = clone $query;
|
667
|
if ($single) {
|
668
|
$other_query = clone $query;
|
669
|
}
|
670
|
}
|
671
|
|
672
|
$rows = array();
|
673
|
$other = array();
|
674
|
$normal_count = 0;
|
675
|
|
676
|
if ($options) {
|
677
|
// Gather the normal results first (not "other" options).
|
678
|
$query->addExpression('COUNT(wsd.data)', 'datacount');
|
679
|
$result = $query
|
680
|
->condition('wsd.data', array_keys($options), 'IN')
|
681
|
->fields('wsd', array('data'))
|
682
|
->groupBy('wsd.data')
|
683
|
->execute();
|
684
|
foreach ($result as $data) {
|
685
|
$display_option = isset($options[$data['data']]) ? $options[$data['data']] : $data['data'];
|
686
|
$rows[$data['data']] = array(webform_filter_xss($display_option), $data['datacount']);
|
687
|
$normal_count += $data['datacount'];
|
688
|
}
|
689
|
|
690
|
// Order the results according to the normal options array.
|
691
|
$ordered_rows = array();
|
692
|
foreach (array_intersect_key($options, $rows) as $key => $label) {
|
693
|
$ordered_rows[] = $rows[$key];
|
694
|
}
|
695
|
$rows = $ordered_rows;
|
696
|
}
|
697
|
|
698
|
|
699
|
// Add a row for displaying the total unknown or user-entered values.
|
700
|
if ($component['extra']['other_option']) {
|
701
|
$count_query->addExpression('COUNT(*)', 'datacount');
|
702
|
$full_count = $count_query->execute()->fetchField();
|
703
|
$other_count = $full_count - $normal_count;
|
704
|
$display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
|
705
|
$other_text = ($other_count && !$single) ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
|
706
|
$rows[] = array($display_option, $other_text);
|
707
|
|
708
|
// If showing all results, execute the "other" query and append their rows.
|
709
|
if ($single) {
|
710
|
$other_query->addExpression('COUNT(wsd.data)', 'datacount');
|
711
|
$other_query
|
712
|
->fields('wsd', array('data'))
|
713
|
->groupBy('wsd.data');
|
714
|
if ($options) {
|
715
|
$other_query->condition('wsd.data', array_keys($options), 'NOT IN');
|
716
|
}
|
717
|
$other_result = $other_query->execute();
|
718
|
foreach ($other_result as $data) {
|
719
|
$other[] = array(check_plain($data['data']), $data['datacount']);
|
720
|
}
|
721
|
if ($other) {
|
722
|
array_unshift($other, '<strong>' . t('Other responses') . '</strong>');
|
723
|
}
|
724
|
}
|
725
|
}
|
726
|
|
727
|
return array(
|
728
|
'table_rows' => $rows,
|
729
|
'other_data' => $other,
|
730
|
);
|
731
|
}
|
732
|
|
733
|
/**
|
734
|
* Implements _webform_table_component().
|
735
|
*/
|
736
|
function _webform_table_select($component, $value) {
|
737
|
// Convert submitted 'safe' values to un-edited, original form.
|
738
|
$options = _webform_select_options($component, TRUE);
|
739
|
|
740
|
$value = (array) $value;
|
741
|
ksort($value, SORT_NUMERIC);
|
742
|
$items = array();
|
743
|
// Set the value as a single string.
|
744
|
foreach ($value as $option_value) {
|
745
|
if ($option_value !== '') {
|
746
|
if (isset($options[$option_value])) {
|
747
|
$items[] = webform_filter_xss($options[$option_value]);
|
748
|
}
|
749
|
else {
|
750
|
$items[] = check_plain($option_value);
|
751
|
}
|
752
|
}
|
753
|
}
|
754
|
|
755
|
return implode('<br />', $items);
|
756
|
}
|
757
|
|
758
|
/**
|
759
|
* Implements _webform_action_set_component().
|
760
|
*/
|
761
|
function _webform_action_set_select($component, &$element, &$form_state, $value) {
|
762
|
// Set the value as an array for multiple select or single value otherwise.
|
763
|
if ($element['#type'] == 'checkboxes') {
|
764
|
$checkbox_values = $element['#options'];
|
765
|
array_walk($checkbox_values, function(&$option_value, $key) use($value) {
|
766
|
$option_value = (int)(strval($key) === $value);
|
767
|
});
|
768
|
}
|
769
|
else {
|
770
|
$value = $component['extra']['multiple'] ? array($value) : $value;
|
771
|
}
|
772
|
$element['#value'] = $value;
|
773
|
form_set_value($element, $value, $form_state);
|
774
|
}
|
775
|
|
776
|
/**
|
777
|
* Implements _webform_csv_headers_component().
|
778
|
*/
|
779
|
function _webform_csv_headers_select($component, $export_options) {
|
780
|
$headers = array(
|
781
|
0 => array(),
|
782
|
1 => array(),
|
783
|
2 => array(),
|
784
|
);
|
785
|
|
786
|
if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
|
787
|
$headers[0][] = '';
|
788
|
$headers[1][] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
|
789
|
$items = _webform_select_options($component, TRUE);
|
790
|
if ($component['extra']['other_option']) {
|
791
|
if ($export_options['header_keys']) {
|
792
|
$other_label = $component['form_key'] . '_other';
|
793
|
}
|
794
|
else {
|
795
|
$other_label = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
|
796
|
}
|
797
|
$items[$other_label] = $other_label;
|
798
|
}
|
799
|
$count = 0;
|
800
|
foreach ($items as $key => $item) {
|
801
|
// Empty column per sub-field in main header.
|
802
|
if ($count != 0) {
|
803
|
$headers[0][] = '';
|
804
|
$headers[1][] = '';
|
805
|
}
|
806
|
if ($export_options['select_keys']) {
|
807
|
$headers[2][] = $key;
|
808
|
}
|
809
|
else {
|
810
|
$headers[2][] = $item;
|
811
|
}
|
812
|
$count++;
|
813
|
}
|
814
|
}
|
815
|
else {
|
816
|
$headers[0][] = '';
|
817
|
$headers[1][] = '';
|
818
|
$headers[2][] = $export_options['header_keys'] ? $component['form_key'] : $component['name'];
|
819
|
}
|
820
|
return $headers;
|
821
|
}
|
822
|
|
823
|
/**
|
824
|
* Implements _webform_csv_data_component().
|
825
|
*/
|
826
|
function _webform_csv_data_select($component, $export_options, $value) {
|
827
|
$options = _webform_select_options($component, TRUE);
|
828
|
$return = array();
|
829
|
|
830
|
if ($component['extra']['multiple']) {
|
831
|
foreach ($options as $key => $item) {
|
832
|
// Strict search is needed to avoid a key of 0 from matching an empty
|
833
|
// value.
|
834
|
$index = array_search((string)$key, (array)$value, TRUE);
|
835
|
if ($index !== FALSE) {
|
836
|
if ($export_options['select_format'] == 'separate') {
|
837
|
$return[] = 'X';
|
838
|
}
|
839
|
else {
|
840
|
$return[] = $export_options['select_keys'] ? $key : $item;
|
841
|
}
|
842
|
unset($value[$index]);
|
843
|
}
|
844
|
elseif ($export_options['select_format'] == 'separate') {
|
845
|
$return[] = '';
|
846
|
}
|
847
|
}
|
848
|
|
849
|
// Any remaining items in the $value array will be user-added options.
|
850
|
if ($component['extra']['other_option']) {
|
851
|
$return[] = count($value) ? implode(',', $value) : '';
|
852
|
}
|
853
|
}
|
854
|
else {
|
855
|
$key = $value[0];
|
856
|
if ($export_options['select_keys']) {
|
857
|
$return = $key;
|
858
|
}
|
859
|
else {
|
860
|
$return = isset($options[$key]) ? $options[$key] : $key;
|
861
|
}
|
862
|
}
|
863
|
|
864
|
if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
|
865
|
$return = implode(',', (array) $return);
|
866
|
}
|
867
|
|
868
|
return $return;
|
869
|
}
|
870
|
|
871
|
|
872
|
/**
|
873
|
* Implements _webform_options_component().
|
874
|
*
|
875
|
* This function is confusingly an alias of _webform_select_options(). However
|
876
|
* this version is intended to be accessed publicly via
|
877
|
* webform_component_invoke(), since it is a Webform "hook", rather than an
|
878
|
* internal, private function. To get the list of select list options for
|
879
|
* a component, use:
|
880
|
*
|
881
|
* @code
|
882
|
* $options = webform_component_invoke($component['type'], 'options', $component);
|
883
|
* @endcode
|
884
|
*/
|
885
|
function _webform_options_select($component, $flat = FALSE) {
|
886
|
return _webform_select_options($component, $flat);
|
887
|
}
|
888
|
|
889
|
/**
|
890
|
* Menu callback; Return a predefined list of select options as JSON.
|
891
|
*/
|
892
|
function webform_select_options_ajax($source_name = '') {
|
893
|
$info = _webform_select_options_info();
|
894
|
|
895
|
$component['extra']['options_source'] = $source_name;
|
896
|
if ($source_name && isset($info[$source_name])) {
|
897
|
$options = _webform_select_options_to_text(_webform_select_options($component, FALSE, FALSE));
|
898
|
}
|
899
|
else {
|
900
|
$options = '';
|
901
|
}
|
902
|
|
903
|
$return = array(
|
904
|
'elementId' => module_exists('options_element') ? 'edit-items-options-options-field-widget' : 'edit-extra-items',
|
905
|
'options' => $options,
|
906
|
);
|
907
|
|
908
|
drupal_json_output($return);
|
909
|
}
|
910
|
|
911
|
/**
|
912
|
* Generate a list of options for a select list.
|
913
|
*/
|
914
|
function _webform_select_options($component, $flat = FALSE, $filter = TRUE) {
|
915
|
if ($component['extra']['options_source']) {
|
916
|
$options = _webform_select_options_callback($component['extra']['options_source'], $component, $flat);
|
917
|
}
|
918
|
else {
|
919
|
$options = _webform_select_options_from_text($component['extra']['items'], $flat);
|
920
|
}
|
921
|
|
922
|
// Replace tokens if needed in the options.
|
923
|
if ($filter) {
|
924
|
$node = node_load($component['nid']);
|
925
|
$options = _webform_select_replace_tokens($options, $node);
|
926
|
}
|
927
|
|
928
|
return isset($options) ? $options : array();
|
929
|
}
|
930
|
|
931
|
/**
|
932
|
* Replace tokens in the values of a list of select options.
|
933
|
*/
|
934
|
function _webform_select_replace_tokens($options, $node) {
|
935
|
foreach ($options as $key => $option) {
|
936
|
if (is_array($option)) {
|
937
|
$options[$key] = _webform_select_replace_tokens($option, $node);
|
938
|
}
|
939
|
else {
|
940
|
$options[$key] = webform_replace_tokens($option, $node);
|
941
|
}
|
942
|
}
|
943
|
return $options;
|
944
|
}
|
945
|
|
946
|
/**
|
947
|
* Load Webform select option info from 3rd party modules.
|
948
|
*/
|
949
|
function _webform_select_options_info() {
|
950
|
static $info;
|
951
|
if (!isset($info)) {
|
952
|
$info = array();
|
953
|
|
954
|
foreach (module_implements('webform_select_options_info') as $module) {
|
955
|
$additions = module_invoke($module, 'webform_select_options_info');
|
956
|
foreach ($additions as $key => $addition) {
|
957
|
$additions[$key]['module'] = $module;
|
958
|
}
|
959
|
$info = array_merge($info, $additions);
|
960
|
}
|
961
|
drupal_alter('webform_select_options_info', $info);
|
962
|
}
|
963
|
return $info;
|
964
|
}
|
965
|
|
966
|
/**
|
967
|
* Execute a select option callback.
|
968
|
*
|
969
|
* @param $name
|
970
|
* The name of the options group.
|
971
|
* @param $component
|
972
|
* The full Webform component.
|
973
|
* @param $flat
|
974
|
* Whether the information returned should exclude any nested groups.
|
975
|
*/
|
976
|
function _webform_select_options_callback($name, $component, $flat = FALSE) {
|
977
|
$info = _webform_select_options_info();
|
978
|
|
979
|
// Include any necessary files.
|
980
|
if (isset($info[$name]['file'])) {
|
981
|
$pathinfo = pathinfo($info[$name]['file']);
|
982
|
$path = ($pathinfo['dirname'] ? $pathinfo['dirname'] . '/' : '') . basename($pathinfo['basename'], '.' . $pathinfo['extension']);
|
983
|
module_load_include($pathinfo['extension'], $info[$name]['module'], $path);
|
984
|
}
|
985
|
|
986
|
// Execute the callback function.
|
987
|
if (isset($info[$name]['options callback']) && is_callable($info[$name]['options callback'])) {
|
988
|
$function = $info[$name]['options callback'];
|
989
|
|
990
|
$arguments = array();
|
991
|
if (isset($info[$name]['options arguments'])) {
|
992
|
$arguments = $info[$name]['options arguments'];
|
993
|
}
|
994
|
|
995
|
return $function($component, $flat, $arguments);
|
996
|
}
|
997
|
}
|
998
|
|
999
|
/**
|
1000
|
* Utility function to split user-entered values from new-line separated
|
1001
|
* text into an array of options.
|
1002
|
*
|
1003
|
* @param string $text
|
1004
|
* Text to be converted into a select option array.
|
1005
|
* @param bool $flat
|
1006
|
* Optional. If specified, return the option array and exclude any optgroups.
|
1007
|
* @return array
|
1008
|
* An array of options suitable for use as a #options property. Note that
|
1009
|
* values are not filtered and may contain tokens. Individual values should be
|
1010
|
* run through webform_replace_tokens() if displaying to an end-user.
|
1011
|
*/
|
1012
|
function _webform_select_options_from_text($text, $flat = FALSE) {
|
1013
|
static $option_cache = array();
|
1014
|
|
1015
|
// Keep each processed option block in an array indexed by the MD5 hash of
|
1016
|
// the option text and the value of the $flat variable.
|
1017
|
$md5 = md5($text);
|
1018
|
|
1019
|
// Check if this option block has been previously processed.
|
1020
|
if (!isset($option_cache[$flat][$md5])) {
|
1021
|
$options = array();
|
1022
|
$rows = array_filter(explode("\n", trim($text)));
|
1023
|
$group = NULL;
|
1024
|
foreach ($rows as $option) {
|
1025
|
$option = trim($option);
|
1026
|
/*
|
1027
|
* If the Key of the option is within < >, treat as an optgroup
|
1028
|
*
|
1029
|
* <Group 1>
|
1030
|
* creates an optgroup with the label "Group 1"
|
1031
|
*
|
1032
|
* <>
|
1033
|
* Unsets the current group, allowing items to be inserted at the root element.
|
1034
|
*/
|
1035
|
if (preg_match('/^\<([^>]*)\>$/', $option, $matches)) {
|
1036
|
if (empty($matches[1])) {
|
1037
|
unset($group);
|
1038
|
}
|
1039
|
elseif (!$flat) {
|
1040
|
$group = $matches[1];
|
1041
|
}
|
1042
|
}
|
1043
|
elseif (preg_match('/^([^|]+)\|(.*)$/', $option, $matches)) {
|
1044
|
$key = $matches[1];
|
1045
|
$value = $matches[2];
|
1046
|
isset($group) ? $options[$group][$key] = $value : $options[$key] = $value;
|
1047
|
}
|
1048
|
else {
|
1049
|
isset($group) ? $options[$group][$option] = $option : $options[$option] = $option;
|
1050
|
}
|
1051
|
}
|
1052
|
|
1053
|
$option_cache[$flat][$md5] = $options;
|
1054
|
}
|
1055
|
|
1056
|
// Return our options from the option_cache array.
|
1057
|
return $option_cache[$flat][$md5];
|
1058
|
}
|
1059
|
|
1060
|
/**
|
1061
|
* Convert an array of options into text.
|
1062
|
*/
|
1063
|
function _webform_select_options_to_text($options) {
|
1064
|
$output = '';
|
1065
|
$previous_key = FALSE;
|
1066
|
|
1067
|
foreach ($options as $key => $value) {
|
1068
|
// Convert groups.
|
1069
|
if (is_array($value)) {
|
1070
|
$output .= '<' . $key . '>' . "\n";
|
1071
|
foreach ($value as $subkey => $subvalue) {
|
1072
|
$output .= $subkey . '|' . $subvalue . "\n";
|
1073
|
}
|
1074
|
$previous_key = $key;
|
1075
|
}
|
1076
|
// Typical key|value pairs.
|
1077
|
else {
|
1078
|
// Exit out of any groups.
|
1079
|
if (isset($options[$previous_key]) && is_array($options[$previous_key])) {
|
1080
|
$output .= "<>\n";
|
1081
|
}
|
1082
|
// Skip empty rows.
|
1083
|
if ($options[$key] !== '') {
|
1084
|
$output .= $key . '|' . $value . "\n";
|
1085
|
}
|
1086
|
$previous_key = $key;
|
1087
|
}
|
1088
|
}
|
1089
|
|
1090
|
return $output;
|
1091
|
}
|
1092
|
|
1093
|
/**
|
1094
|
* Utility function to shuffle an array while preserving key-value pairs.
|
1095
|
*/
|
1096
|
function _webform_shuffle_options(&$array) {
|
1097
|
// First shuffle the array keys, then use them as the basis for ordering
|
1098
|
// the options.
|
1099
|
$aux = array();
|
1100
|
$keys = array_keys($array);
|
1101
|
shuffle($keys);
|
1102
|
foreach ($keys as $key) {
|
1103
|
$aux[$key] = $array[$key];
|
1104
|
}
|
1105
|
$array = $aux;
|
1106
|
}
|