function webform_client_form_submit in Webform 6.2
Same name and namespace in other branches
- 5.2 webform.module \webform_client_form_submit()
- 5 webform.module \webform_client_form_submit()
- 6.3 webform.module \webform_client_form_submit()
- 7.4 webform.module \webform_client_form_submit()
- 7.3 webform.module \webform_client_form_submit()
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 1697
Code
function webform_client_form_submit($form, &$form_state) {
global $user, $base_url;
module_load_include('inc', 'webform', 'webform_submissions');
webform_load_components();
$node = node_load(array(
'nid' => $form_state['values']['details']['nid'],
));
// Check for a multi-page form that is not yet complete.
$submit_op = empty($node->webform['submit_text']) ? t('Submit') : $node->webform['submit_text'];
if ($form_state['values']['op'] != $submit_op) {
// Checkboxes need post-processing to maintain their values.
_webform_client_form_submit_process($node, $form_state['values']['submitted'], array(
'select',
'grid',
));
// 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 (is_array($form_state['storage']['submitted'])) {
foreach ($form_state['storage']['submitted'] as $key => $val) {
$form_state['values']['submitted'][$key] = $val;
}
}
// Rebuild the form, allowing the rebuild to determine the next page.
$form_state['rebuild'] = TRUE;
return;
}
if (isset($form_state['storage']['submitted'])) {
// Merge any stored submission data for multistep forms.
$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 form state storage now that we're done with the pages.
unset($form_state['rebuild']);
unset($form_state['storage']);
// Remove the variable so it doesn't show up in the additional processing.
unset($original_values);
}
// 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']);
// Convert additional email addresses into actual values.
foreach ($node->webform['additional_emails'] as $cid => $value) {
if (is_array($form_state['values']['submitted'][$cid])) {
$node->webform['additional_emails'][$cid] = array();
foreach ($form_state['values']['submitted'][$cid] as $submitted_value) {
if ($submitted_value) {
$node->webform['additional_emails'][$cid][] = $submitted_value;
}
}
}
else {
$node->webform['additional_emails'][$cid] = $form_state['values']['submitted'][$cid];
}
if (empty($node->webform['additional_emails'][$cid])) {
unset($node->webform['additional_emails'][$cid]);
}
}
// Perform additional submit processing.
if (trim($node->webform['additional_submit'])) {
// Support for Drupal 5 validation code.
$form_values =& $form_state['values'];
// We use eval here (rather than drupal_eval) because the user needs access to local variables.
eval('?>' . $node->webform['additional_submit']);
}
// Save the submission to the database.
if (empty($form_state['values']['details']['sid'])) {
// No sid was found thus insert it in the datatabase.
$form_state['values']['details']['sid'] = webform_submission_insert($node, $form_state['values']['submitted']);
$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();
setcookie($cookie_name . '[' . $time . ']', $time, $time + $node->webform['submit_interval'] + 86400);
}
}
else {
// Sid was found thus update the existing sid in the datatbase.
webform_submission_update($node, $form_state['values']['details']['sid'], $form_state['values']['submitted']);
$form_state['values']['details']['is_new'] = FALSE;
}
$sid = $form_state['values']['details']['sid'];
// Check if this form is sending an email.
if ((!empty($node->webform['email']) || !empty($node->webform['additional_emails'])) && $form_state['values']['details']['is_new']) {
// Set values for the name, address, and subject for the email.
$email_from_name = $node->webform['email_from_name'];
$email_from_address = $node->webform['email_from_address'];
$email_subject = $node->webform['email_subject'];
foreach (array(
'from_name',
'from_address',
'subject',
) as $field) {
if ($node->webform['email_' . $field] == 'default') {
${'email_' . $field} = _webform_filter_values(webform_variable_get('webform_default_' . $field), $node, $form_state['values']['submitted'], FALSE, TRUE);
}
elseif (is_numeric($node->webform['email_' . $field])) {
if (is_array($form_state['values']['submitted'][${'email_' . $field}])) {
$values = array();
foreach ($form_state['values']['submitted'][${'email_' . $field}] as $key => $value) {
$values[] = _webform_filter_values($value, $node, $form_state['values']['submitted'], FALSE, TRUE);
}
${'email_' . $field} = implode(', ', $values);
}
else {
${'email_' . $field} = _webform_filter_values($form_state['values']['submitted'][${'email_' . $field}], $node, $form_state['values']['submitted'], FALSE, TRUE);
}
}
else {
${'email_' . $field} = _webform_filter_values(${'email_' . $field}, $node, $form_state['values']['submitted'], FALSE, TRUE);
}
}
// Create a themed message for mailing.
// Check for a node-specific message:
$emails = $node->webform['additional_emails'];
if ($node->webform['email']) {
$emails['default'] = $node->webform['email'];
}
$messages = array();
$headers = array();
$froms = array();
$subjects = array();
foreach ($emails as $cid => $email) {
$messages[$cid] = theme(array(
'webform_mail_' . $node->nid,
'webform_mail',
'webform_mail_message',
), $form_state['values'], $node, $sid, $cid);
$headers[$cid] = theme(array(
'webform_mail_headers_' . $node->nid,
'webform_mail_headers',
), $form_state['values'], $node, $sid, $cid);
// Assemble the FROM string.
if (isset($headers[$cid]['From'])) {
$froms[$cid] = $headers[$cid]['From'];
unset($headers[$cid]['From']);
}
elseif (drupal_strlen($email_from_name) > 0) {
$froms[$cid] = '"' . mime_header_encode($email_from_name) . '" <' . $email_from_address . '>';
}
else {
$froms[$cid] = $email_from_address;
}
// Update the subject if set in the themed headers.
if (isset($headers[$cid]['Subject'])) {
$subjects[$cid] = $headers[$cid]['Subject'];
unset($headers[$cid]['Subject']);
}
else {
$subjects[$cid] = $email_subject;
}
// Update the to e-mail if set in the themed headers.
if (isset($headers[$cid]['To'])) {
$emails[$cid] = $headers[$cid]['To'];
unset($headers[$cid]['To']);
}
}
// Verify that this submission is not attempting to send any spam hacks.
if (_webform_submission_spam_check($emails['default'], $subjects['default'], $froms['default'], $headers['default'])) {
watchdog('webform', 'Possible spam attempt from @remote_addr' . "<br />\n" . nl2br(htmlentities($messages['default'])), array(
'@remote_add' => ip_address(),
));
drupal_set_message(t('Illegal information. Data not submitted.'), 'error');
return FALSE;
}
// Mail the webform results.
foreach ($emails as $cid => $address) {
// In the case of checkboxes or multiple select, multiple e-mails may need
// to be sent out.
if (is_array($address)) {
foreach ($address as $single_address) {
drupal_mail('webform', 'submission', $single_address, user_preferred_language($user), array(
'message' => $messages[$cid],
'subject' => $subjects[$cid],
'headers' => $headers[$cid],
), $froms[$cid]);
// Debugging output for email.
if (variable_get('webform_debug', 0) >= 2) {
drupal_set_message('E-mail Headers: <pre>' . htmlentities(print_r($headers[$cid], TRUE)) . '</pre>To: ' . $single_address . '<br />From: ' . htmlentities($froms[$cid]) . '<br />Subject: ' . $subjects[$cid] . '<br />E-mail Body: <pre>' . $messages[$cid] . '</pre>');
}
}
}
else {
drupal_mail('webform', 'submission', $address, user_preferred_language($user), array(
'message' => $messages[$cid],
'subject' => $subjects[$cid],
'headers' => $headers[$cid],
), $froms[$cid]);
// Debugging output for email.
if (variable_get('webform_debug', 0) >= 2) {
drupal_set_message('E-mail Headers: <pre>' . htmlentities(print_r($headers[$cid], TRUE)) . '</pre>To: ' . $address . '<br />From: ' . htmlentities($froms[$cid]) . '<br />Subject: ' . $subjects[$cid] . '<br />E-mail Body: <pre>' . $messages[$cid] . '</pre>');
}
}
}
}
// More debugging output.
if (variable_get('webform_debug', 0) >= 2) {
drupal_set_message('$form_state is: <pre>' . htmlentities(print_r($form_state, TRUE)) . '</pre>');
drupal_set_message('$_SERVER is: <pre>' . htmlentities(print_r($_SERVER, TRUE)) . '</pre>');
drupal_set_message('$_POST is: <pre>' . htmlentities(print_r($_POST, TRUE)) . '</pre>');
}
// Log to watchdog if normal debug is on.
if (variable_get('webform_debug', 0) >= 1) {
watchdog('webform', 'Submission posted to %title. <a href="!url">Results</a>. !details', array(
'%title' => $node->title,
'!url' => url('node/' . $node->nid . '/submission/' . $sid),
'!results' => "<br />\n<pre>" . htmlentities(print_r($form_state['values'], TRUE)) . '</pre>',
));
}
// Check confirmation field to see if redirect should be to another node or a message.
if (isset($form_state['values']['submission'])) {
drupal_set_message(t('Submission updated.'));
$redirect = NULL;
}
elseif (valid_url(trim($node->webform['confirmation']), TRUE)) {
$redirect = trim($node->webform['confirmation']);
}
elseif (preg_match('/^internal:/', trim(strip_tags($node->webform['confirmation'])))) {
$path = preg_replace('/^internal:/', '', trim(strip_tags($node->webform['confirmation'])));
$redirect = array(
trim($path),
'sid=' . $sid,
);
}
elseif (preg_match('/^message:/', $node->webform['confirmation'])) {
$message = preg_replace('/^message:/', '', $node->webform['confirmation']);
drupal_set_message($message);
$redirect = NULL;
}
else {
$redirect = array(
'node/' . $node->nid . '/done',
'sid=' . $sid,
);
}
$form_state['redirect'] = $redirect;
}