You are here

function webform_client_form in Webform 5.2

Same name and namespace in other branches
  1. 5 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: An object containing information about the form submission if we're displaying a result.

$enabled: 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. Note: The position of this parameter depends on all other parameters being specified when using drupal_get_form().

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 1228

Code

function webform_client_form($node, $submission, $enabled, $preview, $form_values = NULL) {
  include_once drupal_get_path('module', 'webform') . '/webform_components.inc';
  webform_load_components();
  if (isset($submission->sid)) {
    drupal_set_title(t('Submission #@sid', array(
      '@sid' => $submission->sid,
    )));
  }

  // 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['submission'] = array(
      '#type' => 'value',
      '#value' => $submission,
    );
    $form['navigation'] = array(
      '#prefix' => '<div class="webform-submission-navigation">',
      '#suffix' => '</div>',
    );
    $form['navigation']['previous'] = array(
      '#value' => $previous ? l(t('Previous submission'), 'node/' . $node->nid . '/submission/' . $previous . ($enabled ? '/edit' : ''), array(
        'class' => 'webform-submission-previous',
      ), $enabled ? 'destination=node/' . $node->nid . '/submission/' . $previous . '/edit' : NULL) : '<span class="webform-submission-previous">' . t('Previous submission') . '</span>',
    );
    $form['navigation']['next'] = array(
      '#value' => $next ? l(t('Next submission'), 'node/' . $node->nid . '/submission/' . $next . ($enabled ? '/edit' : ''), array(
        'class' => 'webform-submission-next',
      ), $enabled ? 'destination=node/' . $node->nid . '/submission/' . $next . '/edit' : NULL) : '<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']['form'] = array(
      '#value' => '<div>' . t('Form: !form', array(
        '!form' => l($node->title, 'node/' . $node->nid),
      )) . '</div>',
    );
    $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;

  // Add a css class for all client forms.
  $form['#attributes'] = array(
    'class' => 'webform-client-form',
  );

  // Set the encoding type (necessary for file uploads).
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['#base'] = 'webform_client_form';

  // Set the form action to the node ID in case this is being displayed on the
  // teaser, subsequent pages should be on the node page directly.
  if (arg(2) != 'submission') {
    $form['#action'] = url('node/' . $node->nid);
  }
  if (is_array($node->webform['components']) && !empty($node->webform['components'])) {

    // 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->webform['components'], $component_tree, 0, $page_count);
    if (!$preview && $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'] != (empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text'])) {
          $form['#redirect'] = FALSE;
        }
        $form['details']['page_num'] = array(
          '#type' => 'hidden',
          '#value' => $page_num,
        );
        $form['details']['page_count'] = array(
          '#type' => 'hidden',
          '#value' => $page_count,
        );

        // Add the submit button(s).
        if ($page_num > 1) {
          $form['previous'] = array(
            '#type' => 'submit',
            '#value' => $prev_page,
            '#weight' => 1000,
          );
        }
        if ($page_num == $page_count) {
          $form['submit'] = array(
            '#type' => 'submit',
            '#value' => empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text'],
            '#weight' => 1001,
          );
        }
        elseif ($page_num < $page_count) {
          $form['next'] = array(
            '#type' => 'submit',
            '#value' => $next_page,
            '#weight' => 1001,
          );
        }
      }
      else {
        $page_num = 1;

        // Add the submit button.
        $form['submit'] = array(
          '#type' => 'submit',
          '#value' => empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text'],
          '#weight' => 1000,
        );
      }
    }

    // Recursively add components to the form. Microweights keep things 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 && $enabled) {

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