You are here

function webform_client_form_submit in Webform 6.3

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.2 webform.module \webform_client_form_submit()
  4. 7.4 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. 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 2329

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']));
  if (!$sid) {

    // Create a submission object.
    $submission = (object) array(
      'nid' => $node->nid,
      'uid' => $user->uid,
      'submitted' => time(),
      'remote_addr' => ip_address(),
      'is_draft' => $is_draft,
      'data' => webform_submission_data($node, $form_state['values']['submitted']),
    );
  }
  else {

    // To maintain time and user information, load the existing submission.
    $submission = webform_get_submission($node->webform['nid'], $sid);
    $submission->is_draft = $is_draft;

    // 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.
    $new_data = webform_submission_data($node, $form_state['values']['submitted']);
    $submission->data = $new_data + $submission->data;
  }

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

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

    // 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'][$form_state['values']['details']['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']) {
    $submission = webform_get_submission($node->webform['nid'], $sid, TRUE);
    webform_submission_send_mail($node, $submission);
  }

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

  // Clean up the redirect URL and filter it for webform tokens.
  $redirect_url = trim($node->webform['redirect_url']);
  $redirect_url = _webform_filter_values($redirect_url, $node, $submission, NULL, FALSE, TRUE);

  // Remove the domain name from the redirect.
  $redirect_url = preg_replace('/^' . preg_quote($GLOBALS['base_url'], '/') . '\\//', '', $redirect_url);

  // Check confirmation and redirect_url fields.
  $message = NULL;
  $redirect = NULL;
  $external_url = FALSE;
  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) {
    $redirect = NULL;
  }
  elseif (!empty($form_state['values']['details']['finished'])) {
    $message = t('Submission updated.');
  }
  elseif ($redirect_url == '<none>') {
    $redirect = NULL;
  }
  elseif ($redirect_url == '<confirmation>') {
    $redirect = array(
      'node/' . $node->nid . '/done',
      'sid=' . $sid,
    );
  }
  elseif (valid_url($redirect_url, TRUE)) {
    $redirect = $redirect_url;
    $external_url = TRUE;
  }
  elseif ($redirect_url && strpos($redirect_url, 'http') !== 0) {
    $parts = parse_url($redirect_url);
    $query = $parts['query'] ? $parts['query'] . '&sid=' . $sid : 'sid=' . $sid;
    $redirect = array(
      $parts['path'],
      $query,
      $parts['fragment'],
    );
  }

  // Show a message if manually set.
  if (isset($message)) {
    drupal_set_message($message);
  }
  elseif (!$is_draft && !$external_url && (!empty($redirect_url) && $redirect_url != '<confirmation>') && !empty($confirmation)) {
    drupal_set_message(check_markup($confirmation, $node->webform['confirmation_format'], FALSE));
  }
  $form_state['redirect'] = $redirect;
}