Commit a5dba069 authored by Dries's avatar Dries

- #440876 by Dave Reid, sun: reuse comment.module's anonymous cookie information for contact forms.

parent 0e98ff20
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
Drupal.behaviors.comment = { Drupal.behaviors.comment = {
attach: function (context, settings) { attach: function (context, settings) {
$.each(['name', 'homepage', 'mail'], function () { $.each(['name', 'homepage', 'mail'], function () {
var cookie = Drupal.comment.getCookie('comment_info_' + this); var cookie = $.cookie('Drupal.visitor.' + this);
if (cookie) { if (cookie) {
$('#comment-form input[name=' + this + ']', context).once('comment').val(cookie); $('#comment-form input[name=' + this + ']', context).once('comment').val(cookie);
} }
...@@ -12,25 +12,4 @@ Drupal.behaviors.comment = { ...@@ -12,25 +12,4 @@ Drupal.behaviors.comment = {
} }
}; };
Drupal.comment = {};
Drupal.comment.getCookie = function (name) {
var search = name + '=';
var returnValue = '';
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset != -1) {
offset += search.length;
var end = document.cookie.indexOf(';', offset);
if (end == -1) {
end = document.cookie.length;
}
returnValue = decodeURIComponent(document.cookie.substring(offset, end).replace(/\+/g, '%20'));
}
}
return returnValue;
};
})(jQuery); })(jQuery);
...@@ -1637,7 +1637,8 @@ function comment_form($form, &$form_state, $comment) { ...@@ -1637,7 +1637,8 @@ function comment_form($form, &$form_state, $comment) {
$node = node_load($comment->nid); $node = node_load($comment->nid);
if (!$user->uid && variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { if (!$user->uid && variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
$form_state['#attached']['js'][] = drupal_get_path('module', 'comment') . '/comment.js'; $form['#attached']['library'][] = array('system', 'cookie');
$form['#attached']['js'][] = drupal_get_path('module', 'comment') . '/comment.js';
} }
$comment = (array) $comment; $comment = (array) $comment;
...@@ -1957,15 +1958,6 @@ function comment_form_validate($form, &$form_state) { ...@@ -1957,15 +1958,6 @@ function comment_form_validate($form, &$form_state) {
$comment = (object) $form_state['values']; $comment = (object) $form_state['values'];
field_attach_form_validate('comment', $comment, $form, $form_state); field_attach_form_validate('comment', $comment, $form, $form_state);
if ($user->uid === 0) {
foreach (array('name', 'homepage', 'mail') as $field) {
// Set cookie for 365 days.
if (isset($form_state['values'][$field])) {
setcookie('comment_info_' . $field, $form_state['values'][$field], REQUEST_TIME + 31536000, '/');
}
}
}
if (isset($form_state['values']['date'])) { if (isset($form_state['values']['date'])) {
if (strtotime($form_state['values']['date']) === FALSE) { if (strtotime($form_state['values']['date']) === FALSE) {
form_set_error('date', t('You have to specify a valid date.')); form_set_error('date', t('You have to specify a valid date.'));
...@@ -2068,6 +2060,11 @@ function comment_form_submit($form, &$form_state) { ...@@ -2068,6 +2060,11 @@ function comment_form_submit($form, &$form_state) {
$node = node_load($form_state['values']['nid']); $node = node_load($form_state['values']['nid']);
$comment = comment_form_submit_build_comment($form, $form_state); $comment = comment_form_submit_build_comment($form, $form_state);
if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) { if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {
// Save the anonymous user information to a cookie for reuse.
if (!$comment->uid) {
user_cookie_save($form_state['values']);
}
comment_save($comment); comment_save($comment);
// Explain the approval queue if necessary. // Explain the approval queue if necessary.
if ($comment->status == COMMENT_NOT_PUBLISHED) { if ($comment->status == COMMENT_NOT_PUBLISHED) {
...@@ -2425,4 +2422,3 @@ function comment_filter_format_delete($format, $fallback) { ...@@ -2425,4 +2422,3 @@ function comment_filter_format_delete($format, $fallback) {
->condition('format', $format->format) ->condition('format', $format->format)
->execute(); ->execute();
} }
// $Id$
(function ($) {
Drupal.behaviors.contact = {
attach: function(context) {
$.each(['name', 'mail'], function () {
var cookie = $.cookie('Drupal.user.' + this);
if (cookie) {
$('#contact-site-form input[name=' + this + ']', context).once('comment').val(cookie);
}
});
}
};
})(jQuery);
...@@ -52,6 +52,11 @@ function contact_site_form($form, &$form_state) { ...@@ -52,6 +52,11 @@ function contact_site_form($form, &$form_state) {
} }
} }
if (!$user->uid) {
$form['#attached']['library'][] = array('system', 'cookie');
$form['#attached']['js'][] = drupal_get_path('module', 'contact') . '/contact.js';
}
$form['#token'] = $user->uid ? $user->name . $user->mail : ''; $form['#token'] = $user->uid ? $user->name . $user->mail : '';
$form['name'] = array( $form['name'] = array(
'#type' => 'textfield', '#type' => 'textfield',
...@@ -117,10 +122,15 @@ function contact_site_form_validate($form, &$form_state) { ...@@ -117,10 +122,15 @@ function contact_site_form_validate($form, &$form_state) {
* Form submission handler for contact_site_form(). * Form submission handler for contact_site_form().
*/ */
function contact_site_form_submit($form, &$form_state) { function contact_site_form_submit($form, &$form_state) {
global $language; global $user, $language;
$values = $form_state['values']; $values = $form_state['values'];
// Save the anonymous user information to a cookie for reuse.
if (!$user->uid) {
user_cookie_save($values);
}
// E-mail address of the sender: as the form field is a text field, // E-mail address of the sender: as the form field is a text field,
// all instances of \r and \n have been automatically stripped from it. // all instances of \r and \n have been automatically stripped from it.
$from = $values['mail']; $from = $values['mail'];
......
...@@ -3226,3 +3226,20 @@ function user_login_destination() { ...@@ -3226,3 +3226,20 @@ function user_login_destination() {
return $destination; return $destination;
} }
/**
* Saves visitor information as a cookie so it can be reused.
*
* @param $values
* An array of submitted form values with identifying information about the
* current user, typically $form_state['values'] from a submit handler.
* @param $fields
* An array of key values from $values to be saved into a cookie.
*/
function user_cookie_save(array $values, array $fields = array('name', 'mail', 'homepage')) {
foreach ($fields as $field) {
if (isset($values[$field])) {
// Set cookie for 365 days.
setrawcookie('Drupal.visitor.' . $field, rawurlencode($values[$field]), REQUEST_TIME + 31536000, '/');
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment