2 /*=====================================================================*\
3 || ###################################################################
5 || # Copyright (c)2004-2008 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 (c)2004 - 2008, Blue Static
33 class NotificationCenter
39 private $bug = array();
45 private $original = array();
51 private $modified = array();
54 * Role list: a list of user IDs with their relations to the bug
57 private $roles = array(
58 '-notapplicable-' => array(),
59 'reporter' => array(),
60 'assignee' => array(),
61 'favorite' => array(),
63 'commenter' => array()
70 private $users = array();
73 * A list of notices per-user that are combined together in NotificationCenter::finalize()
76 private $notices = array();
79 * Sets the bug data so that all methods in this class have access to
80 * it when sending emails.
82 * @param array Original bug data
83 * @param array Modified bug data
85 public function setBugData($original, $modified = array())
87 if (sizeof($modified) > 0)
89 $this->bug
= $modified;
93 $this->bug
= $original;
96 $this->original
= $original;
97 $this->modified
= $modified;
99 $this->roles
['-notapplicable-'] = (sizeof($modified) > 0 ? array($original['assignedto'], $modified['assignedto']) : array($original['assignedto']));
100 $this->roles
['reporter'] = array($original['userid']);
101 $this->roles
['assignee'][] = (sizeof($modified) > 0 ? $modified['assignedto'] : $original['assignedto']);
103 $this->_fetchUserCache();
107 * Fetches all the users who could be related to the bug and sticks
108 * their information into an array.
110 private function _fetchUserCache()
112 $newbuggers = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "useremail WHERE relation = " . bugdar
::$emailOptions['relations']['-notapplicable-'] . " AND mask & " . bugdar
::$emailOptions['notifications']['newbug']);
113 foreach ($newbuggers as $newbug)
115 $this->roles
['-notapplicable-']["$newbug[userid]"] = $newbug['userid'];
118 $favorites = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "favorite WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
119 foreach ($favorites as $fav)
121 $this->roles
['favorite']["$fav[userid]"] = $fav['userid'];
124 $voters = BSApp
::$db->queryFirst("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
125 $this->roles
['voter'] = preg_split('#,#', $voters['userids'], 0, PREG_SPLIT_NO_EMPTY
);
127 $commenters = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
128 foreach ($commenters as $comment)
130 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
133 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favorite'], $this->roles
['voter'], $this->roles
['commenter']);
134 $masterids = BSFunctions
::array_strip_empty(array_unique($masterids));
136 if (is_array($masterids) && sizeof($masterids) > 0)
138 $userinfo = BSApp
::$db->query("
139 SELECT user.*, useremail.*
140 FROM " . TABLE_PREFIX
. "useremail AS useremail
141 LEFT JOIN " . TABLE_PREFIX
. "user AS user
142 ON (user.userid = useremail.userid)
143 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
145 foreach ($userinfo as $user)
147 if (!is_array($this->users
["$user[userid]"]))
149 $this->users
["$user[userid]"] = $user;
150 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
152 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
158 * Sends the appropriate emails for changes to bugs. This function
159 * works a lot like the Logging class by taking BugAPI->record and
160 * BugAPI->values and then comparing the two arries and sending emails
161 * with the differences.
163 public function sendBugChangeNotice()
165 if (!isset($this->modified
['bugid']))
170 // fields with custom mask information
171 if ($this->original
['assignedto'] != $this->modified
['assignedto'])
173 if ($this->original
['assignedto'] != '')
175 $this->_noticeNoLongerAssigned($this->original
['assignedto']);
177 if ($this->modified
['assignedto'] != '')
179 $this->_noticeNowAssigned($this->modified
['assignedto']);
182 if ($this->original
['status'] != $this->modified
['status'])
184 $this->_noticeStatusChange($this->original
['status'], $this->modified
['status']);
186 if ($this->original
['resolution'] != $this->modified
['resolution'])
188 $this->_noticeResolutionChange($this->original
['resolution'], $this->modified
['resolution']);
190 if ($this->original
['duplicates'] != $this->modified
['duplicates'])
192 $this->_noticeDuplicatesChange($this->original
['duplicates'], $this->modified
['duplicates']);
195 // other standard fields that don't have custom masks
196 if ($this->original
['severity'] != $this->modified
['severity'])
198 $this->_noticeSeverityChange($this->original
['severity'], $this->modified
['severity']);
200 if ($this->original
['priority'] != $this->modified
['priority'])
202 $this->_noticePriorityChange($this->original
['priority'], $this->modified
['priority']);
204 if (($this->original
['product'] != $this->modified
['product']) || ($this->original
['component'] != $this->modified
['component']) || ($this->original
['version'] != $this->modified
['version']))
206 $this->_noticePCVChange(array($this->original
['product'], $this->original
['component'], $this->original
['version']), array($this->modified
['product'], $this->modified
['component'], $this->modified
['version']));
214 foreach ($dofields as $field => $lookup)
216 if ($this->original
["$field"] != $this->modified["$field"])
218 $this->_noticeOtherChange($field, $this->original
["$field"], $this->modified["$field"]);
224 * Sends an email to the specified user ID that they are no longer the
225 * person assigned to the bug.
227 * @param integer User ID to send to
229 private function _noticeNoLongerAssigned($userid)
231 if ($this->users
["$userid"]['options'][0] & bugdar::$emailOptions['notifications']['assignedto'] && in_array($userid, $this->roles['-notapplicable-']))
233 $user = construct_user_display(bugdar::$userinfo, false);
235 $email = get_email_text('notice_unassigned');
236 $this->notices["$userid"][] = sprintf($email['part'], $user);
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);
251 $email = get_email_text('notice_assigned');
252 $this->notices["$userid"][] = sprintf($email['part'], $user);
257 * Sends a message to inform users that the status has changed.
259 * @param integer Old status
260 * @param integer New status
262 private function _noticeStatusChange($old, $new)
264 $userlist = $this->_fetchUsersWithOnBit('statusresolve');
266 $old = bugdar
::$datastore['status'][$old]['status'];
267 $new = bugdar
::$datastore['status'][$new]['status'];
269 foreach ($userlist as $userid => $user)
271 $email = get_email_text('notice_status');
272 $this->notices
["$user[userid]"][] = sprintf($email['part'], $new, $old);
277 * Sends an email to inform users that the resolution has changed.
279 * @param integer Old resolution
280 * @param integer New resolution
282 private function _noticeResolutionChange($old, $new)
284 $userlist = $this->_fetchUsersWithOnBit('statusresolve');
286 $old = bugdar
::$datastore['resolution'][$old]['resolution'];
287 $new = bugdar
::$datastore['resolution'][$new]['resolution'];
289 foreach ($userlist as $userid => $user)
291 $email = get_email_text('notice_resolution');
292 $this->notices
["$user[userid]"][] = sprintf($email['part'], $new, $old);
297 * Informs users that the duplicates list has changed.
299 * @param string Old duplicates list
300 * @param string New duplicates list
302 private function _noticeDuplicatesChange($old, $new)
304 $userlist = $this->_fetchUsersWithOnBit('duplicates');
306 foreach ($userlist as $userid => $user)
308 $email = get_email_text('notice_duplicates');
309 $this->notices
["$user[userid]"][] = sprintf($email['part'], $old, $new);
314 * Sends an email to inform users that the severity has changed.
316 * @param integer Old severity
317 * @param integer New severity
319 private function _noticeSeverityChange($old, $new)
321 $userlist = $this->_fetchUsersWithOnBit('otherfield');
323 $old = bugdar
::$datastore['severity'][$old]['severity'];
324 $new = bugdar
::$datastore['severity'][$new]['severity'];
326 foreach ($userlist as $userid => $user)
328 $this->notices
["$user[userid]"][] = sprintf($email['part'], $old, $new);
333 * Informs users that the priority changed.
335 * @param integer Old priority
336 * @param integer New priority
338 private function _noticePriorityChange($old, $new)
340 $userlist = $this->_fetchUsersWithOnBit('otherfield');
342 $old = bugdar
::$datastore['priority'][$old]['priority'];
343 $new = bugdar
::$datastore['priority'][$new]['priority'];
345 foreach ($userlist as $userid => $user)
347 $email = get_email_text('notice_priority');
348 $this->notices
["$user[userid]"][] = sprintf($email['part'], $old, $new);
353 * Sends an email telling users that the product, component, or version
354 * has changed. This is done all at once because you really need to see
355 * the whole thing in the notice.
357 * @param array Original PCV
358 * @param array Modified PCV
360 private function _noticePCVChange($old, $new)
362 $userlist = $this->_fetchUsersWithOnBit('otherfield');
364 $products = &bugdar
::$datastore['product'];
365 $versions = &bugdar
::$datastore['version'];
367 $old = $products[$old[0]]['title'] . '/' . ($old[1] ? $products[$old[1]]['title'] . '/' : '') . $versions[$old[2]]['version'];
368 $new = $products[$new[0]]['title'] . '/' . ($new[1] ? $products[$new[1]]['title'] . '/' : '') . $versions[$new[2]]['version'];
370 foreach ($userlist as $userid => $user)
372 $email = get_email_text('notice_product');
373 $this->notices
["$user[userid]"][] = sprintf($email['part'], $old, $new);
378 * Sends the appropriate users information about a new comment being
379 * posted to the bug report.
381 * @param array CommentAPI->values array
383 public function sendNewCommentNotice($comment)
385 $userlist = $this->_fetchUsersWithOnBit('newcomment');
386 foreach ($userlist as $userid => $user)
388 $user = construct_user_display(bugdar
::$userinfo, false);
389 $date = BSApp
::$date->format(bugdar
::$options['dateformat'], $comment['dateline']);
391 $email = get_email_text('notice_comment');
392 $this->notices
["$userid"][] = sprintf($email['part'], $user, $date, $comment['comment']);
397 * A notice for an individual field changing.
399 * @param string Field name
400 * @param mixed Original value
401 * @param mixed Modified value
403 private function _noticeOtherChange($name, $old, $new)
405 $userlist = $this->_fetchUsersWithOnBit('otherfield');
406 foreach ($userlist as $userid => $user)
408 $email = get_email_text('notice_other');
409 $this->notices["$user[userid
]"][] = sprintf($email['part'], $name, $old, $new);
414 * Sends appropriate users a notice when a new attachment has been
417 * @param array AttachmentAPI->values array
418 * @param array List of all attachments made obsolete
419 * @param array Newly-inserted attachment ID
421 public function sendNewAttachmentNotice($attachment, $obsolete, $id)
423 $userlist = $this->_fetchUsersWithOnBit('newattachment');
424 foreach ($userlist as $userid => $user)
426 $user = construct_user_display(bugdar::$userinfo, false);
427 $obsoletes = implode(', ', (array)$obsolete);
429 $email = get_email_text('notice_attachment');
430 $this->notices["$userid"][] = sprintf($email['part'], $user, $attachment['filename'], $attachment['description'], $attachment['filesize'], $obsoletes, bugdar
::$options['trackerurl'], $attachment['attachmentid']);
435 * Sends a new bug notification notice to all those who have the option
436 * turned no. This does not use fetchUsersWithOnBit() because a
437 * query is more effective.
439 * @param array Bug values array
440 * @param array Comment values array
442 public function sendNewBugNotice($bug, $comment)
444 $userinfo = BSApp
::$db->query("
445 SELECT user.*, useremail.*
446 FROM " . TABLE_PREFIX
. "useremail AS useremail
447 LEFT JOIN " . TABLE_PREFIX
. "user AS user
448 ON (user.userid = useremail.userid)
449 WHERE useremail.relation = 0
450 AND useremail.mask & " . bugdar
::$emailOptions['notifications']['newbug'] . "
452 foreach ($userinfo as $userInfo)
454 if (!is_array($this->users
["$userInfo[userid]"]))
456 $user = construct_user_display(bugdar
::$userinfo, false);
457 $this->users
["$userInfo[userid]"] = $userInfo;
458 $product = bugdar
::$datastore['product']["$bug[product]"]['title'] . '/' . ($bug['component'] ? bugdar
::$datastore['product']["$bug[component]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$bug[version]"]['version'];
460 $email = get_email_text('notice_new_bug');
461 $this->notices
["$userInfo[userid]"][] = sprintf($email['part'], $bug['bugid'], $bug['summary'], $user, $product, $comment['comment']);
462 unset($this->users
["$userInfo[userid]"]['mask'], $this->users
["$userInfo[userid]"]['relation']);
464 $this->users
["$userInfo[userid]"]['options']["$userInfo[relation]"] = $userInfo['mask'];
469 * Generates an array of users who have a given email notification flag
470 * turned on in their bitfields.
472 * @param string Notification bitfield name
474 * @return array Array of users and their data
476 private function _fetchUsersWithOnBit($bitname)
480 foreach ($this->users
as $user)
482 foreach (bugdar
::$emailOptions['relations'] as $name => $bit)
484 if (in_array($user['userid'], $this->roles
["$name"]) && $user['options']["$bit"] & bugdar
::$emailOptions['notifications']["$bitname"])
486 $idlist[] = $user['userid'];
491 $masters = array_unique($idlist);
494 foreach ($masters as $userid)
496 $return["$userid"] = &$this->users
["$userid"];
503 * Compiles and sends the actual emails to users.
505 public function finalize()
507 // get the current bug for permissions checks
508 $bug = BSApp::$db->queryFirst("SELECT
* FROM
" . TABLE_PREFIX . "bug WHERE bugid
= " . $this->bug['bugid']);
509 foreach ($this->notices as $userid => $noticelist)
511 if ($userid == bugdar::$userinfo['userid'])
513 BSApp::debug("skipping user
$userid because they
're the one doing the thing");
517 // we wouldn't want people who favorite bugs getting hidden notices
518 if (!check_bug_permissions($bug, $this->users
["$userid"]))
520 BSApp::debug("skipping user
$userid ({$this
->users
[$userid
]['email']}) because of permissions
");
524 $parts = implode("\n\n
", $noticelist);
526 $email = get_email_text('bug_notification');
528 $body = sprintf($email['bodyText'], $this->users[$userid]['displayname'], bugdar::$options['trackertitle'], $this->bug['summary'], $this->bug['bugid'], bugdar::$options['trackerurl'], $parts);
530 $mail = new BSMail();
531 $mail->setSubject(sprintf($email['subject'], bugdar::$options['trackertitle'], $this->bug['summary']));
532 $mail->setBodyText($body);
533 $mail->setFromAddress(MAIL_FROM_ADDRESS);
534 $mail->setFromName(MAIL_FROM_NAME);
536 if (!empty($this->users["$userid"]['email']))
538 $mail->send($this->users
[$userid]['email'], $this->users
[$userid]['displayname']);
542 BSApp
::debug("not sending an email to " . $userid . " because they don't have one?");
548 * Returns the locale name from a given user ID
550 * @param integer User ID
552 * @return string Locale
554 private function _localeFromUserId($userid)
556 $langcode = bugdar
::$datastore['language'][$this->users
[$userid]['languageid']]['langcode'];
559 $langcode = bugdar
::$datastore['language'][bugdar
::$options['defaultlanguage']]['langcode'];