1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-02-24 20:13:22 +01:00

[ticket/11103] Private Message type notification

Also cleanup

PHPBB3-11103
This commit is contained in:
Nathan Guse 2012-09-08 12:28:58 -05:00
parent e45fb0025e
commit 32a966b21d
5 changed files with 162 additions and 21 deletions

View File

@ -87,9 +87,9 @@ class phpbb_notifications_service
while ($row = $this->db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
{ {
$type_class_name = $this->get_type_class_name($row['item_type'], true); $item_type_class_name = $this->get_item_type_class_name($row['item_type'], true);
$notification = new $type_class_name($this->phpbb_container, $row); $notification = new $item_type_class_name($this->phpbb_container, $row);
$notification->users($this->users); $notification->users($this->users);
$user_ids = array_merge($user_ids, $notification->users_to_query()); $user_ids = array_merge($user_ids, $notification->users_to_query());
@ -123,13 +123,18 @@ class phpbb_notifications_service
/** /**
* Add a notification * Add a notification
* *
* @param string $type Type identifier * @param string $item_type Type identifier
* @param int $type_id Identifier within the type * @param int $item_id Identifier within the type
* @param array $data Data specific for this type that will be inserted * @param array $data Data specific for this type that will be inserted
*/ */
public function add_notifications($type, $data) public function add_notifications($item_type, $data)
{ {
$type_class_name = $this->get_type_class_name($type); $item_type_class_name = $this->get_item_type_class_name($item_type);
$item_id = $item_type_class_name::get_item_id($data);
// Update any existing notifications for this item
$this->update_notifications($item_type, $item_id, $data);
$notify_users = array(); $notify_users = array();
$notification_objects = $notification_methods = array(); $notification_objects = $notification_methods = array();
@ -150,10 +155,22 @@ class phpbb_notifications_service
} }
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
// 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
$sql = 'SELECT user_id FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
AND item_id = " . (int) $item_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
unset($notify_users[$row['user_id']]);
}
$this->db->sql_freeresult($result);
// Go through each user so we can insert a row in the DB and then notify them by their desired means // 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) foreach ($notify_users as $user => $methods)
{ {
$notification = new $type_class_name($this->phpbb_container); $notification = new $item_type_class_name($this->phpbb_container);
$notification->user_id = (int) $user; $notification->user_id = (int) $user;
@ -188,34 +205,49 @@ class phpbb_notifications_service
/** /**
* Update a notification * Update a notification
* *
* @param string $type Type identifier * @param string $item_type Type identifier
* @param int $type_id Identifier within the type * @param int $item_id Identifier within the type
* @param array $data Data specific for this type that will be updated * @param array $data Data specific for this type that will be updated
*/ */
public function update_notifications($type, $type_id, $data) public function update_notifications($item_type, $item_id, $data)
{ {
$type_class_name = $this->get_type_class_name($type); $item_type_class_name = $this->get_item_type_class_name($item_type);
$notification = new $type_class_name($this->phpbb_container); $notification = new $item_type_class_name($this->phpbb_container);
$update_array = $notification->create_update_array($data); $update_array = $notification->create_update_array($data);
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . ' $sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $update_array) . " SET ' . $this->db->sql_build_array('UPDATE', $update_array) . "
WHERE item_type = '" . $this->db->sql_escape($type) . "' WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
AND item_id = " . (int) $type_id; AND item_id = " . (int) $item_id;
$this->db->sql_query($sql); $this->db->sql_query($sql);
} }
/** /**
* Helper to get the notifications type class name and clean it if unsafe * Delete a notification
*
* @param string $item_type Type identifier
* @param int $item_id Identifier within the type
* @param array $data Data specific for this type that will be updated
*/ */
private function get_type_class_name(&$type, $safe = false) public function delete_notifications($item_type, $item_id)
{
$sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . "
WHERE item_type = '" . $this->db->sql_escape($item_type) . "'
AND item_id = " . (int) $item_id;
$this->db->sql_query($sql);
}
/**
* Helper to get the notifications item type class name and clean it if unsafe
*/
private function get_item_type_class_name(&$item_type, $safe = false)
{ {
if (!$safe) if (!$safe)
{ {
$type = preg_replace('#[^a-z]#', '', $type); $item_type = preg_replace('#[^a-z]#', '', $item_type);
} }
return 'phpbb_notifications_type_' . $type; return 'phpbb_notifications_type_' . $item_type;
} }
} }

View File

@ -157,7 +157,7 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type
{ {
// Defaults // Defaults
$data = array_merge(array( $data = array_merge(array(
'item_type' => $this->get_type(), 'item_type' => $this->get_item_type(),
'time' => time(), 'time' => time(),
'unread' => true, 'unread' => true,

View File

@ -21,7 +21,9 @@ if (!defined('IN_PHPBB'))
*/ */
interface phpbb_notifications_type_interface interface phpbb_notifications_type_interface
{ {
public function get_type(); public static function get_item_type();
public static function get_item_id($post);
public function get_title(); public function get_title();

View File

@ -0,0 +1,97 @@
<?php
/**
*
* @package notifications
* @copyright (c) 2012 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Private message notifications class
* @package notifications
*/
class phpbb_notifications_type_pm extends phpbb_notifications_type_base
{
/**
* Get the type of notification this is
* phpbb_notifications_type_
*/
public static function get_item_type()
{
return 'pm';
}
/**
* Get the id of the
*
* @param array $pm The data from the private message
*/
public static function get_item_id($pm)
{
return $pm['msg_id'];
}
/**
* Get the title of this notification
*
* @return string
*/
public function get_title()
{
$user_data = $this->get_user($this->get_data('author_id'));
$username = get_username_string('no_profile', $user_data['user_id'], $user_data['username'], $user_data['user_colour']);
return $username . ' sent you a private message titled: ' . $this->get_data('message_subject');
}
/**
* Get the url to this item
*
* @return string URL
*/
public function get_url()
{
return append_sid($this->phpbb_root_path . 'ucp.' . $this->php_ext, "i=pm&amp;mode=view&p={$this->item_id}");
}
/**
* Users needed to query before this notification can be displayed
*
* @return array Array of user_ids
*/
public function users_to_query()
{
return array($this->data['author_id']);
}
/**
* Function for preparing the data for insertion in an SQL query
* (The service handles insertion)
*
* @param array $post Data from submit_post
*
* @return array Array of data ready to be inserted into the database
*/
public function create_insert_array($post)
{
$this->item_id = $post['msg_id'];
$this->set_data('author_id', $post['author_id']);
$this->set_data('message_subject', $post['message_subject']);
$this->time = $post['message_time'];
return parent::create_insert_array($post);
}
}

View File

@ -25,11 +25,21 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base
* Get the type of notification this is * Get the type of notification this is
* phpbb_notifications_type_ * phpbb_notifications_type_
*/ */
public function get_type() public static function get_item_type()
{ {
return 'post'; return 'post';
} }
/**
* Get the id of the
*
* @param array $post The data from the post
*/
public static function get_item_id($post)
{
return $post['post_id'];
}
/** /**
* Get the title of this notification * Get the title of this notification
* *