You are here

function webform_form in Webform 5.2

Same name and namespace in other branches
  1. 5 webform.module \webform_form()
  2. 6.2 webform.module \webform_form()

Implementation of hook_form(). Creates the standard form for editing or creating a webform.

File

./webform.module, line 460

Code

function webform_form(&$node, &$param) {
  $form['webform'] = array(
    '#type' => 'markup',
    '#tree' => TRUE,
  );

  // Set node defaults if empty.
  if (!isset($node->nid) && !isset($node->webform)) {
    $node->nid = 0;
    $additions = webform_load($node);
    $node->webform = $additions->webform;
    $node->nid = NULL;
  }

  /* Save Components in a value (helps with clone.module) */
  $form['webform']['components'] = array(
    '#type' => 'value',
    '#value' => $node->webform['components'],
  );

  /* Start Edit Form */
  $form['webform']['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -4,
    '#parents' => array(
      'webform',
    ),
  );
  $form['webform']['settings']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $node->title,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#tree' => FALSE,
  );
  $form['webform']['settings']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('Text to be shown as teaser and before the form.'),
    '#default_value' => $node->body,
    '#cols' => 40,
    '#rows' => 10,
    '#tree' => FALSE,
  );
  $form['webform']['settings']['confirmation'] = array(
    '#type' => 'textarea',
    '#title' => t('Confirmation message or redirect URL'),
    '#description' => t('Message to be shown upon successful submission or a path to a redirect page. Preface message with <em>message:</em> for a simple message that does not require a page refresh. Redirect pages must start with <em>http://</em> for external sites or <em>internal:</em> for an internal path. i.e. <em>http://www.example.com</em> or <em>internal:node/10</em>'),
    '#default_value' => $node->webform['confirmation'],
    '#cols' => 40,
    '#rows' => 10,
  );
  $form['webform']['settings']['format'] = filter_form($node->format);

  /* End Edit Form */

  /* Start per-role submission control */
  $form['webform']['role_control'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform access control'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -3,
    '#parents' => array(
      'webform',
    ),
    '#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>.'),
    '#access' => variable_get('webform_submission_access_control', 1),
  );
  $user_roles = user_roles();
  $form['webform']['role_control']['roles'] = array(
    '#default_value' => $node->webform['roles'],
    '#options' => $user_roles,
    '#type' => 'checkboxes',
    '#title' => t('Roles that can submit this webform'),
    '#description' => t('Uncheck all roles to prevent new submissions. The %authenticated role applies to any user signed into the site, regardless of other assigned roles.', array(
      '%authenticated' => $user_roles[2],
    )),
  );

  /* End per-role submission control */

  /* Start E-mail Settings Form */
  $form['webform']['mail_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform mail settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -2,
    '#parents' => array(
      'webform',
    ),
    '#theme' => 'webform_mail_settings_form',
  );
  $form['webform']['mail_settings']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('E-mail to address'),
    '#maxlength' => 255,
    '#default_value' => $node->webform['email'],
    '#description' => t('Form submissions will be e-mailed to this address. Leave blank for none. Multiple e-mail addresses may be separated by commas.'),
  );
  $form['webform']['mail_settings']['email_components'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#theme' => 'webform_mail_components_form',
    '#title' => t('Conditional e-mail recipients'),
    '#description' => t('The settings below allow you to send e-mails to multiple recipients based off the value of a component.'),
    '#node' => $node,
  );
  $options = _webform_component_options($node->webform['components'], 'email');
  $default_value = array();
  if (is_array($node->webform['components'])) {
    foreach ($node->webform['components'] as $cid => $component) {
      if (isset($component['extra']['email']) && $component['extra']['email']) {
        $default_value[] = $cid;
      }
    }
  }
  $form['webform']['mail_settings']['email_components']['email_components'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $default_value,
    '#parents' => array(
      'webform',
      'email_components',
    ),
  );
  foreach (array(
    'from_name',
    'from_address',
    'subject',
  ) as $field) {
    switch ($field) {
      case 'from_name':
        $default_value = webform_variable_get('webform_default_from_name');
        $title = t('E-mail from name');
        $description = t('After adding components to this form any email, select, or hidden form element may be selected as the sender\'s name for e-mails.');
        break;
      case 'from_address':
        $default_value = webform_variable_get('webform_default_from_address');
        $title = t('E-mail from address');
        $description = t('After adding components to this form any textfield, select, or hidden form element may be selected as the sender\'s e-mail address.');
        break;
      case 'subject':
        $default_value = webform_variable_get('webform_default_subject');
        $title = t('E-mail subject');
        $description = t('After adding components to this form any textfield, select, or hidden form element may be selected as the subject for e-mails.');
        break;
    }
    $form['webform']['mail_settings']['email_' . $field . '_option'] = array(
      '#title' => $title,
      '#type' => 'radios',
      '#default_value' => is_numeric($node->webform['email_' . $field]) ? 'component' : (empty($default_value) || $node->webform['email_' . $field] != 'default' && isset($node->webform['email_' . $field]) ? 'custom' : 'default'),
      '#description' => $description,
    );
    if (!empty($default_value)) {
      $form['webform']['mail_settings']['email_' . $field . '_option']['#options']['default'] = $default_value;
    }
    $form['webform']['mail_settings']['email_' . $field . '_option']['#options']['custom'] = 'custom';
    $form['webform']['mail_settings']['email_' . $field . '_option']['#options']['component'] = 'component';
    $form['webform']['mail_settings']['email_' . $field . '_custom'] = array(
      '#type' => 'textfield',
      '#size' => 40,
      '#default_value' => !is_numeric($node->webform['email_' . $field]) && $node->webform['email_' . $field] != 'default' ? $node->webform['email_' . $field] : NULL,
    );
    $options = _webform_component_options($node->webform['components'], $field == 'from_address' ? 'email' : 'string');
    $form['webform']['mail_settings']['email_' . $field . '_component'] = array(
      '#type' => 'select',
      '#default_value' => is_numeric($node->webform['email_' . $field]) ? $node->webform['email_' . $field] : NULL,
      '#options' => empty($options) ? array(
        '' => 'No available components',
      ) : $options,
      '#disabled' => empty($options) ? TRUE : FALSE,
      '#weight' => 6,
    );
  }

  /* End mail settings form */

  /* Start advanced settings form */
  $form['webform']['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -1,
    '#parents' => array(
      'webform',
    ),
  );
  $form['webform']['advanced']['teaser'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show complete form in teaser'),
    '#default_value' => $node->webform['teaser'],
    '#description' => t('Display the entire form in the teaser display of this node.'),
  );
  $form['webform']['advanced']['submit_limit'] = array(
    '#type' => 'fieldset',
    '#title' => t('Limit the number of submissions a user may send within a specified time period'),
    '#theme' => 'webform_advanced_submit_limit_form',
  );
  $form['webform']['advanced']['submit_limit']['enforce_limit'] = array(
    '#type' => 'radios',
    '#options' => array(
      'no' => t('Unlimited'),
      'yes' => 'Limit to !count submission(s) !timespan',
    ),
    '#default_value' => $node->webform['submit_limit'] == -1 ? 'no' : 'yes',
    '#parents' => array(
      'webform',
      'enforce_limit',
    ),
  );
  $form['webform']['advanced']['submit_limit']['submit_limit'] = array(
    '#type' => 'textfield',
    '#maxlength' => 2,
    '#size' => 2,
    '#default_value' => $node->webform['submit_limit'] != -1 ? $node->webform['submit_limit'] : '',
    '#parents' => array(
      'webform',
      'submit_limit',
    ),
  );
  $form['webform']['advanced']['submit_limit']['submit_interval'] = array(
    '#type' => 'select',
    '#options' => array(
      '-1' => t('ever'),
      '1600' => t('every hour'),
      '86400' => t('every day'),
      '604800' => t('every week'),
    ),
    '#default_value' => $node->webform['submit_interval'],
    '#parents' => array(
      'webform',
      'submit_interval',
    ),
  );
  $form['webform']['advanced']['submit_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Submit button text'),
    '#default_value' => $node->webform['submit_text'],
    '#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.'),
  );
  if (user_access('use PHP for additional processing')) {
    $form['webform']['advanced']['additional_validate'] = array(
      '#type' => 'textarea',
      '#title' => t('Additional Validation'),
      '#description' => t('Enter PHP code to perform additional validation for this form. Include the &lt;?php ?&gt; tags. $form_id and $form_values are available variables. If validation fails, use the form_set_error function to prevent the form from being submitted. Use the same syntax as a _validate function used in the <a href="http://api.drupal.org/api/file/developer/topics/forms_api.html">Forms API</a>.'),
      '#default_value' => $node->webform['additional_validate'],
      '#cols' => 40,
      '#rows' => 10,
    );
    $form['webform']['advanced']['additional_submit'] = array(
      '#type' => 'textarea',
      '#title' => t('Additional Processing'),
      '#description' => t('Enter PHP code to perform additional processing for this form (after the validation). Include the &lt;?php ?&gt; tags. $form_id and $form_values are available variables, use the same syntax as a _submit function used in the <a href="http://api.drupal.org/api/file/developer/topics/forms_api.html">Forms API</a>.'),
      '#default_value' => $node->webform['additional_submit'],
      '#cols' => 40,
      '#rows' => 10,
    );
  }
  else {
    $form['webform']['advanced']['additional_validate'] = array(
      '#type' => 'value',
      '#value' => $node->webform['additional_validate'],
    );
    $form['webform']['advanced']['additional_submit'] = array(
      '#type' => 'value',
      '#value' => $node->webform['additional_submit'],
    );
  }

  /* End Advanced Settings Form */
  return $form;
}