select.inc 38 KB

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