diff --git a/phpBB/includes/mcp/mcp_queue.php b/phpBB/includes/mcp/mcp_queue.php index 77e778fc38..b23e5f4e45 100644 --- a/phpBB/includes/mcp/mcp_queue.php +++ b/phpBB/includes/mcp/mcp_queue.php @@ -34,6 +34,7 @@ class mcp_queue { global $auth, $db, $user, $template, $cache; global $config, $phpbb_root_path, $phpEx, $action; + global $phpbb_notifications; include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx); @@ -84,6 +85,9 @@ class mcp_queue if (isset($topic_info[$topic_id]['topic_first_post_id'])) { $post_id = (int) $topic_info[$topic_id]['topic_first_post_id']; + + // Mark the notification as read + $phpbb_notifications->mark_notifications_read('topic_in_queue', $topic_id, $user->data['user_id']); } else { @@ -91,6 +95,9 @@ class mcp_queue } } + // Mark the notification as read + $phpbb_notifications->mark_notifications_read('post_in_queue', $post_id, $user->data['user_id']); + $post_info = get_post_data(array($post_id), 'm_approve', true); if (!sizeof($post_info)) diff --git a/phpBB/includes/notification/manager.php b/phpBB/includes/notification/manager.php index c5fd41c901..6c74fa965e 100644 --- a/phpBB/includes/notification/manager.php +++ b/phpBB/includes/notification/manager.php @@ -324,8 +324,6 @@ class phpbb_notification_manager // Make sure not to send new notifications to users who've already been notified about this item // This may happen when an item was added, but now new users are able to see the item - // todo Users should not receive notifications from multiple events from the same item (ex: for a topic reply with a quote including your username) - // Probably should be handled within each type? $sql = 'SELECT user_id FROM ' . NOTIFICATIONS_TABLE . " WHERE item_type = '" . $this->db->sql_escape($item_type) . "' @@ -342,6 +340,11 @@ class phpbb_notification_manager return; } + // Allow notifications to perform actions before creating the insert array (such as run a query to cache some data needed for all notifications) + $notification = $this->get_item_type_class($item_type_class_name); + $pre_create_data = $notification->pre_create_insert_array($data, $notify_users); + unset($notification); + // Go through each user so we can insert a row in the DB and then notify them by their desired means foreach ($notify_users as $user => $methods) { @@ -350,7 +353,7 @@ class phpbb_notification_manager $notification->user_id = (int) $user; // Store the creation array in our new rows that will be inserted later - $new_rows[] = $notification->create_insert_array($data); + $new_rows[] = $notification->create_insert_array($data, $pre_create_data); // Users are needed to send notifications $user_ids = array_merge($user_ids, $notification->users_to_query()); diff --git a/phpBB/includes/notification/type/approve_post.php b/phpBB/includes/notification/type/approve_post.php index 98dfe678c2..9275ec2f6d 100644 --- a/phpBB/includes/notification/type/approve_post.php +++ b/phpBB/includes/notification/type/approve_post.php @@ -111,14 +111,15 @@ class phpbb_notification_type_approve_post extends phpbb_notification_type_post * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('post_subject', $post['post_subject']); - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/approve_topic.php b/phpBB/includes/notification/type/approve_topic.php index 496dc688ad..325ccd0eab 100644 --- a/phpBB/includes/notification/type/approve_topic.php +++ b/phpBB/includes/notification/type/approve_topic.php @@ -111,12 +111,13 @@ class phpbb_notification_type_approve_topic extends phpbb_notification_type_topi * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/base.php b/phpBB/includes/notification/type/base.php index a41a5a8ac3..45dc463061 100644 --- a/phpBB/includes/notification/type/base.php +++ b/phpBB/includes/notification/type/base.php @@ -164,10 +164,11 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i * (The service handles insertion) * * @param array $type_data Data unique to this notification type + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($type_data) + public function create_insert_array($type_data, $pre_create_data = array()) { // Defaults $this->data = array_merge(array( @@ -257,6 +258,22 @@ abstract class phpbb_notification_type_base implements phpbb_notification_type_i return true; } + /** + * Pre create insert array function + * This allows you to perform certain actions, like run a query + * and load data, before create_insert_array() is run. The data + * returned from this function will be sent to create_insert_array(). + * + * @param array $type_data Data unique to this notification type + * @param array $notify_users Notify users list + * Formated from find_users_for_notification() + * @return array Whatever you want to send to create_insert_array(). + */ + public function pre_create_insert_array($type_data, $notify_users) + { + return array(); + } + /** * -------------- Helper functions ------------------- */ diff --git a/phpBB/includes/notification/type/disapprove_post.php b/phpBB/includes/notification/type/disapprove_post.php index d1f7dfa85d..1bf9242c52 100644 --- a/phpBB/includes/notification/type/disapprove_post.php +++ b/phpBB/includes/notification/type/disapprove_post.php @@ -98,10 +98,11 @@ class phpbb_notification_type_disapprove_post extends phpbb_notification_type_ap * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('disapprove_reason', $post['disapprove_reason']); diff --git a/phpBB/includes/notification/type/disapprove_topic.php b/phpBB/includes/notification/type/disapprove_topic.php index 89c06b344f..f3e0be4883 100644 --- a/phpBB/includes/notification/type/disapprove_topic.php +++ b/phpBB/includes/notification/type/disapprove_topic.php @@ -98,14 +98,15 @@ class phpbb_notification_type_disapprove_topic extends phpbb_notification_type_a * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('disapprove_reason', $post['disapprove_reason']); - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/interface.php b/phpBB/includes/notification/type/interface.php index 56fad9fd80..084a819af7 100644 --- a/phpBB/includes/notification/type/interface.php +++ b/phpBB/includes/notification/type/interface.php @@ -41,7 +41,9 @@ interface phpbb_notification_type_interface public function mark_unread($return); - public function create_insert_array($type_data); + public function pre_create_insert_array($type_data, $notify_users); + + public function create_insert_array($type_data, $pre_create_data); public function users_to_query(); diff --git a/phpBB/includes/notification/type/pm.php b/phpBB/includes/notification/type/pm.php index 9f9a3e2892..c2065fef99 100644 --- a/phpBB/includes/notification/type/pm.php +++ b/phpBB/includes/notification/type/pm.php @@ -170,15 +170,16 @@ class phpbb_notification_type_pm extends phpbb_notification_type_base * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($pm) + public function create_insert_array($pm, $pre_create_data = array()) { $this->set_data('from_user_id', $pm['from_user_id']); $this->set_data('message_subject', $pm['message_subject']); - return parent::create_insert_array($pm); + return parent::create_insert_array($pm, $pre_create_data); } } diff --git a/phpBB/includes/notification/type/post.php b/phpBB/includes/notification/type/post.php index d6f6c16e59..76a7846f30 100644 --- a/phpBB/includes/notification/type/post.php +++ b/phpBB/includes/notification/type/post.php @@ -242,11 +242,6 @@ class phpbb_notification_type_post extends phpbb_notification_type_base */ public function users_to_query() { - /*$responders[] = array( - 'poster_id' => $post['poster_id'], - 'username' => (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''), - );*/ - $responders = $this->get_data('responders'); $users = array( $this->get_data('poster_id'), @@ -263,15 +258,47 @@ class phpbb_notification_type_post extends phpbb_notification_type_base return $users; } + /** + * Pre create insert array function + * This allows you to perform certain actions, like run a query + * and load data, before create_insert_array() is run. The data + * returned from this function will be sent to create_insert_array(). + * + * @param array $post Post data from submit_post + * @param array $notify_users Notify users list + * Formated from find_users_for_notification() + * @return array Whatever you want to send to create_insert_array(). + */ + public function pre_create_insert_array($post, $notify_users) + { + if (!sizeof($notify_users)) + { + return array(); + } + + $tracking_data = array(); + $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . ' + WHERE topic_id = ' . (int) $post['topic_id'] . ' + AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users)); + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $tracking_data[$row['user_id']] = $row['mark_time']; + } + + return $tracking_data; + } + /** * Function for preparing the data for insertion in an SQL query * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('poster_id', $post['poster_id']); @@ -287,7 +314,14 @@ class phpbb_notification_type_post extends phpbb_notification_type_base $this->time = $post['post_time']; - return parent::create_insert_array($post); + // Topics can be "read" before they are public (while awaiting approval). + // Make sure that if the user has read the topic, it's marked as read in the notification + if (isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->time) + { + $this->unread = false; + } + + return parent::create_insert_array($post, $pre_create_data); } /** diff --git a/phpBB/includes/notification/type/post_in_queue.php b/phpBB/includes/notification/type/post_in_queue.php index 1d75ca4dc9..f00a83de36 100644 --- a/phpBB/includes/notification/type/post_in_queue.php +++ b/phpBB/includes/notification/type/post_in_queue.php @@ -120,17 +120,28 @@ class phpbb_notification_type_post_in_queue extends phpbb_notification_type_post return $notify_users; } + /** + * Get the url to this item + * + * @return string URL + */ + public function get_url() + { + return append_sid($this->phpbb_root_path . 'mcp.' . $this->php_ext, "i=queue&mode=approve_details&f={$this->get_data('forum_id')}&p={$this->item_id}"); + } + /** * Function for preparing the data for insertion in an SQL query * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/report_pm.php b/phpBB/includes/notification/type/report_pm.php index af1f71433c..db0ad6ac6e 100644 --- a/phpBB/includes/notification/type/report_pm.php +++ b/phpBB/includes/notification/type/report_pm.php @@ -203,15 +203,16 @@ class phpbb_notification_type_report_pm extends phpbb_notification_type_pm * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('reporter_id', $this->user->data['user_id']); $this->set_data('reason_title', strtoupper($post['reason_title'])); $this->set_data('reason_description', $post['reason_description']); - return parent::create_insert_array($post); + return parent::create_insert_array($post, $pre_create_data); } } diff --git a/phpBB/includes/notification/type/report_pm_closed.php b/phpBB/includes/notification/type/report_pm_closed.php index 534e1288a6..f5790ba449 100644 --- a/phpBB/includes/notification/type/report_pm_closed.php +++ b/phpBB/includes/notification/type/report_pm_closed.php @@ -124,14 +124,15 @@ class phpbb_notification_type_report_pm_closed extends phpbb_notification_type_p * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('closer_id', $post['closer_id']); - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/report_post.php b/phpBB/includes/notification/type/report_post.php index 2346e4e57c..d7a0d58167 100644 --- a/phpBB/includes/notification/type/report_post.php +++ b/phpBB/includes/notification/type/report_post.php @@ -150,15 +150,16 @@ class phpbb_notification_type_report_post extends phpbb_notification_type_post_i * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('reporter_id', $this->user->data['user_id']); $this->set_data('reason_title', strtoupper($post['reason_title'])); $this->set_data('reason_description', $post['reason_description']); - return parent::create_insert_array($post); + return parent::create_insert_array($post, $pre_create_data); } } diff --git a/phpBB/includes/notification/type/report_post_closed.php b/phpBB/includes/notification/type/report_post_closed.php index d8537a8152..0d5c5b292e 100644 --- a/phpBB/includes/notification/type/report_post_closed.php +++ b/phpBB/includes/notification/type/report_post_closed.php @@ -124,14 +124,15 @@ class phpbb_notification_type_report_post_closed extends phpbb_notification_type * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('closer_id', $post['closer_id']); - $data = parent::create_insert_array($post); + $data = parent::create_insert_array($post, $pre_create_data); $this->time = $data['time'] = time(); diff --git a/phpBB/includes/notification/type/topic.php b/phpBB/includes/notification/type/topic.php index 1c3d216b18..cb38b0274e 100644 --- a/phpBB/includes/notification/type/topic.php +++ b/phpBB/includes/notification/type/topic.php @@ -76,7 +76,7 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base public function find_users_for_notification($topic, $options = array()) { $options = array_merge(array( - 'ignore_users' => array(), + 'ignore_users' => array(), ), $options); // Let's continue to use the phpBB subscriptions system, at least for now. @@ -207,15 +207,47 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base return array($this->data['poster_id']); } + /** + * Pre create insert array function + * This allows you to perform certain actions, like run a query + * and load data, before create_insert_array() is run. The data + * returned from this function will be sent to create_insert_array(). + * + * @param array $post Post data from submit_post + * @param array $notify_users Notify users list + * Formated from find_users_for_notification() + * @return array Whatever you want to send to create_insert_array(). + */ + public function pre_create_insert_array($post, $notify_users) + { + if (!sizeof($notify_users)) + { + return array(); + } + + $tracking_data = array(); + $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . ' + WHERE topic_id = ' . (int) $post['topic_id'] . ' + AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users)); + $result = $this->db->sql_query($sql); + while ($row = $this->db->sql_fetchrow($result)) + { + $tracking_data[$row['user_id']] = $row['mark_time']; + } + + return $tracking_data; + } + /** * Function for preparing the data for insertion in an SQL query * (The service handles insertion) * * @param array $post Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($post) + public function create_insert_array($post, $pre_create_data = array()) { $this->set_data('poster_id', $post['poster_id']); @@ -227,6 +259,13 @@ class phpbb_notification_type_topic extends phpbb_notification_type_base $this->time = $post['post_time']; - return parent::create_insert_array($post); + // Topics can be "read" before they are public (while awaiting approval). + // Make sure that if the user has read the topic, it's marked as read in the notification + if (isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->time) + { + $this->unread = false; + } + + return parent::create_insert_array($post, $pre_create_data); } } diff --git a/phpBB/includes/notification/type/topic_in_queue.php b/phpBB/includes/notification/type/topic_in_queue.php index bee0ebbb22..176ec0b901 100644 --- a/phpBB/includes/notification/type/topic_in_queue.php +++ b/phpBB/includes/notification/type/topic_in_queue.php @@ -113,17 +113,28 @@ class phpbb_notification_type_topic_in_queue extends phpbb_notification_type_top return $notify_users; } + /** + * Get the url to this item + * + * @return string URL + */ + public function get_url() + { + return append_sid($this->phpbb_root_path . 'mcp.' . $this->php_ext, "i=queue&mode=approve_details&f={$this->item_parent_id}&t={$this->item_id}"); + } + /** * Function for preparing the data for insertion in an SQL query * (The service handles insertion) * * @param array $topic Data from submit_post + * @param array $pre_create_data Data from pre_create_insert_array() * * @return array Array of data ready to be inserted into the database */ - public function create_insert_array($topic) + public function create_insert_array($topic, $pre_create_data = array()) { - $data = parent::create_insert_array($topic); + $data = parent::create_insert_array($topic, $pre_create_data); $this->time = $data['time'] = time();