webform.pages.inc

Tracking 7.x-4.x branch
  1. drupal
    1. 6 contributions/webform/includes/webform.pages.inc
    2. 7 contributions/webform/includes/webform.pages.inc

Menu callbacks and functions for configuring and editing webforms.

Functions & methods

NameDescription
theme_webform_advanced_redirection_formTheme the redirection setting on the webform node form.
theme_webform_advanced_submit_limit_formTheme the submit limit fieldset on the webform node form.
theme_webform_advanced_total_submit_limit_formTheme the total submit limit fieldset on the webform node form.
webform_configure_formMain configuration form for editing a webform node.
webform_configure_form_submitSubmit handler for webform_configure_form().
webform_configure_form_submit_saveSubmit handler for webform_configure_form() that saves the node.
webform_configure_form_validateValidate handler for webform_configure_form().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Menu callbacks and functions for configuring and editing webforms.
  6. */
  7. /**
  8. * Main configuration form for editing a webform node.
  9. */
  10. function webform_configure_form($form, &$form_state, $node) {
  11. $form['#attached']['library'][] = array('webform', 'admin');
  12. $form['#node'] = $node;
  13. $form['#submit'] = array(
  14. 'webform_configure_form_submit',
  15. 'webform_configure_form_submit_save',
  16. );
  17. $form['nid'] = array(
  18. '#type' => 'value',
  19. '#value' => $node->nid,
  20. );
  21. /* Start Edit Form */
  22. $form['submission'] = array(
  23. '#type' => 'fieldset',
  24. '#title' => t('Submission settings'),
  25. '#collapsible' => TRUE,
  26. '#collapsed' => FALSE,
  27. '#weight' => -4,
  28. );
  29. $form['submission']['confirmation'] = array(
  30. '#type' => 'text_format',
  31. '#title' => t('Confirmation message'),
  32. '#description' => t('Message to be shown upon successful submission. If the redirection location is set to <em>Confirmation page</em> it will be shown on its own page, otherwise this displays as a message. Supports Webform token replacements.'),
  33. '#default_value' => $node->webform['confirmation'],
  34. '#cols' => 40,
  35. '#rows' => 10,
  36. '#format' => $node->webform['confirmation_format'],
  37. '#parents' => array('confirmation'),
  38. );
  39. // Redirection settings.
  40. if (strpos($node->webform['redirect_url'], '<') === 0) {
  41. $redirect = trim($node->webform['redirect_url'], '<>');
  42. // Redirection is set to front page.
  43. if ($redirect == 'front') {
  44. $redirect = 'url';
  45. $redirect_url = $node->webform['redirect_url'];
  46. }
  47. else {
  48. $redirect_url = '';
  49. }
  50. }
  51. else {
  52. $redirect = 'url';
  53. $redirect_url = $node->webform['redirect_url'];
  54. }
  55. $form['submission']['redirection'] = array(
  56. '#type' => 'item',
  57. '#title' => t('Redirection location'),
  58. '#theme' => 'webform_advanced_redirection_form',
  59. '#description' => t('Choose where to redirect the user upon successful submission.') . ' ' . t('The <em>Custom URL</em> option supports Webform token replacements.'),
  60. );
  61. $form['submission']['tokens'] = array(
  62. '#markup' => theme('webform_token_help', array('groups' => array('node', 'submission'))),
  63. );
  64. $form['submission']['redirection']['redirect']= array(
  65. '#type' => 'radios',
  66. '#default_value' => $redirect,
  67. '#options' => array(
  68. 'confirmation' => t('Confirmation page'),
  69. 'url' => t('Custom URL'),
  70. 'none' => t('No redirect (reload current page)'),
  71. ),
  72. );
  73. $form['submission']['redirection']['redirect_url'] = array(
  74. '#type' => 'textfield',
  75. '#title' => t('Redirect URL'),
  76. '#description' => t('URL to redirect the user to upon successful submission.'),
  77. '#default_value' => $redirect_url,
  78. '#maxlength' => 255,
  79. );
  80. // Submission limit settings for all submissions.
  81. $form['submission']['total_submit_limit'] = array(
  82. '#type' => 'item',
  83. '#title' => t('Total submissions limit'),
  84. '#theme' => 'webform_advanced_total_submit_limit_form',
  85. '#description' => t('Limit the total number of allowed submissions.'),
  86. );
  87. $form['submission']['total_submit_limit']['enforce_total_limit'] = array(
  88. '#type' => 'radios',
  89. '#options' => array('no' => t('Unlimited'), 'yes' => 'Limit to !count total submission(s) !timespan'),
  90. '#default_value' => $node->webform['total_submit_limit'] == -1 ? 'no' : 'yes',
  91. '#parents' => array('enforce_total_limit'),
  92. );
  93. $form['submission']['total_submit_limit']['total_submit_limit'] = array(
  94. '#type' => 'textfield',
  95. '#maxlength' => 8,
  96. '#size' => 8,
  97. '#default_value' => $node->webform['total_submit_limit'] != -1 ? $node->webform['total_submit_limit'] : '',
  98. '#parents' => array('total_submit_limit'),
  99. );
  100. $form['submission']['total_submit_limit']['total_submit_interval'] = array(
  101. '#type' => 'select',
  102. '#options' => array(
  103. '-1' => t('ever'),
  104. '3600' => t('every hour'),
  105. '86400' => t('every day'),
  106. '604800' => t('every week'),
  107. ),
  108. '#default_value' => $node->webform['total_submit_interval'],
  109. '#parents' => array('total_submit_interval'),
  110. );
  111. // Submission limit per user settings.
  112. $form['submission']['submit_limit'] = array(
  113. '#type' => 'item',
  114. '#title' => t('Per user submission limit'),
  115. '#theme' => 'webform_advanced_submit_limit_form',
  116. '#description' => t('Limit the number of submissions <em>per user</em>. A user is identified by their user login if logged-in, or by their IP Address and Cookie if anonymous. Use of cookies may be modified in the global <a href="!url">Webform settings</a>.', array('!url' => url('admin/config/content/webform'))),
  117. );
  118. $form['submission']['submit_limit']['enforce_limit'] = array(
  119. '#type' => 'radios',
  120. '#options' => array('no' => t('Unlimited'), 'yes' => 'Limit each user to !count submission(s) !timespan'),
  121. '#default_value' => $node->webform['submit_limit'] == -1 ? 'no' : 'yes',
  122. '#parents' => array('enforce_limit'),
  123. );
  124. $form['submission']['submit_limit']['submit_limit'] = array(
  125. '#type' => 'textfield',
  126. '#maxlength' => 2,
  127. '#size' => 2,
  128. '#default_value' => $node->webform['submit_limit'] != -1 ? $node->webform['submit_limit'] : '',
  129. '#parents' => array('submit_limit'),
  130. );
  131. $form['submission']['submit_limit']['submit_interval'] = array(
  132. '#type' => 'select',
  133. '#options' => array(
  134. '-1' => t('ever'),
  135. '3600' => t('every hour'),
  136. '86400' => t('every day'),
  137. '604800' => t('every week'),
  138. ),
  139. '#default_value' => $node->webform['submit_interval'],
  140. '#parents' => array('submit_interval'),
  141. );
  142. $form['submission']['status'] = array(
  143. '#type' => 'radios',
  144. '#title' => t('Status of this form'),
  145. '#default_value' => $node->webform['status'] == 0 ? 0 : 1,
  146. '#description' => t('Closing a form prevents any further submissions by any users.'),
  147. '#parents' => array('status'),
  148. '#options' => array(1 => t('Open'), 0 => t('Closed')),
  149. );
  150. /* End Edit Form */
  151. /* Start per-role submission control */
  152. $form['role_control'] = array(
  153. '#type' => 'fieldset',
  154. '#title' => t('Submission access'),
  155. '#collapsible' => TRUE,
  156. '#collapsed' => FALSE,
  157. '#weight' => -3,
  158. '#description' => t('These permissions affect which roles can submit this webform. It does not prevent access to the webform page. If needing to prevent access to the webform page entirely, use a content access module such as <a href="http://drupal.org/project/taxonomy_access">Taxonomy Access</a> or <a href="http://drupal.org/project/node_privacy_byrole">Node Privacy by Role</a>.'),
  159. '#access' => variable_get('webform_submission_access_control', 1),
  160. );
  161. $user_roles = user_roles();
  162. foreach ($user_roles as $rid => $rname) {
  163. if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
  164. continue;
  165. }
  166. $user_roles[$rid] = webform_tt("user:rid:$rid:name", $rname);
  167. }
  168. $form['role_control']['roles'] = array(
  169. '#default_value' => $node->webform['roles'],
  170. '#options' => $user_roles,
  171. '#type' => 'checkboxes',
  172. '#title' => t('Roles that can submit this webform'),
  173. '#description' => t('The %authenticated role applies to any user signed into the site, regardless of other assigned roles.', array('%authenticated' => $user_roles[2])),
  174. );
  175. /* End per-role submission control */
  176. /* Start advanced settings form */
  177. $form['advanced'] = array(
  178. '#type' => 'fieldset',
  179. '#title' => t('Advanced settings'),
  180. '#collapsible' => TRUE,
  181. '#collapsed' => TRUE,
  182. '#weight' => -1,
  183. );
  184. $form['advanced']['block'] = array(
  185. '#type' => 'checkbox',
  186. '#title' => t('Available as block'),
  187. '#default_value' => $node->webform['block'],
  188. '#description' => t('If enabled this webform will be available as a block.'),
  189. '#access' => user_access('administer blocks') || user_access('administer site configuration') || user_access('use panels dashboard'),
  190. );
  191. $form['advanced']['teaser'] = array(
  192. '#type' => 'checkbox',
  193. '#title' => t('Show complete form in teaser'),
  194. '#default_value' => $node->webform['teaser'],
  195. '#description' => t('Display the entire form in the teaser display of this node.'),
  196. );
  197. $form['advanced']['allow_draft'] = array(
  198. '#type' => 'checkbox',
  199. '#title' => t('Show "Save draft" button'),
  200. '#default_value' => $node->webform['allow_draft'],
  201. '#description' => t('Allow your users to save and finish the form later. This option is available only for authenticated users.'),
  202. );
  203. $form['advanced']['auto_save'] = array(
  204. '#type' => 'checkbox',
  205. '#title' => t('Automatically save as draft between pages'),
  206. '#default_value' => $node->webform['auto_save'],
  207. '#description' => t('Automatically save partial submissions when users click the "Next" or "Previous" buttons in a multipage form.'),
  208. );
  209. $form['advanced']['submit_notice'] = array(
  210. '#type' => 'checkbox',
  211. '#title' => t('Show the notification about previous submissions.'),
  212. '#default_value' => $node->webform['submit_notice'],
  213. '#description' => t('Show the previous submissions notification that appears when users have previously submitted this form.'),
  214. );
  215. $form['advanced']['submit_text'] = array(
  216. '#type' => 'textfield',
  217. '#title' => t('Submit button text'),
  218. '#default_value' => $node->webform['submit_text'],
  219. '#description' => t('By default the submit button on this form will have the label <em>Submit</em>. Enter a new title here to override the default.'),
  220. );
  221. /* End Advanced Settings Form */
  222. $form['actions'] = array(
  223. '#type' => 'actions',
  224. '#weight' => 300,
  225. );
  226. $form['actions']['submit'] = array(
  227. '#type' => 'submit',
  228. '#value' => t('Save configuration'),
  229. );
  230. return $form;
  231. }
  232. /**
  233. * Validate handler for webform_configure_form().
  234. */
  235. function webform_configure_form_validate($form, &$form_state) {
  236. // Ensure the entered e-mail addresses are valid.
  237. if (!empty($form_state['values']['email'])) {
  238. $emails = explode(',', $form_state['values']['email']);
  239. foreach ($emails as $email) {
  240. if (!valid_email_address(trim($email))) {
  241. form_error($form['submission']['redirect_url'], t('The entered email address %address is not a valid address.', array('%address' => $email)));
  242. break;
  243. }
  244. }
  245. }
  246. // Ensure the entered redirect URL is valid.
  247. if ($form_state['values']['redirect'] == 'url') {
  248. $redirect_url = trim($form_state['values']['redirect_url']);
  249. if (empty($redirect_url)) {
  250. form_error($form['submission']['redirection']['redirect_url'], t('A valid URL is required for custom redirection.'));
  251. }
  252. elseif (strpos($redirect_url, 'http') === 0 && !valid_url(_webform_filter_values($redirect_url, $form['#node']), TRUE)) {
  253. form_error($form['submission']['redirection']['redirect_url'], t('The entered URL is not a valid address.'));
  254. }
  255. else {
  256. form_set_value($form['submission']['redirection']['redirect_url'], $redirect_url, $form_state);
  257. }
  258. }
  259. elseif ($form_state['values']['redirect'] == 'confirmation') {
  260. form_set_value($form['submission']['redirection']['redirect_url'], '<confirmation>', $form_state);
  261. }
  262. else {
  263. form_set_value($form['submission']['redirection']['redirect_url'], '<none>', $form_state);
  264. }
  265. }
  266. /**
  267. * Submit handler for webform_configure_form().
  268. */
  269. function webform_configure_form_submit($form, &$form_state) {
  270. // Edit the node by reference just to shorten it up.
  271. $node = &$form['#node'];
  272. // Save the confirmation.
  273. $node->webform['confirmation'] = $form_state['values']['confirmation']['value'];
  274. $node->webform['confirmation_format'] = $form_state['values']['confirmation']['format'];
  275. // Save the redirect URL
  276. $node->webform['redirect_url'] = $form_state['values']['redirect_url'];
  277. // Overall form status.
  278. $node->webform['status'] = $form_state['values']['status'];
  279. // Save roles.
  280. $node->webform['roles'] = array_keys(array_filter($form_state['values']['roles']));
  281. // Set the block option.
  282. $node->webform['block'] = $form_state['values']['block'];
  283. // Set the Show complete form in teaser setting.
  284. $node->webform['teaser'] = $form_state['values']['teaser'];
  285. // Set the draft option.
  286. $node->webform['allow_draft'] = $form_state['values']['allow_draft'];
  287. // Set the auto-save draft option.
  288. $node->webform['auto_save'] = $form_state['values']['auto_save'];
  289. // Set the submit limit to -1 if set to unlimited.
  290. if ($form_state['values']['enforce_limit'] == 'no') {
  291. $node->webform['submit_limit'] = -1;
  292. $node->webform['submit_interval'] = -1;
  293. }
  294. else {
  295. $node->webform['submit_limit'] = $form_state['values']['submit_limit'];
  296. $node->webform['submit_interval'] = $form_state['values']['submit_interval'];
  297. }
  298. // Set the total submit limit to -1 if set to unlimited.
  299. if ($form_state['values']['enforce_total_limit'] == 'no') {
  300. $node->webform['total_submit_limit'] = -1;
  301. $node->webform['total_submit_interval'] = -1;
  302. }
  303. else {
  304. $node->webform['total_submit_limit'] = $form_state['values']['total_submit_limit'];
  305. $node->webform['total_submit_interval'] = $form_state['values']['total_submit_interval'];
  306. }
  307. // Set submit notice.
  308. $node->webform['submit_notice'] = $form_state['values']['submit_notice'];
  309. // Set submit button text.
  310. $node->webform['submit_text'] = $form_state['values']['submit_text'];
  311. }
  312. /**
  313. * Submit handler for webform_configure_form() that saves the node.
  314. *
  315. * This is separate from webform_configure_form_submit() to allow other modules
  316. * to add properties if needed into the $form['#node'] object before save.
  317. */
  318. function webform_configure_form_submit_save($form, &$form_state) {
  319. node_save($form['#node']);
  320. drupal_set_message(t('The form settings have been updated.'));
  321. }
  322. /**
  323. * Theme the redirection setting on the webform node form.
  324. */
  325. function theme_webform_advanced_redirection_form($variables) {
  326. $form = $variables['form'];
  327. // Add special class for setting the active radio button.
  328. $form['redirect_url']['#attributes']['class'] = array('webform-set-active');
  329. // Remove title and description for Redirect URL field.
  330. $form['redirect_url']['#title'] = NULL;
  331. $form['redirect_url']['#description'] = NULL;
  332. $form['redirect']['confirmation']['#theme_wrappers'] = array('webform_inline_radio');
  333. $form['redirect']['url']['#theme_wrappers'] = array('webform_inline_radio');
  334. $form['redirect']['none']['#theme_wrappers'] = array('webform_inline_radio');
  335. $form['redirect']['url']['#inline_element'] = $form['redirect']['url']['#title'] . ': ' . drupal_render($form['redirect_url']);
  336. $form['redirect']['url']['#title'] = NULL;
  337. return drupal_render_children($form);
  338. }
  339. /**
  340. * Theme the submit limit fieldset on the webform node form.
  341. */
  342. function theme_webform_advanced_submit_limit_form($variables) {
  343. $form = $variables['form'];
  344. $form['submit_limit']['#attributes']['class'] = array('webform-set-active');
  345. $form['submit_interval']['#attributes']['class'] = array('webform-set-active');
  346. // Remove div wrappers around limit options.
  347. $form['submit_limit']['#theme_wrappers'] = array();
  348. $form['submit_interval']['#theme_wrappers'] = array();
  349. $replacements = array(
  350. '!count' => drupal_render($form['submit_limit']),
  351. '!timespan' => drupal_render($form['submit_interval']),
  352. );
  353. $form['enforce_limit']['no']['#theme_wrappers'] = array('webform_inline_radio');
  354. $form['enforce_limit']['yes']['#title'] = NULL;
  355. $form['enforce_limit']['yes']['#inline_element'] = t('Limit each user to !count submission(s) !timespan', $replacements);
  356. $form['enforce_limit']['yes']['#theme_wrappers'] = array('webform_inline_radio');
  357. return drupal_render_children($form);
  358. }
  359. /**
  360. * Theme the total submit limit fieldset on the webform node form.
  361. */
  362. function theme_webform_advanced_total_submit_limit_form($variables) {
  363. $form = $variables['form'];
  364. $form['total_submit_limit']['#attributes']['class'] = array('webform-set-active');
  365. $form['total_submit_interval']['#attributes']['class'] = array('webform-set-active');
  366. // Remove div wrappers around limit options.
  367. $form['total_submit_limit']['#theme_wrappers'] = array();
  368. $form['total_submit_interval']['#theme_wrappers'] = array();
  369. $replacements = array(
  370. '!count' => drupal_render($form['total_submit_limit']),
  371. '!timespan' => drupal_render($form['total_submit_interval']),
  372. );
  373. $form['enforce_total_limit']['no']['#theme_wrappers'] = array('webform_inline_radio');
  374. $form['enforce_total_limit']['yes']['#title'] = NULL;
  375. $form['enforce_total_limit']['yes']['#inline_element'] = t('Limit to !count total submission(s) !timespan', $replacements);
  376. $form['enforce_total_limit']['yes']['#theme_wrappers'] = array('webform_inline_radio');
  377. return drupal_render_children($form);
  378. }