View source
<?php
define('PM_EMAIL_NOTIFY_LEVEL_DISABLED', 0);
define('PM_EMAIL_NOTIFY_LEVEL_THREAD', 4);
define('PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE', 8);
define('PM_EMAIL_NOTIFY_LEVEL_ALL', 12);
define('PM_EMAIL_NOTIFY_LEVEL_DEFAULT', -1);
function pm_email_notify_perm() {
return array(
'set privatemsg e-mail notification level',
'change privatemsg e-mail notification for indirect messages',
);
}
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',
'access arguments' => array(
'administer privatemsg settings',
),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
return $items;
}
function _pm_email_notify_user_level($uid = NULL) {
if ($uid) {
$keys = array(
'user' => array(
$uid,
),
'global' => array(
0,
),
);
}
else {
$keys = array(
'global' => array(
0,
),
);
}
return privatemsg_get_setting('email_notify_level', $keys, PM_EMAIL_NOTIFY_LEVEL_ALL);
}
function _pm_email_notify_only_user($uid) {
$keys = array(
'user' => array(
$uid,
),
'global' => array(
0,
),
);
return privatemsg_get_setting('email_notify_only_user', $keys);
}
function _pm_email_notify_show_sender($uid) {
$keys = array(
'user' => array(
$uid,
),
'global' => array(
0,
),
);
return privatemsg_get_setting('show_sender_mail', $keys);
}
function _pm_email_notify_send_check($uid, $message) {
static $notifications = array();
$mid = $message['mid'];
$thread_id = $message['thread_id'];
$level = _pm_email_notify_user_level($uid);
if ($level == PM_EMAIL_NOTIFY_LEVEL_DISABLED) {
return FALSE;
}
if ($level == PM_EMAIL_NOTIFY_LEVEL_ALL) {
return TRUE;
}
if (!isset($notifications[$uid][$mid])) {
$notifications[$uid][$mid] = FALSE;
if ($level == PM_EMAIL_NOTIFY_LEVEL_THREAD) {
$notifications[$uid][$mid] = $mid == $thread_id;
}
elseif ($level == PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE) {
if ($mid == $thread_id) {
$notifications[$uid][$mid] = TRUE;
}
else {
$unread_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE thread_id = %d AND is_new = 1 AND recipient = %d AND type IN ('user', 'hidden')", $thread_id, $uid));
$notifications[$uid][$mid] = $unread_count == 1;
}
}
}
return $notifications[$uid][$mid];
}
function pm_email_notify_privatemsg_message_insert($message) {
foreach ($message['recipients'] as $recipient) {
pm_email_notify_send_mail($recipient, $message);
}
}
function pm_email_notify_privatemsg_message_recipient_changed($mid, $thread_id, $recipient_id, $type, $added) {
$types = array(
'user',
);
if ($added) {
if ($message = privatemsg_message_load($mid)) {
if (!_pm_email_notify_only_user($recipient_id)) {
$types[] = 'hidden';
}
if (in_array($type, $types) && _pm_email_notify_send_check($recipient_id, $message) && ($recipient = privatemsg_user_load($recipient_id))) {
pm_email_notify_send_mail($recipient, $message);
}
}
}
}
function pm_email_notify_send_mail($recipient, $message) {
if (isset($recipient->uid) && !empty($recipient->mail) && _pm_email_notify_send_check($recipient->uid, $message)) {
$params['recipient'] = $recipient;
$params['message'] = $message;
if (_pm_email_notify_show_sender($message['author']->uid)) {
$from = $message['author']->mail;
}
else {
$from = variable_get('pm_email_notify_from', '');
}
drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL);
}
}
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'], $language));
switch ($key) {
case 'notice':
$message['subject'] = strtr(_pm_email_notify_text('subject', $language), $variables);
$message['body'] = strtr(_pm_email_notify_text('body', $language), $variables);
break;
}
}
function _pm_email_notify_token($recipient, $message, $language) {
$tokens = array(
'!author_uid' => $message['author']->uid,
'!author' => privatemsg_recipient_format($message['author'], array(
'plain' => TRUE,
)),
'!pm_subject' => trim(drupal_html_to_text(check_plain($message['subject']))),
'!pm_body' => trim(drupal_html_to_text(check_markup($message['body'], $message['format'], FALSE))),
'!thread' => $message['thread_id'],
'!user_uid' => $recipient->uid,
'!message' => url(privatemsg_get_dynamic_url_prefix($recipient->uid) . '/view/' . $message['thread_id'], array(
'absolute' => TRUE,
'language' => $language,
)),
'!settings' => url('user/' . $recipient->uid . '/edit', array(
'absolute' => TRUE,
'language' => $language,
)),
);
drupal_alter('pm_email_notify_token', $tokens, $recipient, $message, $language);
return $tokens;
}
function _pm_email_notify_token_list() {
$tokens = drupal_map_assoc(array(
'!author_uid',
'!author',
'!pm_subject',
'!pm_body',
'!thread',
'!user_uid',
'!message',
'!settings',
));
$tokens['!message'] = '!message (URL)';
$tokens['!settings'] = '!settings (URL)';
drupal_alter('pm_email_notify_token_list', $tokens);
return implode(', ', $tokens);
}
function _pm_email_notify_source_text($key) {
$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;
}
}
return $text;
}
function _pm_email_notify_text($key, $language = NULL, $translate = TRUE) {
$text = _pm_email_notify_source_text($key);
$translated = pm_email_notify_tt('pm_email_notify:mail:' . $key, $text, $language, !$translate);
if ($translate) {
return $translated;
}
return $text;
}
function pm_email_notify_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
if ($category == 'account') {
$form['privatemsg']['pm_show_sender_mail'] = array(
'#type' => 'checkbox',
'#title' => t('Show my email address'),
'#description' => t('Use my private email address as a sender for notification mails to users I have sent private messages to. If unchecked, the notification will be sent by a generic account.'),
'#default_value' => _pm_email_notify_show_sender($account->uid),
'#access' => privatemsg_user_access('write privatemsg'),
);
if (privatemsg_user_access('read privatemsg', $account)) {
if (privatemsg_user_access('set privatemsg e-mail notification level')) {
$form['privatemsg']['pm_email_notify_level'] = array(
'#type' => 'radios',
'#title' => t('Send me an e-mail notification...'),
'#options' => array(
PM_EMAIL_NOTIFY_LEVEL_DISABLED => t('Never.'),
PM_EMAIL_NOTIFY_LEVEL_THREAD => t('Only for a new conversation'),
PM_EMAIL_NOTIFY_LEVEL_UNREAD_ONCE => t("Only once for a conversation until I've read the messages"),
PM_EMAIL_NOTIFY_LEVEL_ALL => t('Every time I receive a message'),
),
'#default_value' => _pm_email_notify_user_level($account->uid),
);
}
else {
$is_enabled = _pm_email_notify_user_level();
$form['privatemsg']['pm_email_notify_level'] = array(
'#type' => 'checkbox',
'#title' => t('Receive email notification for incoming private messages'),
'#default_value' => $is_enabled ? PM_EMAIL_NOTIFY_LEVEL_DEFAULT : PM_EMAIL_NOTIFY_LEVEL_DISABLED,
'#access' => $is_enabled,
);
}
$form['privatemsg']['pm_email_only_user'] = array(
'#type' => 'checkbox',
'#title' => t("Don't send me e-mail notifications for mass messages."),
'#default_value' => _pm_email_notify_only_user($account->uid),
'#access' => privatemsg_user_access('change privatemsg e-mail notification for indirect messages'),
);
}
}
return $form;
case 'submit':
if (isset($edit['pm_email_notify_level'])) {
privatemsg_set_setting('user', $account->uid, 'email_notify_level', $edit['pm_email_notify_level']);
unset($edit['pm_email_notify_level']);
}
if (isset($edit['pm_email_only_user'])) {
privatemsg_set_setting('user', $account->uid, 'email_notify_only_user', $edit['pm_email_only_user']);
unset($edit['pm_email_only_user']);
}
if (isset($edit['pm_show_sender_mail'])) {
privatemsg_set_setting('user', $account->uid, 'show_sender_mail', $edit['pm_show_sender_mail']);
unset($edit['pm_show_sender_mail']);
}
break;
case 'delete':
privatemsg_del_setting('user', $account->uid, 'email_notify_level');
privatemsg_del_setting('user', $account->uid, 'email_notify_only_user');
privatemsg_del_setting('user', $account->uid, 'show_sender_mail');
break;
}
}
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' => 'pm_email_notify_locale_refresh',
),
);
}
}
function pm_email_notify_locale_refresh() {
i18nstrings_update('pm_email_notify:mail:subject', _pm_email_notify_source_text('subject'));
i18nstrings_update('pm_email_notify:mail:body', _pm_email_notify_source_text('body'));
return TRUE;
}
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;
}
}