]>
src.bluestatic.org Git - bugdar.git/blob - includes/class_notification.php
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 * @param array Original custom fields data
198 * @param array Modified custom fields data
200 function send_bug_changes_notice ( $original , $modified )
202 if (! isset ( $this- > modified
[ 'bugid' ]))
207 if ( $this- > original
[ 'assignedto' ] != $this- > modified
[ 'assignedto' ])
209 if ( $this- > original
[ 'assignedto' ] != '' )
211 $this- > notice_no_longer_assigned ( $this- > original
[ 'assignedto' ]);
213 if ( $this- > modified
[ 'assignedto' ] != '' )
215 $this- > notice_now_assigned ( $this- > modified
[ 'assignedto' ]);
219 if ( $this- > original
[ 'status' ] != $this- > modified
[ 'status' ])
221 $this- > notice_status_change ( $this- > original
[ 'status' ], $this- > modified
[ 'status' ]);
223 if ( $this- > original
[ 'resolution' ] != $this- > modified
[ 'resoultion' ])
225 $this- > notice_resolution_change ( $this- > original
[ 'resolution' ], $this- > modified
[ 'resolution' ]);
228 if ( $this- > original
[ 'duplicates' ] != $this- > modified
[ 'duplicates' ])
230 $this- > notice_duplicates_change ( $this- > original
[ 'duplicates' ], $this- > modified
[ 'duplicates' ]);
235 'severity' => 'severityid' ,
241 'priority' => 'priorityid'
243 foreach ( $dofields AS $field => $lookup )
245 if ( $this- > original
[ " $field" ] != $this- >modified[" $field" ])
247 $this- > notice_other_change ( $field , $this- > original
[ " $field" ], $this- >modified[" $field" ]);
251 foreach ( $modified AS $field => $value )
253 if ( $field == 'bugid' )
257 if ( $original [ " $field" ] != $modified [" $field" ])
259 $this- > notice_other_change ( $field , $original [ " $field" ], $modified [" $field" ]);
264 // ###################################################################
266 * Sends an email to the specified user ID that they are no longer the
267 * person assigned to the bug.
271 * @param integer User ID to send to
273 function notice_no_longer_assigned ( $userid )
275 if ( $this- > users
[ " $userid" ]['options'][0] & $this- >registry->emailoptions['notifications']['assignedto'] AND in_array( $userid , $this- >roles['-notapplicable-']))
277 $this- >notices[" $userid" ][] = sprintf (
278 $this- > registry
-> lang
-> string ( 'You are no longer assigned to this bug, per %1 $s\' s changes.' ),
280 construct_user_display ( $this- > registry
-> userinfo
, false )
285 // ###################################################################
287 * Informs the user that they have been made the assignee of the bug.
291 * @param integer User ID
293 function notice_now_assigned ( $userid )
295 if ( $this- > users
[ " $userid" ]['options'][0] & $this- >registry->emailoptions['notifications']['assignedto'] AND in_array( $userid , $this- >roles['-notapplicable-']))
297 $this- >notices[" $userid" ][] = sprintf (
298 $this- > registry
-> lang
-> string ( 'You have been assigned to this bug by %1 $s .' ),
300 construct_user_display ( $this- > registry
-> userinfo
, false )
305 // ###################################################################
307 * Sends a message to inform users that the status has changed.
311 * @param integer Old status
312 * @param integer New status
314 function notice_status_change ( $old , $new )
316 $userlist = $this- > fetch_users_with_on_bit ( 'statusresolve' );
317 foreach ( $userlist AS $userid => $user )
319 $this- > notices
[ " $user [userid]" ][] = sprintf (
320 $this- > registry
-> lang
-> string ( 'The status field has changed from "%1 $s" to "%2 $s" .' ),
322 $this- > registry
-> datastore
[ 'status' ][ " $old" ]['status'],
323 $this- >registry->datastore['status'][" $new" ][ 'status' ]
328 // ###################################################################
330 * Sends an email to inform users that the resolution has changed.
334 * @param integer Old resolution
335 * @param integer New resolution
337 function notice_resolution_change ( $old , $new )
339 $userlist = $this- > fetch_users_with_on_bit ( 'statusresolve' );
340 foreach ( $userlist AS $userid => $user )
342 $this- > notices
[ " $user [userid]" ][] = sprintf (
343 $this- > registry
-> lang
-> string ( 'The resolution field has changed from "%1 $s" to "%2 $s" .' ),
345 $this- > registry
-> datastore
[ 'resolution' ][ " $old" ]['resolution'],
346 $this- >registry->datastore['resolution'][" $new" ][ 'resolution' ]
351 // ###################################################################
353 * Informs users that the duplicates list has changed.
357 * @param string Old duplicates list
358 * @param string New duplicates list
360 function notice_duplicates_change ( $old , $new )
362 $userlist = $this- > fetch_useres_with_on_bit ( 'duplicates' );
363 foreach ( $userlist AS $userid => $user )
365 $this- > notices
[ " $user [userid]" ][] = sprintf (
366 $this- > registry
-> lang
-> string ( 'The duplicates list has changed from "%1 $s" to %2 $s" .' ),
374 // ###################################################################
376 * Sends the appropriate users information about a new comment being
377 * posted to the bug report.
381 * @param array CommentAPI->values array
383 function send_new_comment_notice ( $comment )
385 $userlist = $this- > fetch_users_with_on_bit ( 'newcomment' );
386 foreach ( $userlist AS $userid => $user )
388 $this- > notices
[ " $user [userid]" ][] = sprintf (
389 $this- > registry
-> lang
-> string ( 'The following comment was added by %1 $s on %2 $s :
390 ============================================
392 ============================================' ),
394 construct_user_display ( $this- > registry
-> userinfo
, false ),
395 $this- > registry
-> modules
[ 'date' ]-> format ( $this- > registry
-> options
[ 'dateformat' ], $comment [ 'dateline' ]),
401 // ###################################################################
403 * A notice for an individual field changing.
407 * @param string Field name
408 * @param mixed Original value
409 * @param mixed Modified value
411 function notice_other_change ( $name , $old , $new )
413 $userlist = $this- > fetch_users_with_on_bit ( 'otherfield' );
414 foreach ( $userlist AS $userid => $user )
416 $this- > notices
[ " $user [userid]" ][] = sprintf (
417 $this- > registry
-> lang
-> string ( 'The %1 $s field changed from "%2 $s" to "%3 $s" .' ),
426 // ###################################################################
428 * Sends appropriate users a notice when a new attachment has been
433 * @param array AttachmentAPI->values array
434 * @param array List of all attachments made obsolete
435 * @param array Newly-inserted attachment ID
437 function send_new_attachment_notice ( $attachment , $obsolete , $id )
439 $userlist = $this- > fetch_users_with_on_bit ( 'newattachment' );
440 foreach ( $userlist AS $userid => $user )
442 $this- > notices
[ " $userid" ][] = sprintf(
443 $this- >registry->lang->string('%1 $s has uploaded a new attachment:
444 ============================================
447 File size: %4 $s Bytes
450 ============================================'),
452 construct_user_display( $this- >registry->userinfo, false),
453 $attachment ['filename'],
454 $attachment ['description'],
455 $attachment ['filesize'],
456 implode(', ', (array) $obsolete ),
457 $this- >registry->options['trackerurl'] . '/viewattachment.php?attachmentid=' . $id
462 // ###################################################################
464 * Generates an array of users who have a given email notification flag
465 * turned on in their bitfields.
469 * @param string Notification bitfield name
471 * @return array Array of users and their data
473 function fetch_users_with_on_bit( $bitname )
477 foreach ( $this- >users AS $user )
479 foreach ( $this- >registry->emailoptions['relations'] AS $name => $bit )
481 if (in_array( $user ['userid'], $this- >roles[" $name" ]) AND $user [ 'options' ][ " $bit" ] & $this- >registry->emailoptions['notifications'][" $bitname" ])
483 $idlist [] = $user [ 'userid' ];
488 $masters = array_unique ( $idlist );
491 foreach ( $masters AS $userid )
493 $return [ " $userid" ] =& $this- >users[" $userid" ];
500 /*=====================================================================*\
501 || ###################################################################
504 || ###################################################################
505 \*=====================================================================*/