You are here

function webform_client_form_submit in Webform 7.4

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

Submit handler for saving the form values and sending e-mails.

1 string reference to 'webform_client_form_submit'
webform_client_form in ./webform.module
Client form generation function.

File

./webform.module, line 3350
This module provides a simple way to create forms and questionnaires.

Code

function webform_client_form_submit($form, &$form_state) {
  module_load_include('inc', 'webform', 'includes/webform.submissions');
  module_load_include('inc', 'webform', 'includes/webform.components');
  global $user;
  if (empty($form_state['save_draft']) && empty($form_state['webform_completed'])) {
    return;
  }
  $node = $form['#node'];
  $sid = $form_state['values']['details']['sid'] ? (int) $form_state['values']['details']['sid'] : NULL;

  // Check if user is submitting as a draft.
  $is_draft = (int) (!empty($form_state['save_draft']));

  // To maintain time and user information, load the existing submission.
  // If a draft is deleted while a user is working on completing it, $sid will
  // exist, but webform_get_submission() will not find the draft. So, make a new
  // submission.
  if ($sid && ($submission = webform_get_submission($node->webform['nid'], $sid))) {

    // Store original data on object for use in update hook.
    $submission->original = clone $submission;

    // Merge with new submission data. The + operator maintains numeric keys.
    // This maintains existing data with just-submitted data when a user resumes
    // a submission previously saved as a draft. Remove any existing data on
    // this and previous pages. If components are hidden, they may be in the
    // $submission->data but absent entirely from $new_data.
    $page_map = webform_get_conditional_sorter($node)
      ->getPageMap();
    for ($page_nr = 1; $page_nr <= $form_state['webform']['page_num']; $page_nr++) {
      $submission->data = array_diff_key($submission->data, $page_map[$page_nr]);
    }
    $submission->data = webform_submission_data($node, $form_state['values']['submitted']) + $submission->data;
  }
  else {

    // Create a new submission object.
    $submission = webform_submission_create($node, $user, $form_state);

    // Since this is a new submission, a new sid is needed.
    $sid = NULL;
  }

  // Save draft state, and for drafts, save the current page (if clicking next)
  // or the previous page (if not) as the last valid page.
  $submission->is_draft = $is_draft;
  $submission->highest_valid_page = 0;
  if ($is_draft) {
    $submission->highest_valid_page = end($form_state['clicked_button']['#parents']) == 'next' && $form_state['values']['op'] != '__AUTOSAVE__' ? $form_state['webform']['page_num'] : $form_state['webform']['page_num'] - 1;
  }

  // If there is no data to be saved (such as on a multipage form with no fields
  // on the first page), process no further. Submissions with no data cannot
  // be loaded from the database as efficiently, so we don't save them at all.
  if (empty($submission->data)) {
    return;
  }

  // Save the submission to the database.
  if (!$sid) {

    // No sid was found thus insert it in the dataabase.
    $form_state['values']['details']['sid'] = $sid = webform_submission_insert($node, $submission);
    $form_state['values']['details']['is_new'] = TRUE;

    // Save the new details in storage. When ajax calls for file upload/remove,
    // $form_state['values']['details'] is missing. This allows the proper
    // submission to be retrieved in webform_client_form. See #2562703.
    $form_state['storage']['details'] = $form_state['values']['details'];

    // Set a cookie including the server's submission time. The cookie expires
    // in the length of the interval plus a day to compensate for timezones.
    $tracking_mode = webform_variable_get('webform_tracking_mode');
    if ($tracking_mode === 'cookie' || $tracking_mode === 'strict') {
      $cookie_name = 'webform-' . $node->nid;
      $time = REQUEST_TIME;
      $params = session_get_cookie_params();
      setcookie($cookie_name . '[' . $time . ']', $time, $time + $node->webform['submit_interval'] + 86400, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
    }

    // Save session information about this submission for anonymous users,
    // allowing them to access or edit their submissions.
    if (!$user->uid && user_access('access own webform submissions')) {
      $_SESSION['webform_submission'][$sid] = $node->nid;
    }
  }
  else {

    // Sid was found thus update the existing sid in the database.
    webform_submission_update($node, $submission);
    $form_state['values']['details']['is_new'] = FALSE;
  }

  // Check if this form is sending an email.
  if (!$is_draft && !$form_state['values']['details']['finished']) {
    drupal_static_reset('webform_get_submission');
    $submission = webform_get_submission($node->webform['nid'], $sid);
    webform_submission_send_mail($node, $submission);
  }

  // Strip out empty tags added by WYSIWYG editors if needed.
  $message = strlen(trim(strip_tags($node->webform['confirmation']))) > 0;

  // Check confirmation and redirect_url fields.
  $redirect = NULL;
  $redirect_url = trim($node->webform['redirect_url']);
  if (isset($form['actions']['draft']['#value']) && $form_state['values']['op'] == $form['actions']['draft']['#value']) {
    $message = t('Submission saved. You may return to this form later and it will restore the current values.');
  }
  elseif ($is_draft) {

    // No redirect needed. No confirmation message needed.
    $message = FALSE;
  }
  elseif (!empty($form_state['values']['details']['finished'])) {
    $message = t('Submission updated.');
    $redirect = "node/{$node->nid}/submission/{$sid}";
  }
  elseif (!empty($node->webform['confirmation_block'])) {
    $message = FALSE;

    // Webform was submitted in a block and the confirmation message is to be
    // displayed in the block.
    $_SESSION['webform_confirmation'][$node->nid] = array(
      'sid' => $sid,
      'confirmation_page' => $redirect_url == '<confirmation>',
    );
    drupal_page_is_cacheable(FALSE);
  }
  elseif ($redirect_url == '<none>') {

    // No redirect needed. Show a confirmatin message if there is one.
  }
  elseif ($redirect_url == '<confirmation>') {

    // No confirmation message needed because it will be shown on the
    // confirmation page.
    $message = FALSE;
    $query = array(
      'sid' => $sid,
    );
    if ((int) $user->uid === 0) {
      $query['token'] = webform_get_submission_access_token($submission);
    }
    $redirect = array(
      'node/' . $node->nid . '/done',
      array(
        'query' => $query,
      ),
    );
  }
  else {

    // Clean up the redirect URL, filter it for tokens and detect external
    // domains. If the redirect is to an external URL, then don't show the
    // confirmation message.
    $redirect = webform_replace_url_tokens($redirect_url, $node, $submission);
    if ($redirect[1]['#webform_external']) {
      $message = FALSE;
    }
  }

  // Show a message if manually set.
  if (is_string($message)) {
    drupal_set_message($message);
  }
  elseif ($message) {
    drupal_set_message(webform_replace_tokens($node->webform['confirmation'], $node, $submission, NULL, $node->webform['confirmation_format']));
  }
  $form_state['redirect'] = $redirect;
}