webform.pages.inc 16 KB

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