[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/webform/includes/ -> webform.pages.inc (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7