1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
*
|
6 |
|
|
* Contains functions which utilize the database and other internal helpers.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
/**
|
10 |
|
|
* Get the notification preferences for a specific user.
|
11 |
|
|
*
|
12 |
|
|
* @param integer $uid
|
13 |
|
|
* @return mixed
|
14 |
|
|
* StdClass if found, else NULL
|
15 |
|
|
*/
|
16 |
|
|
function comment_notify_get_user_notification_setting($uid) {
|
17 |
|
|
$users = &drupal_static(__FUNCTION__);
|
18 |
|
|
if (!isset($users[$uid])) {
|
19 |
|
|
if (is_null($uid)) {
|
20 |
|
|
throw new Exception('Cannot get user preference, uid missing');
|
21 |
|
|
}
|
22 |
|
|
// Handle anonymous users with defaults.
|
23 |
|
|
if ($uid == 0) {
|
24 |
|
|
$users[0] = new stdClass();
|
25 |
|
|
$users[0]->comment_notify = comment_notify_variable_registry_get('default_registered_mailalert');
|
26 |
|
|
$users[0]->node_notify = comment_notify_variable_registry_get('node_notify_default_mailalert');
|
27 |
|
|
}
|
28 |
|
|
else {
|
29 |
|
|
$setting = db_select('comment_notify_user_settings', 'cnus')
|
30 |
|
|
->fields('cnus')
|
31 |
|
|
->condition('uid', $uid)
|
32 |
|
|
->execute()
|
33 |
|
|
->fetchObject();
|
34 |
|
|
|
35 |
|
|
if (!$setting) {
|
36 |
|
|
return NULL;
|
37 |
|
|
}
|
38 |
|
|
else {
|
39 |
|
|
$users[$uid] = $setting;
|
40 |
|
|
}
|
41 |
|
|
}
|
42 |
|
|
}
|
43 |
|
|
return $users[$uid];
|
44 |
|
|
}
|
45 |
|
|
|
46 |
|
|
function comment_notify_get_default_notification_setting() {
|
47 |
|
|
return (object) array(
|
48 |
|
|
'comment_notify' => comment_notify_variable_registry_get('default_registered_mailalert'),
|
49 |
|
|
'node_notify' => comment_notify_variable_registry_get('node_notify_default_mailalert')
|
50 |
|
|
);
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* Remove comment notification preferences for a user.
|
55 |
|
|
*
|
56 |
|
|
* @param integer $uid
|
57 |
|
|
* @return boolean
|
58 |
|
|
*/
|
59 |
|
|
function comment_notify_delete_user_notification_setting($uid) {
|
60 |
|
|
return (bool)db_delete('comment_notify_user_settings')
|
61 |
|
|
->condition('uid', $uid)
|
62 |
|
|
->execute();
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* Get a user's default preference for comment notification.
|
67 |
|
|
*
|
68 |
|
|
* @param integer $uid
|
69 |
|
|
* @return integer
|
70 |
|
|
*/
|
71 |
|
|
function comment_notify_get_user_comment_notify_preference($uid) {
|
72 |
|
|
$setting = comment_notify_get_user_notification_setting($uid);
|
73 |
|
|
if (!$setting) {
|
74 |
|
|
$setting = comment_notify_get_default_notification_setting();
|
75 |
|
|
}
|
76 |
|
|
return $setting->comment_notify;
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Get a user's default preference for node update notification.
|
81 |
|
|
*
|
82 |
|
|
* This is notification on nodes where the user is the author.
|
83 |
|
|
*
|
84 |
|
|
* @param integer $uid
|
85 |
|
|
* @return integer
|
86 |
|
|
*/
|
87 |
|
|
function comment_notify_get_user_node_notify_preference($uid) {
|
88 |
|
|
$setting = comment_notify_get_user_notification_setting($uid);
|
89 |
|
|
if (!$setting) {
|
90 |
|
|
$setting = comment_notify_get_default_notification_setting();
|
91 |
|
|
}
|
92 |
|
|
return $setting->node_notify;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Sets the notification preferences for a specific user.
|
97 |
|
|
*
|
98 |
|
|
* @param integer $uid
|
99 |
|
|
* @param integer $node_notification
|
100 |
|
|
* @param integer $comment_notification
|
101 |
|
|
* @return boolean
|
102 |
|
|
*/
|
103 |
|
|
function comment_notify_set_user_notification_setting($uid, $node_notification = NULL, $comment_notification = NULL) {
|
104 |
|
|
if (!$uid) {
|
105 |
|
|
throw new Exception('Cannot set user preference, uid missing');
|
106 |
|
|
}
|
107 |
|
|
$fields = array('uid' => $uid);
|
108 |
|
|
|
109 |
|
|
if (!is_null($node_notification)) {
|
110 |
|
|
$fields['node_notify'] = $node_notification;
|
111 |
|
|
}
|
112 |
|
|
if (!is_null($comment_notification)) {
|
113 |
|
|
$fields['comment_notify'] = $comment_notification;
|
114 |
|
|
}
|
115 |
|
|
if (comment_notify_get_user_notification_setting($uid)) {
|
116 |
|
|
$query = db_update('comment_notify_user_settings');
|
117 |
|
|
$query->condition('uid', $uid);
|
118 |
|
|
}
|
119 |
|
|
else {
|
120 |
|
|
$query = db_insert('comment_notify_user_settings');
|
121 |
|
|
}
|
122 |
|
|
return (bool)$query
|
123 |
|
|
->fields($fields)
|
124 |
|
|
->execute();
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Add a notification against a comment.
|
129 |
|
|
*
|
130 |
|
|
* @param integer $cid
|
131 |
|
|
* @param integer $notify
|
132 |
|
|
* @param string $notify_hash
|
133 |
|
|
* @return boolean
|
134 |
|
|
*/
|
135 |
|
|
function comment_notify_add_notification($cid, $notify, $notify_hash) {
|
136 |
|
|
return (bool)db_insert('comment_notify')
|
137 |
|
|
->fields(array(
|
138 |
|
|
'cid' => $cid,
|
139 |
|
|
'notify' => $notify === NULL ? 0 : $notify,
|
140 |
|
|
'notify_hash' => $notify_hash,
|
141 |
|
|
))
|
142 |
|
|
->execute();
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
/**
|
146 |
|
|
* Remove all the notifications linked with a comment
|
147 |
|
|
*
|
148 |
|
|
* @param integer $cid
|
149 |
|
|
* @return boolean
|
150 |
|
|
*/
|
151 |
|
|
function comment_notify_remove_all_notifications($cid) {
|
152 |
|
|
return (bool)db_delete('comment_notify')
|
153 |
|
|
->condition('cid', $cid)
|
154 |
|
|
->execute();
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* Updated a notification with a different notification type
|
159 |
|
|
*
|
160 |
|
|
* @param integer $cid
|
161 |
|
|
* @param integer $notify
|
162 |
|
|
* @return boolean
|
163 |
|
|
*/
|
164 |
|
|
function comment_notify_update_notification($cid, $notify) {
|
165 |
|
|
return (bool)db_update('comment_notify')
|
166 |
|
|
->fields(array(
|
167 |
|
|
'notify' => $notify === NULL ? 0 : $notify,
|
168 |
|
|
))
|
169 |
|
|
->condition('cid', $cid)
|
170 |
|
|
->execute();
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
|
|
* Get the type of notification for a comment notification record.
|
175 |
|
|
*
|
176 |
|
|
* @param integer $cid
|
177 |
|
|
* @return integer
|
178 |
|
|
*/
|
179 |
|
|
function comment_notify_get_notification_type($cid) {
|
180 |
|
|
return db_select('comment_notify', 'cn')
|
181 |
|
|
->fields('cn', array('notify'))
|
182 |
|
|
->condition('cid', $cid)
|
183 |
|
|
->execute()
|
184 |
|
|
->fetchField();
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
/**
|
188 |
|
|
* Get a list of mails which need to be contacted for a node.
|
189 |
|
|
*
|
190 |
|
|
* @param integer $nid
|
191 |
|
|
* @return QueryStatement
|
192 |
|
|
*/
|
193 |
|
|
function comment_notify_get_watchers($nid) {
|
194 |
|
|
$cids = db_query("SELECT c.cid FROM {comment} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users} u ON c.uid = u.uid WHERE c.nid = :nid AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array(
|
195 |
|
|
':nid' => $nid,
|
196 |
|
|
':status' => COMMENT_PUBLISHED,
|
197 |
|
|
':notify' => COMMENT_NOTIFY_DISABLED,
|
198 |
|
|
))->fetchCol();
|
199 |
|
|
return comment_load_multiple($cids);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/**
|
203 |
|
|
* Record that the owner of a comment notification request has already been notified.
|
204 |
|
|
*
|
205 |
|
|
* @param integer $cid
|
206 |
|
|
* @return boolean
|
207 |
|
|
*/
|
208 |
|
|
function comment_notify_mark_comment_as_notified($comment) {
|
209 |
|
|
// First, mark the passed comment (an object, so passed by reference).
|
210 |
|
|
$comment->notified = 1;
|
211 |
|
|
|
212 |
|
|
// Next, store this fact in the DB as well.
|
213 |
|
|
return (bool)db_update('comment_notify')
|
214 |
|
|
->fields(array(
|
215 |
|
|
'notified' => 1,
|
216 |
|
|
))
|
217 |
|
|
->condition('cid', $comment->cid)
|
218 |
|
|
->execute();
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
/**
|
222 |
|
|
* Unsubscribe all comment notification requests associated with an email.
|
223 |
|
|
*
|
224 |
|
|
* If the email belongs to a user, it will unsubscribe all of their Comment Notify records.
|
225 |
|
|
* If it does not, then it will unsubscribe all anonymous users.
|
226 |
|
|
*
|
227 |
|
|
* @param string $mail
|
228 |
|
|
* @return boolean
|
229 |
|
|
*/
|
230 |
|
|
function comment_notify_unsubscribe_by_email($mail) {
|
231 |
|
|
$update_query = db_update('comment_notify');
|
232 |
|
|
$update_query->fields(array('notify' => 0));
|
233 |
|
|
|
234 |
|
|
$comment_query = db_select('comment', 'c');
|
235 |
|
|
$comment_query->fields('c', array('cid'));
|
236 |
|
|
|
237 |
|
|
$uid = db_select('users', 'u')
|
238 |
|
|
->fields('u', array('uid'))
|
239 |
|
|
->condition('mail', $mail)
|
240 |
|
|
->execute()
|
241 |
|
|
->fetchField();
|
242 |
|
|
if ($uid) {
|
243 |
|
|
$comment_query->condition('uid', $uid);
|
244 |
|
|
}
|
245 |
|
|
else {
|
246 |
|
|
$comment_query->condition('mail', $mail);
|
247 |
|
|
}
|
248 |
|
|
$update_query->condition('cid', $comment_query, 'IN');
|
249 |
|
|
|
250 |
|
|
return (bool)$update_query->execute();
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
/**
|
254 |
|
|
* Unsubscribe comment notification requests associated with a hash.
|
255 |
|
|
*
|
256 |
|
|
* This is used in the unsubscribe link.
|
257 |
|
|
*
|
258 |
|
|
* @param string $hash
|
259 |
|
|
* @return boolean
|
260 |
|
|
*/
|
261 |
|
|
function comment_notify_unsubscribe_by_hash($hash) {
|
262 |
|
|
$notification = db_select('comment_notify')
|
263 |
|
|
->fields('comment_notify')
|
264 |
|
|
->condition('notify_hash', $hash)
|
265 |
|
|
->execute()->fetchAll();
|
266 |
|
|
|
267 |
|
|
// If this notification is at the node level, delete all notifications for this node.
|
268 |
|
|
if (COMMENT_NOTIFY_NODE == $notification[0]->notify) {
|
269 |
|
|
// Get all this user's comments for this node.
|
270 |
|
|
$result = db_query("SELECT c.cid
|
271 |
|
|
FROM {comment} c, (
|
272 |
|
|
SELECT oc.nid, oc.uid
|
273 |
|
|
FROM {comment} AS oc, comment_notify AS ocn
|
274 |
|
|
WHERE oc.cid = ocn.cid
|
275 |
|
|
AND ocn.notify_hash = :hash
|
276 |
|
|
) AS o
|
277 |
|
|
WHERE o.nid = c.nid
|
278 |
|
|
AND o.uid = c.uid", array(':hash' => $hash));
|
279 |
|
|
|
280 |
|
|
$cids = $result->fetchCol();
|
281 |
|
|
|
282 |
|
|
// Update all comment notifications to be disabled.
|
283 |
|
|
return (bool)db_update('comment_notify')
|
284 |
|
|
->fields(array(
|
285 |
|
|
'notify' => 0,
|
286 |
|
|
))
|
287 |
|
|
->condition('cid', $cids, 'IN')
|
288 |
|
|
->execute();
|
289 |
|
|
}
|
290 |
|
|
else {
|
291 |
|
|
// Update this notification to be disabled.
|
292 |
|
|
return (bool)db_update('comment_notify')
|
293 |
|
|
->fields(array(
|
294 |
|
|
'notify' => 0,
|
295 |
|
|
))
|
296 |
|
|
->condition('notify_hash', $hash)
|
297 |
|
|
->execute();
|
298 |
|
|
}
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
/**
|
302 |
|
|
* Helper function to centralize variable management and defaults.
|
303 |
|
|
*
|
304 |
|
|
* All variables fall under the "comment_notify" psuedo namespace. This ensures
|
305 |
|
|
* consistancy, and eliminates some verbosity in the calling code. In addition
|
306 |
|
|
* by storing all of the variables in one place, we avoid repeating duplicate
|
307 |
|
|
* defaults which are harder to maintain.
|
308 |
|
|
*
|
309 |
|
|
* @param string $name
|
310 |
|
|
* @return mixed
|
311 |
|
|
*/
|
312 |
|
|
function comment_notify_variable_registry_get($name) {
|
313 |
|
|
$variables = array();
|
314 |
|
|
$variables['author_subject'] = t('[site:name] :: new comment for your post.');
|
315 |
|
|
$variables['available_alerts'] = array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT);
|
316 |
|
|
$variables['default_anon_mailalert'] = COMMENT_NOTIFY_NODE;
|
317 |
|
|
$variables['node_notify_default_mailtext'] = AUTHOR_MAILTEXT;
|
318 |
|
|
$variables['default_registered_mailalert'] = COMMENT_NOTIFY_DISABLED;
|
319 |
|
|
$variables['node_notify_default_mailalert'] = 0;
|
320 |
|
|
$variables['watcher_subject'] = '[site:name] :: new comment on [comment:node:title]';
|
321 |
|
|
$variables['comment_notify_default_mailtext'] = DEFAULT_MAILTEXT;
|
322 |
|
|
$variables['node_types'] = array('article' => 'article');
|
323 |
|
|
|
324 |
|
|
// Errors
|
325 |
|
|
$variables['error_anonymous_email_missing'] = 'If you want to subscribe to comments you must supply a valid e-mail address.';
|
326 |
|
|
return variable_get("comment_notify_" . $name, $variables[$name]);
|
327 |
|
|
}
|
328 |
|
|
|
329 |
|
|
/**
|
330 |
|
|
* Helper function to centralize setting variables.
|
331 |
|
|
*
|
332 |
|
|
* @param string $name
|
333 |
|
|
* @param mixed $value
|
334 |
|
|
* @return boolean
|
335 |
|
|
*/
|
336 |
|
|
function comment_notify_variable_registry_set($name, $value) {
|
337 |
|
|
return variable_set("comment_notify_" . $name, $value);
|
338 |
|
|
} |