grid.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <?php
  2. /**
  3. * @file
  4. * Webform module grid component.
  5. */
  6. // Grid depends on functions provided by select.
  7. webform_component_include('select');
  8. /**
  9. * Implements _webform_defaults_component().
  10. */
  11. function _webform_defaults_grid() {
  12. return array(
  13. 'name' => '',
  14. 'form_key' => NULL,
  15. 'required' => 0,
  16. 'pid' => 0,
  17. 'weight' => 0,
  18. 'value' => '',
  19. 'extra' => array(
  20. 'options' => '',
  21. 'questions' => '',
  22. 'optrand' => 0,
  23. 'qrand' => 0,
  24. 'unique' => 0,
  25. 'title_display' => 0,
  26. 'custom_option_keys' => 0,
  27. 'custom_question_keys' => 0,
  28. 'sticky' => TRUE,
  29. 'description' => '',
  30. 'description_above' => FALSE,
  31. 'private' => FALSE,
  32. 'analysis' => TRUE,
  33. ),
  34. );
  35. }
  36. /**
  37. * Implements _webform_theme_component().
  38. */
  39. function _webform_theme_grid() {
  40. return array(
  41. 'webform_grid' => array(
  42. 'render element' => 'element',
  43. 'file' => 'components/grid.inc',
  44. ),
  45. 'webform_display_grid' => array(
  46. 'render element' => 'element',
  47. 'file' => 'components/grid.inc',
  48. ),
  49. );
  50. }
  51. /**
  52. * Implements _webform_edit_component().
  53. */
  54. function _webform_edit_grid($component) {
  55. $form = array();
  56. $form['help'] = array(
  57. '#type' => 'fieldset',
  58. '#collapsible' => TRUE,
  59. '#collapsed' => !empty($component['cid']),
  60. '#title' => t('About options and questions&hellip;'),
  61. '#description' => t('Options and questions may be configured here, in additional nested Select Options components, or even both.'),
  62. '#weight' => -4,
  63. 'pros_and_cons' => array(
  64. '#theme' => 'table',
  65. '#header' => array('', t('Options and questions configured <strong>here</strong>'), t('Configured in additional <strong>nested</strong> components'), t('Both')),
  66. '#rows' => array(
  67. array(t('Questions'), t('Enter the questions below.'), t('Configure and save this grid, then add additional Select Options components nested (indented) below this grid.'), t('Additional questions from nested components will be displayed below any questions configured here.')),
  68. array(t('Options'), t('Enter options below.'), t('May be different for each question. Initially the same as defined below.'), t('Options from additional nested components will be merged with any options configured here.')),
  69. array(t('Checkboxes'), t('No. Radio buttons only.'), t('Yes. Some or all questions may be multiple choice with check boxes.'), ''),
  70. array(t('Default'), t('Yes. Must be same for all questions.'), t('Yes. May all be the same or different.'), ''),
  71. array(t('Pre-built option lists'), t('No.'), t('Yes.'), ''),
  72. array(t('Required'), t('Yes. Must be same for all questions.'), t('Yes. May all be the same or different.'), ''),
  73. array(t('Question conditionals'), t('No.'), t('Yes. Individual questions may be used in conditional rules and/or actions.'), t('The whole grid may be conditionally shown or required.')),
  74. array(t('Other types of nested components'), t('No.'), t('Yes. Other component types may also be included in the grid. They will be displayed where the options would normally be.'), ''),
  75. ),
  76. ),
  77. );
  78. if (module_exists('options_element')) {
  79. $form['options'] = array(
  80. '#type' => 'fieldset',
  81. '#title' => t('Options'),
  82. '#collapsible' => TRUE,
  83. '#attributes' => array('class' => array('webform-options-element')),
  84. '#element_validate' => array('_webform_edit_validate_options'),
  85. '#weight' => -3,
  86. );
  87. $form['options']['options'] = array(
  88. '#type' => 'options',
  89. '#options' => _webform_select_options_from_text($component['extra']['options'], TRUE),
  90. '#default_value' => $component['value'],
  91. '#default_value_allowed' => TRUE,
  92. '#optgroups' => FALSE,
  93. '#key_type' => 'mixed',
  94. '#key_type_toggle' => t('Customize option keys (Advanced)'),
  95. '#key_type_toggled' => $component['extra']['custom_option_keys'],
  96. '#default_value_pattern' => '^%.+\[.+\]$',
  97. '#description' => t('<strong>Options to select across the top</strong>, such as "Poor" through "Excellent". Indicate the default to the left of the desired item. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
  98. );
  99. $form['questions'] = array(
  100. '#type' => 'fieldset',
  101. '#title' => t('Questions'),
  102. '#collapsible' => TRUE,
  103. '#attributes' => array('class' => array('webform-options-element')),
  104. '#element_validate' => array('_webform_edit_validate_options'),
  105. '#weight' => -2,
  106. );
  107. $form['questions']['options'] = array(
  108. '#type' => 'options',
  109. '#options' => _webform_select_options_from_text($component['extra']['questions'], TRUE),
  110. '#optgroups' => FALSE,
  111. '#default_value' => FALSE,
  112. '#default_value_allowed' => FALSE,
  113. '#key_type' => 'mixed',
  114. '#key_type_toggle' => t('Customize question keys (Advanced)'),
  115. '#key_type_toggled' => $component['extra']['custom_question_keys'],
  116. '#description' => t('<strong>Questions list down the side of the grid.</strong> For a heading column on the right, append "|" and the right-side heading. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help'),
  117. );
  118. }
  119. else {
  120. $form['extra']['options'] = array(
  121. '#type' => 'textarea',
  122. '#title' => t('Options'),
  123. '#default_value' => $component['extra']['options'],
  124. '#description' => t('Options to select across the top, such as "Poor" through "Excellent" or "Stronly Disagree" through "Strongly Agree".') .
  125. '<p>' . t('One key-value option per line. <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.') . '</p>' . theme('webform_token_help'),
  126. '#cols' => 60,
  127. '#rows' => 5,
  128. '#weight' => -3,
  129. '#wysiwyg' => FALSE,
  130. '#element_validate' => array('_webform_edit_validate_select'),
  131. );
  132. $form['extra']['questions'] = array(
  133. '#type' => 'textarea',
  134. '#title' => t('Questions'),
  135. '#default_value' => $component['extra']['questions'],
  136. '#description' => t('Questions list down the side of the grid. One question per line. <strong>Key-value pairs MUST be specified as "safe_key|Some readable question"</strong>. For a heading column on the right, append "|" and the right-side heading. Use of only alphanumeric characters and underscores is recommended in keys.') . ' ' . theme('webform_token_help') . ' ' .
  137. '<p>' . t('<strong>Or for more control</strong> over the appearance and configuration, create additional additional Select Options or other type components nested under this grid. They will operate as separate components, but be displayed within this grid.') . '</p>',
  138. '#cols' => 60,
  139. '#rows' => 5,
  140. '#weight' => -2,
  141. '#wysiwyg' => FALSE,
  142. '#element_validate' => array('_webform_edit_validate_select'),
  143. );
  144. $form['value'] = array(
  145. '#type' => 'textfield',
  146. '#title' => t('Default value'),
  147. '#default_value' => $component['value'],
  148. '#description' => t('The default option of the grid identified by its key.') . ' ' . theme('webform_token_help'),
  149. '#size' => 60,
  150. '#maxlength' => 1024,
  151. '#weight' => 1,
  152. );
  153. }
  154. $form['display']['optrand'] = array(
  155. '#type' => 'checkbox',
  156. '#title' => t('Randomize Options'),
  157. '#default_value' => $component['extra']['optrand'],
  158. '#description' => t('Randomizes the order of options on the top when they are displayed in the form.'),
  159. '#parents' => array('extra', 'optrand'),
  160. );
  161. $form['display']['qrand'] = array(
  162. '#type' => 'checkbox',
  163. '#title' => t('Randomize Questions'),
  164. '#default_value' => $component['extra']['qrand'],
  165. '#description' => t('Randomize the order of the questions on the side when they are displayed in the form.'),
  166. '#parents' => array('extra', 'qrand'),
  167. );
  168. $form['display']['sticky'] = array(
  169. '#type' => 'checkbox',
  170. '#title' => t('Sticky table header'),
  171. '#default_value' => $component['extra']['sticky'],
  172. '#description' => t('Use a sticky (non-scrolling) table header.'),
  173. '#parents' => array('extra', 'sticky'),
  174. );
  175. $form['validation']['unique'] = array(
  176. '#type' => 'checkbox',
  177. '#title' => t('Unique'),
  178. '#return_value' => 1,
  179. '#description' => t('Check that all entered values for this field are unique. The same value is not allowed to be used twice.'),
  180. '#weight' => 1,
  181. '#default_value' => $component['extra']['unique'],
  182. '#parents' => array('extra', 'unique'),
  183. );
  184. return $form;
  185. }
  186. /**
  187. * Implements _webform_render_component().
  188. */
  189. function _webform_render_grid($component, $value = NULL, $filter = TRUE, $submission = NULL) {
  190. $node = isset($component['nid']) ? node_load($component['nid']) : NULL;
  191. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  192. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  193. if ($filter) {
  194. $questions = _webform_select_replace_tokens($questions, $node);
  195. $options = _webform_select_replace_tokens($options, $node);
  196. }
  197. $element = array(
  198. '#type' => 'webform_grid',
  199. '#title' => $filter ? webform_filter_xss($component['name']) : $component['name'],
  200. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  201. '#required' => $component['required'],
  202. '#weight' => $component['weight'],
  203. '#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
  204. '#grid_questions' => $questions,
  205. '#grid_options' => $options,
  206. '#default_value' => isset($value) || !strlen($component['value']) ? $value : array_fill_keys(array_keys($questions), $component['value']),
  207. '#grid_default' => $component['value'],
  208. '#optrand' => $component['extra']['optrand'],
  209. '#qrand' => $component['extra']['qrand'],
  210. '#sticky' => $component['extra']['sticky'],
  211. '#theme' => 'webform_grid',
  212. '#theme_wrappers' => array('webform_element'),
  213. '#process' => array('webform_expand_grid'),
  214. '#element_validate' => array('webform_validate_grid'),
  215. '#translatable' => array('title', 'description', 'grid_options', 'grid_questions'),
  216. );
  217. // Enforce uniqueness.
  218. if ($component['extra']['unique']) {
  219. $element['#element_validate'][] = '_webform_edit_grid_unique_validate';
  220. }
  221. return $element;
  222. }
  223. /**
  224. * A Form API #process function for Webform grid fields.
  225. */
  226. function webform_expand_grid($element) {
  227. $options = $element['#grid_options'];
  228. $questions = $element['#grid_questions'];
  229. $weights = array();
  230. // Process questions and options from nested components.
  231. foreach (element_children($element) as $key) {
  232. $question = $element[$key];
  233. // Both forms and grid displays have #webform_component.
  234. if (isset($question['#webform_component']) &&
  235. $question['#webform_component']['type'] == 'select' &&
  236. !$question['#webform_component']['extra']['aslist'] &&
  237. !$question['#webform_component']['extra']['other_option']) {
  238. $options = webform_grid_merge_options($options, $question['#options']);
  239. $weights[$key] = $question['#weight'];
  240. }
  241. }
  242. // Add the internal grid questions.
  243. $weight = -1000;
  244. $value = isset($element['#default_value']) ? $element['#default_value'] : array();
  245. foreach ($questions as $key => $question) {
  246. if ($question != '') {
  247. $question_value = isset($value[$key]) && $value[$key] !== '' ? $value[$key] : NULL;
  248. $element[$key] = array(
  249. '#grid_question' => TRUE,
  250. '#title' => $question,
  251. '#required' => $element['#required'],
  252. '#options' => $element['#grid_options'],
  253. '#type' => 'radios',
  254. '#default_value' => $question_value,
  255. '#value' => $question_value,
  256. '#process' => array('form_process_radios', 'webform_expand_select_ids'),
  257. // Webform handles validation manually.
  258. '#validated' => TRUE,
  259. '#webform_validated' => FALSE,
  260. '#translatable' => array('title'),
  261. '#weight' => $weight,
  262. );
  263. // Add HTML5 required attribute, if needed.
  264. if ($element['#required']) {
  265. $element[$key]['#attributes']['required'] = 'required';
  266. }
  267. $weights[$key] = $weight;
  268. $weight++;
  269. }
  270. }
  271. if (!empty($element['#optrand'])) {
  272. _webform_shuffle_options($options);
  273. }
  274. $element['#grid_options'] = $options;
  275. asort($weights);
  276. if (!empty($element['#qrand'])) {
  277. _webform_shuffle_options($weights);
  278. }
  279. if ($weights) {
  280. $weight = min($weights);
  281. }
  282. foreach ($weights as $key => $old_weight) {
  283. $element[$key]['#options'] = webform_grid_remove_options($options, $element[$key]['#options']);
  284. $element[$key]['#weight'] = $weight++;
  285. $element['#grid_questions'][$key] = $element[$key]['#title'];
  286. }
  287. return $element;
  288. }
  289. /**
  290. * Helper. Merge select component options in order.
  291. *
  292. * @param array $existing
  293. * An array of existing values into which any values from $new that aren't in
  294. * $existing are inserted.
  295. * @param array $new
  296. * Values to be inserted into $existing.
  297. *
  298. * @return array
  299. * The merged array.
  300. */
  301. function webform_grid_merge_options(array $existing, array $new) {
  302. $insert = NULL;
  303. $queue = array();
  304. foreach ($new as $key => $value) {
  305. if (isset($existing[$key])) {
  306. // Insert the queue before the found item.
  307. $insert = array_search($key, array_keys($existing));
  308. if ($queue) {
  309. $existing = array_slice($existing, 0, $insert, TRUE) +
  310. $queue +
  311. array_slice($existing, $insert, NULL, TRUE);
  312. $insert += count($queue);
  313. $queue = array();
  314. }
  315. $insert++;
  316. }
  317. elseif (is_null($insert)) {
  318. // It is not yet clear yet where to put this item. Add it to the queue.
  319. $queue[$key] = $value;
  320. }
  321. else {
  322. // PHP array_splice does not preserved the keys of the inserted array,
  323. // but array_slice does (if the preserve keys parameter is TRUE).
  324. $existing = array_slice($existing, 0, $insert, TRUE) +
  325. array($key => $value) +
  326. array_slice($existing, $insert, NULL, TRUE);
  327. $insert++;
  328. }
  329. }
  330. // Append any left over queued items.
  331. $existing += $queue;
  332. return $existing;
  333. }
  334. /**
  335. * Helper. Replace missing options with empty values.
  336. *
  337. * @param array $header
  338. * An array of options to be used at the grid table header.
  339. * @param array $row_options
  340. * An array of options to be used for this row.
  341. *
  342. * @return array
  343. * The $row_options with any missing options replaced with empty values.
  344. */
  345. function webform_grid_remove_options(array $header, array $row_options) {
  346. foreach ($header as $key => $value) {
  347. if (!isset($row_options[$key])) {
  348. $header[$key] = '';
  349. }
  350. }
  351. return $header;
  352. }
  353. /**
  354. * Implements _webform_display_component().
  355. */
  356. function _webform_display_grid($component, $value, $format = 'html', $submission = array()) {
  357. $node = node_load($component['nid']);
  358. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  359. $questions = _webform_select_replace_tokens($questions, $node);
  360. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  361. $options = _webform_select_replace_tokens($options, $node);
  362. $element = array(
  363. '#title' => $component['name'],
  364. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  365. '#weight' => $component['weight'],
  366. '#format' => $format,
  367. '#grid_questions' => $questions,
  368. '#grid_options' => $options,
  369. '#default_value' => $value,
  370. '#sticky' => $component['extra']['sticky'],
  371. '#theme' => 'webform_display_grid',
  372. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  373. '#sorted' => TRUE,
  374. '#translatable' => array('#title', '#grid_questions', '#grid_options'),
  375. );
  376. foreach ($questions as $key => $question) {
  377. if ($question !== '') {
  378. $element[$key] = array(
  379. '#title' => $question,
  380. '#value' => isset($value[$key]) ? $value[$key] : NULL,
  381. '#translatable' => array('#title', '#value'),
  382. );
  383. }
  384. }
  385. return $element;
  386. }
  387. /**
  388. * Preprocess function for displaying a grid component.
  389. */
  390. function template_preprocess_webform_display_grid(&$variables) {
  391. $element =& $variables['element'];
  392. // Expand the grid, suppressing randomization. This builds the grid
  393. // questions and options.
  394. $element['#qrand'] = FALSE;
  395. $element['#optrand'] = FALSE;
  396. $element['#required'] = FALSE;
  397. $element = webform_expand_grid($element);
  398. }
  399. /**
  400. * Format the text output for this component.
  401. */
  402. function theme_webform_display_grid($variables) {
  403. $element = $variables['element'];
  404. $format = $element['#format'];
  405. if ($format == 'html') {
  406. $right_titles = _webform_grid_right_titles($element);
  407. $rows = array();
  408. // Set the header for the table.
  409. $header = _webform_grid_header($element, $right_titles);
  410. foreach (element_children($element) as $question_key) {
  411. $question_element = $element[$question_key];
  412. $row = array();
  413. $questions = explode('|', $question_element['#title'], 2);
  414. $values = $question_element['#value'];
  415. $values = is_array($values) ? $values : array($values);
  416. $row[] = array('data' => webform_filter_xss($questions[0]), 'class' => array('webform-grid-question'));
  417. if (isset($element['#grid_questions'][$question_key])) {
  418. foreach ($element['#grid_options'] as $option_value => $option_label) {
  419. if (in_array($option_value, $values)) {
  420. $row[] = array('data' => '<strong>X</strong>', 'class' => array('checkbox', 'webform-grid-option'));
  421. }
  422. else {
  423. $row[] = array('data' => '&nbsp;', 'class' => array('checkbox', 'webform-grid-option'));
  424. }
  425. }
  426. }
  427. else {
  428. $question_element['#title_display'] = 'none';
  429. $row[] = array(
  430. 'data' => drupal_render($question_element),
  431. 'colspan' => count($element['#grid_options']),
  432. );
  433. }
  434. if ($right_titles) {
  435. $row[] = array('data' => isset($questions[1]) ? webform_filter_xss($questions[1]) : '', 'class' => array('webform-grid-question'));
  436. }
  437. $rows[] = $row;
  438. }
  439. $option_count = count($header) - 1;
  440. $output = theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => $element['#sticky'], 'attributes' => array('class' => array('webform-grid', 'webform-grid-' . $option_count))));
  441. }
  442. else {
  443. $items = array();
  444. foreach (element_children($element) as $question_key) {
  445. $question_element = $element[$question_key];
  446. if (isset($element['#grid_questions'][$question_key])) {
  447. $values = $question_element['#value'];
  448. $values = is_array($values) ? $values : array($values);
  449. foreach ($values as $value_key => $value) {
  450. if (isset($element['#grid_options'][$value])) {
  451. $values[$value_key] = $element['#grid_options'][$value];
  452. }
  453. else {
  454. unset($values[$value_key]);
  455. }
  456. }
  457. $value = implode(', ', $values);
  458. }
  459. else {
  460. $element[$question_key]['#title'] = '';
  461. $value = drupal_render($element[$question_key]);
  462. }
  463. $items[] = ' - ' . _webform_grid_question_header($question_element['#title']) . ': ' . $value;
  464. }
  465. $output = implode("\n", $items);
  466. }
  467. return $output;
  468. }
  469. /**
  470. * Implements _webform_analysis_component().
  471. */
  472. function _webform_analysis_grid($component, $sids = array(), $single = FALSE, $join = NULL) {
  473. // Generate the list of options and questions.
  474. $node = node_load($component['nid']);
  475. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  476. $questions = _webform_select_replace_tokens($questions, $node);
  477. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  478. $options = _webform_select_replace_tokens($options, $node);
  479. // Generate a lookup table of results.
  480. $query = db_select('webform_submitted_data', 'wsd')
  481. ->fields('wsd', array('no', 'data'))
  482. ->condition('wsd.nid', $component['nid'])
  483. ->condition('wsd.cid', $component['cid'])
  484. ->condition('wsd.data', '', '<>')
  485. ->groupBy('wsd.no')
  486. ->groupBy('wsd.data');
  487. $query->addExpression('COUNT(wsd.sid)', 'datacount');
  488. if (count($sids)) {
  489. $query->condition('wsd.sid', $sids, 'IN');
  490. }
  491. if ($join) {
  492. $query->innerJoin($join, 'ws2_', 'wsd.sid = ws2_.sid');
  493. }
  494. $result = $query->execute();
  495. $counts = array();
  496. foreach ($result as $data) {
  497. $counts[$data->no][$data->data] = $data->datacount;
  498. }
  499. // Create an entire table to be put into the returned row.
  500. $rows = array();
  501. $header = array('');
  502. // Add options as a header row.
  503. foreach ($options as $option) {
  504. $header[] = webform_filter_xss($option);
  505. }
  506. // Add questions as each row.
  507. foreach ($questions as $qkey => $question) {
  508. $row = array(webform_filter_xss($question));
  509. foreach ($options as $okey => $option) {
  510. $row[] = !empty($counts[$qkey][$okey]) ? $counts[$qkey][$okey] : 0;
  511. }
  512. $rows[] = $row;
  513. }
  514. // Return return the table unless there are no internal questions in the grid.
  515. if ($rows) {
  516. return array(
  517. 'table_header' => $header,
  518. 'table_rows' => $rows,
  519. );
  520. }
  521. }
  522. /**
  523. * Implements _webform_table_component().
  524. */
  525. function _webform_table_grid($component, $value) {
  526. $node = node_load($component['nid']);
  527. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  528. $questions = _webform_select_replace_tokens($questions, $node);
  529. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  530. $options = _webform_select_replace_tokens($options, $node);
  531. $output = '';
  532. // Set the value as a single string.
  533. foreach ($questions as $key => $label) {
  534. if (isset($value[$key]) && isset($options[$value[$key]])) {
  535. $output .= webform_filter_xss(_webform_grid_question_header($label)) . ': ' . webform_filter_xss($options[$value[$key]]) . '<br />';
  536. }
  537. }
  538. // Return output if the grid contains internal questions.
  539. if (count($questions)) {
  540. return $output;
  541. }
  542. }
  543. /**
  544. * Implements _webform_csv_headers_component().
  545. */
  546. function _webform_csv_headers_grid($component, $export_options) {
  547. $node = node_load($component['nid']);
  548. $items = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  549. $items = _webform_select_replace_tokens($items, $node);
  550. $header = array();
  551. $header[0] = array('');
  552. $header[1] = array($export_options['header_keys'] ? $component['form_key'] : $component['name']);
  553. $count = 0;
  554. foreach ($items as $key => $item) {
  555. // Empty column per sub-field in main header.
  556. if ($count != 0) {
  557. $header[0][] = '';
  558. $header[1][] = '';
  559. }
  560. // The value for this option.
  561. $header[2][] = $export_options['header_keys'] ? $key : _webform_grid_question_header($item);
  562. $count++;
  563. }
  564. return $header;
  565. }
  566. /**
  567. * Implements _webform_csv_data_component().
  568. */
  569. function _webform_csv_data_grid($component, $export_options, $value) {
  570. $node = node_load($component['nid']);
  571. $questions = _webform_select_options_from_text($component['extra']['questions'], TRUE);
  572. $questions = _webform_select_replace_tokens($questions, $node);
  573. $options = _webform_select_options_from_text($component['extra']['options'], TRUE);
  574. $options = _webform_select_replace_tokens($options, $node);
  575. $return = array();
  576. foreach ($questions as $key => $question) {
  577. if (isset($value[$key]) && isset($options[$value[$key]])) {
  578. $return[] = $export_options['select_keys'] ? $value[$key] : $options[$value[$key]];
  579. }
  580. else {
  581. $return[] = '';
  582. }
  583. }
  584. return $return;
  585. }
  586. /**
  587. * A Form API element validate function to check that all choices are unique.
  588. */
  589. function _webform_edit_grid_unique_validate($element) {
  590. // Grids may contain nested multiple value select components.
  591. // Create a flat array of values.
  592. $values = array();
  593. $element['#value'] = (array) $element['#value'];
  594. array_walk_recursive($element['#value'], function ($a) use (&$values) {
  595. $values[] = $a;
  596. });
  597. $nr_unique = count(array_unique($values));
  598. $nr_values = count($values);
  599. $nr_possible = count($element['#grid_options']);
  600. if (strlen($element['#grid_default']) && isset($element['#grid_options'][$element['#grid_default']])) {
  601. // A default is defined and is one of the options. Don't count default values
  602. // toward uniqueness.
  603. $nr_defaults = count(array_keys($element['#value'], $element['#grid_default']));
  604. if ($nr_defaults) {
  605. $nr_values -= $nr_defaults;
  606. $nr_unique--;
  607. }
  608. }
  609. if ($nr_unique < $nr_values && $nr_unique < $nr_possible) {
  610. // Fewer unique values than values means that at least one value is duplicated.
  611. // Fewer unique values than possible values means that there is a possible choice
  612. // that wasn't used.
  613. form_error($element, t('!title is not allowed to have the same answer for more than one question.', array('!title' => $element['#title'])));
  614. }
  615. }
  616. /**
  617. * Theme function to render a grid component.
  618. */
  619. function theme_webform_grid($variables) {
  620. $element = $variables['element'];
  621. $right_titles = _webform_grid_right_titles($element);
  622. $rows = array();
  623. // Set the header for the table.
  624. $header = _webform_grid_header($element, $right_titles);
  625. foreach (element_children($element) as $key) {
  626. $question_element = $element[$key];
  627. $title_element =& $question_element;
  628. if ($question_element['#type'] == 'select_or_other') {
  629. $title_element =& $question_element['select'];
  630. }
  631. $question_titles = explode('|', $title_element['#title'], 2);
  632. // Create a row with the question title.
  633. $required = !empty($question_element['#required']) ? theme('form_required_marker', array('element' => $question_element)) : '';
  634. $row = array(array('data' => t('!title !required', array('!title' => webform_filter_xss($question_titles[0]), '!required' => $required)), 'class' => array('webform-grid-question')));
  635. // Render each radio button in the row.
  636. if ($question_element['#type'] == 'radios' || $question_element['#type'] == 'checkboxes') {
  637. $radios = form_process_radios($question_element);
  638. foreach (element_children($radios) as $key) {
  639. $radio_title = $radios[$key]['#title'];
  640. if (!strlen($radio_title)) {
  641. $row[] = '&nbsp;';
  642. }
  643. else {
  644. $radios[$key]['#title'] = $question_element['#title'] . ' - ' . $radio_title;
  645. $radios[$key]['#title_display'] = 'invisible';
  646. $row[] = array('data' => drupal_render($radios[$key]), 'class' => array('checkbox', 'webform-grid-option'), 'data-label' => array($radio_title));
  647. }
  648. }
  649. }
  650. else {
  651. $title_element['#title_display'] = 'none';
  652. $row[] = array(
  653. 'data' => drupal_render($question_element),
  654. 'colspan' => count($element['#grid_options']),
  655. );
  656. }
  657. if ($right_titles) {
  658. $row[] = array('data' => isset($question_titles[1]) ? webform_filter_xss($question_titles[1]) : '', 'class' => array('webform-grid-question'));
  659. }
  660. // Convert the parents array into a string, excluding the "submitted" wrapper.
  661. $nested_level = $question_element['#parents'][0] == 'submitted' ? 1 : 0;
  662. $parents = str_replace('_', '-', implode('--', array_slice($question_element['#parents'], $nested_level)));
  663. $rows[] = array(
  664. 'data' => $row,
  665. 'class' => empty($question_element['#grid_question'])
  666. ? array(
  667. 'webform-component',
  668. 'webform-component-' . str_replace('_', '-', $question_element['#type']),
  669. 'webform-component--' . $parents,
  670. )
  671. : array(),
  672. );
  673. }
  674. $option_count = count($header) - 1;
  675. return theme('table', array(
  676. 'header' => $header,
  677. 'rows' => $rows,
  678. 'sticky' => $element['#sticky'],
  679. 'attributes' => array(
  680. 'class' => array(
  681. 'webform-grid',
  682. 'webform-grid-' . $option_count,
  683. ),
  684. ),
  685. ));
  686. }
  687. /**
  688. * Generate a table header suitable for form or html display.
  689. *
  690. * @param array $element
  691. * The element array.
  692. * @param bool $right_titles
  693. * If TRUE, display a right-side title column.
  694. *
  695. * @return array
  696. * An array of headers.
  697. */
  698. function _webform_grid_header(array $element, $right_titles) {
  699. $titles = explode('|', $element['#title'], 2);
  700. $title_left = $titles[0];
  701. $header = array(
  702. array(
  703. 'data' => _webform_grid_header_title($element, $title_left),
  704. 'class' => array('webform-grid-question'),
  705. 'scope' => 'col',
  706. ),
  707. );
  708. foreach ($element['#grid_options'] as $option) {
  709. $header[] = array(
  710. 'data' => webform_filter_xss($option),
  711. 'class' => array('checkbox', 'webform-grid-option'),
  712. 'scope' => 'col',
  713. );
  714. }
  715. if ($right_titles) {
  716. $title_right = isset($titles[1]) ? $titles[1] : $title_left;
  717. $header[] = array(
  718. 'data' => _webform_grid_header_title($element, $title_right),
  719. 'class' => array('webform-grid-question'),
  720. 'scope' => 'col',
  721. );
  722. }
  723. return $header;
  724. }
  725. /**
  726. * Create internal component title for table header, if any.
  727. */
  728. function _webform_grid_header_title($element, $title) {
  729. $header_title = '';
  730. if ($element['#title_display'] == 'internal') {
  731. $header_title = $title;
  732. }
  733. return $header_title;
  734. }
  735. /**
  736. * Determine if a right-side title column has been specified.
  737. */
  738. function _webform_grid_right_titles($element) {
  739. if ($element['#title_display'] == 'internal' && substr_count($element['#title'], '|')) {
  740. return TRUE;
  741. }
  742. foreach ($element['#grid_questions'] as $question_key => $question) {
  743. if (substr_count($question, '|')) {
  744. return TRUE;
  745. }
  746. }
  747. return FALSE;
  748. }
  749. /**
  750. * Create a question header for left, right or left/right question headers.
  751. */
  752. function _webform_grid_question_header($text) {
  753. return implode('/', array_filter(explode('|', $text)));
  754. }
  755. /**
  756. * Element validation for Webform grid fields.
  757. *
  758. * Requires a component implementation because the default required validation
  759. * passes when at least one value is supplied, rather than every value. This
  760. * makes the server validation match the browser validation.
  761. */
  762. function webform_validate_grid($element, $form_state) {
  763. if ($element['#required']) {
  764. $values = $form_state['input'];
  765. foreach ($element['#parents'] as $key) {
  766. $values = isset($values[$key]) ? $values[$key] : $values;
  767. }
  768. // Remove any values that aren't grid question (i.e. nested components).
  769. $grid_questions = $element['#grid_questions'];
  770. $values = array_intersect_key($values, $grid_questions);
  771. // Remove any unanswered grid questions.
  772. $answers = array_filter($values, function ($item) {
  773. return !is_null($item);
  774. });
  775. // Give required errors for any questions that aren't answered.
  776. foreach (array_diff_key($grid_questions, $answers) as $question_key => $question) {
  777. // If the question is still required (e.g not modified by an after_build
  778. // function), give the required error.
  779. if (!empty($element[$question_key]['#required'])) {
  780. form_error($element[$question_key], t('!question field within !name is required.', array('!question' => $question, '!name' => $element['#title'])));
  781. }
  782. }
  783. }
  784. }