31ce4a0148734b4a2d71d48dfe4ac34949713358
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
48 var $original = array();
55 var $modified = array();
58 * Role list: a list of user IDs with their relations to the bug
63 '-notapplicable-' => array(),
64 'reporter' => array(),
65 'assignee' => array(),
66 'favorite' => array(),
68 'commenter' => array()
79 * A list of notices per-user that are combined together in NotificationCenter::finalize()
83 var $notices = array();
85 // ###################################################################
87 * Sets the bug data so that all methods in this class have access to
88 * it when sending emails.
92 * @param array Original bug data
93 * @param array Modified bug data
95 function set_bug_data($original, $modified = array())
97 if (sizeof($modified) > 0)
99 $this->bug
= $modified;
103 $this->bug
= $original;
106 $this->original
= $original;
107 $this->modified
= $modified;
109 $this->roles
['-notapplicable-'] = (sizeof($modified) > 0 ?
array($original['assignedto'], $modified['assignedto']) : array($original['assignedto']));
110 $this->roles
['reporter'] = array($original['userid']);
111 $this->roles
['assignee'][] = (sizeof($modified) > 0 ?
$modified['assignedto'] : $original['assignedto']);
113 $this->fetch_user_cache();
116 // ###################################################################
118 * Fetches all the users who could be related to the bug and sticks
119 * their information into an array.
123 function fetch_user_cache()
125 $newbuggers = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "useremail WHERE relation = " . bugdar
::$emailOptions['relations']['-notapplicable-'] . " AND mask & " . bugdar
::$emailOptions['notifications']['newbug']);
126 foreach ($newbuggers as $newbug)
128 $this->roles
['-notapplicable-']["$newbug[userid]"] = $newbug['userid'];
131 $favorites = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "favorite WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
132 foreach ($favorites as $fav)
134 $this->roles
['favorite']["$fav[userid]"] = $fav['userid'];
137 $voters = BSApp
::$db->queryFirst("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
138 $this->roles
['voter'] = preg_split('#,#', $voters['userids'], 0, PREG_SPLIT_NO_EMPTY
);
140 $commenters = BSApp
::$db->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . BSApp
::$input->clean($this->bug
['bugid'], TYPE_UINT
));
141 foreach ($commenters as $comment)
143 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
146 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favorite'], $this->roles
['voter'], $this->roles
['commenter']);
147 $masterids = BSFunctions
::array_strip_empty(array_unique($masterids));
149 if (is_array($masterids) AND sizeof($masterids) > 0)
151 $userinfo = BSApp
::$db->query("
152 SELECT user.*, useremail.*
153 FROM " . TABLE_PREFIX
. "useremail AS useremail
154 LEFT JOIN " . TABLE_PREFIX
. "user AS user
155 ON (user.userid = useremail.userid)
156 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
158 foreach ($userinfo as $user)
160 if (!is_array($this->users
["$user[userid]"]))
162 $this->users
["$user[userid]"] = $user;
163 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
165 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
170 // ###################################################################
172 * Sends the appropriate emails for changes to bugs. This function
173 * works a lot like the Logging class by taking BugAPI->record and
174 * BugAPI->values and then comparing the two arries and sending emails
175 * with the differences.
179 function send_bug_changes_notice()
181 if (!isset($this->modified
['bugid']))
186 // fields with custom mask information
187 if ($this->original
['assignedto'] != $this->modified
['assignedto'])
189 if ($this->original
['assignedto'] != '')
191 $this->notice_no_longer_assigned($this->original
['assignedto']);
193 if ($this->modified
['assignedto'] != '')
195 $this->notice_now_assigned($this->modified
['assignedto']);
198 if ($this->original
['status'] != $this->modified
['status'])
200 $this->notice_status_change($this->original
['status'], $this->modified
['status']);
202 if ($this->original
['resolution'] != $this->modified
['resolution'])
204 $this->notice_resolution_change($this->original
['resolution'], $this->modified
['resolution']);
206 if ($this->original
['duplicates'] != $this->modified
['duplicates'])
208 $this->notice_duplicates_change($this->original
['duplicates'], $this->modified
['duplicates']);
211 // other standard fields that don't have custom masks
212 if ($this->original
['severity'] != $this->modified
['severity'])
214 $this->notice_severity_change($this->original
['severity'], $this->modified
['severity']);
216 if ($this->original
['priority'] != $this->modified
['priority'])
218 $this->notice_priority_change($this->original
['priority'], $this->modified
['priority']);
220 if (($this->original
['product'] != $this->modified
['product']) OR ($this->original
['component'] != $this->modified
['component']) OR ($this->original
['version'] != $this->modified
['version']))
222 $this->notice_pcv_change(array($this->original
['product'], $this->original
['component'], $this->original
['version']), array($this->modified
['product'], $this->modified
['component'], $this->modified
['version']));
230 foreach ($dofields AS $field => $lookup)
232 if ($this->original
["$field"] != $this->modified
["$field"])
234 $this->notice_other_change($field, $this->original
["$field"], $this->modified
["$field"]);
239 // ###################################################################
241 * Sends an email to the specified user ID that they are no longer the
242 * person assigned to the bug.
246 * @param integer User ID to send to
248 function notice_no_longer_assigned($userid)
250 if ($this->users
["$userid"]['options'][0] & bugdar
::$emailOptions['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
252 $user = construct_user_display(bugdar
::$userinfo, false
);
253 eval('$part = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_unassigned.part', $this->_localeFromUserId($userid))) . '";');
254 $this->notices
["$userid"][] = $part;
258 // ###################################################################
260 * Informs the user that they have been made the assignee of the bug.
264 * @param integer User ID
266 function notice_now_assigned($userid)
268 if ($this->users
["$userid"]['options'][0] & bugdar
::$emailOptions['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
270 $user = construct_user_display(bugdar
::$userinfo, false
);
271 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_assigned.part', $this->_localeFromUserId($userid))) . '";');
272 $this->notices
["$userid"][] = $email;
276 // ###################################################################
278 * Sends a message to inform users that the status has changed.
282 * @param integer Old status
283 * @param integer New status
285 function notice_status_change($old, $new)
287 $userlist = $this->fetch_users_with_on_bit('statusresolve');
288 foreach ($userlist AS $userid => $user)
290 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_status.part', $this->_localeFromUserId($userid))) . '";');
291 $this->notices
["$user[userid]"][] = $email;
295 // ###################################################################
297 * Sends an email to inform users that the resolution has changed.
301 * @param integer Old resolution
302 * @param integer New resolution
304 function notice_resolution_change($old, $new)
306 $userlist = $this->fetch_users_with_on_bit('statusresolve');
307 foreach ($userlist AS $userid => $user)
309 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_resolution.part', $this->_localeFromUserId($userid))) . '";');
310 $this->notices
["$user[userid]"][] = $email;
314 // ###################################################################
316 * Informs users that the duplicates list has changed.
320 * @param string Old duplicates list
321 * @param string New duplicates list
323 function notice_duplicates_change($old, $new)
325 $userlist = $this->fetch_useres_with_on_bit('duplicates');
326 foreach ($userlist AS $userid => $user)
328 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_duplicates.part', $this->_localeFromUserId($userid))) . '";');
329 $this->notices
["$user[userid]"][] = $email;
333 // ###################################################################
335 * Sends an email to inform users that the severity has changed.
339 * @param integer Old severity
340 * @param integer New severity
342 function notice_severity_change($old, $new)
344 $userlist = $this->fetch_users_with_on_bit('otherfield');
345 foreach ($userlist AS $userid => $user)
347 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_severity.part', $this->_localeFromUserId($userid))) . '";');
348 $this->notices
["$user[userid]"][] = $email;
352 // ###################################################################
354 * Informs users that the priority changed.
358 * @param integer Old priority
359 * @param integer New priority
361 function notice_priority_change($old, $new)
363 $userlist = $this->fetch_users_with_on_bit('otherfield');
364 foreach ($userlist AS $userid => $user)
366 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_priority.part', $this->_localeFromUserId($userid))) . '";');
367 $this->notices
["$user[userid]"][] = $email;
371 // ###################################################################
373 * Sends an email telling users that the product, component, or version
374 * has changed. This is done all at once because you really need to see
375 * the whole thing in the notice.
379 * @param array Original PCV
380 * @param array Modified PCV
382 function notice_pcv_change($old, $new)
384 $userlist = $this->fetch_users_with_on_bit('otherfield');
386 $old = bugdar
::$datastore['product']["$old[0]"]['title'] . '/' . ($old[1] ? bugdar
::$datastore['product']["$old[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$old[2]"]['version'];
387 $new = bugdar
::$datastore['product']["$new[0]"]['title'] . '/' . ($new[1] ? bugdar
::$datastore['product']["$new[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$new[2]"]['version'];
389 foreach ($userlist AS $userid => $user)
391 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_product.part', $this->_localeFromUserId($userid))) . '";');
392 $this->notices
["$user[userid]"][] = $email;
396 // ###################################################################
398 * Sends the appropriate users information about a new comment being
399 * posted to the bug report.
403 * @param array CommentAPI->values array
405 function send_new_comment_notice($comment)
407 $userlist = $this->fetch_users_with_on_bit('newcomment');
408 foreach ($userlist AS $userid => $user)
410 $user = construct_user_display(bugdar
::$userinfo, false
);
411 $date = $this->registry
->modules
['date']->format(bugdar
::$options['dateformat'], $comment['dateline']);
413 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_comment.part', $this->_localeFromUserId($userid))) . '";');
414 $this->notices
["$userid"][] = $email;
418 // ###################################################################
420 * A notice for an individual field changing.
424 * @param string Field name
425 * @param mixed Original value
426 * @param mixed Modified value
428 function notice_other_change($name, $old, $new)
430 $userlist = $this->fetch_users_with_on_bit('otherfield');
431 foreach ($userlist AS $userid => $user)
433 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_other.part', $this->_localeFromUserId($userid))) . '";');
434 $this->notices
["$user[userid]"][] = $email;
438 // ###################################################################
440 * Sends appropriate users a notice when a new attachment has been
445 * @param array AttachmentAPI->values array
446 * @param array List of all attachments made obsolete
447 * @param array Newly-inserted attachment ID
449 function send_new_attachment_notice($attachment, $obsolete, $id)
451 $userlist = $this->fetch_users_with_on_bit('newattachment');
452 foreach ($userlist AS $userid => $user)
454 $user = construct_user_display(bugdar
::$userinfo, false
);
455 $obsoletes = implode(', ', (array)$obsolete);
457 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_attachment.part', $this->_localeFromUserId($userid))) . '";');
458 $this->notices
["$userid"][] = $email;
462 // ###################################################################
464 * Sends a new bug notification notice to all those who have the option
465 * turned no. This does not use fetch_users_with_on_bit() because a
466 * query is more effective.
470 * @param array Bug values array
471 * @param array Comment values array
473 function send_new_bug_notice($bug, $comment)
475 $userinfo = BSApp
::$db->query("
476 SELECT user.*, useremail.*
477 FROM " . TABLE_PREFIX
. "useremail AS useremail
478 LEFT JOIN " . TABLE_PREFIX
. "user AS user
479 ON (user.userid = useremail.userid)
480 WHERE useremail.relation = 0
481 AND useremail.mask & " . bugdar
::$emailOptions['notifications']['newbug'] . "
483 foreach ($userinfo as $userInfo)
485 if (!is_array($this->users
["$userInfo[userid]"]))
487 $user = construct_user_display(bugdar
::$userinfo, false
);
488 $this->users
["$userInfo[userid]"] = $userInfo;
489 $product = bugdar
::$datastore['product']["$bug[product]"]['title'] . '/' . ($bug['component'] ? bugdar
::$datastore['product']["$bug[component]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$bug[version]"]['version'];
490 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_new_bug.part', $this->_localeFromUserId($userInfo['userid']))) . '";');
491 $this->notices
["$userInfo[userid]"][] = $email;
492 unset($this->users
["$userInfo[userid]"]['mask'], $this->users
["$userInfo[userid]"]['relation']);
494 $this->users
["$userInfo[userid]"]['options']["$userInfo[relation]"] = $userInfo['mask'];
498 // ###################################################################
500 * Generates an array of users who have a given email notification flag
501 * turned on in their bitfields.
505 * @param string Notification bitfield name
507 * @return array Array of users and their data
509 function fetch_users_with_on_bit($bitname)
513 foreach ($this->users
AS $user)
515 foreach (bugdar
::$emailOptions['relations'] AS $name => $bit)
517 if (in_array($user['userid'], $this->roles
["$name"]) AND $user['options']["$bit"] & bugdar
::$emailOptions['notifications']["$bitname"])
519 $idlist[] = $user['userid'];
524 $masters = array_unique($idlist);
527 foreach ($masters AS $userid)
529 $return["$userid"] =& $this->users
["$userid"];
535 // ###################################################################
537 * Compiles and sends the actual emails to users.
543 // get the current bug for permissions checks
544 $bug = BSApp
::$db->query_first("SELECT * FROM " . TABLE_PREFIX
. "bug WHERE bugid = " . $this->bug
['bugid']);
545 foreach ($this->notices
AS $userid => $noticelist)
547 if ($userid == bugdar
::$userinfo['userid'])
549 $this->registry
->debug("skipping user $userid because they're the one doing the thing");
553 // we wouldn't want people who favorite bugs getting hidden notices
554 if (!check_bug_permissions($bug, $this->users
["$userid"]))
556 $this->registry
->debug("skipping user $userid ({$this->users[$userid]['email']}) because of permissions");
560 $parts = implode("\n\n", $noticelist);
562 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('bugnotification.xml', $this->_localeFromUserId($userid))) . '";');
563 $email = $this->registry
->xml
->parse($email, true
);
564 $this->registry
->mail
->setSubject($email['email']['subject']['value']);
565 $this->registry
->mail
->setBodyText($email['email']['bodyText']['value']);
567 if (!empty($this->users
["$userid"]['email']))
569 $this->registry
->mail
->send($this->users
["$userid"]['email'], $this->users
["$userid"]['displayname']);
573 $this->registry
->debug("not sending an email to " . $userid . " because they don't have one?");
578 // ###################################################################
580 * Returns the locale name from a given user ID
582 * @param integer User ID
584 * @return string Locale
586 function _localeFromUserId($userid)
588 $langcode = bugdar
::$datastore['language'][$this->users
[$userid]['languageid']]['langcode'];
591 $langcode = bugdar
::$datastore['language'][bugdar
::$options['defaultlanguage']]['langcode'];
597 /*=====================================================================*\
598 || ###################################################################
601 || ###################################################################
602 \*=====================================================================*/