You are here

function webform_client_form in Webform 6.2

Same name and namespace in other branches
  1. 5.2 webform.module \webform_client_form()
  2. 5 webform.module \webform_client_form()
  3. 6.3 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

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

$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.

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 1423

Code

function webform_client_form(&$form_state, $node, $submission, $enabled = FALSE, $preview = FALSE) {
  module_load_include('inc', 'webform', 'webform_components');
  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(
        'attributes' => array(
          'class' => 'webform-submission-previous',
        ),
        'query' => $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(
        'attributes' => array(
          'class' => 'webform-submission-next',
        ),
        'query' => $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'] = array(
    'webform_form_' . $node->nid,
    'webform_form',
  );

  // 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';

  // 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 (empty($submission)) {
    $form['#action'] = url('node/' . $node->nid);
  }
  $form['#submit'][] = 'webform_client_form_submit';
  $form['#validate'][] = 'webform_client_form_validate';
  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');

        // Check if we're in a multipage form and determine the page number.
        if (!empty($form_state['storage']['submitted'])) {
          $page_num = $form_state['values']['details']['page_num'];
          $errors = form_get_errors();
          if (empty($errors)) {
            if ($form_state['values']['op'] == $prev_page && $page_num > 1) {
              $page_num--;
            }
            elseif ($form_state['values']['op'] == $next_page && $page_num < $page_count) {
              $page_num++;
            }
          }
        }
        else {
          $page_num = 1;
        }
        $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) {
      $component_value = isset($form_state['values']['submitted'][$component['form_key']]) ? $form_state['values']['submitted'][$component['form_key']] : NULL;
      _webform_client_form_add_component($cid, $component, $component_value, $form['submitted'], $form, $submission, $page_num, $enabled);
      if (isset($form['submitted'][$component['form_key']])) {
        $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;
}