Newer
Older
<?php
// $Id$
/**
* @file
* Notifies users about new Private Messages via Email.
*/
/**
* Implements hook_menu().
*/
function pm_email_notify_menu() {
$items['admin/settings/messages/notify'] = array(
'title' => 'E-mail notify',
'description' => 'E-mail notification settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('pm_email_notify_admin_settings_form'),
'file' => 'pm_email_notify.admin.inc',
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'access arguments' => array('read privatemsg'),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
/**
* Retrieve notification setting of a user.
*
* This function retrieves user's pm notification preference from database,
* if user preference doesn't exist - it uses default value instead
*
* @param $uid
* User uid
*/
function _pm_email_notify_is_enabled($uid) {
static $notifications = array();
// Cache the result set in case this method is executed in batched operation which will perform many unnecessary repeated selects for the same user
if ( !isset($notifications[$uid]) ) {
$mail_notification = db_result(db_query('SELECT email_notify_is_enabled FROM {pm_email_notify} WHERE user_id = %d', $uid));
if ($mail_notification === FALSE) {
// db_result() returns FALSE if result was not found.
$mail_notification = variable_get('pm_email_notify_default', TRUE);
}
$notifications[$uid] = $mail_notification;
}
return $notifications[$uid];
}
/**
* Implements hook_privatemsg_message_insert().
*/
function pm_email_notify_privatemsg_message_insert($message) {
foreach ($message['recipients'] as $recipient) {
// check if recipient enabled email notifications
if (_pm_email_notify_is_enabled($recipient->uid)) {
// send them a new pm notification email if they did
$params['recipient'] = $recipient;
$params['message'] = $message;
drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params);
}
}
}
/**
* Implements hook_mail().
*/
function pm_email_notify_mail($key, &$message, $params) {
$language = $message['language'];
$variables = user_mail_tokens($params['recipient'], $language);
$variables = array_merge($variables, _pm_email_notify_token($params['recipient'], $params['message']));
switch ($key) {
case 'notice':
Sascha Grossenbacher
committed
$message['subject'] = strtr(_pm_email_notify_text('subject', $language), $variables);
$message['body'] = strtr(_pm_email_notify_text('body', $language), $variables);
break;
}
}
/**
* Return an array of token to value mappings for user e-mail messages.
*
* @param $message
* The private message array being sent. Must contain at
* least the fields 'author', 'subject', 'thread_id' and 'body'.
* @return
* Array of mappings from token names to values (for use with strtr()).
*/
function _pm_email_notify_token($recipient, $message) {
$tokens = array(
'!author' => $message['author']->name,
'!pm_subject' => drupal_html_to_text(check_plain($message['subject'])),
'!pm_body' => drupal_html_to_text(check_markup($message['body'], $message['format'], FALSE)),
'!thread' => $message['thread_id'],
'!user_uid' => $recipient->uid,
'!message' => url('messages/view/' . $message['thread_id'], array('absolute' => TRUE)),
'!settings' => url('user/' . $recipient->uid . '/edit', array('absolute' => TRUE)),
);
return $tokens;
}
/**
Sascha Grossenbacher
committed
* Return (if possible, translated) body/subject strings.
*
* @param $key
* Defines with string to return, either subject or body.
* @param $language
* Optionally define into which language should be translated. Defaults to the
* active language.
* @param $translate
* Define if the translated text should be returned or the source. This is
* used in the settings page to allow editing the source but still update the
* translation system.
* @return
* Either the translated text or the source, depending on the $translate
* flag.
Sascha Grossenbacher
committed
function _pm_email_notify_text($key, $language = NULL, $translate = TRUE) {
$text = variable_get('pm_email_notify_' . $key, FALSE);
if (empty($text)) {
switch ($key) {
case 'subject':
$text = 'New private message at !site.';
break;
case 'body':
$text = "Hi !username,\n\nThis is an automatic reminder from the site !site. You have received a new private message from !author.\n\nTo read your message, follow this link:\n!message\n\nIf you don't want to receive these emails again, change your preferences here:\n!settings";
break;
}
}
// Always call tt() so that the key and source is saved and can be translated.
// Only update the source if called from the settings page and $translate is
// FALSE.
$translated = pm_email_notify_tt('pm_email_notify:mail:' . $key, $text, $language, !$translate);
// Only return the translated text if requested so.
if ($translate) {
return $translated;
}
return $text;
}
/**
* Implements hook_user().
*
* Display settings form and store its information.
*/
function pm_email_notify_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
Sascha Grossenbacher
committed
if ($category == 'account' && privatemsg_user_access('read privatemsg')) {
$form['enable_pm_mail'] = array(
'#type' => 'fieldset',
'#title' => t('Privatemsg e-mail notification'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 10,
);
$form['enable_pm_mail']['pm_send_notifications'] = array(
'#type' => 'checkbox',
'#title' => t('Receive email notification for incoming private messages'),
'#default_value' => _pm_email_notify_is_enabled($account->uid),
);
}
return $form;
case 'submit':
Sascha Grossenbacher
committed
if ($category == 'account' && privatemsg_user_access('read privatemsg')) {
$pm_email_enabled = $edit['pm_send_notifications'];
unset($edit['pm_send_notifications']);
// Update database entry with user preference.
Sascha Grossenbacher
committed
$exists = db_result(db_query("SELECT 1 FROM {pm_email_notify} WHERE user_id = %d", $account->uid));
if ($exists) {
// If there is an existing entry, update.
db_query("UPDATE {pm_email_notify} SET email_notify_is_enabled = %d WHERE user_id = %d", $pm_email_enabled, $account->uid);
}
else {
// If not, create a new one.
db_query("INSERT INTO {pm_email_notify} (email_notify_is_enabled, user_id) VALUES (%d, %d)", $pm_email_enabled, $account->uid);
}
}
break;
case 'delete':
db_query("DELETE FROM {pm_email_notify} WHERE user_id = %d", $account->uid);
break;
}
}
Sascha Grossenbacher
committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* Implementation of hook_locale().
*/
function pm_email_notify_locale($op = 'groups', $group = NULL) {
switch($op) {
case 'groups':
return array('pm_email_notify' => t('Privatemsg Email Notification'));
break;
case 'info':
return array(
'pm_email_notify' => array(
'format' => FALSE,
'refresh callback' => FALSE,
)
);
}
}
/**
* Wrapper function for tt().
*/
function pm_email_notify_tt($name, $string, $language = NULL, $update = FALSE) {
static $tt;
if (!isset($tt)) {
$tt = variable_get('i18n_tt', 'tt');
if (!function_exists($tt)) {
$tt = FALSE;
}
}
if ($tt) {
return $tt($name, $string, isset($language) ? $language->language : NULL, $update);
}
else {
return $string;
}
}