You are here

function webform_form in Webform 5

Same name and namespace in other branches
  1. 5.2 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 563

Code

function webform_form(&$node, &$param) {

  /* Start Edit Form */
  $form['webform'] = array(
    '#type' => 'fieldset',
    '#title' => t('Webform Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -4,
  );
  $form['webform']['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => $node->title,
    '#maxlength' => 128,
    '#required' => TRUE,
  );
  $form['webform']['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,
    '#required' => TRUE,
  );
  $form['webform']['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. 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->confirmation,
    '#cols' => 40,
    '#rows' => 10,
    '#required' => TRUE,
  );
  $form['webform']['format'] = filter_form($node->format);

  /* End Edit Form */

  /* Start Components Form */
  $form['components'] = array(
    '#type' => 'fieldset',
    '#title' => t('Components'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#weight' => -3,
  );
  if (is_array($node->webformcomponents) && !empty($node->webformcomponents)) {
    $options = array();
    foreach ($node->webformcomponents as $cid => $component) {
      $options[$cid] = check_plain($component['name']);
      $form['components'][$cid]['weight'] = array(
        '#type' => 'weight',
        '#delta' => count($node->webformcomponents) > 10 ? count($node->webformcomponents) : 10,
        '#title' => t("Weight"),
        '#default_value' => $component['weight'],
      );
      $form['components'][$cid]['mandatory'] = array(
        '#type' => 'checkbox',
        '#delta' => $component['mandatory'],
        '#title' => t("Mandatory"),
        '#default_value' => $component['mandatory'],
        '#access' => !in_array($component['type'], array(
          'markup',
          'fieldset',
          'pagebreak',
        )),
      );
    }
    if ($node->selected_component > 0) {
      $default_value = $node->selected_component;
    }
    else {
      $comp_keys = array_keys($node->webformcomponents);
      $default_value = array_shift($comp_keys);
    }
    $form['components']['selected_component'] = array(
      '#type' => 'radios',
      '#options' => $options,
      '#default_value' => $default_value,
      '#DANGEROUS_SKIP_CHECK' => TRUE,
    );
    $form['components']['delete_component'] = array(
      '#type' => 'submit',
      '#value' => t('Delete Selected'),
      '#weight' => 2,
    );
    $form['components']['edit_component'] = array(
      '#type' => 'submit',
      '#value' => t('Edit Selected'),
      '#weight' => 2,
    );
  }
  $component_types = _webform_load_components();
  natcasesort($component_types);
  $form['components']['webform_newfield_type'] = array(
    '#type' => 'select',
    '#title' => t('Add a new component'),
    '#default_value' => $node->components['webform_newfield_type'],
    '#options' => $component_types,
    '#description' => t('Each component adds a new field to the form. Any number components (even of the same type) may be added to the form. Select a component type to add above.'),
    '#weight' => 3,
  );
  $form['components']['add_component'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
    '#weight' => 4,
  );

  /* End Components Form */

  /* Start E-mail Settings Form */
  $form['mailsettings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Mail Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => -2,
  );
  $form['mailsettings']['email'] = array(
    '#type' => 'textfield',
    '#title' => t("E-mail to address"),
    '#default_value' => $node->email,
    '#description' => t('Form submissions will be e-mailed to this address. Leave blank for none.'),
  );

  // Build arrays of possible return email addresses and email subject lines from elements on the form.
  $possible_email_from_name = array(
    'default' => variable_get('webform_default_from_name', variable_get('site_name', '')) . ' [' . t('Default') . ']',
    'none' => '[' . t('None') . ']',
  );
  $possible_email_from_address = array(
    'default' => variable_get('webform_default_from_email', variable_get('site_mail', ini_get('sendmail_from'))) . ' [' . t('Default') . ']',
    'none' => '[' . t('None') . ']',
  );
  $possible_email_subject = array(
    'default' => variable_get('webform_default_subject', t('Form submission from: ') . t('$title')) . ' [' . t('Default') . ']',
    'none' => '[' . t('None') . ']',
  );
  if (is_array($node->webformcomponents) && !empty($node->webformcomponents)) {
    foreach ($node->webformcomponents as $cid => $component) {
      $type = $component['type'];
      if (in_array($type, array(
        'textfield',
        'hidden',
        'select',
      ))) {
        $possible_email_from_name[$component['name']] = $component['name'];
        $possible_email_subject[$component['name']] = $component['name'];
      }
      if (in_array($type, array(
        'email',
        'hidden',
        'select',
      ))) {
        $possible_email_from_address[$component['name']] = $component['name'];
      }
    }
  }
  $form['mailsettings']['email_from_name'] = array(
    '#type' => 'select',
    '#title' => t('E-mail from name'),
    '#default_value' => $node->email_from_name,
    '#options' => $possible_email_from_name,
    '#description' => t('After adding components to this form, any textfield or hidden form element may be selected as the sender\'s name for e-mails.'),
    '#weight' => 6,
    '#DANGEROUS_SKIP_CHECK' => TRUE,
  );
  $form['mailsettings']['email_from_address'] = array(
    '#type' => 'select',
    '#title' => t('E-mail from address'),
    '#default_value' => $node->email_from_address,
    '#options' => $possible_email_from_address,
    '#description' => t('After adding components to this form, any e-mail or hidden form element may be selected as the sender\'s address for e-mails.'),
    '#weight' => 7,
    '#DANGEROUS_SKIP_CHECK' => TRUE,
  );
  $form['mailsettings']['email_subject'] = array(
    '#type' => 'select',
    '#title' => t('E-mail subject'),
    '#default_value' => $node->email_subject,
    '#options' => $possible_email_subject,
    '#description' => t('After adding components to this form, any textfield or hidden form element may be selected as the subject line for e-mails.'),
    '#weight' => 8,
    '#DANGEROUS_SKIP_CHECK' => TRUE,
  );

  /* End mail settings form */

  /* Start advanced settings form */
  $form['advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -1,
  );
  $form['advanced']['submitlimit'] = array(
    '#type' => 'fieldset',
    '#title' => t('Limit the number of submission a user may send within a specified time period.'),
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['advanced']['submitlimit']['unlimited'] = array(
    '#name' => 'enforce_limit',
    // Override the naming scheme to force these radios into the same group.
    '#type' => 'radio',
    '#return_value' => 'no',
    '#title' => t('Unlimited'),
    '#default_value' => $node->submit_limit > 0 ? false : 'no',
    '#parents' => array(
      'advanced',
      'submitlimit',
    ),
  );
  $form['advanced']['submitlimit']['limited'] = array(
    '#name' => 'enforce_limit',
    // Override the naming scheme to force these radios into the same group.
    '#type' => 'radio',
    '#return_value' => 'yes',
    '#prefix' => '<br />',
    '#suffix' => t('Limit to '),
    '#default_value' => $node->submit_limit > 0 ? 'yes' : false,
    '#parents' => array(
      'advanced',
      'submitlimit',
    ),
  );
  $form['advanced']['submitlimit']['submit_limit'] = array(
    '#type' => 'textfield',
    '#maxlength' => 2,
    '#size' => 2,
    '#suffix' => ' ' . t('submission(s)') . ' ',
    '#default_value' => $node->submit_limit > 0 ? $node->submit_limit : "",
    '#attributes' => array(
      'style' => 'width: 2em; display: inline;',
      'onchange' => "javascript: document.getElementsByName('enforce_limit').item(1).checked = true;",
      'onclick' => "javascript: document.getElementsByName('enforce_limit').item(1).checked = true;",
    ),
  );
  $form['advanced']['submitlimit']['submit_interval'] = array(
    '#type' => 'select',
    '#options' => array(
      '157784630' => t('ever'),
      // 5 years.
      '1600' => t('every hour'),
      '86400' => t('every day'),
      '604800' => t('every week'),
    ),
    '#default_value' => $node->submit_interval,
    '#attributes' => array(
      'onchange' => "javascript: document.getElementsByName('enforce_limit').item(1).checked = true;",
      'onclick' => "javascript: document.getElementsByName('enforce_limit').item(1).checked = true;",
    ),
  );
  if (user_access('use PHP for additional processing')) {
    $form['advanced']['additional_validate'] = array(
      '#type' => 'textarea',
      '#title' => 'Additional Validation',
      '#description' => t('Enter PHP code to preform 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/4.7/file/developer/topics/forms_api.html">Forms API</a>.'),
      '#default_value' => $node->additional_validate,
      '#cols' => 40,
      '#rows' => 10,
    );
    $form['advanced']['additional_submit'] = array(
      '#type' => 'textarea',
      '#title' => 'Additional Processing',
      '#description' => t('Enter PHP code to preform 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/4.7/file/developer/topics/forms_api.html">Forms API</a>.'),
      '#default_value' => $node->additional_submit,
      '#cols' => 40,
      '#rows' => 10,
    );
  }
  else {
    $form['advanced']['additional_validate'] = array(
      '#type' => 'value',
      '#value' => $node->additional_validate,
    );
    $form['advanced']['additional_submit'] = array(
      '#type' => 'value',
      '#value' => $node->additional_submit,
    );
  }
  $form['advanced']['redirect_post'] = array(
    '#type' => 'checkbox',
    '#title' => t("Redirect POST Values"),
    '#description' => t("Forward the contents of the POST array to the redirect URL. Use this option for custom processing of the form information. No processing will be done by webform. The confirmation option above MUST be a full redirect URL for this option to have an effect."),
    '#default_value' => $node->redirect_post,
  );

  /* End Advanced Settings Form */

  // Add hidden form elements containing the contents of the components.
  if (is_array($node->webformcomponents) && !empty($node->webformcomponents)) {
    $form['webformcomponents'] = array(
      '#tree' => TRUE,
    );
    foreach ($node->webformcomponents as $cid => $component) {

      // Create a hidden field with the component's values.
      $form['webformcomponents'][$cid] = array(
        '#type' => 'hidden',
        '#value' => base64_encode(serialize($component)),
      );
    }
  }
  return $form;
}