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 * Global bugsys registry
65 * Role list: a list of user IDs with their relations to the bug
70 '-notapplicable-' => array(),
71 'reporter' => array(),
72 'assignee' => array(),
73 'favorite' => array(),
75 'commenter' => array()
86 * A list of notices per-user that are combined together in NotificationCenter::finalize()
90 var $notices = array();
92 // ###################################################################
94 * Constructor: set database objects
98 function __construct()
102 $this->registry
=& $bugsys;
105 // ###################################################################
107 * (PHP 4) Constructor
111 function NotificationCenter()
113 $this->__construct();
116 // ###################################################################
118 * Sets the bug data so that all methods in this class have access to
119 * it when sending emails.
123 * @param array Original bug data
124 * @param array Modified bug data
126 function set_bug_data($original, $modified = array())
128 if (sizeof($modified) > 0)
130 $this->bug
= $modified;
134 $this->bug
= $original;
137 $this->original
= $original;
138 $this->modified
= $modified;
140 $this->roles
['-notapplicable-'] = (sizeof($modified) > 0 ? array($original['assignedto'], $modified['assignedto']) : array($original['assignedto']));
141 $this->roles
['reporter'] = array($original['userid']);
142 $this->roles
['assignee'][] = (sizeof($modified) > 0 ? $modified['assignedto'] : $original['assignedto']);
144 $this->fetch_user_cache();
147 // ###################################################################
149 * Fetches all the users who could be related to the bug and sticks
150 * their information into an array.
154 function fetch_user_cache()
156 $newbuggers = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "useremail WHERE relation = " . $this->registry
->emailoptions
['relations']['-notapplicable-'] . " AND mask & " . $this->registry
->emailoptions
['notifications']['newbug']);
157 foreach ($newbuggers as $newbug)
159 $this->roles
['-notapplicable-']["$newbug[userid]"] = $newbug['userid'];
162 $favorites = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "favorite WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
163 foreach ($favorites as $fav)
165 $this->roles
['favorite']["$fav[userid]"] = $fav['userid'];
168 $voters = $this->registry
->db
->query_first("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
169 $this->roles
['voter'] = preg_split('#,#', $voters['userids'], 0, PREG_SPLIT_NO_EMPTY
);
171 $commenters = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
172 foreach ($commenters as $comment)
174 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
177 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favorite'], $this->roles
['voter'], $this->roles
['commenter']);
178 $masterids = $this->registry
->funct
->array_strip_empty(array_unique($masterids));
180 if (is_array($masterids) AND sizeof($masterids) > 0)
182 $userinfo = $this->registry
->db
->query("
183 SELECT user.*, useremail.*
184 FROM " . TABLE_PREFIX
. "useremail AS useremail
185 LEFT JOIN " . TABLE_PREFIX
. "user AS user
186 ON (user.userid = useremail.userid)
187 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
189 foreach ($userinfo as $user)
191 if (!is_array($this->users
["$user[userid]"]))
193 $this->users
["$user[userid]"] = $user;
194 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
196 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
201 // ###################################################################
203 * Sends the appropriate emails for changes to bugs. This function
204 * works a lot like the Logging class by taking BugAPI->objdata and
205 * BugAPI->values and then comparing the two arries and sending emails
206 * with the differences.
210 function send_bug_changes_notice()
212 if (!isset($this->modified
['bugid']))
217 // fields with custom mask information
218 if ($this->original
['assignedto'] != $this->modified
['assignedto'])
220 if ($this->original
['assignedto'] != '')
222 $this->notice_no_longer_assigned($this->original
['assignedto']);
224 if ($this->modified
['assignedto'] != '')
226 $this->notice_now_assigned($this->modified
['assignedto']);
229 if ($this->original
['status'] != $this->modified
['status'])
231 $this->notice_status_change($this->original
['status'], $this->modified
['status']);
233 if ($this->original
['resolution'] != $this->modified
['resolution'])
235 $this->notice_resolution_change($this->original
['resolution'], $this->modified
['resolution']);
237 if ($this->original
['duplicates'] != $this->modified
['duplicates'])
239 $this->notice_duplicates_change($this->original
['duplicates'], $this->modified
['duplicates']);
242 // other standard fields that don't have custom masks
243 if ($this->original
['severity'] != $this->modified
['severity'])
245 $this->notice_severity_change($this->original
['severity'], $this->modified
['severity']);
247 if ($this->original
['priority'] != $this->modified
['priority'])
249 $this->notice_priority_change($this->original
['priority'], $this->modified
['priority']);
251 if (($this->original
['product'] != $this->modified
['product']) OR ($this->original
['component'] != $this->modified
['component']) OR ($this->original
['version'] != $this->modified
['version']))
253 $this->notice_pcv_change(array($this->original
['product'], $this->original
['component'], $this->original
['version']), array($this->modified
['product'], $this->modified
['component'], $this->modified
['version']));
261 foreach ($dofields AS $field => $lookup)
263 if ($this->original
["$field"] != $this->modified["$field"])
265 $this->notice_other_change($field, $this->original
["$field"], $this->modified["$field"]);
270 // ###################################################################
272 * Sends an email to the specified user ID that they are no longer the
273 * person assigned to the bug.
277 * @param integer User ID to send to
279 function notice_no_longer_assigned($userid)
281 if ($this->users
["$userid"]['options'][0] & $this->registry->emailoptions['notifications']['assignedto'] AND in_array($userid, $this->roles['-notapplicable-']))
283 $user = construct_user_display(bugdar::$userinfo, false);
284 eval('$part = "' . $this->registry->template->fetch(FetchEmailPath('notice_unassigned
.part
', $this->_localeFromUserId($userid))) . '";');
285 $this->notices["$userid"][] = $part;
289 // ###################################################################
291 * Informs the user that they have been made the assignee of the bug.
295 * @param integer User ID
297 function notice_now_assigned($userid)
299 if ($this->users
["$userid"]['options'][0] & $this->registry->emailoptions['notifications']['assignedto'] AND in_array($userid, $this->roles['-notapplicable-']))
301 $user = construct_user_display(bugdar::$userinfo, false);
302 eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_assigned
.part
', $this->_localeFromUserId($userid))) . '";');
303 $this->notices["$userid"][] = $email;
307 // ###################################################################
309 * Sends a message to inform users that the status has changed.
313 * @param integer Old status
314 * @param integer New status
316 function notice_status_change($old, $new)
318 $userlist = $this->fetch_users_with_on_bit('statusresolve');
319 foreach ($userlist AS $userid => $user)
321 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_status.part', $this->_localeFromUserId($userid))) . '";');
322 $this->notices
["$user[userid]"][] = $email;
326 // ###################################################################
328 * Sends an email to inform users that the resolution has changed.
332 * @param integer Old resolution
333 * @param integer New resolution
335 function notice_resolution_change($old, $new)
337 $userlist = $this->fetch_users_with_on_bit('statusresolve');
338 foreach ($userlist AS $userid => $user)
340 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_resolution.part', $this->_localeFromUserId($userid))) . '";');
341 $this->notices
["$user[userid]"][] = $email;
345 // ###################################################################
347 * Informs users that the duplicates list has changed.
351 * @param string Old duplicates list
352 * @param string New duplicates list
354 function notice_duplicates_change($old, $new)
356 $userlist = $this->fetch_useres_with_on_bit('duplicates');
357 foreach ($userlist AS $userid => $user)
359 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_duplicates.part', $this->_localeFromUserId($userid))) . '";');
360 $this->notices
["$user[userid]"][] = $email;
364 // ###################################################################
366 * Sends an email to inform users that the severity has changed.
370 * @param integer Old severity
371 * @param integer New severity
373 function notice_severity_change($old, $new)
375 $userlist = $this->fetch_users_with_on_bit('otherfield');
376 foreach ($userlist AS $userid => $user)
378 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_severity.part', $this->_localeFromUserId($userid))) . '";');
379 $this->notices
["$user[userid]"][] = $email;
383 // ###################################################################
385 * Informs users that the priority changed.
389 * @param integer Old priority
390 * @param integer New priority
392 function notice_priority_change($old, $new)
394 $userlist = $this->fetch_users_with_on_bit('otherfield');
395 foreach ($userlist AS $userid => $user)
397 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_priority.part', $this->_localeFromUserId($userid))) . '";');
398 $this->notices
["$user[userid]"][] = $email;
402 // ###################################################################
404 * Sends an email telling users that the product, component, or version
405 * has changed. This is done all at once because you really need to see
406 * the whole thing in the notice.
410 * @param array Original PCV
411 * @param array Modified PCV
413 function notice_pcv_change($old, $new)
415 $userlist = $this->fetch_users_with_on_bit('otherfield');
417 $old = bugdar
::$datastore['product']["$old[0]"]['title'] . '/' . ($old[1] ? bugdar
::$datastore['product']["$old[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$old[2]"]['version'];
418 $new = bugdar
::$datastore['product']["$new[0]"]['title'] . '/' . ($new[1] ? bugdar
::$datastore['product']["$new[1]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$new[2]"]['version'];
420 foreach ($userlist AS $userid => $user)
422 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_product.part', $this->_localeFromUserId($userid))) . '";');
423 $this->notices
["$user[userid]"][] = $email;
427 // ###################################################################
429 * Sends the appropriate users information about a new comment being
430 * posted to the bug report.
434 * @param array CommentAPI->values array
436 function send_new_comment_notice($comment)
438 $userlist = $this->fetch_users_with_on_bit('newcomment');
439 foreach ($userlist AS $userid => $user)
441 $user = construct_user_display(bugdar
::$userinfo, false);
442 $date = $this->registry
->modules
['date']->format(bugdar
::$options['dateformat'], $comment['dateline']);
444 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_comment.part', $this->_localeFromUserId($userid))) . '";');
445 $this->notices
["$userid"][] = $email;
449 // ###################################################################
451 * A notice for an individual field changing.
455 * @param string Field name
456 * @param mixed Original value
457 * @param mixed Modified value
459 function notice_other_change($name, $old, $new)
461 $userlist = $this->fetch_users_with_on_bit('otherfield');
462 foreach ($userlist AS $userid => $user)
464 eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_other
.part
', $this->_localeFromUserId($userid))) . '";');
465 $this->notices["$user[userid
]"][] = $email;
469 // ###################################################################
471 * Sends appropriate users a notice when a new attachment has been
476 * @param array AttachmentAPI->values array
477 * @param array List of all attachments made obsolete
478 * @param array Newly-inserted attachment ID
480 function send_new_attachment_notice($attachment, $obsolete, $id)
482 $userlist = $this->fetch_users_with_on_bit('newattachment');
483 foreach ($userlist AS $userid => $user)
485 $user = construct_user_display(bugdar::$userinfo, false);
486 $obsoletes = implode(', ', (array)$obsolete);
488 eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_attachment
.part
', $this->_localeFromUserId($userid))) . '";');
489 $this->notices["$userid"][] = $email;
493 // ###################################################################
495 * Sends a new bug notification notice to all those who have the option
496 * turned no. This does not use fetch_users_with_on_bit() because a
497 * query is more effective.
501 * @param array Bug values array
502 * @param array Comment values array
504 function send_new_bug_notice($bug, $comment)
506 $userinfo = $this->registry
->db
->query("
507 SELECT user.*, useremail.*
508 FROM " . TABLE_PREFIX
. "useremail AS useremail
509 LEFT JOIN " . TABLE_PREFIX
. "user AS user
510 ON (user.userid = useremail.userid)
511 WHERE useremail.relation = 0
512 AND useremail.mask & " . $this->registry
->emailoptions
['notifications']['newbug'] . "
514 foreach ($userinfo as $userInfo)
516 if (!is_array($this->users
["$userInfo[userid]"]))
518 $user = construct_user_display(bugdar
::$userinfo, false);
519 $this->users
["$userInfo[userid]"] = $userInfo;
520 $product = bugdar
::$datastore['product']["$bug[product]"]['title'] . '/' . ($bug['component'] ? bugdar
::$datastore['product']["$bug[component]"]['title'] . '/' : '') . bugdar
::$datastore['version']["$bug[version]"]['version'];
521 eval('$email = "' . $this->registry
->template
->fetch(FetchEmailPath('notice_new_bug.part', $this->_localeFromUserId($userInfo['userid']))) . '";');
522 $this->notices
["$userInfo[userid]"][] = $email;
523 unset($this->users
["$userInfo[userid]"]['mask'], $this->users
["$userInfo[userid]"]['relation']);
525 $this->users
["$userInfo[userid]"]['options']["$userInfo[relation]"] = $userInfo['mask'];
529 // ###################################################################
531 * Generates an array of users who have a given email notification flag
532 * turned on in their bitfields.
536 * @param string Notification bitfield name
538 * @return array Array of users and their data
540 function fetch_users_with_on_bit($bitname)
544 foreach ($this->users
AS $user)
546 foreach ($this->registry
->emailoptions
['relations'] AS $name => $bit)
548 if (in_array($user['userid'], $this->roles
["$name"]) AND $user['options']["$bit"] & $this->registry
->emailoptions
['notifications']["$bitname"])
550 $idlist[] = $user['userid'];
555 $masters = array_unique($idlist);
558 foreach ($masters AS $userid)
560 $return["$userid"] =& $this->users
["$userid"];
566 // ###################################################################
568 * Compiles and sends the actual emails to users.
574 // get the current bug for permissions checks
575 $bug = $this->registry->db->query_first("SELECT
* FROM
" . TABLE_PREFIX . "bug WHERE bugid
= " . $this->bug['bugid']);
576 foreach ($this->notices AS $userid => $noticelist)
578 if ($userid == bugdar::$userinfo['userid'])
580 $this->registry->debug("skipping user
$userid because they
're the one doing the thing");
584 // we wouldn't want people who favorite bugs getting hidden notices
585 if (!check_bug_permissions($bug, $this->users
["$userid"]))
587 $this->registry->debug("skipping user
$userid ({$this
->users
[$userid
]['email']}) because of permissions
");
591 $parts = implode("\n\n
", $noticelist);
593 eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('bugnotification
.xml
', $this->_localeFromUserId($userid))) . '";');
594 $email = $this->registry->xml->parse($email, true);
595 $this->registry->mail->setSubject($email['email']['subject']['value']);
596 $this->registry->mail->setBodyText($email['email']['bodyText']['value']);
598 if (!empty($this->users["$userid"]['email']))
600 $this->registry
->mail
->send($this->users
["$userid"]['email'], $this->users["$userid"]['displayname']);
604 $this->registry
->debug("not sending an email to " . $userid . " because they don't have one?");
609 // ###################################################################
611 * Returns the locale name from a given user ID
613 * @param integer User ID
615 * @return string Locale
617 function _localeFromUserId($userid)
619 $langcode = bugdar
::$datastore['language'][$this->users
[$userid]['languageid']]['langcode'];
622 $langcode = bugdar
::$datastore['language'][bugdar
::$options['defaultlanguage']]['langcode'];
628 /*=====================================================================*\
629 || ###################################################################
632 || ###################################################################
633 \*=====================================================================*/