2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright ©2002-2007 Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
25 * This class determines which emails need to be sent out based on user
26 * options and bug changes, and then it sends said emails.
29 * @copyright Copyright ©2002 - 2007, Blue Static
34 class NotificationCenter
40 private $bug = array();
46 private $original = array();
52 private $modified = array();
55 * Role list: a list of user IDs with their relations to the bug
58 private $roles = array(
59 '-notapplicable-' => array(),
60 'reporter' => array(),
61 'assignee' => array(),
62 'favorite' => array(),
64 'commenter' => array()
71 private $users = array();
74 * A list of notices per-user that are combined together in NotificationCenter::finalize()
77 private $notices = array();
80 * Sets the bug data so that all methods in this class have access to
81 * it when sending emails.
83 * @param array Original bug data
84 * @param array Modified bug data
86 public function setBugData($original, $modified = array())
88 if (sizeof($modified) > 0)
90 $this->bug
= $modified;
94 $this->bug
= $original;
97 $this->original
= $original;
98 $this->modified
= $modified;
100 $this->roles
['-notapplicable-'] = (sizeof($modified) > 0 ?
array($original['assignedto'], $modified['assignedto']) : array($original['assignedto']));
101 $this->roles
['reporter'] = array($original['userid']);
102 $this->roles
['assignee'][] = (sizeof($modified) > 0 ?
$modified['assignedto'] : $original['assignedto']);
104 $this->_fetchUserCache();
108 * Fetches all the users who could be related to the bug and sticks
109 * their information into an array.
111 private function _fetchUserCache()
113 $newbuggers = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "useremail WHERE relation = " . bugdar
::$emailOptions['relations']['-notapplicable-'] . " AND mask & " . bugdar
::$emailOptions['notifications']['newbug']);
114 foreach ($newbuggers as $newbug)
116 $this->roles
['-notapplicable-']["$newbug[userid]"] = $newbug['userid'];
119 $favorites = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "favorite WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
120 foreach ($favorites as $fav)
122 $this->roles
['favorite']["$fav[userid]"] = $fav['userid'];
125 $voters = BSApp
::$db->queryFirst("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
126 $this->roles
['voter'] = preg_split('#,#', $voters['userids'], 0, PREG_SPLIT_NO_EMPTY
);
128 $commenters = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
129 foreach ($commenters as $comment)
131 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
134 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favorite'], $this->roles
['voter'], $this->roles
['commenter']);
135 $masterids = BSFunctions
::array_strip_empty(array_unique($masterids));
137 if (is_array($masterids) && sizeof($masterids) > 0)
139 $userinfo = BSApp
::$db->query("
140 SELECT user.*, useremail.*
141 FROM " . TABLE_PREFIX
. "useremail AS useremail
142 LEFT JOIN " . TABLE_PREFIX
. "user AS user
143 ON (user.userid = useremail.userid)
144 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
146 foreach ($userinfo as $user)
148 if (!is_array($this->users
["$user[userid]"]))
150 $this->users
["$user[userid]"] = $user;
151 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
153 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
159 * Sends the appropriate emails for changes to bugs. This function
160 * works a lot like the Logging class by taking BugAPI->record and
161 * BugAPI->values and then comparing the two arries and sending emails
162 * with the differences.
164 public function sendBugChangeNotice()
166 if (!isset($this->modified
['bugid']))
171 // fields with custom mask information
172 if ($this->original
['assignedto'] != $this->modified
['assignedto'])
174 if ($this->original
['assignedto'] != '')
176 $this->_noticeNoLongerAssigned($this->original
['assignedto']);
178 if ($this->modified
['assignedto'] != '')
180 $this->_noticeNowAssigned($this->modified
['assignedto']);
183 if ($this->original
['status'] != $this->modified
['status'])
185 $this->_noticeStatusChange($this->original
['status'], $this->modified
['status']);
187 if ($this->original
['resolution'] != $this->modified
['resolution'])
189 $this->_noticeResolutionChange($this->original
['resolution'], $this->modified
['resolution']);
191 if ($this->original
['duplicates'] != $this->modified
['duplicates'])
193 $this->_noticeDuplicatesChange($this->original
['duplicates'], $this->modified
['duplicates']);
196 // other standard fields that don't have custom masks
197 if ($this->original
['severity'] != $this->modified
['severity'])
199 $this->_noticeSeverityChange($this->original
['severity'], $this->modified
['severity']);
201 if ($this->original
['priority'] != $this->modified
['priority'])
203 $this->_noticePriorityChange($this->original
['priority'], $this->modified
['priority']);
205 if (($this->original
['product'] != $this->modified
['product']) ||
($this->original
['component'] != $this->modified
['component']) ||
($this->original
['version'] != $this->modified
['version']))
207 $this->_noticePCVChange(array($this->original
['product'], $this->original
['component'], $this->original
['version']), array($this->modified
['product'], $this->modified
['component'], $this->modified
['version']));
215 foreach ($dofields as $field => $lookup)
217 if ($this->original
["$field"] != $this->modified
["$field"])
219 $this->_noticeOtherChange($field, $this->original
["$field"], $this->modified
["$field"]);
225 * Sends an email to the specified user ID that they are no longer the
226 * person assigned to the bug.
228 * @param integer User ID to send to
230 private function _noticeNoLongerAssigned($userid)
232 if ($this->users
["$userid"]['options'][0] & bugdar
::$emailOptions['notifications']['assignedto'] && in_array($userid, $this->roles
['-notapplicable-']))
234 $user = construct_user_display(bugdar
::$userinfo, false
);
235 eval('$part = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_unassigned.part', $this->_localeFromUserId($userid))) . '";');
236 $this->notices
["$userid"][] = $part;
241 * Informs the user that they have been made the assignee of the bug.
243 * @param integer User ID
245 private function _noticeNowAssigned($userid)
247 if ($this->users
["$userid"]['options'][0] & bugdar
::$emailOptions['notifications']['assignedto'] && in_array($userid, $this->roles
['-notapplicable-']))
249 $user = construct_user_display(bugdar
::$userinfo, false
);
250 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_assigned.part', $this->_localeFromUserId($userid))) . '";');
251 $this->notices
["$userid"][] = $email;
256 * Sends a message to inform users that the status has changed.
258 * @param integer Old status
259 * @param integer New status
261 private function _noticeStatusChange($old, $new)
263 $userlist = $this->_fetchUsersWithOnBit('statusresolve');
264 foreach ($userlist as $userid => $user)
266 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_status.part', $this->_localeFromUserId($userid))) . '";');
267 $this->notices
["$user[userid]"][] = $email;
272 * Sends an email to inform users that the resolution has changed.
274 * @param integer Old resolution
275 * @param integer New resolution
277 private function _noticeResolutionChange($old, $new)
279 $userlist = $this->_fetchUsersWithOnBit('statusresolve');
280 foreach ($userlist as $userid => $user)
282 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_resolution.part', $this->_localeFromUserId($userid))) . '";');
283 $this->notices
["$user[userid]"][] = $email;
288 * Informs users that the duplicates list has changed.
290 * @param string Old duplicates list
291 * @param string New duplicates list
293 private function _noticeDuplicatesChange($old, $new)
295 $userlist = $this->_fetchUsersWithOnBit('duplicates');
296 foreach ($userlist as $userid => $user)
298 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_duplicates.part', $this->_localeFromUserId($userid))) . '";');
299 $this->notices
["$user[userid]"][] = $email;
304 * Sends an email to inform users that the severity has changed.
306 * @param integer Old severity
307 * @param integer New severity
309 private function _noticeSeverityChange($old, $new)
311 $userlist = $this->_fetchUsersWithOnBit('otherfield');
312 foreach ($userlist as $userid => $user)
314 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_severity.part', $this->_localeFromUserId($userid))) . '";');
315 $this->notices
["$user[userid]"][] = $email;
320 * Informs users that the priority changed.
322 * @param integer Old priority
323 * @param integer New priority
325 private function _noticePriorityChange($old, $new)
327 $userlist = $this->_fetchUsersWithOnBit('otherfield');
328 foreach ($userlist as $userid => $user)
330 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_priority.part', $this->_localeFromUserId($userid))) . '";');
331 $this->notices
["$user[userid]"][] = $email;
336 * Sends an email telling users that the product, component, or version
337 * has changed. This is done all at once because you really need to see
338 * the whole thing in the notice.
340 * @param array Original PCV
341 * @param array Modified PCV
343 private function _noticePCVChange($old, $new)
345 $userlist = $this->_fetchUsersWithOnBit('otherfield');
347 $old = bugdar
::$datastore['product']["$old[0]"]['title'] . '/' . ($old[1] ? bugdar
::$datastore['product']["$old[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$old[2]"]['version'];
348 $new = bugdar
::$datastore['product']["$new[0]"]['title'] . '/' . ($new[1] ? bugdar
::$datastore['product']["$new[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$new[2]"]['version'];
350 foreach ($userlist as $userid => $user)
352 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_product.part', $this->_localeFromUserId($userid))) . '";');
353 $this->notices
["$user[userid]"][] = $email;
358 * Sends the appropriate users information about a new comment being
359 * posted to the bug report.
361 * @param array CommentAPI->values array
363 public function sendNewCommentNotice($comment)
365 $userlist = $this->_fetchUsersWithOnBit('newcomment');
366 foreach ($userlist as $userid => $user)
368 $user = construct_user_display(bugdar
::$userinfo, false
);
369 $date = $this->registry
->modules
['date']->format(bugdar
::$options['dateformat'], $comment['dateline']);
371 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_comment.part', $this->_localeFromUserId($userid))) . '";');
372 $this->notices
["$userid"][] = $email;
377 * A notice for an individual field changing.
379 * @param string Field name
380 * @param mixed Original value
381 * @param mixed Modified value
383 private function _noticeOtherChange($name, $old, $new)
385 $userlist = $this->_fetchUsersWithOnBit('otherfield');
386 foreach ($userlist as $userid => $user)
388 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_other.part', $this->_localeFromUserId($userid))) . '";');
389 $this->notices
["$user[userid]"][] = $email;
394 * Sends appropriate users a notice when a new attachment has been
397 * @param array AttachmentAPI->values array
398 * @param array List of all attachments made obsolete
399 * @param array Newly-inserted attachment ID
401 public function sendNewAttachmentNotice($attachment, $obsolete, $id)
403 $userlist = $this->_fetchUsersWithOnBit('newattachment');
404 foreach ($userlist as $userid => $user)
406 $user = construct_user_display(bugdar
::$userinfo, false
);
407 $obsoletes = implode(', ', (array)$obsolete);
409 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_attachment.part', $this->_localeFromUserId($userid))) . '";');
410 $this->notices
["$userid"][] = $email;
415 * Sends a new bug notification notice to all those who have the option
416 * turned no. This does not use fetchUsersWithOnBit() because a
417 * query is more effective.
419 * @param array Bug values array
420 * @param array Comment values array
422 public function sendNewBugNotice($bug, $comment)
424 $userinfo = BSApp
::$db->query("
425 SELECT user.*, useremail.*
426 FROM " . TABLE_PREFIX
. "useremail AS useremail
427 LEFT JOIN " . TABLE_PREFIX
. "user AS user
428 ON (user.userid = useremail.userid)
429 WHERE useremail.relation = 0
430 AND useremail.mask & " . bugdar
::$emailOptions['notifications']['newbug'] . "
432 foreach ($userinfo as $userInfo)
434 if (!is_array($this->users
["$userInfo[userid]"]))
436 $user = construct_user_display(bugdar
::$userinfo, false
);
437 $this->users
["$userInfo[userid]"] = $userInfo;
438 $product = bugdar
::$datastore['product']["$bug[product]"]['title'] . '/' . ($bug['component'] ? bugdar
::$datastore['product']["$bug[component]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$bug[version]"]['version'];
439 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_new_bug.part', $this->_localeFromUserId($userInfo['userid']))) . '";');
440 $this->notices
["$userInfo[userid]"][] = $email;
441 unset($this->users
["$userInfo[userid]"]['mask'], $this->users
["$userInfo[userid]"]['relation']);
443 $this->users
["$userInfo[userid]"]['options']["$userInfo[relation]"] = $userInfo['mask'];
448 * Generates an array of users who have a given email notification flag
449 * turned on in their bitfields.
451 * @param string Notification bitfield name
453 * @return array Array of users and their data
455 private function _fetchUsersWithOnBit($bitname)
459 foreach ($this->users
as $user)
461 foreach (bugdar
::$emailOptions['relations'] as $name => $bit)
463 if (in_array($user['userid'], $this->roles
["$name"]) && $user['options']["$bit"] & bugdar
::$emailOptions['notifications']["$bitname"])
465 $idlist[] = $user['userid'];
470 $masters = array_unique($idlist);
473 foreach ($masters as $userid)
475 $return["$userid"] =& $this->users
["$userid"];
482 * Compiles and sends the actual emails to users.
484 public function finalize()
486 // get the current bug for permissions checks
487 $bug = BSApp
::$db->query_first("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid = " . $this->bug
['bugid']);
488 foreach ($this->_notices
as $userid => $noticelist)
490 if ($userid == bugdar
::$userinfo['userid'])
492 BSApp
::debug("skipping user $userid because they're the one doing the thing");
496 // we wouldn't want people who favorite bugs getting hidden notices
497 if (!check_bug_permissions($bug, $this->users
["$userid"]))
499 BSApp
::debug("skipping user $userid ({$this->users[$userid]['email']}) because of permissions");
503 $parts = implode("\n\n", $noticelist);
505 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('bugnotification.xml', $this->_localeFromUserId($userid))) . '";');
506 $email = $this->registry
->xml
->parse($email, true
);
507 $this->registry
->mail
->setSubject($email['email']['subject']['value']);
508 $this->registry
->mail
->setBodyText($email['email']['bodyText']['value']);
510 if (!empty($this->users
["$userid"]['email']))
512 $this->registry
->mail
->send($this->users
["$userid"]['email'], $this->users
["$userid"]['displayname']);
516 BSApp
::debug("not sending an email to " . $userid . " because they don't have one?");
522 * Returns the locale name from a given user ID
524 * @param integer User ID
526 * @return string Locale
528 private function _localeFromUserId($userid)
530 $langcode = bugdar
::$datastore['language'][$this->users
[$userid]['languageid']]['langcode'];
533 $langcode = bugdar
::$datastore['language'][bugdar
::$options['defaultlanguage']]['langcode'];
539 /*=====================================================================*\
540 || ###################################################################
543 || ###################################################################
544 \*=====================================================================*/