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