1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Notifies users about new Private Messages via Email.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Retrieve notification setting of a user.
|
10
|
*
|
11
|
* This function retrieves user's pm notification preference from database,
|
12
|
* if user preference doesn't exist - it uses default value instead
|
13
|
*
|
14
|
* @param $uid
|
15
|
* User uid
|
16
|
*/
|
17
|
function _pm_email_notify_is_enabled($uid) {
|
18
|
$notifications = &drupal_static(__FUNCTION__, array());
|
19
|
// Cache the result set in case this method is executed in batched operation which will perform many unnecessary repeated selects for the same user
|
20
|
if (!isset($notifications[$uid]) ) {
|
21
|
$mail_notification = db_query('SELECT email_notify_is_enabled FROM {pm_email_notify} WHERE user_id = :uid', array(':uid' => $uid))->fetchField();
|
22
|
if ($mail_notification === FALSE) { //db_result returns FALSE if result was not found.
|
23
|
$mail_notification = variable_get('pm_email_notify_default', TRUE);
|
24
|
}
|
25
|
$notifications[$uid] = $mail_notification;
|
26
|
}
|
27
|
return $notifications[$uid];
|
28
|
}
|
29
|
|
30
|
/**
|
31
|
* Implements hook_privatemsg_message_insert().
|
32
|
*/
|
33
|
function pm_email_notify_privatemsg_message_insert($message) {
|
34
|
foreach ($message->recipients as $recipient) {
|
35
|
// check if recipient enabled email notifications
|
36
|
if (isset($recipient->uid) && _pm_email_notify_is_enabled($recipient->uid)) {
|
37
|
// send them a new pm notification email if they did
|
38
|
$params['recipient'] = $recipient;
|
39
|
$params['message'] = $message;
|
40
|
// token replace for email from address
|
41
|
$data = array(
|
42
|
'privatemsg_message' => $params['message'],
|
43
|
'privatemsg_recipient' => $params['recipient'],
|
44
|
);
|
45
|
$options = array(
|
46
|
'language' => user_preferred_language($params['recipient']),
|
47
|
// Don't sanitize output since this is used in an email, not a browser.
|
48
|
'sanitize' => FALSE,
|
49
|
// Custom token to avoid custom token handling.
|
50
|
'privatemsg-display-invalid' => FALSE,
|
51
|
);
|
52
|
$from = trim(token_replace(variable_get('pm_email_notify_from', ''), $data, $options));
|
53
|
drupal_mail('pm_email_notify', 'notice', $recipient->mail, user_preferred_language($recipient), $params, !empty($from) ? $from : NULL);
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Implements hook_mail().
|
60
|
*/
|
61
|
function pm_email_notify_mail($key, &$message, $params) {
|
62
|
switch ($key) {
|
63
|
case 'notice':
|
64
|
$data = array(
|
65
|
'privatemsg_message' => $params['message'],
|
66
|
'privatemsg_recipient' => $params['recipient'],
|
67
|
);
|
68
|
$options = array(
|
69
|
'language' => user_preferred_language($params['recipient']),
|
70
|
// Don't sanitize output since this is used in an email, not a browser.
|
71
|
'sanitize' => FALSE,
|
72
|
// Custom token to avoid custom token handling.
|
73
|
'privatemsg-display-invalid' => FALSE,
|
74
|
);
|
75
|
|
76
|
$message['subject'] = trim(token_replace(variable_get('pm_email_notify_subject', 'New private message at [site:name].'), $data, $options));
|
77
|
$message['body'][] = trim(token_replace(variable_get('pm_email_notify_body', _pm_email_notify_default_body()), $data, $options));
|
78
|
break;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Returns default email notification body.
|
84
|
*/
|
85
|
function _pm_email_notify_default_body() {
|
86
|
return "Hi [privatemsg_message:recipient],\n\nThis is an automatic reminder from the site [site:name]. You have received a new private message from [privatemsg_message:author].\n\nTo read your message, follow this link:\n[privatemsg_message:url]\n\nIf you don't want to receive these emails again, change your preferences here:\n[privatemsg_message:recipient:edit-url]";
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Implements hook_form_alter().
|
91
|
*/
|
92
|
function pm_email_notify_form_alter(&$form, &$form_state, $form_id) {
|
93
|
if (($form_id == 'user_register_form' || $form_id == 'user_profile_form') && $form['#user_category'] == 'account' && privatemsg_user_access('read privatemsg')) {
|
94
|
$form['privatemsg']['pm_send_notifications'] = array(
|
95
|
'#type' => 'checkbox',
|
96
|
'#title' => t('Receive email notification for incoming private messages'),
|
97
|
'#default_value' => _pm_email_notify_is_enabled($form['#user']->uid),
|
98
|
'#states' => array(
|
99
|
'visible' => array(
|
100
|
':input[name="pm_enable"]' => array('checked' => TRUE),
|
101
|
),
|
102
|
),
|
103
|
);
|
104
|
}
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Implements hook_user_update().
|
109
|
*/
|
110
|
function pm_email_notify_user_update(&$edit, $account, $category) {
|
111
|
if (isset($edit['pm_send_notifications']) && privatemsg_user_access('read privatemsg', $account)) {
|
112
|
db_merge('pm_email_notify')
|
113
|
->fields(array('email_notify_is_enabled' => $edit['pm_send_notifications']))
|
114
|
->key(array('user_id' => $account->uid))
|
115
|
->execute();
|
116
|
}
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* Implements hook_form_FORM_ID_alter().
|
121
|
*/
|
122
|
function pm_email_notify_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
|
123
|
$form['pm_email_notify'] = array(
|
124
|
'#type' => 'fieldset',
|
125
|
'#title' => t('E-mail notify'),
|
126
|
'#group' => 'settings',
|
127
|
'#weight' => 22,
|
128
|
);
|
129
|
|
130
|
$form['pm_email_notify']['pm_email_notify_default'] = array(
|
131
|
'#type' => 'checkbox',
|
132
|
'#title' => t('Notify users of new private messages by default'),
|
133
|
'#default_value' => variable_get('pm_email_notify_default', TRUE),
|
134
|
'#weight' => 0,
|
135
|
);
|
136
|
|
137
|
$form['pm_email_notify']['pm_email_notify_desc'] = array(
|
138
|
'#type' => 'item',
|
139
|
'#title' => t('Customize the email messages sent to users upon receipt of a new private message.'),
|
140
|
'#weight' => 1,
|
141
|
);
|
142
|
|
143
|
$form['pm_email_notify']['pm_email_notify_from'] = array(
|
144
|
'#type' => 'textfield',
|
145
|
'#title' => t('From e-mail address for notifications'),
|
146
|
'#default_value' => variable_get('pm_email_notify_from',''),
|
147
|
'#weight' => 2,
|
148
|
'#description' => t('This is the e-mail address that notifications will come from. Leave blank to use the site default.'),
|
149
|
);
|
150
|
|
151
|
$form['pm_email_notify']['pm_email_notify_subject'] = array(
|
152
|
'#type' => 'textfield',
|
153
|
'#title' => t('Subject of notification messages'),
|
154
|
'#default_value' => variable_get('pm_email_notify_subject', t('New private message at [site:name].')),
|
155
|
'#weight' => 2,
|
156
|
);
|
157
|
|
158
|
$form['pm_email_notify']['pm_email_notify_body'] = array(
|
159
|
'#type' => 'textarea',
|
160
|
'#title' => t('Body of notification messages'),
|
161
|
'#default_value' => variable_get('pm_email_notify_body', _pm_email_notify_default_body()),
|
162
|
'#weight' => 3,
|
163
|
);
|
164
|
|
165
|
if (module_exists('token')) {
|
166
|
$form['pm_email_notify']['token'] = array(
|
167
|
'#type' => 'fieldset',
|
168
|
'#title' => t('Token browser'),
|
169
|
'#weight' => -1,
|
170
|
'#collapsible' => TRUE,
|
171
|
'#collapsed' => TRUE,
|
172
|
'#weight' => 4,
|
173
|
);
|
174
|
$form['pm_email_notify']['token']['browser'] = array(
|
175
|
'#theme' => 'token_tree',
|
176
|
'#token_types' => array('privatemsg_message'),
|
177
|
);
|
178
|
}
|
179
|
else {
|
180
|
$form['pm_email_notify']['tokens'] = array(
|
181
|
'#type' => 'item',
|
182
|
'#value' => t('Available variables are: !author, !author_uid, !pm_subject, !pm_body, !thread, !site, !login_url, !uri, !uri_brief, !message (URL) and !settings (URL).'),
|
183
|
'#weight' => 4,
|
184
|
);
|
185
|
}
|
186
|
|
187
|
return system_settings_form($form);
|
188
|
}
|