You are here

function webform_client_form in Webform 6.3

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

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

$is_draft: Optional. Set to TRUE if displaying a draft.

$filter: Whether or not to filter the contents of descriptions and values when building the form. Values need to be unfiltered to be editable by Form Builder.

1 string reference to 'webform_client_form'
webform_forms in ./webform.module
Implements hook_forms().

File

./webform.module, line 1736

Code

function webform_client_form(&$form_state, $node, $submission, $is_draft = FALSE, $filter = TRUE) {
  global $user;
  module_load_include('inc', 'webform', 'includes/webform.components');
  module_load_include('inc', 'webform', 'includes/webform.submissions');
  $form['#process'] = array(
    'webform_client_form_includes',
  );

  // If in a multi-step form, a submission ID may be specified in form state.
  // Load this submission. This allows anonymous users to use auto-save.
  if (empty($submission) && !empty($form_state['values']['details']['sid'])) {
    $submission = webform_get_submission($node->nid, $form_state['values']['details']['sid']);
    $is_draft = $submission->is_draft;
  }

  // Bind arguments to $form to make them available in theming and form_alter.
  $form['#node'] = $node;
  $form['#submission'] = $submission;
  $form['#is_draft'] = $is_draft;
  $form['#filter'] = $filter;

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

  // Sometimes when displaying a webform as a teaser or block, a custom action
  // property is set to direct the user to the node page.
  if (!empty($node->webform['action'])) {
    $form['#action'] = $node->webform['action'];
  }
  $form['#submit'] = array(
    'webform_client_form_pages',
    'webform_client_form_submit',
  );
  $form['#validate'] = array(
    '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.
    if (!isset($form_state['storage']['component_tree'])) {
      $form_state['webform']['component_tree'] = array();
      $form_state['webform']['page_count'] = 1;
      $form_state['webform']['page_num'] = 1;
      _webform_components_tree_build($node->webform['components'], $form_state['webform']['component_tree'], 0, $form_state['webform']['page_count']);
    }
    else {
      $form_state['webform']['component_tree'] = $form_state['storage']['component_tree'];
      $form_state['webform']['page_count'] = $form_state['storage']['page_count'];
      $form_state['webform']['page_num'] = $form_state['storage']['page_num'];
    }

    // Shorten up our variable names.
    $component_tree = $form_state['webform']['component_tree'];
    $page_count = $form_state['webform']['page_count'];
    $page_num = $form_state['webform']['page_num'];
    if ($page_count > 1) {
      $next_page_labels = array();
      $prev_page_labels = array();
    }

    // Recursively add components to the form. The unfiltered version of the
    // form (typically used in Form Builder), includes all components.
    foreach ($component_tree['children'] as $cid => $component) {
      $component_value = isset($form_state['values']['submitted'][$cid]) ? $form_state['values']['submitted'][$cid] : NULL;
      if ($filter == FALSE || _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
        if ($component['type'] == 'pagebreak') {
          $next_page_labels[$component['page_num'] - 1] = !empty($component['extra']['next_page_label']) ? t($component['extra']['next_page_label']) : t('Next Page >');
          $prev_page_labels[$component['page_num']] = !empty($component['extra']['prev_page_label']) ? t($component['extra']['prev_page_label']) : t('< Previous Page');
        }
        _webform_client_form_add_component($node, $component, $component_value, $form['submitted'], $form, $form_state, $submission, 'form', $page_num, $filter);
      }
    }

    // These form details help managing data upon submission.
    $form['details']['nid'] = array(
      '#type' => 'value',
      '#value' => $node->nid,
    );
    $form['details']['sid'] = array(
      '#type' => 'hidden',
      '#value' => isset($submission->sid) ? $submission->sid : '',
    );
    $form['details']['uid'] = array(
      '#type' => 'value',
      '#value' => isset($submission->uid) ? $submission->uid : $user->uid,
    );
    $form['details']['page_num'] = array(
      '#type' => 'hidden',
      '#value' => $page_num,
    );
    $form['details']['page_count'] = array(
      '#type' => 'hidden',
      '#value' => $page_count,
    );
    $form['details']['finished'] = array(
      '#type' => 'hidden',
      '#value' => isset($submission->is_draft) ? !$submission->is_draft : 0,
    );

    // Add buttons for pages, drafts, and submissions.
    $form['actions'] = array(
      '#tree' => FALSE,
      '#weight' => 1000,
      '#prefix' => '<div id="edit-actions" class="form-actions form-wrapper">',
      '#suffix' => '</div>',
    );

    // Add the draft button.
    if ($node->webform['allow_draft'] && (empty($submission) || $submission->is_draft) && $user->uid != 0) {
      $form['actions']['draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save Draft'),
        '#weight' => -2,
        '#validate' => array(),
        '#attributes' => array(
          'formnovalidate' => 'formnovalidate',
        ),
      );
    }
    if ($page_count > 1) {

      // Add the submit button(s).
      if ($page_num > 1) {
        $form['actions']['previous'] = array(
          '#type' => 'submit',
          '#value' => $prev_page_labels[$page_num],
          '#weight' => 5,
          '#validate' => array(),
          '#attributes' => array(
            'formnovalidate' => 'formnovalidate',
          ),
        );
      }
      if ($page_num == $page_count) {
        $form['actions']['submit'] = array(
          '#type' => 'submit',
          '#value' => empty($node->webform['submit_text']) ? t('Submit') : t($node->webform['submit_text']),
          '#weight' => 10,
        );
      }
      elseif ($page_num < $page_count) {
        $form['actions']['next'] = array(
          '#type' => 'submit',
          '#value' => $next_page_labels[$page_num],
          '#weight' => 10,
        );
      }
    }
    else {

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