From dd088bc16f1aa1fb3bb375d5e925a838ac154221 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 1 Sep 2008 11:51:49 -0400 Subject: [PATCH] PHP5-ing and refactoring class_notification.php --- attachment.php | 6 +- editreport.php | 8 +- includes/class_notification.php | 404 ++++++++++++++------------------ newreport.php | 4 +- 4 files changed, 182 insertions(+), 240 deletions(-) diff --git a/attachment.php b/attachment.php index 0af8c15..c96972a 100755 --- a/attachment.php +++ b/attachment.php @@ -56,7 +56,7 @@ if (!check_bug_permissions($bug)) require_once('./includes/class_logging.php'); $notif = new NotificationCenter(); -$notif->set_bug_data($bug); +$notif->setBugData($bug); // ################################################################### @@ -115,7 +115,7 @@ if ($_POST['do'] == 'insert') $obsoletes = $input->inputClean('obsoletes', TYPE_UINT); - $notif->send_new_attachment_notice($attachapi->values, $obsoletes, $attachapi->insertid); + $notif->sendNewAttachmentNotice($attachapi->values, $obsoletes, $attachapi->insertid); // mark obsoletes if (is_array($obsoletes) AND sizeof($obsoletes) > 0) @@ -143,7 +143,7 @@ if ($_POST['do'] == 'insert') $comment->set('dateline', $attachapi->values['dateline']); $comment->insert(); - $notif->send_new_comment_notice($comment->values); + $notif->sendNewCommentNotice($comment->values); } // update the last post data diff --git a/editreport.php b/editreport.php index ada44ad..2ca297c 100644 --- a/editreport.php +++ b/editreport.php @@ -151,8 +151,8 @@ if ($_POST['do'] == 'update') $comment->insert(); // we redefine the bug data later, but it needs to be here in order to generate the user list - $notif->set_bug_data($bugapi->record); - $notif->send_new_comment_notice($comment->values); + $notif->setBugData($bugapi->record); + $notif->sendNewCommentNotice($comment->values); $bugapi->set('lastposttime', $comment->values['dateline']); $bugapi->set('lastpostby', bugdar::$userinfo['userid']); @@ -204,7 +204,7 @@ if ($_POST['do'] == 'update') // ------------------------------------------------------------------- // handle logging and perform updates - $notif->set_bug_data($bugapi->record, array_merge($bugapi->record, $bugapi->values)); + $notif->setBugData($bugapi->record, array_merge($bugapi->record, $bugapi->values)); $log->add_data(false, $bugapi->values, $log->getCommonFields(), true); if (!$message->hasErrors()) @@ -221,7 +221,7 @@ if ($_POST['do'] == 'update') $log->update_history(); - $notif->send_bug_changes_notice(); + $notif->sendBugChangeNotice(); $notif->finalize(); diff --git a/includes/class_notification.php b/includes/class_notification.php index 31ce4a0..5d24e98 100644 --- a/includes/class_notification.php +++ b/includes/class_notification.php @@ -34,32 +34,28 @@ class NotificationCenter { /** - * Bug information - * @var array - * @access private - */ - var $bug = array(); + * Bug information + * @var array + */ + private $bug = array(); /** - * Original bug data - * @var array - * @access private - */ - var $original = array(); + * Original bug data + * @var array + */ + private $original = array(); /** - * Modified bug data - * @var array - * @access private - */ - var $modified = array(); + * Modified bug data + * @var array + */ + private $modified = array(); /** - * Role list: a list of user IDs with their relations to the bug - * @var array - * @access private - */ - var $roles = array( + * Role list: a list of user IDs with their relations to the bug + * @var array + */ + private $roles = array( '-notapplicable-' => array(), 'reporter' => array(), 'assignee' => array(), @@ -69,30 +65,25 @@ class NotificationCenter ); /** - * User cache list - * @var array - * @access private - */ - var $users = array(); + * User cache list + * @var array + */ + private $users = array(); /** - * A list of notices per-user that are combined together in NotificationCenter::finalize() - * @var array - * @access private - */ - var $notices = array(); + * A list of notices per-user that are combined together in NotificationCenter::finalize() + * @var array + */ + private $notices = array(); - // ################################################################### /** - * Sets the bug data so that all methods in this class have access to - * it when sending emails. - * - * @access public - * - * @param array Original bug data - * @param array Modified bug data - */ - function set_bug_data($original, $modified = array()) + * Sets the bug data so that all methods in this class have access to + * it when sending emails. + * + * @param array Original bug data + * @param array Modified bug data + */ + public function setBugData($original, $modified = array()) { if (sizeof($modified) > 0) { @@ -110,17 +101,14 @@ class NotificationCenter $this->roles['reporter'] = array($original['userid']); $this->roles['assignee'][] = (sizeof($modified) > 0 ? $modified['assignedto'] : $original['assignedto']); - $this->fetch_user_cache(); + $this->_fetchUserCache(); } - // ################################################################### /** - * Fetches all the users who could be related to the bug and sticks - * their information into an array. - * - * @access private - */ - function fetch_user_cache() + * Fetches all the users who could be related to the bug and sticks + * their information into an array. + */ + private function _fetchUserCache() { $newbuggers = BSApp::$db->query("SELECT userid FROM " . TABLE_PREFIX . "useremail WHERE relation = " . bugdar::$emailOptions['relations']['-notapplicable-'] . " AND mask & " . bugdar::$emailOptions['notifications']['newbug']); foreach ($newbuggers as $newbug) @@ -146,7 +134,7 @@ class NotificationCenter $masterids = array_merge($this->roles['-notapplicable-'], $this->roles['reporter'], $this->roles['assignee'], $this->roles['favorite'], $this->roles['voter'], $this->roles['commenter']); $masterids = BSFunctions::array_strip_empty(array_unique($masterids)); - if (is_array($masterids) AND sizeof($masterids) > 0) + if (is_array($masterids) && sizeof($masterids) > 0) { $userinfo = BSApp::$db->query(" SELECT user.*, useremail.* @@ -167,16 +155,13 @@ class NotificationCenter } } - // ################################################################### /** - * Sends the appropriate emails for changes to bugs. This function - * works a lot like the Logging class by taking BugAPI->record and - * BugAPI->values and then comparing the two arries and sending emails - * with the differences. - * - * @access public - */ - function send_bug_changes_notice() + * Sends the appropriate emails for changes to bugs. This function + * works a lot like the Logging class by taking BugAPI->record and + * BugAPI->values and then comparing the two arries and sending emails + * with the differences. + */ + public function sendBugChangeNotice() { if (!isset($this->modified['bugid'])) { @@ -188,38 +173,38 @@ class NotificationCenter { if ($this->original['assignedto'] != '') { - $this->notice_no_longer_assigned($this->original['assignedto']); + $this->_noticeNoLongerAssigned($this->original['assignedto']); } if ($this->modified['assignedto'] != '') { - $this->notice_now_assigned($this->modified['assignedto']); + $this->_noticeNowAssigned($this->modified['assignedto']); } } if ($this->original['status'] != $this->modified['status']) { - $this->notice_status_change($this->original['status'], $this->modified['status']); + $this->_noticeStatusChange($this->original['status'], $this->modified['status']); } if ($this->original['resolution'] != $this->modified['resolution']) { - $this->notice_resolution_change($this->original['resolution'], $this->modified['resolution']); + $this->_noticeResolutionChange($this->original['resolution'], $this->modified['resolution']); } if ($this->original['duplicates'] != $this->modified['duplicates']) { - $this->notice_duplicates_change($this->original['duplicates'], $this->modified['duplicates']); + $this->_noticeDuplicatesChange($this->original['duplicates'], $this->modified['duplicates']); } // other standard fields that don't have custom masks if ($this->original['severity'] != $this->modified['severity']) { - $this->notice_severity_change($this->original['severity'], $this->modified['severity']); + $this->_noticeSeverityChange($this->original['severity'], $this->modified['severity']); } if ($this->original['priority'] != $this->modified['priority']) { - $this->notice_priority_change($this->original['priority'], $this->modified['priority']); + $this->_noticePriorityChange($this->original['priority'], $this->modified['priority']); } - if (($this->original['product'] != $this->modified['product']) OR ($this->original['component'] != $this->modified['component']) OR ($this->original['version'] != $this->modified['version'])) + if (($this->original['product'] != $this->modified['product']) || ($this->original['component'] != $this->modified['component']) || ($this->original['version'] != $this->modified['version'])) { - $this->notice_pcv_change(array($this->original['product'], $this->original['component'], $this->original['version']), array($this->modified['product'], $this->modified['component'], $this->modified['version'])); + $this->_noticePCVChange(array($this->original['product'], $this->original['component'], $this->original['version']), array($this->modified['product'], $this->modified['component'], $this->modified['version'])); } $dofields = array( @@ -227,27 +212,24 @@ class NotificationCenter 'dependency' => -1, 'hidden' => -1 ); - foreach ($dofields AS $field => $lookup) + foreach ($dofields as $field => $lookup) { if ($this->original["$field"] != $this->modified["$field"]) { - $this->notice_other_change($field, $this->original["$field"], $this->modified["$field"]); + $this->_noticeOtherChange($field, $this->original["$field"], $this->modified["$field"]); } } } - // ################################################################### /** - * Sends an email to the specified user ID that they are no longer the - * person assigned to the bug. - * - * @access private - * - * @param integer User ID to send to - */ - function notice_no_longer_assigned($userid) + * Sends an email to the specified user ID that they are no longer the + * person assigned to the bug. + * + * @param integer User ID to send to + */ + private function _noticeNoLongerAssigned($userid) { - if ($this->users["$userid"]['options'][0] & bugdar::$emailOptions['notifications']['assignedto'] AND in_array($userid, $this->roles['-notapplicable-'])) + if ($this->users["$userid"]['options'][0] & bugdar::$emailOptions['notifications']['assignedto'] && in_array($userid, $this->roles['-notapplicable-'])) { $user = construct_user_display(bugdar::$userinfo, false); eval('$part = "' . $this->registry->template->fetch(FetchEmailPath('notice_unassigned.part', $this->_localeFromUserId($userid))) . '";'); @@ -255,17 +237,14 @@ class NotificationCenter } } - // ################################################################### /** - * Informs the user that they have been made the assignee of the bug. - * - * @access private - * - * @param integer User ID - */ - function notice_now_assigned($userid) + * Informs the user that they have been made the assignee of the bug. + * + * @param integer User ID + */ + private function _noticeNowAssigned($userid) { - if ($this->users["$userid"]['options'][0] & bugdar::$emailOptions['notifications']['assignedto'] AND in_array($userid, $this->roles['-notapplicable-'])) + if ($this->users["$userid"]['options'][0] & bugdar::$emailOptions['notifications']['assignedto'] && in_array($userid, $this->roles['-notapplicable-'])) { $user = construct_user_display(bugdar::$userinfo, false); eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_assigned.part', $this->_localeFromUserId($userid))) . '";'); @@ -273,139 +252,118 @@ class NotificationCenter } } - // ################################################################### /** - * Sends a message to inform users that the status has changed. - * - * @access private - * - * @param integer Old status - * @param integer New status - */ - function notice_status_change($old, $new) + * Sends a message to inform users that the status has changed. + * + * @param integer Old status + * @param integer New status + */ + private function _noticeStatusChange($old, $new) { - $userlist = $this->fetch_users_with_on_bit('statusresolve'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('statusresolve'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_status.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Sends an email to inform users that the resolution has changed. - * - * @access private - * - * @param integer Old resolution - * @param integer New resolution - */ - function notice_resolution_change($old, $new) + * Sends an email to inform users that the resolution has changed. + * + * @param integer Old resolution + * @param integer New resolution + */ + private function _noticeResolutionChange($old, $new) { - $userlist = $this->fetch_users_with_on_bit('statusresolve'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('statusresolve'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_resolution.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Informs users that the duplicates list has changed. - * - * @access private - * - * @param string Old duplicates list - * @param string New duplicates list - */ - function notice_duplicates_change($old, $new) + * Informs users that the duplicates list has changed. + * + * @param string Old duplicates list + * @param string New duplicates list + */ + private function _noticeDuplicatesChange($old, $new) { - $userlist = $this->fetch_useres_with_on_bit('duplicates'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('duplicates'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_duplicates.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Sends an email to inform users that the severity has changed. - * - * @access private - * - * @param integer Old severity - * @param integer New severity - */ - function notice_severity_change($old, $new) + * Sends an email to inform users that the severity has changed. + * + * @param integer Old severity + * @param integer New severity + */ + private function _noticeSeverityChange($old, $new) { - $userlist = $this->fetch_users_with_on_bit('otherfield'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('otherfield'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_severity.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Informs users that the priority changed. - * - * @access private - * - * @param integer Old priority - * @param integer New priority - */ - function notice_priority_change($old, $new) + * Informs users that the priority changed. + * + * @param integer Old priority + * @param integer New priority + */ + private function _noticePriorityChange($old, $new) { - $userlist = $this->fetch_users_with_on_bit('otherfield'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('otherfield'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_priority.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Sends an email telling users that the product, component, or version - * has changed. This is done all at once because you really need to see - * the whole thing in the notice. - * - * @access private - * - * @param array Original PCV - * @param array Modified PCV - */ - function notice_pcv_change($old, $new) + * Sends an email telling users that the product, component, or version + * has changed. This is done all at once because you really need to see + * the whole thing in the notice. + * + * @param array Original PCV + * @param array Modified PCV + */ + private function _noticePCVChange($old, $new) { - $userlist = $this->fetch_users_with_on_bit('otherfield'); + $userlist = $this->_fetchUsersWithOnBit('otherfield'); $old = bugdar::$datastore['product']["$old[0]"]['title'] . '/' . ($old[1] ? bugdar::$datastore['product']["$old[1]"]['title'] . '/' : '') . bugdar::$datastore['version']["$old[2]"]['version']; $new = bugdar::$datastore['product']["$new[0]"]['title'] . '/' . ($new[1] ? bugdar::$datastore['product']["$new[1]"]['title'] . '/' : '') . bugdar::$datastore['version']["$new[2]"]['version']; - foreach ($userlist AS $userid => $user) + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_product.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** * Sends the appropriate users information about a new comment being * posted to the bug report. * - * @access public - * * @param array CommentAPI->values array */ - function send_new_comment_notice($comment) + public function sendNewCommentNotice($comment) { - $userlist = $this->fetch_users_with_on_bit('newcomment'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('newcomment'); + foreach ($userlist as $userid => $user) { $user = construct_user_display(bugdar::$userinfo, false); $date = $this->registry->modules['date']->format(bugdar::$options['dateformat'], $comment['dateline']); @@ -415,41 +373,35 @@ class NotificationCenter } } - // ################################################################### /** - * A notice for an individual field changing. - * - * @access private - * - * @param string Field name - * @param mixed Original value - * @param mixed Modified value - */ - function notice_other_change($name, $old, $new) + * A notice for an individual field changing. + * + * @param string Field name + * @param mixed Original value + * @param mixed Modified value + */ + private function _noticeOtherChange($name, $old, $new) { - $userlist = $this->fetch_users_with_on_bit('otherfield'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('otherfield'); + foreach ($userlist as $userid => $user) { eval('$email = "' . $this->registry->template->fetch(FetchEmailPath('notice_other.part', $this->_localeFromUserId($userid))) . '";'); $this->notices["$user[userid]"][] = $email; } } - // ################################################################### /** - * Sends appropriate users a notice when a new attachment has been - * added. - * - * @access public - * - * @param array AttachmentAPI->values array - * @param array List of all attachments made obsolete - * @param array Newly-inserted attachment ID - */ - function send_new_attachment_notice($attachment, $obsolete, $id) + * Sends appropriate users a notice when a new attachment has been + * added. + * + * @param array AttachmentAPI->values array + * @param array List of all attachments made obsolete + * @param array Newly-inserted attachment ID + */ + public function sendNewAttachmentNotice($attachment, $obsolete, $id) { - $userlist = $this->fetch_users_with_on_bit('newattachment'); - foreach ($userlist AS $userid => $user) + $userlist = $this->_fetchUsersWithOnBit('newattachment'); + foreach ($userlist as $userid => $user) { $user = construct_user_display(bugdar::$userinfo, false); $obsoletes = implode(', ', (array)$obsolete); @@ -459,18 +411,15 @@ class NotificationCenter } } - // ################################################################### /** - * Sends a new bug notification notice to all those who have the option - * turned no. This does not use fetch_users_with_on_bit() because a - * query is more effective. - * - * @access public - * - * @param array Bug values array - * @param array Comment values array - */ - function send_new_bug_notice($bug, $comment) + * Sends a new bug notification notice to all those who have the option + * turned no. This does not use fetchUsersWithOnBit() because a + * query is more effective. + * + * @param array Bug values array + * @param array Comment values array + */ + public function sendNewBugNotice($bug, $comment) { $userinfo = BSApp::$db->query(" SELECT user.*, useremail.* @@ -495,26 +444,23 @@ class NotificationCenter } } - // ################################################################### /** - * Generates an array of users who have a given email notification flag - * turned on in their bitfields. - * - * @access private - * - * @param string Notification bitfield name - * - * @return array Array of users and their data - */ - function fetch_users_with_on_bit($bitname) + * Generates an array of users who have a given email notification flag + * turned on in their bitfields. + * + * @param string Notification bitfield name + * + * @return array Array of users and their data + */ + private function _fetchUsersWithOnBit($bitname) { $idlist = array(); - foreach ($this->users AS $user) + foreach ($this->users as $user) { - foreach (bugdar::$emailOptions['relations'] AS $name => $bit) + foreach (bugdar::$emailOptions['relations'] as $name => $bit) { - if (in_array($user['userid'], $this->roles["$name"]) AND $user['options']["$bit"] & bugdar::$emailOptions['notifications']["$bitname"]) + if (in_array($user['userid'], $this->roles["$name"]) && $user['options']["$bit"] & bugdar::$emailOptions['notifications']["$bitname"]) { $idlist[] = $user['userid']; } @@ -524,7 +470,7 @@ class NotificationCenter $masters = array_unique($idlist); $return = array(); - foreach ($masters AS $userid) + foreach ($masters as $userid) { $return["$userid"] =& $this->users["$userid"]; } @@ -532,28 +478,25 @@ class NotificationCenter return $return; } - // ################################################################### /** - * Compiles and sends the actual emails to users. - * - * @access public - */ - function finalize() + * Compiles and sends the actual emails to users. + */ + public function finalize() { // get the current bug for permissions checks $bug = BSApp::$db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . $this->bug['bugid']); - foreach ($this->notices AS $userid => $noticelist) + foreach ($this->_notices as $userid => $noticelist) { if ($userid == bugdar::$userinfo['userid']) { - $this->registry->debug("skipping user $userid because they're the one doing the thing"); + BSApp::debug("skipping user $userid because they're the one doing the thing"); continue; } // we wouldn't want people who favorite bugs getting hidden notices if (!check_bug_permissions($bug, $this->users["$userid"])) { - $this->registry->debug("skipping user $userid ({$this->users[$userid]['email']}) because of permissions"); + BSApp::debug("skipping user $userid ({$this->users[$userid]['email']}) because of permissions"); continue; } @@ -570,20 +513,19 @@ class NotificationCenter } else { - $this->registry->debug("not sending an email to " . $userid . " because they don't have one?"); + BSApp::debug("not sending an email to " . $userid . " because they don't have one?"); } } } - // ################################################################### /** - * Returns the locale name from a given user ID - * - * @param integer User ID - * - * @return string Locale - */ - function _localeFromUserId($userid) + * Returns the locale name from a given user ID + * + * @param integer User ID + * + * @return string Locale + */ + private function _localeFromUserId($userid) { $langcode = bugdar::$datastore['language'][$this->users[$userid]['languageid']]['langcode']; if (!$langcode) diff --git a/newreport.php b/newreport.php index c339a2b..a00df94 100755 --- a/newreport.php +++ b/newreport.php @@ -103,7 +103,7 @@ if ($_POST['do'] == 'insert') $comment->set('bugid', $bug->insertid); $comment->insert(); - $notif->send_new_bug_notice($bug->values, $comment->values); + $notif->sendNewBugNotice($bug->values, $comment->values); $bug = new BugAPI(); // need to destroy because update will think the insert fields need to be changed, too $bug->set('bugid', $comment->values['bugid']); @@ -118,7 +118,7 @@ if ($_POST['do'] == 'insert') $bug->set('hiddenlastpostbyname', bugdar::$userinfo['displayname']); $bug->update(); - $notif->set_bug_data($bug->record); + $notif->setBugData($bug->record); $notif->finalize(); -- 2.22.5