You are here

function webform_client_form in Webform 5

Same name and namespace in other branches
  1. 5.2 webform.module \webform_client_form()
  2. 6.3 webform.module \webform_client_form()
  3. 6.2 webform.module \webform_client_form()
  4. 7.4 webform.module \webform_client_form()
  5. 7.3 webform.module \webform_client_form()

Client form generation function. If this is displaying an existing submission, pass in the $submission variable with the contents of the submission to be displayed.

Parameters

$node: The current webform node.

$submission: Optional. An array of values for the form if we're displaying a result.

$enabled: Optional. If displaying a result, specify if form elements are enabled for editing.

$form_values: The current form values of a submission, used in multipage webforms.

1 string reference to 'webform_client_form'
webform_forms in ./webform.module
Implementation of hook_forms(). All webform_client_form forms share the same form handler

File

./webform.module, line 1209

Code

function webform_client_form(&$node, $submission = array(), $enabled = false, $form_values = NULL) {
  global $user;
  _webform_load_components();

  // Load all the components.
  if ($_POST['op'] == t('Preview')) {
    $preview = true;
  }
  if (module_exists('profile')) {
    profile_load_profile($user);
  }
  if ($node->redirect_post && (valid_url(trim($node->confirmation), true) || preg_match('/^internal:/', $node->confirmation))) {
    $form['#action'] = trim($node->confirmation);
  }

  // Set a header for navigating results.
  if ($submission && user_access('access webform results')) {

    // Add CSS to display submission info. Don't preprocess because this CSS file is used rarely.
    drupal_add_css(drupal_get_path('module', 'webform') . '/webform.css', 'module', 'all', FALSE);
    $previous = db_result(db_query('SELECT MAX(sid) FROM {webform_submissions} WHERE nid = %d AND sid < %d', array(
      $node->nid,
      $submission['sid'],
    )));
    $next = db_result(db_query('SELECT MIN(sid) FROM {webform_submissions} WHERE nid = %d AND sid > %d', array(
      $node->nid,
      $submission['sid'],
    )));
    $form['navigation'] = array(
      '#prefix' => '<div class="webform-submission-navigation">',
      '#suffix' => '</div>',
    );
    $form['navigation']['previous'] = array(
      '#value' => $previous ? l(t('Previous submission'), 'node/' . $node->nid . '/results/' . ($enabled ? 'edit' : 'view') . '/' . $previous, array(
        'class' => 'webform-submission-previous',
      )) : '<span class="webform-submission-previous">' . t('Previous submission') . '</span>',
    );
    $form['navigation']['next'] = array(
      '#value' => $next ? l(t('Next submission'), 'node/' . $node->nid . '/results/' . ($enabled ? 'edit' : 'view') . '/' . $next, array(
        'class' => 'webform-submission-next',
      )) : '<span class="webform-submission-next">' . t('Next submission') . '</span>',
    );
    $form['submission_info'] = array(
      '#title' => t('Submission Information'),
      '#type' => 'fieldset',
      '#collapsible' => FALSE,
    );
    $account = user_load(array(
      'uid' => $submission['uid'],
    ));
    $form['submission_info']['user_picture'] = array(
      '#value' => theme('user_picture', $account),
    );
    $form['submission_info']['submitted'] = array(
      '#value' => '<div>' . t('Submitted by !name', array(
        '!name' => theme('username', $account),
      )) . '</div>',
    );
    $form['submission_info']['time'] = array(
      '#value' => '<div>' . format_date($submission['submitted'], 'large') . '</div>',
    );
    $form['submission_info']['ip_address'] = array(
      '#value' => '<div>' . $submission['remote_addr'] . '</div>',
    );
  }

  // Add a theme function for this form.
  $form['#theme'] = 'webform_form_' . $node->nid;

  // Set the encoding type (necessary for file uploads).
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['#base'] = 'webform_client_form';
  if (is_array($node->webformcomponents) && !empty($node->webformcomponents)) {

    // Prepare a new form array.
    $form['submitted'] = array(
      '#tree' => TRUE,
    );
    $form['details'] = array(
      '#tree' => true,
    );

    // Put the components into a tree structure.
    $component_tree = array();
    $page_count = 1;
    $page_num = 1;
    _webform_components_tree_build($node->webformcomponents, $component_tree, 0, $page_count);
    if (!$preview && empty($submission) || $enabled) {
      if ($page_count > 1) {
        $next_page = t('Next Page >');
        $prev_page = t('< Previous Page');
        if (isset($form_values)) {
          $page_num = $form_values['details']['page_num'];
          if ($form_values['op'] == $prev_page && $page_num > 1) {
            $page_num--;
          }
          else {
            if ($form_values['op'] == $next_page && $page_num < $page_count) {
              $page_num++;
            }
          }
        }
        else {
          $page_num = 1;
        }
        $form['#multistep'] = TRUE;
        if ($_POST['op'] != t('Submit')) {
          $form['#redirect'] = FALSE;
        }
        $form['details']['page_num'] = array(
          '#type' => 'hidden',
          '#value' => $page_num,
        );

        // Add the submit button(s).
        if ($page_num > 1) {
          $form['submitbutton_prev'] = array(
            '#type' => 'submit',
            '#value' => $prev_page,
            '#weight' => 1000,
          );
        }
        if ($page_num == $page_count) {
          $form['submitbutton'] = array(
            '#type' => 'submit',
            '#value' => t('Submit'),
            '#weight' => 1001,
          );
        }
        else {
          if ($page_num < $page_count) {
            $form['submitbutton_next'] = array(
              '#type' => 'submit',
              '#value' => $next_page,
              '#weight' => 1001,
            );
          }
        }
      }
      else {
        $page_num = 1;

        // Add the submit button.
        $form['submitbutton'] = array(
          '#type' => 'submit',
          '#value' => t('Submit'),
          '#weight' => 1000,
        );
      }
    }

    // Recursively add components to the form. Microweights keep thins in webform order.
    $microweight = 0.001;
    foreach ($component_tree['children'] as $cid => $component) {
      _webform_client_form_add_component($cid, $component, $form['submitted'], $form, $submission, $page_num, $enabled);
      $form['submitted'][$component['form_key']]['#weight'] += $microweight;
      $microweight += 0.001;
    }

    // Do not display the submit button if this is a preview or submission view.
    if (!$preview && empty($submission) || $enabled) {

      // Additional hidden elements.
      $form['details']['email_subject'] = array(
        '#type' => 'hidden',
        '#value' => $node->email_subject,
      );
      $form['details']['email_from_name'] = array(
        '#type' => 'hidden',
        '#value' => $node->email_from_name,
      );
      $form['details']['email_from_address'] = array(
        '#type' => 'hidden',
        '#value' => $node->email_from_address,
      );
      $form['details']['nid'] = array(
        '#type' => 'value',
        '#value' => $node->nid,
      );
      if (array_key_exists('sid', $submission)) {
        $form['details']['sid'] = array(
          '#type' => 'hidden',
          '#value' => $submission['sid'],
        );
      }
    }
  }
  return $form;
}