select.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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. 'mandatory' => 0,
  14. 'pid' => 0,
  15. 'weight' => 0,
  16. 'value' => '',
  17. 'extra' => array(
  18. 'items' => '',
  19. 'multiple' => NULL,
  20. 'aslist' => NULL,
  21. 'optrand' => 0,
  22. 'other_option' => NULL,
  23. 'other_text' => t('Other...'),
  24. 'title_display' => 0,
  25. 'description' => '',
  26. 'custom_keys' => FALSE,
  27. 'options_source' => '',
  28. 'private' => FALSE,
  29. ),
  30. );
  31. }
  32. /**
  33. * Implements _webform_theme_component().
  34. */
  35. function _webform_theme_select() {
  36. return array(
  37. 'webform_display_select' => array(
  38. 'render element' => 'element',
  39. 'file' => 'components/select.inc',
  40. ),
  41. );
  42. }
  43. /**
  44. * Implements _webform_edit_component().
  45. */
  46. function _webform_edit_select($component) {
  47. $form = array(
  48. '#attached' => array(
  49. 'js' => array(
  50. drupal_get_path('module', 'webform') . '/js/select-admin.js' => array('preprocess' => FALSE),
  51. array('data' => array('webform' => array('selectOptionsUrl' => url('webform/ajax/options/' . $component['nid']))), 'type' => 'setting'),
  52. ),
  53. ),
  54. );
  55. $other = array();
  56. if ($info = _webform_select_options_info()) {
  57. $options = array('' => t('None'));
  58. foreach ($info as $name => $source) {
  59. $options[$name] = $source['title'];
  60. }
  61. $other['options_source'] = array(
  62. '#title' => t('Load a pre-built option list'),
  63. '#type' => 'select',
  64. '#options' => $options,
  65. '#default_value' => $component['extra']['options_source'],
  66. '#weight' => 1,
  67. '#description' => t('Use a pre-built list of options rather than entering options manually. Options will not be editable if using pre-built list.'),
  68. '#parents' => array('extra', 'options_source'),
  69. '#weight' => 5,
  70. );
  71. }
  72. if (module_exists('select_or_other')) {
  73. $other['other_option'] = array(
  74. '#type' => 'checkbox',
  75. '#title' => t('Allow "Other..." option'),
  76. '#default_value' => $component['extra']['other_option'],
  77. '#description' => t('Check this option if you want to allow users to enter an option not on the list.'),
  78. '#parents' => array('extra', 'other_option'),
  79. '#weight' => 2,
  80. );
  81. $other['other_text'] = array(
  82. '#type' => 'textfield',
  83. '#title' => t('Text for "Other..." option'),
  84. '#default_value' => $component['extra']['other_text'],
  85. '#description' => t('If allowing other options, enter text to be used for other-enabling option.'),
  86. '#parents' => array('extra', 'other_text'),
  87. '#weight' => 3,
  88. );
  89. }
  90. if (module_exists('options_element')) {
  91. $options = _webform_select_options($component, FALSE, FALSE);
  92. $form['items'] = array(
  93. '#type' => 'fieldset',
  94. '#title' => t('Options'),
  95. '#collapsible' => TRUE,
  96. '#attributes' => array('class' => array('webform-options-element')),
  97. '#element_validate' => array('_webform_edit_validate_options'),
  98. '#weight' => 2,
  99. );
  100. $form['items']['options'] = array(
  101. '#type' => 'options',
  102. '#limit' => 500,
  103. '#optgroups' => $component['extra']['aslist'],
  104. '#multiple' => $component['extra']['multiple'],
  105. '#multiple_toggle' => t('Multiple'),
  106. '#default_value' => $component['value'],
  107. '#options' => $options,
  108. '#key_type' => 'mixed',
  109. '#key_type_toggle' => t('Customize keys (Advanced)'),
  110. '#key_type_toggled' => $component['extra']['custom_keys'],
  111. '#default_value_pattern' => '^%.+\[.+\]$',
  112. '#disabled' => !empty($component['extra']['options_source']),
  113. '#weight' => 1,
  114. );
  115. $form['items']['options']['option_settings'] = $other;
  116. }
  117. else {
  118. $form['extra']['items'] = array(
  119. '#type' => 'textarea',
  120. '#title' => t('Options'),
  121. '#default_value' => $component['extra']['items'],
  122. '#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'),
  123. '#cols' => 60,
  124. '#rows' => 5,
  125. '#weight' => 0,
  126. '#required' => TRUE,
  127. '#wysiwyg' => FALSE,
  128. '#element_validate' => array('_webform_edit_validate_select'),
  129. );
  130. if (!empty($component['extra']['options_source'])) {
  131. $form['extra']['items']['#attributes'] = array('readonly' => 'readonly');
  132. }
  133. $form['extra'] = array_merge($form['extra'], $other);
  134. $form['value'] = array(
  135. '#type' => 'textfield',
  136. '#title' => t('Default value'),
  137. '#default_value' => $component['value'],
  138. '#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'),
  139. '#size' => 60,
  140. '#maxlength' => 1024,
  141. '#weight' => 0,
  142. );
  143. $form['extra']['multiple'] = array(
  144. '#type' => 'checkbox',
  145. '#title' => t('Multiple'),
  146. '#default_value' => $component['extra']['multiple'],
  147. '#description' => t('Check this option if the user should be allowed to choose multiple values.'),
  148. '#weight' => 0,
  149. );
  150. }
  151. $form['display']['aslist'] = array(
  152. '#type' => 'checkbox',
  153. '#title' => t('Listbox'),
  154. '#default_value' => $component['extra']['aslist'],
  155. '#description' => t('Check this option if you want the select component to be of listbox type instead of radio buttons or checkboxes.'),
  156. '#parents' => array('extra', 'aslist'),
  157. );
  158. $form['display']['optrand'] = array(
  159. '#type' => 'checkbox',
  160. '#title' => t('Randomize options'),
  161. '#default_value' => $component['extra']['optrand'],
  162. '#description' => t('Randomizes the order of the options when they are displayed in the form.'),
  163. '#parents' => array('extra', 'optrand'),
  164. );
  165. return $form;
  166. }
  167. /**
  168. * Element validation callback. Ensure keys are not duplicated.
  169. */
  170. function _webform_edit_validate_select($element, &$form_state) {
  171. // Check for duplicate key values to prevent unexpected data loss. Require
  172. // all options to include a safe_key.
  173. if (!empty($element['#value'])) {
  174. $lines = explode("\n", trim($element['#value']));
  175. $existing_keys = array();
  176. $duplicate_keys = array();
  177. $missing_keys = array();
  178. $long_keys = array();
  179. $group = '';
  180. foreach ($lines as $line) {
  181. $matches = array();
  182. $line = trim($line);
  183. if (preg_match('/^\<([^>]*)\>$/', $line, $matches)) {
  184. $group = $matches[1];
  185. $key = NULL; // No need to store group names.
  186. }
  187. elseif (preg_match('/^([^|]*)\|(.*)$/', $line, $matches)) {
  188. $key = $matches[1];
  189. if (strlen($key) > 128) {
  190. $long_keys[] = $key;
  191. }
  192. }
  193. else {
  194. $missing_keys[] = $line;
  195. }
  196. if (isset($key)) {
  197. if (isset($existing_keys[$group][$key])) {
  198. $duplicate_keys[$key] = $key;
  199. }
  200. else {
  201. $existing_keys[$group][$key] = $key;
  202. }
  203. }
  204. }
  205. if (!empty($missing_keys)) {
  206. form_error($element, t('Every option must have a key specified. Specify each option as "safe_key|Some readable option".'));
  207. }
  208. if (!empty($long_keys)) {
  209. form_error($element, t('Option keys must be less than 128 characters. The following keys exceed this limit:') . theme('item_list', $long_keys));
  210. }
  211. if (!empty($duplicate_keys)) {
  212. 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)));
  213. }
  214. }
  215. return TRUE;
  216. }
  217. /**
  218. * Set the appropriate webform values when using the options element module.
  219. */
  220. function _webform_edit_validate_options($element, &$form_state) {
  221. $key = end($element['#parents']);
  222. $element_options = $form_state['values'][$key]['options'];
  223. unset($form_state['values'][$key]);
  224. $form_state['values']['extra'][$key] = form_options_to_text($element_options['options'], 'custom');
  225. // Options saved for select components.
  226. if ($key == 'items') {
  227. $form_state['values']['extra']['multiple'] = $element_options['multiple'];
  228. $form_state['values']['extra']['custom_keys'] = $element_options['custom_keys'];
  229. $form_state['values']['value'] = is_array($element_options['default_value']) ? implode(', ', $element_options['default_value']) : $element_options['default_value'];
  230. }
  231. // Options saved for grid components.
  232. else {
  233. $form_state['values']['extra']['custom_' . rtrim($key, 's') . '_keys'] = $element_options['custom_keys'];
  234. }
  235. }
  236. /**
  237. * Implements _webform_render_component().
  238. */
  239. function _webform_render_select($component, $value = NULL, $filter = TRUE) {
  240. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  241. $element = array(
  242. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  243. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  244. '#required' => $component['mandatory'],
  245. '#weight' => $component['weight'],
  246. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  247. '#theme_wrappers' => array('webform_element'),
  248. '#pre_render' => array(), // Needed to disable double-wrapping of radios and checkboxes.
  249. '#translatable' => array('title', 'description', 'options'),
  250. );
  251. // Convert the user-entered options list into an array.
  252. $default_value = $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'];
  253. $options = _webform_select_options($component, !$component['extra']['aslist'], $filter);
  254. if ($component['extra']['optrand']) {
  255. _webform_shuffle_options($options);
  256. }
  257. // Add default options if using a select list with no default. This trigger's
  258. // Drupal 7's adding of the option for us. See @form_process_select().
  259. if ($component['extra']['aslist'] && !$component['extra']['multiple'] && $default_value === '') {
  260. $element['#empty_value'] = '';
  261. }
  262. // Set the component options.
  263. $element['#options'] = $options;
  264. // Set the default value.
  265. if (isset($value)) {
  266. if ($component['extra']['multiple']) {
  267. // Set the value as an array.
  268. $element['#default_value'] = array();
  269. foreach ((array) $value as $key => $option_value) {
  270. $element['#default_value'][] = $option_value;
  271. }
  272. }
  273. else {
  274. // Set the value as a single string.
  275. $element['#default_value'] = '';
  276. foreach ((array) $value as $option_value) {
  277. $element['#default_value'] = $option_value;
  278. }
  279. }
  280. }
  281. elseif ($default_value !== '') {
  282. // Convert default value to a list if necessary.
  283. if ($component['extra']['multiple']) {
  284. $varray = explode(',', $default_value);
  285. foreach ($varray as $key => $v) {
  286. $v = trim($v);
  287. if ($v !== '') {
  288. $element['#default_value'][] = $v;
  289. }
  290. }
  291. }
  292. else {
  293. $element['#default_value'] = $default_value;
  294. }
  295. }
  296. elseif ($component['extra']['multiple']) {
  297. $element['#default_value'] = array();
  298. }
  299. else {
  300. $element['#default_value'] = FALSE;
  301. }
  302. if ($component['extra']['other_option'] && module_exists('select_or_other')) {
  303. // Set display as a select_or_other element:
  304. $element['#type'] = 'select_or_other';
  305. $element['#other'] = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
  306. $element['#other_title'] = $element['#title'] . ' ' . $element['#other'];
  307. $element['#other_title_display'] = 'invisible';
  308. $element['#other_unknown_defaults'] = 'other';
  309. $element['#other_delimiter'] = ', ';
  310. // Merge in Webform's #process function for Select or other.
  311. $element['#process'] = array_merge(element_info_property('select_or_other', '#process'), array('webform_expand_select_or_other'));
  312. if ($component['extra']['multiple']) {
  313. $element['#multiple'] = TRUE;
  314. $element['#select_type'] = 'checkboxes';
  315. }
  316. else {
  317. $element['#multiple'] = FALSE;
  318. $element['#select_type'] = 'radios';
  319. }
  320. if ($component['extra']['aslist']) {
  321. $element['#select_type'] = 'select';
  322. }
  323. }
  324. elseif ($component['extra']['aslist']) {
  325. // Set display as a select list:
  326. $element['#type'] = 'select';
  327. if ($component['extra']['multiple']) {
  328. $element['#size'] = 4;
  329. $element['#multiple'] = TRUE;
  330. }
  331. }
  332. else {
  333. if ($component['extra']['multiple']) {
  334. // Set display as a checkbox set.
  335. $element['#type'] = 'checkboxes';
  336. $element['#theme_wrappers'] = array_merge(array('checkboxes'), $element['#theme_wrappers']);
  337. $element['#process'] = array_merge(element_info_property('checkboxes', '#process'), array('webform_expand_select_ids'));
  338. // Entirely replace the normal expand checkboxes with our custom version.
  339. // This helps render checkboxes in multipage forms.
  340. $process_key = array_search('form_process_checkboxes', $element['#process']);
  341. $element['#process'][$process_key] = 'webform_expand_checkboxes';
  342. }
  343. else {
  344. // Set display as a radio set.
  345. $element['#type'] = 'radios';
  346. $element['#theme_wrappers'] = array_merge(array('radios'), $element['#theme_wrappers']);
  347. $element['#process'] = array_merge(element_info_property('radios', '#process'), array('webform_expand_select_ids'));
  348. }
  349. }
  350. return $element;
  351. }
  352. /**
  353. * Process function to ensure select_or_other elements validate properly.
  354. */
  355. function webform_expand_select_or_other($element) {
  356. // Disable validation for back-button and save draft.
  357. $element['select']['#validated'] = TRUE;
  358. $element['select']['#webform_validated'] = FALSE;
  359. $element['other']['#validated'] = TRUE;
  360. $element['other']['#webform_validated'] = FALSE;
  361. // If the default value contains "select_or_other" (the key of the select
  362. // element for the "other..." choice), discard it and set the "other" value.
  363. if (is_array($element['#default_value']) && in_array('select_or_other', $element['#default_value'])) {
  364. $key = array_search('select_or_other', $element['#default_value']);
  365. unset($element['#default_value'][$key]);
  366. $element['#default_value'] = array_values($element['#default_value']);
  367. $element['other']['#default_value'] = implode(', ', $element['#default_value']);
  368. }
  369. // Sanitize the options in Select or other check boxes and radio buttons.
  370. if ($element['#select_type'] == 'checkboxes' || $element['#select_type'] == 'radios') {
  371. $element['select']['#process'] = array_merge(element_info_property($element['#select_type'], '#process'), array('webform_expand_select_ids'));
  372. }
  373. return $element;
  374. }
  375. /**
  376. * Drupal 6 hack that properly *renders* checkboxes in multistep forms. This is
  377. * different than the value hack needed in Drupal 5, which is no longer needed.
  378. */
  379. function webform_expand_checkboxes($element) {
  380. // Elements that have a value set are already in the form structure cause
  381. // them not to be written when the expand_checkboxes function is called.
  382. $default_value = array();
  383. foreach (element_children($element) as $key) {
  384. if (isset($element[$key]['#default_value'])) {
  385. $default_value[$key] = $element[$key]['#default_value'];
  386. unset($element[$key]);
  387. }
  388. }
  389. $element = form_process_checkboxes($element);
  390. // Escape the values of checkboxes.
  391. foreach (element_children($element) as $key) {
  392. $element[$key]['#return_value'] = check_plain($element[$key]['#return_value']);
  393. $element[$key]['#name'] = $element['#name'] . '[' . $element[$key]['#return_value'] . ']';
  394. }
  395. foreach ($default_value as $key => $val) {
  396. $element[$key]['#default_value'] = $val;
  397. }
  398. return $element;
  399. }
  400. /**
  401. * FAPI process function to rename IDs attached to checkboxes and radios.
  402. */
  403. function webform_expand_select_ids($element) {
  404. $id = $element['#id'] = str_replace('_', '-', _webform_safe_name(strip_tags($element['#id'])));
  405. $delta = 0;
  406. foreach (element_children($element) as $key) {
  407. $delta++;
  408. // Convert the #id for each child to a safe name, regardless of key.
  409. $element[$key]['#id'] = $id . '-' . $delta;
  410. // Prevent scripts or CSS in the labels for each checkbox or radio.
  411. $element[$key]['#title'] = _webform_filter_xss($element[$key]['#title']);
  412. }
  413. return $element;
  414. }
  415. /**
  416. * Implements _webform_display_component().
  417. */
  418. function _webform_display_select($component, $value, $format = 'html') {
  419. return array(
  420. '#title' => $component['name'],
  421. '#weight' => $component['weight'],
  422. '#theme' => 'webform_display_select',
  423. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  424. '#format' => $format,
  425. '#options' => _webform_select_options($component, !$component['extra']['aslist']),
  426. '#value' => (array) $value,
  427. '#translatable' => array('title', 'options'),
  428. );
  429. }
  430. /**
  431. * Implements _webform_submit_component().
  432. *
  433. * Convert FAPI 0/1 values into something saveable.
  434. */
  435. function _webform_submit_select($component, $value) {
  436. // Build a list of all valid keys expected to be submitted.
  437. $options = _webform_select_options($component, TRUE);
  438. $return = NULL;
  439. if (is_array($value)) {
  440. $return = array();
  441. foreach ($value as $key => $option_value) {
  442. // Handle options that are specified options.
  443. if ($option_value !== '' && isset($options[$option_value])) {
  444. // Checkboxes submit an integer value of 0 when unchecked. A checkbox
  445. // with a value of '0' is valid, so we can't use empty() here.
  446. if ($option_value === 0 && !$component['extra']['aslist'] && $component['extra']['multiple']) {
  447. unset($value[$option_value]);
  448. }
  449. else {
  450. $return[] = $option_value;
  451. }
  452. }
  453. // Handle options that are added through the "other" field. Specifically
  454. // exclude the "select_or_other" value, which is added by the select list.
  455. elseif ($component['extra']['other_option'] && module_exists('select_or_other') && $option_value != 'select_or_other') {
  456. $return[] = $option_value;
  457. }
  458. }
  459. }
  460. elseif (is_string($value)) {
  461. $return = $value;
  462. }
  463. return $return;
  464. }
  465. /**
  466. * Format the text output for this component.
  467. */
  468. function theme_webform_display_select($variables) {
  469. $element = $variables['element'];
  470. $component = $element['#webform_component'];
  471. // Flatten the list of options so we can get values easily. These options
  472. // may be translated by hook_webform_display_component_alter().
  473. $options = array();
  474. foreach ($element['#options'] as $key => $value) {
  475. if (is_array($value)) {
  476. foreach ($value as $subkey => $subvalue) {
  477. $options[$subkey] = $subvalue;
  478. }
  479. }
  480. else {
  481. $options[$key] = $value;
  482. }
  483. }
  484. $items = array();
  485. if ($component['extra']['multiple']) {
  486. foreach ((array) $element['#value'] as $option_value) {
  487. if ($option_value !== '') {
  488. // Administer provided values.
  489. if (isset($options[$option_value])) {
  490. $items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$option_value]) : $options[$option_value];
  491. }
  492. // User-specified in the "other" field.
  493. else {
  494. $items[] = $element['#format'] == 'html' ? check_plain($option_value) : $option_value;
  495. }
  496. }
  497. }
  498. }
  499. else {
  500. if (isset($element['#value'][0]) && $element['#value'][0] !== '') {
  501. // Administer provided values.
  502. if (isset($options[$element['#value'][0]])) {
  503. $items[] = $element['#format'] == 'html' ? _webform_filter_xss($options[$element['#value'][0]]) : $options[$element['#value'][0]];
  504. }
  505. // User-specified in the "other" field.
  506. else {
  507. $items[] = $element['#format'] == 'html' ? check_plain($element['#value'][0]) : $element['#value'][0];
  508. }
  509. }
  510. }
  511. if ($element['#format'] == 'html') {
  512. $output = count($items) > 1 ? theme('item_list', array('items' => $items)) : (isset($items[0]) ? $items[0] : ' ');
  513. }
  514. else {
  515. if (count($items) > 1) {
  516. foreach ($items as $key => $item) {
  517. $items[$key] = ' - ' . $item;
  518. }
  519. $output = implode("\n", $items);
  520. }
  521. else {
  522. $output = isset($items[0]) ? $items[0] : ' ';
  523. }
  524. }
  525. return $output;
  526. }
  527. /**
  528. * Implements _webform_analysis_component().
  529. */
  530. function _webform_analysis_select($component, $sids = array(), $single = FALSE) {
  531. $options = _webform_select_options($component, TRUE);
  532. $show_other_results = $single;
  533. $sid_placeholders = count($sids) ? array_fill(0, count($sids), "'%s'") : array();
  534. $sid_filter = count($sids) ? " AND sid IN (" . implode(",", $sid_placeholders) . ")" : "";
  535. $option_operator = $show_other_results ? 'NOT IN' : 'IN';
  536. $query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  537. ->fields('wsd', array('data'))
  538. ->condition('nid', $component['nid'])
  539. ->condition('cid', $component['cid'])
  540. ->condition('data', '', '<>')
  541. ->condition('data', array_keys($options), $option_operator)
  542. ->groupBy('data');
  543. $query->addExpression('COUNT(data)', 'datacount');
  544. if (count($sids)) {
  545. $query->condition('sid', $sids, 'IN');
  546. }
  547. $count_query = db_select('webform_submitted_data', 'wsd', array('fetch' => PDO::FETCH_ASSOC))
  548. ->condition('nid', $component['nid'])
  549. ->condition('cid', $component['cid'])
  550. ->condition('data', '', '<>');
  551. $count_query->addExpression('COUNT(*)', 'datacount');
  552. if (count($sids)) {
  553. $count_query->condition('sid', $sids, 'IN');
  554. }
  555. $result = $query->execute();
  556. $rows = array();
  557. $normal_count = 0;
  558. foreach ($result as $data) {
  559. $display_option = $single ? $data['data'] : $options[$data['data']];
  560. $rows[$data['data']] = array(_webform_filter_xss($display_option), $data['datacount']);
  561. $normal_count += $data['datacount'];
  562. }
  563. if (!$show_other_results) {
  564. // Order the results according to the normal options array.
  565. $ordered_rows = array();
  566. foreach (array_intersect_key($options, $rows) as $key => $label) {
  567. $ordered_rows[] = $rows[$key];
  568. }
  569. // Add a row for any unknown or user-entered values.
  570. if ($component['extra']['other_option']) {
  571. $full_count = $count_query->execute()->fetchField();
  572. $other_count = $full_count - $normal_count;
  573. $display_option = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
  574. $other_text = $other_count ? $other_count . ' (' . l(t('view'), 'node/' . $component['nid'] . '/webform-results/analysis/' . $component['cid']) . ')' : $other_count;
  575. $ordered_rows[] = array($display_option, $other_text);
  576. }
  577. $rows = $ordered_rows;
  578. }
  579. return $rows;
  580. }
  581. /**
  582. * Implements _webform_table_component().
  583. */
  584. function _webform_table_select($component, $value) {
  585. // Convert submitted 'safe' values to un-edited, original form.
  586. $options = _webform_select_options($component, TRUE);
  587. $value = (array) $value;
  588. $items = array();
  589. // Set the value as a single string.
  590. foreach ($value as $option_value) {
  591. if ($option_value !== '') {
  592. if (isset($options[$option_value])) {
  593. $items[] = _webform_filter_xss($options[$option_value]);
  594. }
  595. else {
  596. $items[] = check_plain($option_value);
  597. }
  598. }
  599. }
  600. return implode('<br />', $items);
  601. }
  602. /**
  603. * Implements _webform_csv_headers_component().
  604. */
  605. function _webform_csv_headers_select($component, $export_options) {
  606. $headers = array(
  607. 0 => array(),
  608. 1 => array(),
  609. 2 => array(),
  610. );
  611. if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
  612. $headers[0][] = '';
  613. $headers[1][] = $component['name'];
  614. $items = _webform_select_options($component, TRUE, FALSE);
  615. if ($component['extra']['other_option']) {
  616. $other_label = !empty($component['extra']['other_text']) ? check_plain($component['extra']['other_text']) : t('Other...');
  617. $items[$other_label] = $other_label;
  618. }
  619. $count = 0;
  620. foreach ($items as $key => $item) {
  621. // Empty column per sub-field in main header.
  622. if ($count != 0) {
  623. $headers[0][] = '';
  624. $headers[1][] = '';
  625. }
  626. if ($export_options['select_keys']) {
  627. $headers[2][] = $key;
  628. }
  629. else {
  630. $headers[2][] = $item;
  631. }
  632. $count++;
  633. }
  634. }
  635. else {
  636. $headers[0][] = '';
  637. $headers[1][] = '';
  638. $headers[2][] = $component['name'];
  639. }
  640. return $headers;
  641. }
  642. /**
  643. * Implements _webform_csv_data_component().
  644. */
  645. function _webform_csv_data_select($component, $export_options, $value) {
  646. $options = _webform_select_options($component, TRUE, FALSE);
  647. $return = array();
  648. if ($component['extra']['multiple']) {
  649. foreach ($options as $key => $item) {
  650. $index = array_search($key, (array) $value);
  651. if ($index !== FALSE) {
  652. if ($export_options['select_format'] == 'separate') {
  653. $return[] = 'X';
  654. }
  655. else {
  656. $return[] = $export_options['select_keys'] ? $key : $item;
  657. }
  658. unset($value[$index]);
  659. }
  660. elseif ($export_options['select_format'] == 'separate') {
  661. $return[] = '';
  662. }
  663. }
  664. // Any remaining items in the $value array will be user-added options.
  665. if ($component['extra']['other_option']) {
  666. $return[] = count($value) ? implode(',', $value) : '';
  667. }
  668. }
  669. else {
  670. $key = $value[0];
  671. if ($export_options['select_keys']) {
  672. $return = $key;
  673. }
  674. else {
  675. $return = isset($options[$key]) ? $options[$key] : $key;
  676. }
  677. }
  678. if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
  679. $return = implode(',', (array) $return);
  680. }
  681. return $return;
  682. }
  683. /**
  684. * Menu callback; Return a predefined list of select options as JSON.
  685. */
  686. function webform_select_options_ajax($source_name = '') {
  687. $info = _webform_select_options_info();
  688. $component['extra']['options_source'] = $source_name;
  689. if ($source_name && isset($info[$source_name])) {
  690. $options = _webform_select_options_to_text(_webform_select_options($component, !$component['extra']['aslist'], FALSE));
  691. }
  692. else {
  693. $options = '';
  694. }
  695. $return = array(
  696. 'elementId' => module_exists('options_element') ? 'edit-items-options-options-field-widget' : 'edit-extra-items',
  697. 'options' => $options,
  698. );
  699. drupal_json_output($return);
  700. }
  701. /**
  702. * Generate a list of options for a select list.
  703. */
  704. function _webform_select_options($component, $flat = FALSE, $filter = TRUE) {
  705. if ($component['extra']['options_source']) {
  706. $options = _webform_select_options_callback($component['extra']['options_source'], $component, $flat, $filter);
  707. }
  708. else {
  709. $options = _webform_select_options_from_text($component['extra']['items'], $flat, $filter);
  710. }
  711. return isset($options) ? $options : array();
  712. }
  713. /**
  714. * Load Webform select option info from 3rd party modules.
  715. */
  716. function _webform_select_options_info() {
  717. static $info;
  718. if (!isset($info)) {
  719. $info = array();
  720. foreach (module_implements('webform_select_options_info') as $module) {
  721. $additions = module_invoke($module, 'webform_select_options_info');
  722. foreach ($additions as $key => $addition) {
  723. $additions[$key]['module'] = $module;
  724. }
  725. $info = array_merge($info, $additions);
  726. }
  727. drupal_alter('webform_select_options_info', $info);
  728. }
  729. return $info;
  730. }
  731. /**
  732. * Execute a select option callback.
  733. *
  734. * @param $name
  735. * The name of the options group.
  736. * @param $component
  737. * The full Webform component.
  738. * @param $flat
  739. * Whether the information returned should exclude any nested groups.
  740. * @param $filter
  741. * Whether information returned should be sanitized. Defaults to TRUE.
  742. */
  743. function _webform_select_options_callback($name, $component, $flat = FALSE, $filter = TRUE) {
  744. $info = _webform_select_options_info();
  745. // Include any necessary files.
  746. if (isset($info[$name]['file'])) {
  747. $pathinfo = pathinfo($info[$name]['file']);
  748. $path = ($pathinfo['dirname'] ? $pathinfo['dirname'] . '/' : '') . basename($pathinfo['basename'], '.' . $pathinfo['extension']);
  749. module_load_include($pathinfo['extension'], $info[$name]['module'], $path);
  750. }
  751. // Execute the callback function.
  752. if (isset($info[$name]['options callback']) && function_exists($info[$name]['options callback'])) {
  753. $function = $info[$name]['options callback'];
  754. $arguments = array();
  755. if (isset($info[$name]['options arguments'])) {
  756. $arguments = $info[$name]['options arguments'];
  757. }
  758. return $function($component, $flat, $filter, $arguments);
  759. }
  760. }
  761. /**
  762. * Utility function to split user-entered values from new-line seperated
  763. * text into an array of options.
  764. *
  765. * @param $text
  766. * Text to be converted into a select option array.
  767. * @param $flat
  768. * Optional. If specified, return the option array and exclude any optgroups.
  769. * @param $filter
  770. * Optional. Whether or not to filter returned values.
  771. */
  772. function _webform_select_options_from_text($text, $flat = FALSE, $filter = TRUE) {
  773. static $option_cache = array();
  774. // Keep each processed option block in an array indexed by the MD5 hash of
  775. // the option text and the value of the $flat variable.
  776. $md5 = md5($text);
  777. // Check if this option block has been previously processed.
  778. if (!isset($option_cache[$flat][$md5])) {
  779. $options = array();
  780. $rows = array_filter(explode("\n", trim($text)));
  781. $group = NULL;
  782. foreach ($rows as $option) {
  783. $option = trim($option);
  784. /**
  785. * If the Key of the option is within < >, treat as an optgroup
  786. *
  787. * <Group 1>
  788. * creates an optgroup with the label "Group 1"
  789. *
  790. * <>
  791. * Unsets the current group, allowing items to be inserted at the root element.
  792. */
  793. if (preg_match('/^\<([^>]*)\>$/', $option, $matches)) {
  794. if (empty($matches[1])) {
  795. unset($group);
  796. }
  797. elseif (!$flat) {
  798. $group = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
  799. }
  800. }
  801. elseif (preg_match('/^([^|]+)\|(.*)$/', $option, $matches)) {
  802. $key = $filter ? _webform_filter_values($matches[1], NULL, NULL, NULL, FALSE) : $matches[1];
  803. $value = $filter ? _webform_filter_values($matches[2], NULL, NULL, NULL, FALSE) : $matches[2];
  804. isset($group) ? $options[$group][$key] = $value : $options[$key] = $value;
  805. }
  806. else {
  807. $filtered_option = $filter ? _webform_filter_values($option, NULL, NULL, NULL, FALSE) : $option;
  808. isset($group) ? $options[$group][$filtered_option] = $filtered_option : $options[$filtered_option] = $filtered_option;
  809. }
  810. }
  811. $option_cache[$flat][$md5] = $options;
  812. }
  813. // Return our options from the option_cache array.
  814. return $option_cache[$flat][$md5];
  815. }
  816. /**
  817. * Convert an array of options into text.
  818. */
  819. function _webform_select_options_to_text($options) {
  820. $output = '';
  821. $previous_key = FALSE;
  822. foreach ($options as $key => $value) {
  823. // Convert groups.
  824. if (is_array($value)) {
  825. $output .= '<' . $key . '>' . "\n";
  826. foreach ($value as $subkey => $subvalue) {
  827. $output .= $subkey . '|' . $subvalue . "\n";
  828. }
  829. $previous_key = $key;
  830. }
  831. // Typical key|value pairs.
  832. else {
  833. // Exit out of any groups.
  834. if (isset($options[$previous_key]) && is_array($options[$previous_key])) {
  835. $output .= "<>\n";
  836. }
  837. // Skip empty rows.
  838. if ($options[$key] !== '') {
  839. $output .= $key . '|' . $value . "\n";
  840. }
  841. $previous_key = $key;
  842. }
  843. }
  844. return $output;
  845. }
  846. /**
  847. * Utility function to shuffle an array while preserving key-value pairs.
  848. */
  849. function _webform_shuffle_options(&$array) {
  850. // First shuffle the array keys, then use them as the basis for ordering
  851. // the options.
  852. $aux = array();
  853. $keys = array_keys($array);
  854. shuffle($keys);
  855. foreach ($keys as $key) {
  856. $aux[$key] = $array[$key];
  857. }
  858. $array = $aux;
  859. }