Commit 8caf4da7 authored by Gábor Hojtsy's avatar Gábor Hojtsy

#82499 by Jose A Reyero and a little bit from myself: send emails localized in...

#82499 by Jose A Reyero and a little bit from myself: send emails localized in the language needed in specific situations, and centralize mail composing operations with hook_mail()
parent 7afaf32e
......@@ -1030,9 +1030,17 @@ function language_list($field = 'language', $reset = FALSE) {
// Init language list
if (!isset($languages)) {
$result = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC');
while ($row = db_fetch_object($result)) {
$languages['language'][$row->language] = $row;
if (variable_get('language_count', 1) > 1) {
$result = db_query('SELECT * FROM {languages} ORDER BY weight ASC, name ASC');
while ($row = db_fetch_object($result)) {
$languages['language'][$row->language] = $row;
}
}
else {
// One language only, the locale tables might not even
// be in place, so use the default language only.
$default = language_default();
$languages['language'][$default->language] = $default;
}
}
......
This diff is collapsed.
......@@ -360,48 +360,37 @@ function contact_mail_user(&$form_state, $recipient) {
* Process the personal contact page form submission.
*/
function contact_mail_user_submit($form, &$form_state) {
global $user;
global $user, $language;
$account = user_load(array('uid' => arg(1), 'status' => 1));
// Compose the body:
$message[] = "$account->name,";
$message[] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE)), '!form-url' => url($_GET['q'], array('absolute' => TRUE)), '!site' => variable_get('site_name', 'Drupal')));
$message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE))));
$message[] = t('Message:');
$message[] = $form_state['values']['message'];
// Prepare all fields:
// Send from the current user to the requested user.
$to = $account->mail;
$from = $user->mail;
// Format the subject:
$subject = '['. variable_get('site_name', 'Drupal') .'] '. $form_state['values']['subject'];
// Prepare the body:
$body = drupal_wrap_mail(implode("\n\n", $message));
// Save both users and all form values for email composition.
$values = $form_state['values'];
$values['account'] = $account;
$values['user'] = $user;
// Send the e-mail:
drupal_mail('contact-user-mail', $to, $subject, $body, $from);
// Send the e-mail in the requested user language.
drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
// Send a copy if requested:
// Send a copy if requested, using current page language.
if ($form_state['values']['copy']) {
drupal_mail('contact-user-copy', $from, $subject, $body, $from);
drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
}
// Log the operation:
flood_register_event('contact');
watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
// Set a status message:
drupal_set_message(t('The message has been sent.'));
// Jump to the user's profile page:
// Back to the requested users profile page.
$form_state['redirect'] = "user/$account->uid";
return;
}
/**
* Site-wide contact page
* Site-wide contact page.
*/
function contact_site_page() {
global $user;
......@@ -504,45 +493,68 @@ function contact_mail_page_validate($form, &$form_state) {
* Process the site-wide contact page form submission.
*/
function contact_mail_page_submit($form, &$form_state) {
global $language;
$values = $form_state['values'];
// 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.
$from = $form_state['values']['mail'];
// Compose the body:
$message[] = t("!name sent a message using the contact form at !form.", array('!name' => $form_state['values']['name'], '!form' => url($_GET['q'], array('absolute' => TRUE))));
$message[] = $form_state['values']['message'];
// Load the category information:
$contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $form_state['values']['cid']));
// Format the category:
$subject = t('[!category] !subject', array('!category' => $contact->category, '!subject' => $form_state['values']['subject']));
// Prepare the body:
$body = drupal_wrap_mail(implode("\n\n", $message));
// Send the e-mail to the recipients:
drupal_mail('contact-page-mail', $contact->recipients, $subject, $body, $from);
// If the user requests it, send a copy.
if ($form_state['values']['copy']) {
drupal_mail('contact-page-copy', $from, $subject, $body, $from);
$from = $values['mail'];
// Load category properties and save form values for email composition.
$contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $values['cid']));
$values['contact'] = $contact;
// Send the e-mail to the recipients using the site default language.
drupal_mail('contact', 'page_mail', $contact->recipients, language_default(), $values, $from);
// If the user requests it, send a copy using the current language.
if ($values['copy']) {
drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
}
// Send an auto-reply if necessary:
// Send an auto-reply if necessary using the current language.
if ($contact->reply) {
drupal_mail('contact-page-autoreply', $from, $subject, drupal_wrap_mail($contact->reply), $contact->recipients);
drupal_mail('contact', 'page_autoreply', $from, $language, $values, $contact->recipients);
}
// Log the operation:
flood_register_event('contact');
watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $form_state['values']['name'] ." [$from]", '%category' => $contact->category));
// Update user:
watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $values['name'] ." [$from]", '%category' => $contact->category));
drupal_set_message(t('Your message has been sent.'));
// Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
// Jump to home page rather than back to contact page to avoid
// contradictory messages if flood control has been activated.
$form_state['redirect'] = '';
return;
}
/**
* Implementation of hook_mail().
*/
function contact_mail($key, &$message, $params) {
$language = $message['language'];
switch ($key) {
case 'page_mail':
case 'page_copy':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array('!category' => $contact->category, '!subject' => $params['subject']), $language->language);
$message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
$message['body'][] = $params['message'];
break;
case 'page_autoreply':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array('!category' => $contact->category, '!subject' => $params['subject']), $language->language);
$message['body'][] = $contact->reply;
break;
case 'user_mail':
case 'user_copy':
$user = $params['user'];
$account = $params['account'];
$message['subject'] .= '['. variable_get('site_name', 'Drupal') .'] '. $params['subject'];
$message['body'][] = "$account->name,";
$message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
$message['body'][] = t('Message:', NULL, $language->language);
$message['body'][] = $params['message'];
break;
}
}
......@@ -190,25 +190,33 @@ function locale_locale($op = 'groups') {
* Implementation of hook_user().
*/
function locale_user($type, $edit, &$user, $category = NULL) {
if ($type == 'form' && $category == 'account' && variable_get('language_count', 1) > 1 && variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) == LANGUAGE_NEGOTIATION_PATH) {
global $language;
// If we have more then one language and either creating a user on the
// admin interface or edit the user, show the language selector.
if (variable_get('language_count', 1) > 1 && ($type == 'register' && user_access('administer users') || $type == 'form' && $category == 'account' )) {
$languages = language_list('enabled');
$languages = $languages['1'];
if ($user->language == '') {
$user->language = language_default('language');
}
// If the user is being created, we set the user language to the page language.
$user_language = $user ? user_language($user) : $language;
$names = array();
foreach ($languages as $langcode => $language) {
$names[$langcode] = t($language->name) .' ('. $language->native .')';
}
$form['locale'] = array('#type' => 'fieldset',
'#title' => t('Interface language settings'),
$form['locale'] = array(
'#type' => 'fieldset',
'#title' => t('Language settings'),
'#weight' => 1,
);
$form['locale']['language'] = array('#type' => 'radios',
$form['locale']['language'] = array(
'#type' => 'radios',
'#title' => t('Language'),
'#default_value' => $user->language,
'#default_value' => $user_language->language,
'#options' => $names,
'#description' => t('Selecting a different locale will change the interface language of the site.'),
'#description' => t('Sets the default site interface and e-mail language for this account.'),
);
return $form;
}
......
This diff is collapsed.
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