comparison sites/all/modules/webform/includes/webform.pages.inc @ 0:ff03f76ab3fe

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