You are here

function webform_client_form_pages in Webform 6.3

Same name and namespace in other branches
  1. 7.4 webform.module \webform_client_form_pages()
  2. 7.3 webform.module \webform_client_form_pages()

Handle the processing of pages and conditional logic.

1 string reference to 'webform_client_form_pages'
webform_client_form in ./webform.module
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.

File

./webform.module, line 2210

Code

function webform_client_form_pages($form, &$form_state) {
  $node = node_load($form_state['values']['details']['nid']);

  // Multistep forms may not have any components on the first page.
  if (!isset($form_state['values']['submitted'])) {
    $form_state['values']['submitted'] = array();
  }

  // Move special settings to storage.
  if (isset($form_state['webform']['component_tree'])) {
    $form_state['storage']['component_tree'] = $form_state['webform']['component_tree'];
    $form_state['storage']['page_count'] = $form_state['webform']['page_count'];
    $form_state['storage']['page_num'] = $form_state['webform']['page_num'];
  }

  // Perform post processing by components.
  _webform_client_form_submit_process($node, $form_state['values']['submitted']);

  // Flatten trees within the submission.
  $form_state['values']['submitted_tree'] = $form_state['values']['submitted'];
  $form_state['values']['submitted'] = _webform_client_form_submit_flatten($node, $form_state['values']['submitted']);

  // Assume the form is completed unless the page logic says otherwise.
  $form_state['webform_completed'] = TRUE;

  // Check for a multi-page form that is not yet complete.
  $submit_op = !empty($form['actions']['submit']['#value']) ? $form['actions']['submit']['#value'] : t('Submit');
  $draft_op = !empty($form['actions']['draft']['#value']) ? $form['actions']['draft']['#value'] : t('Save Draft');
  if (!in_array($form_state['values']['op'], array(
    $submit_op,
    $draft_op,
  ))) {

    // Store values from the current page in the form state storage.
    if (is_array($form_state['values']['submitted'])) {
      foreach ($form_state['values']['submitted'] as $key => $val) {
        $form_state['storage']['submitted'][$key] = $val;
      }
    }

    // Update form state values with those from storage.
    if (isset($form_state['storage']['submitted'])) {
      foreach ($form_state['storage']['submitted'] as $key => $val) {
        $form_state['values']['submitted'][$key] = $val;
      }
    }

    // Set the page number.
    if (!isset($form_state['storage']['page_num'])) {
      $form_state['storage']['page_num'] = 1;
    }
    if (end($form_state['clicked_button']['#parents']) == 'next') {
      $direction = 1;
    }
    else {
      $direction = 0;
    }

    // If the next page has no components that need to be displayed, skip it.
    if (isset($direction)) {
      $components = $direction ? $node->webform['components'] : array_reverse($node->webform['components'], TRUE);
      $last_component = end($node->webform['components']);
      foreach ($components as $component) {
        if ($component['type'] == 'pagebreak' && ($direction == 1 && $component['page_num'] > $form_state['storage']['page_num'] || $direction == 0 && $component['page_num'] <= $form_state['storage']['page_num'])) {
          $previous_pagebreak = $component;
          continue;
        }
        if (isset($previous_pagebreak)) {
          $page_num = $previous_pagebreak['page_num'] + $direction - 1;

          // If we've found an component on this page, advance to that page.
          if ($component['page_num'] == $page_num && _webform_client_form_rule_check($node, $component, $page_num, $form_state)) {
            $form_state['storage']['page_num'] = $page_num;
            break;
          }
          elseif ($direction && $component['cid'] == $last_component['cid']) {
            $form_state['storage']['page_num'] = $page_num + 1;
          }
        }
      }
    }

    // The form is done if the page number is greater than the page count.
    $form_state['webform_completed'] = $form_state['storage']['page_num'] > $form_state['storage']['page_count'];
  }

  // Merge any stored submission data for multistep forms.
  if (isset($form_state['storage']['submitted'])) {
    $original_values = is_array($form_state['values']['submitted']) ? $form_state['values']['submitted'] : array();
    unset($form_state['values']['submitted']);
    foreach ($form_state['storage']['submitted'] as $key => $val) {
      $form_state['values']['submitted'][$key] = $val;
    }
    foreach ($original_values as $key => $val) {
      $form_state['values']['submitted'][$key] = $val;
    }

    // Remove the variable so it doesn't show up in the additional processing.
    unset($original_values);
  }

  // Inform the submit handlers that a draft will be saved.
  $form_state['save_draft'] = $form_state['values']['op'] == $draft_op || $node->webform['auto_save'] && !$form_state['webform_completed'];

  // Determine what we need to do on the next page.
  if (!empty($form_state['save_draft']) || !$form_state['webform_completed']) {

    // Rebuild the form and display the current (on drafts) or next page.
    $form_state['rebuild'] = TRUE;
  }
  else {

    // Remove the form state storage now that we're done with the pages.
    $form_state['rebuild'] = FALSE;
    unset($form_state['storage']);
  }
}