webform.components.inc 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. <?php
  2. /**
  3. * @file
  4. * Webform module component handling.
  5. */
  6. /**
  7. * Provides interface and database handling for editing components of a webform.
  8. *
  9. * @author Nathan Haug <nate@lullabot.com>
  10. */
  11. /**
  12. * Overview page of all components for this webform.
  13. */
  14. function webform_components_page($node, $page_number = 1) {
  15. $output = drupal_get_form('webform_components_form', $node);
  16. return array(
  17. '#theme' => 'webform_components_page',
  18. '#node' => $node,
  19. '#form' => $output,
  20. );
  21. }
  22. /**
  23. * Theme the output of the main components page.
  24. *
  25. * This theming provides a way to toggle between the editing modes if Form
  26. * Builder module is available.
  27. */
  28. function theme_webform_components_page($variables) {
  29. $form = $variables['form'];
  30. return drupal_render($form);
  31. }
  32. /**
  33. * The table-based listing of all components for this webform.
  34. */
  35. function webform_components_form($form, $form_state, $node) {
  36. $form = array(
  37. '#tree' => TRUE,
  38. '#node' => $node,
  39. 'components' => array(),
  40. );
  41. $form['nid'] = array(
  42. '#type' => 'value',
  43. '#value' => $node->nid,
  44. );
  45. $options = array();
  46. foreach ($node->webform['components'] as $cid => $component) {
  47. $options[$cid] = check_plain($component['name']);
  48. $form['components'][$cid]['cid'] = array(
  49. '#type' => 'hidden',
  50. '#default_value' => $component['cid'],
  51. );
  52. $form['components'][$cid]['pid'] = array(
  53. '#type' => 'hidden',
  54. '#default_value' => $component['pid'],
  55. );
  56. $form['components'][$cid]['weight'] = array(
  57. '#type' => 'textfield',
  58. '#size' => 4,
  59. '#title' => t('Weight'),
  60. '#default_value' => $component['weight'],
  61. );
  62. $form['components'][$cid]['required'] = array(
  63. '#type' => 'checkbox',
  64. '#title' => t('Required'),
  65. '#default_value' => $component['required'],
  66. '#access' => webform_component_feature($component['type'], 'required'),
  67. );
  68. if (!isset($max_weight) || $component['weight'] > $max_weight) {
  69. $max_weight = $component['weight'];
  70. }
  71. }
  72. $form['add']['name'] = array(
  73. '#type' => 'textfield',
  74. '#size' => 30,
  75. '#maxlength' => NULL,
  76. );
  77. $form['add']['type'] = array(
  78. '#type' => 'select',
  79. '#options' => webform_component_options(),
  80. '#weight' => 3,
  81. '#default_value' => (isset($_GET['cid']) && isset($node->webform['components'][$_GET['cid']])) ? $node->webform['components'][$_GET['cid']]['type'] : 'textfield',
  82. );
  83. $form['add']['required'] = array(
  84. '#type' => 'checkbox',
  85. );
  86. $form['add']['cid'] = array(
  87. '#type' => 'hidden',
  88. '#default_value' => '',
  89. );
  90. $form['add']['pid'] = array(
  91. '#type' => 'hidden',
  92. '#default_value' => (isset($_GET['cid']) && isset($node->webform['components'][$_GET['cid']])) ? $node->webform['components'][$_GET['cid']]['pid'] : 0,
  93. );
  94. $form['add']['weight'] = array(
  95. '#type' => 'textfield',
  96. '#size' => 4,
  97. '#delta' => count($node->webform['components']) > 10 ? count($node->webform['components']) : 10,
  98. );
  99. if (isset($_GET['cid']) && isset($node->webform['components'][$_GET['cid']])) {
  100. // Make the new component appear by default directly after the one that was
  101. // just added.
  102. $form['add']['weight']['#default_value'] = $node->webform['components'][$_GET['cid']]['weight'] + 1;
  103. foreach (array_keys($node->webform['components']) as $cid) {
  104. // Adjust all later components also, to make sure none of them have the
  105. // same weight as the new component.
  106. if ($form['components'][$cid]['weight']['#default_value'] >= $form['add']['weight']['#default_value']) {
  107. $form['components'][$cid]['weight']['#default_value']++;
  108. }
  109. }
  110. }
  111. else {
  112. // If no component was just added, the new component should appear by
  113. // default at the end of the list.
  114. $form['add']['weight']['#default_value'] = isset($max_weight) ? $max_weight + 1 : 0;
  115. }
  116. $form['add']['add'] = array(
  117. '#type' => 'submit',
  118. '#value' => t('Add'),
  119. '#weight' => 45,
  120. '#validate' => array('webform_components_form_add_validate', 'webform_components_form_validate'),
  121. '#submit' => array('webform_components_form_add_submit'),
  122. );
  123. $form['actions'] = array(
  124. '#type' => 'actions',
  125. '#weight' => 45,
  126. );
  127. $form['actions']['submit'] = array(
  128. '#type' => 'submit',
  129. '#value' => t('Save'),
  130. '#access' => count($node->webform['components']) > 0,
  131. );
  132. $form['warning'] = array(
  133. '#weight' => -1,
  134. );
  135. webform_input_vars_check($form, $form_state, 'components', 'warning');
  136. return $form;
  137. }
  138. /**
  139. * Preprocess variables for theming the webform components form.
  140. */
  141. function template_preprocess_webform_components_form(&$variables) {
  142. $form = $variables['form'];
  143. $form['components']['#attached']['library'][] = array('webform', 'admin');
  144. // @todo: Attach these. See http://drupal.org/node/732022.
  145. drupal_add_tabledrag('webform-components', 'order', 'sibling', 'webform-weight');
  146. drupal_add_tabledrag('webform-components', 'match', 'parent', 'webform-pid', 'webform-pid', 'webform-cid');
  147. $node = $form['#node'];
  148. $header = array(t('Label'), t('Form key'), t('Type'), t('Value'), t('Required'), t('Weight'), array('data' => t('Operations'), 'colspan' => 3));
  149. $rows = array();
  150. // Add a row containing form elements for a new item.
  151. unset($form['add']['name']['#title'], $form['add_type']['#description']);
  152. $form['add']['name']['#attributes']['placeholder'] = t('New component name');
  153. $form['add']['cid']['#attributes']['class'][] = 'webform-cid';
  154. $form['add']['pid']['#attributes']['class'][] = 'webform-pid';
  155. $form['add']['weight']['#attributes']['class'][] = 'webform-weight';
  156. $row_data = array(
  157. array('data' => drupal_render($form['add']['name']), 'class' => array('webform-component-name'), 'colspan' => 2),
  158. array('data' => drupal_render($form['add']['type']), 'class' => array('webform-component-type')),
  159. array('data' => '', 'class' => array('webform-component-value')),
  160. array('data' => drupal_render($form['add']['required']), 'class' => array('webform-component-required', 'checkbox')),
  161. array('data' => drupal_render($form['add']['cid']) . drupal_render($form['add']['pid']) . drupal_render($form['add']['weight'])),
  162. array('colspan' => 3, 'data' => drupal_render($form['add']['add']), 'class' => array('webform-component-add')),
  163. );
  164. $add_form = array('data' => $row_data, 'class' => array('draggable', 'webform-add-form', 'tabledrag-leaf'));
  165. if (!empty($node->webform['components'])) {
  166. $component_tree = array();
  167. $page_count = 1;
  168. _webform_components_tree_build($node->webform['components'], $component_tree, 0, $page_count);
  169. $component_tree = _webform_components_tree_sort($component_tree);
  170. // Build the table rows recursively.
  171. foreach ($component_tree['children'] as $cid => $component) {
  172. _webform_components_form_rows($node, $cid, $component, 0, $form, $rows, $add_form);
  173. }
  174. }
  175. else {
  176. $rows[] = array(array('data' => t('No Components, add a component below.'), 'colspan' => 9));
  177. // When there are no components, tabledrag.js will look to the add component
  178. // row to locate the weight column. Because of the name/form_key colspan
  179. // it will mis-count the columns and locate the Required column instead of
  180. // the Weight column.
  181. unset($add_form['data'][0]['colspan']);
  182. array_splice($add_form['data'], 1, 0, '&nbsp;');
  183. }
  184. // Append the add form if not already printed.
  185. if ($add_form) {
  186. $rows[] = $add_form;
  187. }
  188. $variables['rows'] = $rows;
  189. $variables['header'] = $header;
  190. $variables['form'] = $form;
  191. }
  192. /**
  193. * Recursive function for nesting components into a table.
  194. *
  195. * @see preprocess_webform_components_form()
  196. */
  197. function _webform_components_form_rows($node, $cid, $component, $level, &$form, &$rows, &$add_form) {
  198. // Create presentable values.
  199. $form_key = truncate_utf8($component['form_key'], 30, FALSE, TRUE);
  200. $value = truncate_utf8($component['value'], 30, TRUE, TRUE, 20);
  201. // Remove individual titles from the required and weight fields.
  202. unset($form['components'][$cid]['required']['#title']);
  203. unset($form['components'][$cid]['pid']['#title']);
  204. unset($form['components'][$cid]['weight']['#title']);
  205. // Add special classes for weight and parent fields.
  206. $form['components'][$cid]['cid']['#attributes']['class'] = array('webform-cid');
  207. $form['components'][$cid]['pid']['#attributes']['class'] = array('webform-pid');
  208. $form['components'][$cid]['weight']['#attributes']['class'] = array('webform-weight');
  209. // Build indentation for this row.
  210. $indents = '';
  211. for ($n = 1; $n <= $level; $n++) {
  212. $indents .= '<div class="indentation">&nbsp;</div>';
  213. }
  214. // Add each component to a table row.
  215. $row_data = array(
  216. array('data' => $indents . filter_xss($component['name']), 'class' => array('webform-component-name')),
  217. array('data' => check_plain($form_key), 'class' => array('webform-component-formkey')) +
  218. ((string) $component['form_key'] === (string) $form_key ? array() : array('title' => $component['form_key'])),
  219. array('data' => $form['add']['type']['#options'][$component['type']], 'class' => array('webform-component-type')),
  220. array('data' => ($value == '') ? '-' : check_plain($value), 'class' => array('webform-component-value')) +
  221. ($component['value'] == $value ? array() : array('title' => $component['value'])),
  222. array('data' => drupal_render($form['components'][$cid]['required']), 'class' => array('webform-component-required', 'checkbox')),
  223. array('data' => drupal_render($form['components'][$cid]['cid']) . drupal_render($form['components'][$cid]['pid']) . drupal_render($form['components'][$cid]['weight'])),
  224. array('data' => l(t('Edit'), 'node/' . $node->nid . '/webform/components/' . $cid, array('query' => drupal_get_destination())), 'class' => array('webform-component-edit')),
  225. array('data' => l(t('Clone'), 'node/' . $node->nid . '/webform/components/' . $cid . '/clone', array('query' => drupal_get_destination())), 'class' => array('webform-component-clone')),
  226. array('data' => l(t('Delete'), 'node/' . $node->nid . '/webform/components/' . $cid . '/delete', array('query' => drupal_get_destination())), 'class' => array('webform-component-delete')),
  227. );
  228. $row_class = array('draggable');
  229. if (!webform_component_feature($component['type'], 'group')) {
  230. $row_class[] = 'tabledrag-leaf';
  231. }
  232. if ($component['type'] == 'pagebreak') {
  233. $row_class[] = 'tabledrag-root';
  234. $row_class[] = 'webform-pagebreak';
  235. $row_data[0]['class'][] = 'webform-pagebreak';
  236. }
  237. $rows[] = array('data' => $row_data, 'class' => $row_class, 'data-cid' => $cid);
  238. if (isset($component['children']) && is_array($component['children'])) {
  239. foreach ($component['children'] as $cid => $component) {
  240. _webform_components_form_rows($node, $cid, $component, $level + 1, $form, $rows, $add_form);
  241. }
  242. }
  243. // Add the add form if this was the last edited component.
  244. if (isset($_GET['cid']) && $component['cid'] == $_GET['cid'] && $add_form) {
  245. $add_form['data'][0]['data'] = $indents . $add_form['data'][0]['data'];
  246. $rows[] = $add_form;
  247. $add_form = FALSE;
  248. }
  249. }
  250. /**
  251. * Theme the node components form. Use a table to organize the components.
  252. *
  253. * @return string
  254. * Formatted HTML form, ready for display.
  255. */
  256. function theme_webform_components_form($variables) {
  257. $output = '';
  258. $output .= drupal_render_children($variables['form']['warning']);
  259. $output .= theme('table', array('header' => $variables['header'], 'rows' => $variables['rows'], 'attributes' => array('id' => 'webform-components')));
  260. $output .= drupal_render_children($variables['form']);
  261. return $output;
  262. }
  263. /**
  264. * Validate handler for webform_components_form().
  265. */
  266. function webform_components_form_validate($form, &$form_state) {
  267. // Check that no two components end up with the same form key.
  268. $duplicates = array();
  269. $parents = array();
  270. if (isset($form_state['values']['components'])) {
  271. foreach ($form_state['values']['components'] as $cid => $component) {
  272. $form_key = (string) $form['#node']->webform['components'][$cid]['form_key'];
  273. if (isset($parents[$component['pid']]) && ($existing = array_search($form_key, $parents[$component['pid']], TRUE)) && $existing !== FALSE) {
  274. if (!isset($duplicates[$form_key])) {
  275. $duplicates[$form_key] = array($existing);
  276. }
  277. $duplicates[$form_key][] = $cid;
  278. }
  279. $parents[$component['pid']][$cid] = $form_key;
  280. }
  281. }
  282. if (!empty($duplicates)) {
  283. $error = t('The form order failed to save because the following elements have same form keys and are under the same parent. Edit each component and give them a unique form key, then try moving them again.');
  284. $items = array();
  285. foreach ($duplicates as $form_key => $cids) {
  286. foreach ($cids as $cid) {
  287. $items[] = webform_filter_xss($form['#node']->webform['components'][$cid]['name']);
  288. }
  289. }
  290. form_error($form['components'], $error . theme('item_list', array('items' => $items)));
  291. }
  292. }
  293. /**
  294. * Validate handler for webform_component_form() when adding a new component.
  295. */
  296. function webform_components_form_add_validate($form, &$form_state) {
  297. // Check that the entered component name is valid.
  298. if (drupal_strlen(trim($form_state['values']['add']['name'])) <= 0) {
  299. form_error($form['add']['name'], t('When adding a new component, the name field is required.'));
  300. }
  301. }
  302. /**
  303. * Submit handler for webform_components_form() to save component order.
  304. */
  305. function webform_components_form_submit($form, &$form_state) {
  306. $node = node_load($form_state['values']['nid']);
  307. // Update all required and weight values.
  308. $changes = FALSE;
  309. foreach ($node->webform['components'] as $cid => $component) {
  310. if ($component['pid'] != $form_state['values']['components'][$cid]['pid'] || $component['weight'] != $form_state['values']['components'][$cid]['weight'] || $component['required'] != $form_state['values']['components'][$cid]['required']) {
  311. $changes = TRUE;
  312. $node->webform['components'][$cid]['weight'] = $form_state['values']['components'][$cid]['weight'];
  313. $node->webform['components'][$cid]['required'] = $form_state['values']['components'][$cid]['required'];
  314. $node->webform['components'][$cid]['pid'] = $form_state['values']['components'][$cid]['pid'];
  315. }
  316. }
  317. if ($changes) {
  318. node_save($node);
  319. }
  320. drupal_set_message(t('The component positions and required values have been updated.'));
  321. }
  322. /**
  323. * Submit handler for webform_components_form() that adds a new component.
  324. */
  325. function webform_components_form_add_submit($form, &$form_state) {
  326. $node = node_load($form_state['values']['nid']);
  327. $component = $form_state['values']['add'];
  328. // Set the values in the query string for the add component page.
  329. $query = array(
  330. 'name' => $component['name'],
  331. 'required' => $component['required'],
  332. 'pid' => $component['pid'],
  333. 'weight' => $component['weight'],
  334. );
  335. // Forward the "destination" query string value to the next form.
  336. if (isset($_GET['destination'])) {
  337. $query['destination'] = $_GET['destination'];
  338. unset($_GET['destination']);
  339. drupal_static_reset('drupal_get_destination');
  340. }
  341. $form_state['redirect'] = array('node/' . $node->nid . '/webform/components/new/' . $component['type'], array('query' => $query));
  342. }
  343. /**
  344. * Form to configure a webform component.
  345. */
  346. function webform_component_edit_form($form, $form_state, $node, $component, $clone = FALSE) {
  347. drupal_set_title(t('Edit component: @name', array('@name' => $component['name'])), PASS_THROUGH);
  348. $form['#node'] = $node;
  349. $form['#tree'] = TRUE;
  350. // Print the correct field type specification.
  351. // We always need: name and description.
  352. $form['type'] = array(
  353. '#type' => 'value',
  354. '#value' => $component['type'],
  355. );
  356. $form['nid'] = array(
  357. '#type' => 'value',
  358. '#value' => $node->nid,
  359. );
  360. $form['cid'] = array(
  361. '#type' => 'value',
  362. '#value' => isset($component['cid']) ? $component['cid'] : NULL,
  363. );
  364. $form['clone'] = array(
  365. '#type' => 'value',
  366. '#value' => $clone,
  367. );
  368. if (webform_component_feature($component['type'], 'title')) {
  369. $form['name'] = array(
  370. '#type' => 'textfield',
  371. '#default_value' => $component['name'],
  372. '#title' => t('Label'),
  373. '#description' => t('This is used as a descriptive label when displaying this form element.'),
  374. '#required' => TRUE,
  375. '#weight' => -10,
  376. '#maxlength' => NULL,
  377. );
  378. }
  379. $form['form_key'] = array(
  380. '#type' => 'textfield',
  381. '#default_value' => empty($component['form_key']) ? _webform_safe_name($component['name']) : $component['form_key'],
  382. '#title' => t('Form Key'),
  383. '#description' => t('Enter a machine readable key for this form element. May contain only alphanumeric characters and underscores. This key will be used as the name attribute of the form element. This value has no effect on the way data is saved, but may be helpful if doing custom form processing.'),
  384. '#required' => TRUE,
  385. '#weight' => -9,
  386. );
  387. $form['extra'] = array();
  388. if (webform_component_feature($component['type'], 'description')) {
  389. $form['extra']['description'] = array(
  390. '#type' => 'textarea',
  391. '#default_value' => isset($component['extra']['description']) ? $component['extra']['description'] : '',
  392. '#title' => t('Description'),
  393. '#description' => t('A short description of the field used as help for the user when he/she uses the form.') . ' ' . theme('webform_token_help'),
  394. '#weight' => -1,
  395. );
  396. }
  397. // Display settings.
  398. $form['display'] = array(
  399. '#type' => 'fieldset',
  400. '#title' => t('Display'),
  401. '#collapsible' => TRUE,
  402. '#collapsed' => FALSE,
  403. '#weight' => 8,
  404. );
  405. if (webform_component_feature($component['type'], 'title_display')) {
  406. $inline_option = webform_component_feature($component['type'], 'title_inline') ? array('inline' => t('Inline')) : array();
  407. $internal_option = webform_component_feature($component['type'], 'title_internal') ? array('internal' => t('Inside the component')) : array();
  408. if ($inline_option || $internal_option) {
  409. $form['display']['title_display'] = array(
  410. '#type' => 'select',
  411. '#title' => t('Label display'),
  412. '#default_value' => !empty($component['extra']['title_display']) ? $component['extra']['title_display'] : 'before',
  413. '#options' => array('before' => t('Above')) +
  414. $inline_option +
  415. $internal_option +
  416. array('none' => t('None')),
  417. '#description' => t("Determines the placement of the component's label."),
  418. );
  419. }
  420. else {
  421. $form['display']['title_display'] = array(
  422. '#type' => 'checkbox',
  423. '#title' => t('Hide label'),
  424. '#default_value' => strcmp($component['extra']['title_display'], 'none') === 0,
  425. '#return_value' => 'none',
  426. '#description' => t('Do not display the label of this component.'),
  427. );
  428. }
  429. $form['display']['title_display']['#weight'] = 8;
  430. $form['display']['title_display']['#parents'] = array('extra', 'title_display');
  431. }
  432. if (webform_component_feature($component['type'], 'description')) {
  433. $form['display']['description_above'] = array(
  434. '#type' => 'checkbox',
  435. '#default_value' => !empty($component['extra']['description_above']),
  436. '#title' => t('Description above field'),
  437. '#description' => t('Place the description above &mdash; rather than below &mdash; the field.'),
  438. '#weight' => 8.5,
  439. '#parents' => array('extra', 'description_above'),
  440. );
  441. }
  442. if (webform_component_feature($component['type'], 'private')) {
  443. // May user mark fields as Private?
  444. $form['display']['private'] = array(
  445. '#type' => 'checkbox',
  446. '#title' => t('Private'),
  447. '#default_value' => ($component['extra']['private'] == '1' ? TRUE : FALSE),
  448. '#description' => t('Private fields are shown only to users with results access.'),
  449. '#weight' => 45,
  450. '#parents' => array('extra', 'private'),
  451. '#disabled' => empty($node->nid) ? FALSE : !webform_results_access($node),
  452. );
  453. }
  454. if (webform_component_feature($component['type'], 'wrapper_classes')) {
  455. $form['display']['wrapper_classes'] = array(
  456. '#type' => 'textfield',
  457. '#title' => t('Wrapper CSS classes'),
  458. '#default_value' => isset($component['extra']['wrapper_classes']) ? $component['extra']['wrapper_classes'] : '',
  459. '#description' => t('Apply a class to the wrapper around both the field and its label. Separate multiple by spaces.'),
  460. '#weight' => 50,
  461. '#parents' => array('extra', 'wrapper_classes'),
  462. );
  463. }
  464. if (webform_component_feature($component['type'], 'css_classes')) {
  465. $form['display']['css_classes'] = array(
  466. '#type' => 'textfield',
  467. '#title' => t('CSS classes'),
  468. '#default_value' => isset($component['extra']['css_classes']) ? $component['extra']['css_classes'] : '',
  469. '#description' => t('Apply a class to the field. Separate multiple by spaces.'),
  470. '#weight' => 51,
  471. '#parents' => array('extra', 'css_classes'),
  472. );
  473. }
  474. // Validation settings.
  475. $form['validation'] = array(
  476. '#type' => 'fieldset',
  477. '#title' => t('Validation'),
  478. '#collapsible' => TRUE,
  479. '#collapsed' => FALSE,
  480. '#weight' => 5,
  481. );
  482. if (webform_component_feature($component['type'], 'required')) {
  483. $form['validation']['required'] = array(
  484. '#type' => 'checkbox',
  485. '#title' => t('Required'),
  486. '#default_value' => ($component['required'] == '1' ? TRUE : FALSE),
  487. '#description' => t('Check this option if the user must enter a value.'),
  488. '#weight' => -1,
  489. '#parents' => array('required'),
  490. );
  491. }
  492. // Position settings, only shown if JavaScript is disabled.
  493. $form['position'] = array(
  494. '#type' => 'fieldset',
  495. '#title' => t('Position'),
  496. '#collapsible' => TRUE,
  497. '#collapsed' => TRUE,
  498. '#tree' => FALSE,
  499. '#weight' => 20,
  500. '#attributes' => array('class' => array('webform-position')),
  501. );
  502. $options = array('0' => t('Root'));
  503. foreach ($node->webform['components'] as $existing_cid => $value) {
  504. if (webform_component_feature($value['type'], 'group') && (!isset($component['cid']) || $existing_cid != $component['cid'])) {
  505. $options[$existing_cid] = $value['name'];
  506. }
  507. }
  508. $form['position']['pid'] = array(
  509. '#type' => 'select',
  510. '#title' => t('Parent'),
  511. '#default_value' => $component['pid'],
  512. '#description' => t('Optional. You may organize your form by placing this component inside another fieldset.'),
  513. '#options' => $options,
  514. '#access' => count($options) > 1,
  515. '#weight' => 3,
  516. );
  517. $form['position']['weight'] = array(
  518. '#type' => 'textfield',
  519. '#size' => 4,
  520. '#title' => t('Weight'),
  521. '#default_value' => $component['weight'],
  522. '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
  523. '#weight' => 4,
  524. );
  525. // Add the fields specific to this component type:
  526. $additional_form_elements = (array) webform_component_invoke($component['type'], 'edit', $component, $form, $form_state);
  527. if (empty($additional_form_elements)) {
  528. drupal_set_message(t('The webform component of type @type does not have an edit function defined.', array('@type' => $component['type'])));
  529. }
  530. // Merge the additional fields with the current fields:
  531. if (isset($additional_form_elements['extra'])) {
  532. $form['extra'] = array_merge($form['extra'], $additional_form_elements['extra']);
  533. unset($additional_form_elements['extra']);
  534. }
  535. if (isset($additional_form_elements['position'])) {
  536. $form['position'] = array_merge($form['position'], $additional_form_elements['position']);
  537. unset($additional_form_elements['position']);
  538. }
  539. if (isset($additional_form_elements['display'])) {
  540. $form['display'] = array_merge($form['display'], $additional_form_elements['display']);
  541. unset($additional_form_elements['display']);
  542. }
  543. if (isset($additional_form_elements['validation'])) {
  544. $form['validation'] = array_merge($form['validation'], $additional_form_elements['validation']);
  545. unset($additional_form_elements['validation']);
  546. }
  547. elseif (count(element_children($form['validation'])) == 0) {
  548. unset($form['validation']);
  549. }
  550. $form = array_merge($form, $additional_form_elements);
  551. // Ensure that the webform admin library is attached, possibly in addition to
  552. // component-specific attachments.
  553. $form['#attached']['library'][] = array('webform', 'admin');
  554. // Add the submit button.
  555. $form['actions'] = array(
  556. '#type' => 'actions',
  557. '#weight' => 50,
  558. );
  559. $form['actions']['submit'] = array(
  560. '#type' => 'submit',
  561. '#value' => t('Save component'),
  562. );
  563. // Remove fieldsets without any child form controls.
  564. foreach (element_children($form) as $group_key) {
  565. $group = $form[$group_key];
  566. if (isset($group['#type']) && $group['#type'] === 'fieldset' && !element_children($group)) {
  567. unset($form[$group_key]);
  568. }
  569. }
  570. return $form;
  571. }
  572. /**
  573. * Field name validation for the webform unique key. Must be alphanumeric.
  574. */
  575. function webform_component_edit_form_validate($form, &$form_state) {
  576. $node = $form['#node'];
  577. if (!preg_match('/^[a-z0-9_]+$/i', $form_state['values']['form_key'])) {
  578. form_set_error('form_key', t('The form key %form_key is invalid. Please include only lowercase alphanumeric characters and underscores.', array('%form_key' => $form_state['values']['form_key'])));
  579. }
  580. foreach ($node->webform['components'] as $cid => $component) {
  581. if (($component['cid'] != $form_state['values']['cid'] || $form_state['values']['clone']) && ($component['pid'] == $form_state['values']['pid']) && (strcasecmp($component['form_key'], $form_state['values']['form_key']) == 0)) {
  582. form_set_error('form_key', t('The form key %form_key is already in use by the field labeled %existing_field. Please use a unique key.', array('%form_key' => $form_state['values']['form_key'], '%existing_field' => $component['name'])));
  583. }
  584. }
  585. }
  586. /**
  587. * Submit handler for webform_component_edit_form().
  588. */
  589. function webform_component_edit_form_submit($form, &$form_state) {
  590. // Ensure a webform record exists.
  591. $node = $form['#node'];
  592. webform_ensure_record($node);
  593. // Remove extra values that match the default.
  594. if (isset($form_state['values']['extra'])) {
  595. $default = array('type' => $form_state['values']['type'], 'extra' => array());
  596. webform_component_defaults($default);
  597. foreach ($form_state['values']['extra'] as $key => $value) {
  598. if (isset($default['extra'][$key]) && $default['extra'][$key] === $value) {
  599. unset($form_state['values']['extra'][$key]);
  600. }
  601. }
  602. }
  603. // Remove empty attribute values.
  604. if (isset($form_state['values']['extra']['attributes'])) {
  605. foreach ($form_state['values']['extra']['attributes'] as $key => $value) {
  606. if ($value === '') {
  607. unset($form_state['values']['extra']['attributes'][$key]);
  608. }
  609. }
  610. }
  611. if ($form_state['values']['clone']) {
  612. webform_component_clone($node, $form_state['values']);
  613. drupal_set_message(t('Component %name cloned.', array('%name' => $form_state['values']['name'])));
  614. }
  615. elseif (!empty($form_state['values']['cid'])) {
  616. webform_component_update($form_state['values']);
  617. drupal_set_message(t('Component %name updated.', array('%name' => $form_state['values']['name'])));
  618. }
  619. else {
  620. $cid = webform_component_insert($form_state['values']);
  621. drupal_set_message(t('New component %name added.', array('%name' => $form_state['values']['name'])));
  622. }
  623. // Since Webform components have been updated but the node itself has not
  624. // been saved, it is necessary to explicitly clear the cache to make sure
  625. // the updated webform is visible to anonymous users. This resets the page
  626. // and block caches (only).
  627. cache_clear_all();
  628. // Refresh the entity cache, should it be cached in persistent storage.
  629. entity_get_controller('node')->resetCache(array($node->nid));
  630. $form_state['redirect'] = array('node/' . $node->nid . '/webform/components', isset($cid) ? array('query' => array('cid' => $cid)) : array());
  631. }
  632. /**
  633. * Form to confirm deletion of a component.
  634. */
  635. function webform_component_delete_form($form, $form_state, $node, $component) {
  636. $cid = $component['cid'];
  637. $form = array();
  638. $form['node'] = array(
  639. '#type' => 'value',
  640. '#value' => $node,
  641. );
  642. $form['component'] = array(
  643. '#type' => 'value',
  644. '#value' => $component,
  645. );
  646. $component_type = $node->webform['components'][$cid]['type'];
  647. if (webform_component_feature($component_type, 'group')) {
  648. $question = t('Delete the %name fieldset?', array('%name' => $node->webform['components'][$cid]['name']));
  649. $description = t('This will immediately delete the %name @type component and all nested components within %name from the %webform webform. This cannot be undone.',
  650. array(
  651. '%name' => $node->webform['components'][$cid]['name'],
  652. '@type' => webform_component_property($component_type, 'label'),
  653. '%webform' => $node->title,
  654. ));
  655. }
  656. else {
  657. $question = t('Delete the %name component?', array('%name' => $node->webform['components'][$cid]['name']));
  658. $description = t('This will immediately delete the %name component from the %webform webform. This cannot be undone.', array('%name' => $node->webform['components'][$cid]['name'], '%webform' => $node->title));
  659. }
  660. return confirm_form($form, $question, 'node/' . $node->nid . '/webform/components', $description, t('Delete'));
  661. }
  662. /**
  663. * Submit handler for webform_component_delete_form().
  664. */
  665. function webform_component_delete_form_submit($form, &$form_state) {
  666. // Delete the component.
  667. $node = $form_state['values']['node'];
  668. $component = $form_state['values']['component'];
  669. webform_component_delete($node, $component);
  670. drupal_set_message(t('Component %name deleted.', array('%name' => $component['name'])));
  671. // Check if this webform still contains any information.
  672. unset($node->webform['components'][$component['cid']]);
  673. webform_check_record($node);
  674. // Since Webform components have been updated but the node itself has not
  675. // been saved, it is necessary to explicitly clear the cache to make sure
  676. // the updated webform is visible to anonymous users. This resets the page
  677. // and block caches (only).
  678. cache_clear_all();
  679. // Refresh the entity cache, should it be cached in persistent storage.
  680. entity_get_controller('node')->resetCache(array($node->nid));
  681. $form_state['redirect'] = 'node/' . $node->nid . '/webform/components';
  682. }
  683. /**
  684. * Insert a new component into the database.
  685. *
  686. * @param $component
  687. * A full component containing fields from the component form.
  688. *
  689. * @return false|int
  690. * On success return identifier for the components within node.
  691. * FALSE on failure
  692. */
  693. function webform_component_insert(&$component) {
  694. // Allow modules to modify the component before saving.
  695. foreach (module_implements('webform_component_presave') as $module) {
  696. $function = $module . '_webform_component_presave';
  697. $function($component);
  698. }
  699. $component['value'] = isset($component['value']) ? $component['value'] : NULL;
  700. $component['required'] = isset($component['required']) ? $component['required'] : 0;
  701. $component['extra']['private'] = isset($component['extra']['private']) ? $component['extra']['private'] : 0;
  702. if (!isset($component['cid'])) {
  703. if (lock_acquire('webform_component_insert_' . $component['nid'], 5)) {
  704. $next_id_query = db_select('webform_component')->condition('nid', $component['nid']);
  705. $next_id_query->addExpression('MAX(cid) + 1', 'cid');
  706. $component['cid'] = $next_id_query->execute()->fetchField();
  707. if ($component['cid'] == NULL) {
  708. $component['cid'] = 1;
  709. }
  710. lock_release('webform_component_insert_' . $component['nid']);
  711. }
  712. else {
  713. watchdog('webform', 'A Webform component could not be saved because a timeout occurred while trying to acquire a lock for the node. Details: <pre>@component</pre>', array('@component' => print_r($component, TRUE)));
  714. return FALSE;
  715. }
  716. }
  717. db_insert('webform_component')
  718. ->fields(array(
  719. 'nid' => $component['nid'],
  720. 'cid' => $component['cid'],
  721. 'pid' => $component['pid'],
  722. 'form_key' => $component['form_key'],
  723. 'name' => $component['name'],
  724. 'type' => $component['type'],
  725. 'value' => (string) $component['value'],
  726. 'extra' => serialize($component['extra']),
  727. 'required' => $component['required'],
  728. 'weight' => $component['weight'],
  729. ))
  730. ->execute();
  731. // Post-insert actions.
  732. module_invoke_all('webform_component_insert', $component);
  733. return $component['cid'];
  734. }
  735. /**
  736. * Update an existing component with new values.
  737. *
  738. * @param $component
  739. * A full component containing a nid, cid, and all other fields from the
  740. * component form. Additional properties are stored in the extra array.
  741. */
  742. function webform_component_update($component) {
  743. // Allow modules to modify the component before saving.
  744. foreach (module_implements('webform_component_presave') as $module) {
  745. $function = $module . '_webform_component_presave';
  746. $function($component);
  747. }
  748. $component['value'] = isset($component['value']) ? $component['value'] : NULL;
  749. $component['required'] = isset($component['required']) ? $component['required'] : 0;
  750. $component['extra']['private'] = isset($component['extra']['private']) ? $component['extra']['private'] : 0;
  751. db_update('webform_component')
  752. ->fields(array(
  753. 'pid' => $component['pid'],
  754. 'form_key' => $component['form_key'],
  755. 'name' => $component['name'],
  756. 'type' => $component['type'],
  757. 'value' => isset($component['value']) ? $component['value'] : '',
  758. 'extra' => serialize($component['extra']),
  759. 'required' => $component['required'],
  760. 'weight' => $component['weight'],
  761. ))
  762. ->condition('nid', $component['nid'])
  763. ->condition('cid', $component['cid'])
  764. ->execute();
  765. // Post-update actions.
  766. module_invoke_all('webform_component_update', $component);
  767. }
  768. /**
  769. * Delete a Webform component.
  770. */
  771. function webform_component_delete($node, $component) {
  772. // Check if a delete function is available for this component. If so,
  773. // load all submissions and allow the component to delete each one.
  774. webform_component_include($component['type']);
  775. $delete_function = '_webform_delete_' . $component['type'];
  776. if (function_exists($delete_function)) {
  777. module_load_include('inc', 'webform', 'includes/webform.submissions');
  778. $submissions = webform_get_submissions($node->nid);
  779. foreach ($submissions as $submission) {
  780. if (isset($submission->data[$component['cid']])) {
  781. webform_component_invoke($component['type'], 'delete', $component, $submission->data[$component['cid']]);
  782. }
  783. }
  784. }
  785. // Remove database entries.
  786. db_delete('webform_component')
  787. ->condition('nid', $node->nid)
  788. ->condition('cid', $component['cid'])
  789. ->execute();
  790. db_delete('webform_submitted_data')
  791. ->condition('nid', $node->nid)
  792. ->condition('cid', $component['cid'])
  793. ->execute();
  794. // Delete any conditionals dependent on this component.
  795. module_load_include('inc', 'webform', 'includes/webform.conditionals');
  796. foreach ($node->webform['conditionals'] as $rgid => &$conditional) {
  797. $specs = array(
  798. array(
  799. 'field' => 'rules',
  800. 'table' => 'webform_conditional_rules',
  801. 'component' => 'source',
  802. 'component_type' => 'source_type',
  803. 'index' => 'rid',
  804. ),
  805. array(
  806. 'field' => 'actions',
  807. 'table' => 'webform_conditional_actions',
  808. 'component' => 'target',
  809. 'component_type' => 'target_type',
  810. 'index' => 'aid',
  811. ),
  812. );
  813. foreach ($specs as $spec) {
  814. $deleted = array();
  815. $field = $spec['field'];
  816. foreach ($conditional[$field] as $key => $thing) {
  817. if ($thing[$spec['component_type']] === 'component' && $thing[$spec['component']] == $component['cid']) {
  818. $deleted[$key] = $key;
  819. unset($conditional[$field][$key]);
  820. }
  821. }
  822. if ($spec['field'] == 'rules') {
  823. // Rules deleted because of the source component being deleted may have left
  824. // empty sub-conditionals. Delete them, and then the entire rule group if
  825. // there aren't any rules left.
  826. $deleted += webform_delete_empty_subconditionals($conditional);
  827. }
  828. // Delete the conditional if this component is the only source / target.
  829. if (empty($conditional[$field])) {
  830. webform_conditional_delete($node, $conditional);
  831. // Also delete the conditional from the $node so it is not re-created
  832. // later on in webform_node_update().
  833. unset($node->webform['conditionals'][$conditional['rgid']]);
  834. // Loop exit.
  835. break;
  836. }
  837. // Remove the deleted rules / actions from the database.
  838. foreach ($deleted as $key) {
  839. db_delete($spec['table'])
  840. ->condition('nid', $node->nid)
  841. ->condition('rgid', $rgid)
  842. ->condition($spec['index'], $key)
  843. ->execute();
  844. }
  845. }
  846. }
  847. // Delete all elements under this element.
  848. $result = db_select('webform_component', 'c')
  849. ->fields('c')
  850. ->condition('nid', $node->nid)
  851. ->condition('pid', $component['cid'])
  852. ->execute();
  853. foreach ($result as $row) {
  854. $child_component = $node->webform['components'][$row->cid];
  855. webform_component_delete($node, $child_component);
  856. }
  857. // Post-delete actions.
  858. module_invoke_all('webform_component_delete', $component);
  859. }
  860. /**
  861. * Recursively insert components into the database.
  862. *
  863. * @param $node
  864. * The node object containing the current webform.
  865. * @param $component
  866. * A full component containing fields from the component form.
  867. *
  868. * @return false|int
  869. * On success return identifier for the components within node.
  870. * FALSE on failure
  871. */
  872. function webform_component_clone(&$node, &$component) {
  873. $original_cid = $component['cid'];
  874. $component['cid'] = NULL;
  875. $new_cid = webform_component_insert($component);
  876. $component['cid'] = $new_cid;
  877. if (webform_component_feature($component['type'], 'group')) {
  878. foreach ($node->webform['components'] as $cid => $child_component) {
  879. if ($child_component['pid'] == $original_cid) {
  880. $child_component['pid'] = $new_cid;
  881. webform_component_clone($node, $child_component);
  882. }
  883. }
  884. }
  885. return $new_cid;
  886. }
  887. /**
  888. * Check if a component has a particular feature.
  889. *
  890. * @see hook_webform_component_info()
  891. */
  892. function webform_component_feature($type, $feature) {
  893. $components = webform_components();
  894. $defaults = array(
  895. 'analysis' => TRUE,
  896. 'csv' => TRUE,
  897. 'default_value' => TRUE,
  898. 'description' => TRUE,
  899. 'email' => TRUE,
  900. 'email_address' => FALSE,
  901. 'email_name' => FALSE,
  902. 'required' => TRUE,
  903. 'title' => TRUE,
  904. 'title_display' => TRUE,
  905. 'title_inline' => TRUE,
  906. 'title_internal' => FALSE,
  907. 'conditional' => TRUE,
  908. 'conditional_action_set' => FALSE,
  909. 'spam_analysis' => FALSE,
  910. 'group' => FALSE,
  911. 'attachment' => FALSE,
  912. 'private' => TRUE,
  913. 'placeholder' => FALSE,
  914. 'wrapper_classes' => TRUE,
  915. 'css_classes' => TRUE,
  916. 'views_range' => FALSE,
  917. );
  918. return isset($components[$type]['features'][$feature]) ? $components[$type]['features'][$feature] : !empty($defaults[$feature]);
  919. }
  920. /**
  921. * Get a component property from the component definition.
  922. *
  923. * @see hook_webform_component_info()
  924. */
  925. function webform_component_property($type, $property) {
  926. $components = webform_components();
  927. $defaults = array(
  928. 'conditional_type' => 'string',
  929. );
  930. return isset($components[$type][$property]) ? $components[$type][$property] : $defaults[$property];
  931. }
  932. /**
  933. * Create a list of components suitable for a select list.
  934. *
  935. * @param $node
  936. * The webform node.
  937. * @param $component_filter
  938. * Either an array of components, or a string containing a feature name (csv,
  939. * email, required, conditional) on which this list of components will be
  940. * restricted.
  941. * @param $prefix_group
  942. * TRUE to indent with a hyphen, or 'path" to Prepend enclosing group (for example,
  943. * fieldset) name(s)
  944. * @param $pagebreak_groups
  945. * Determine if pagebreaks should be converted to option groups in the
  946. * returned list of options.
  947. *
  948. * @return array
  949. * An array of options.
  950. */
  951. function webform_component_list($node, $component_filter = NULL, $prepend_group = TRUE, $pagebreak_groups = FALSE) {
  952. $options = array();
  953. $page_names = array();
  954. $parent_names = array();
  955. $components = is_array($component_filter) ? $component_filter : $node->webform['components'];
  956. $feature = is_string($component_filter) ? $component_filter : NULL;
  957. foreach ($components as $cid => $component) {
  958. // If this component is a group (for example, fieldset), then remember its name, including any parents.
  959. if ($prepend_group && webform_component_feature($component['type'], 'group')) {
  960. $parent_names[$cid] = ($component['pid'] ? $parent_names[$component['pid']] : '') .
  961. ($prepend_group === 'path' ? $component['name'] . ': ' : '-');
  962. }
  963. $page_num = $component['page_num'];
  964. // If this component is a pagebreak, then generate an option group, ensuring a unique name.
  965. if ($pagebreak_groups && $component['type'] == 'pagebreak') {
  966. $page_name = $component['name'];
  967. // When a $page_name consists only of digits, append a space to ensure it
  968. // is never the same as a $cid.
  969. if ((string) $page_name === (string) (int) $page_name) {
  970. $page_name .= ' ';
  971. }
  972. $copy = 1;
  973. while (in_array($page_name, $page_names)) {
  974. $page_name = $component['name'] . '_' . ++$copy;
  975. }
  976. $page_names[$page_num] = $page_name;
  977. }
  978. // If this component should be included in the options, add it with any prefix, in a page group, as needed.
  979. if (!isset($feature) || webform_component_feature($component['type'], $feature) || $prepend_group === TRUE) {
  980. $prefix = ($prepend_group && $component['pid']) ? $parent_names[$component['pid']] : '';
  981. if ($pagebreak_groups && $page_num > 1) {
  982. $options[$page_names[$page_num]][$cid] = $prefix . $component['name'];
  983. }
  984. else {
  985. $options[$cid] = $prefix . $component['name'];
  986. }
  987. }
  988. }
  989. return $options;
  990. }
  991. /**
  992. * A Form API process function to expand a component list into checkboxes.
  993. */
  994. function webform_component_select($element) {
  995. // Split the select list into checkboxes.
  996. foreach ($element['#options'] as $key => $label) {
  997. $label_length = strlen($label);
  998. $label = preg_replace('/^(\-)+/', '', $label);
  999. $indents = $label_length - strlen($label);
  1000. $element[$key] = array(
  1001. '#title' => $label,
  1002. '#type' => 'checkbox',
  1003. '#default_value' => array_search($key, $element['#value']) !== FALSE,
  1004. '#return_value' => $key,
  1005. '#parents' => array_merge($element['#parents'], array($key)),
  1006. '#indent' => $indents,
  1007. );
  1008. }
  1009. $element['#theme_wrappers'] = array();
  1010. $element['#type'] = 'webform_component_select';
  1011. $element['#theme'] = 'webform_component_select';
  1012. $element['#attached'] = array(
  1013. 'library' => array(
  1014. array('webform', 'admin'),
  1015. array('system', 'drupal.collapse'),
  1016. ),
  1017. 'js' => array(
  1018. 'misc/tableselect.js' => array(),
  1019. ),
  1020. );
  1021. return $element;
  1022. }
  1023. /**
  1024. * Theme the contents of a Webform component select element.
  1025. */
  1026. function theme_webform_component_select($variables) {
  1027. $element = $variables['element'];
  1028. $rows = array();
  1029. $header = array();
  1030. if (!isset($element['#all_checkbox']) || $element['#all_checkbox']) {
  1031. $header = array(array('class' => array('select-all'), 'data' => ' ' . t('Include all components')));
  1032. }
  1033. foreach (element_children($element) as $key) {
  1034. if ($key != 'suffix') {
  1035. $rows[] = array(
  1036. theme('indentation', array('size' => $element[$key]['#indent'])) . drupal_render($element[$key]),
  1037. );
  1038. }
  1039. }
  1040. $element['#type'] = 'fieldset';
  1041. $element['#value'] = NULL;
  1042. $element['#attributes']['class'] = array('webform-component-select-table');
  1043. if (!isset($element['#collapsible']) || $element['#collapsible']) {
  1044. $element['#attributes']['class'][] = 'collapsible';
  1045. }
  1046. if (!isset($element['#collapsed']) || $element['#collapsed']) {
  1047. $element['#attributes']['class'][] = 'collapsed';
  1048. }
  1049. if (empty($rows)) {
  1050. $element['#children'] = t('No available components.');
  1051. }
  1052. else {
  1053. $element['#children'] = '<div class="webform-component-select-wrapper">' . theme('table', array('header' => $header, 'rows' => $rows, 'sticky' => FALSE)) . '</div>';
  1054. }
  1055. if (isset($element['suffix'])) {
  1056. $element['#children'] .= '<div class="webform-component-select-suffix">' . drupal_render($element['suffix']) . '</div>';
  1057. }
  1058. return theme('fieldset', array('element' => $element));
  1059. }
  1060. /**
  1061. * Find a component's parents within a node.
  1062. *
  1063. * @param object $node
  1064. * The webform node.
  1065. * @param array $component
  1066. * The component to start with.
  1067. * @param string|true $what_to_return
  1068. * If TRUE, return complete component arrays. Otherwise, return the property
  1069. * of each component named in this parametre.
  1070. *
  1071. * @return array
  1072. * An array with a value for each parent and for the start component, in order
  1073. * ending with start component. What the value is is controlled by
  1074. * $what_to_return.
  1075. */
  1076. function webform_component_parent_keys($node, array $component, $what_to_return = 'form_key') {
  1077. $parents = array(($what_to_return === TRUE) ? $component : $component[$what_to_return]);
  1078. $pid = $component['pid'];
  1079. while ($pid) {
  1080. $parents[] = ($what_to_return === TRUE) ? $node->webform['components'][$pid] : $node->webform['components'][$pid][$what_to_return];
  1081. $pid = $node->webform['components'][$pid]['pid'];
  1082. }
  1083. return array_reverse($parents);
  1084. }
  1085. /**
  1086. * Populate a component with the defaults for that type.
  1087. */
  1088. function webform_component_defaults(&$component) {
  1089. $defaults = webform_component_invoke($component['type'], 'defaults');
  1090. drupal_alter('webform_component_defaults', $defaults, $component['type']);
  1091. if (!empty($defaults)) {
  1092. foreach ($defaults as $key => $default) {
  1093. if (!isset($component[$key])) {
  1094. $component[$key] = $default;
  1095. }
  1096. }
  1097. foreach ($defaults['extra'] as $extra => $default) {
  1098. if (!isset($component['extra'][$extra])) {
  1099. $component['extra'][$extra] = $default;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. /**
  1105. * Validate an element value is unique with no duplicates in the database.
  1106. */
  1107. function webform_validate_unique($element, $form_state) {
  1108. if ($element['#value'] !== '') {
  1109. $nid = $form_state['values']['details']['nid'];
  1110. $sid = $form_state['values']['details']['sid'];
  1111. $query = db_select('webform_submitted_data')
  1112. ->fields('webform_submitted_data', array('sid'))
  1113. ->condition('nid', $nid)
  1114. ->condition('cid', $element['#webform_component']['cid'])
  1115. ->condition('data', $element['#value'])
  1116. // More efficient than using countQuery() for data checks.
  1117. ->range(0, 1);
  1118. if ($sid) {
  1119. $query->condition('sid', $sid, '<>');
  1120. }
  1121. $count = $query->execute()->fetchField();
  1122. if ($count) {
  1123. form_error($element, t('The value %value has already been submitted once for the %title field. You may have already submitted this form, or you need to use a different value.', array('%value' => $element['#value'], '%title' => $element['#title'])));
  1124. }
  1125. }
  1126. }