2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 [#]gpl[#] 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.
28 * @author Iris Studios, Inc.
29 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 'favourite' => 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;
133 $this->original
= $original;
134 $this->modified
= $modified;
136 $this->roles
['-notapplicable-'] = (sizeof($modified) > 0 ?
array($original['assignedto'], $modified['assignedto']) : array($original['assignedto']));
137 $this->roles
['reporter'] = array($original['userid']);
138 $this->roles
['assignee'] = (sizeof($modified) > 0 ?
array($modified['assignedto']) : array($original['assignedto']));
140 $this->fetch_user_cache();
143 // ###################################################################
145 * Fetches all the users who could be related to the bug and sticks
146 * their information into an array.
150 function fetch_user_cache()
152 $favourites = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "favourite WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
153 while ($fav = $this->registry
->db
->fetch_array($favourites))
155 $this->roles
['favourite']["$fav[userid]"] = $fav['userid'];
158 $voters = $this->registry
->db
->query_first("SELECT userids FROM " . TABLE_PREFIX
. "vote WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
159 $this->roles
['voter'] = preg_split('#,#', $voters['userids'], 0, PREG_SPLIT_NO_EMPTY
);
161 $commenters = $this->registry
->db
->query("SELECT userid FROM " . TABLE_PREFIX
. "comment WHERE bugid = " . $this->registry
->clean($this->bug
['bugid'], TYPE_UINT
));
162 while ($comment = $this->registry
->db
->fetch_array($commenters))
164 $this->roles
['commenter']["$comment[userid]"] = $comment['userid'];
167 $masterids = array_merge($this->roles
['-notapplicable-'], $this->roles
['reporter'], $this->roles
['assignee'], $this->roles
['favourite'], $this->roles
['voter'], $this->roles
['commenter']);
168 $masterids = array_unique($masterids);
170 $userinfo = $this->registry
->db
->query("
171 SELECT user.*, useremail.*
172 FROM " . TABLE_PREFIX
. "useremail AS useremail
173 LEFT JOIN " . TABLE_PREFIX
. "user AS user
174 ON (user.userid = useremail.userid)
175 WHERE useremail.userid IN (" . implode(',', $masterids) . ")
177 while ($user = $this->registry
->db
->fetch_array($userinfo))
179 if (!is_array($this->users
["$user[userid]"]))
181 $this->users
["$user[userid]"] = $user;
182 unset($this->users
["$user[userid]"]['mask'], $this->users
["$user[userid]"]['relation']);
184 $this->users
["$user[userid]"]['options']["$user[relation]"] = $user['mask'];
188 // ###################################################################
190 * Sends the appropriate emails for changes to bugs. This function
191 * works a lot like the Logging class by taking BugAPI->objdata and
192 * BugAPI->values and then comparing the two arries and sending emails
193 * with the differences.
197 function send_bug_changes_notice()
199 if (!isset($this->modified
['bugid']))
204 if ($this->original
['assignedto'] != $this->modified
['assignedto'])
206 if ($this->original
['assignedto'] != '')
208 $this->notice_no_longer_assigned($this->original
['assignedto']);
210 if ($this->modified
['assignedto'] != '')
212 $this->notice_now_assigned($this->modified
['assignedto']);
216 if ($this->original
['status'] != $this->modified
['status'])
218 $this->notice_status_change($this->original
['status'], $this->modified
['status']);
220 if ($this->original
['resolution'] != $this->modified
['resoultion'])
222 $this->notice_resolution_change($this->original
['resolution'], $this->modified
['resolution']);
225 if ($this->original
['duplicates'] != $this->modified
['duplicates'])
227 $this->notice_duplicates_change($this->original
['duplicates'], $this->modified
['duplicates']);
231 // ###################################################################
233 * Sends an email to the specified user ID that they are no longer the
234 * person assigned to the bug.
238 * @param integer User ID to send to
240 function notice_no_longer_assigned($userid)
242 if ($this->users
["$userid"]['options'][0] & $this->registry
->emailoptions
['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
244 $this->notices
["$userid"][] = sprintf(
245 $this->registry
->lang
->string('You are no longer assigned to this bug, per %1$s\'s changes.'),
247 construct_user_display($this->registry
->userinfo
, false
)
252 // ###################################################################
254 * Informs the user that they have been made the assignee of the bug.
258 * @param integer User ID
260 function notice_now_assigned($userid)
262 if ($this->users
["$userid"]['options'][0] & $this->registry
->emailoptions
['notifications']['assignedto'] AND in_array($userid, $this->roles
['-notapplicable-']))
264 $this->notices
["$userid"][] = sprintf(
265 $this->registry
->lang
->string('You have been assigned to this bug by %1$s.'),
267 construct_user_display($this->registry
->userinfo
, false
)
272 // ###################################################################
274 * Sends a message to inform users that the status has changed.
278 * @param integer Old status
279 * @param integer New status
281 function notice_status_change($old, $new)
283 $userlist = $this->fetch_users_with_on_bit('statusresolve');
284 foreach ($userlist AS $userid => $user)
286 $this->notices
["$user[userid]"][] = sprintf(
287 $this->registry
->lang
->string('The status field has changed from "%1$s" to "%2$s".'),
289 $this->registry
->datastore
['status']["$old"]['status'],
290 $this->registry
->datastore
['status']["$new"]['status']
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 $this->notices
["$user[userid]"][] = sprintf(
310 $this->registry
->lang
->string('The resolution field has changed from "%1$s" to "%2$s".'),
312 $this->registry
->datastore
['resolution']["$old"]['resolution'],
313 $this->registry
->datastore
['resolution']["$new"]['resolution']
318 // ###################################################################
320 * Informs users that the duplicates list has changed.
324 * @param string Old duplicates list
325 * @param string New duplicates list
327 function notice_duplicates_change($old, $new)
332 // ###################################################################
334 * Sends the appropriate users information about a new comment being
335 * posted to the bug report.
339 * @param array CommentAPI->values array
341 function send_new_comment_notice($comment)
343 $userlist = $this->fetch_users_with_on_bit('newcomment');
344 foreach ($userlist AS $userid => $user)
346 $this->notices
["$user[userid]"][] = sprintf(
347 $this->registry
->lang
->string('The following comment was added by %1$s on %2$s:
348 ============================================
350 ============================================'),
352 construct_user_display($this->registry
->userinfo
, false
),
353 $this->registry
->modules
['date']->format($this->registry
->options
['dateformat'], $comment['dateline']),
359 // ###################################################################
361 * Generates an array of users who have a given email notification flag
362 * turned on in their bitfields.
366 * @param string Notification bitfield name
368 * @return array Array of users and their data
370 function fetch_users_with_on_bit($bitname)
374 foreach ($this->users
AS $user)
376 foreach ($this->registry
->emailoptions
['relations'] AS $name => $bit)
378 if (in_array($user['userid'], $this->roles
["$name"]) AND $user['options']["$bit"] & $this->registry
->emailoptions
['notifications']["$bitname"])
380 $idlist[] = $user['userid'];
385 $masters = array_unique($idlist);
388 foreach ($masters AS $userid)
390 $return["$userid"] =& $this->users
["$userid"];
397 /*=====================================================================*\
398 || ###################################################################
401 || ###################################################################
402 \*=====================================================================*/