[ticket/11103] Add ability for notification types to load special data

For consistency, links to posts do not include topic_id. As is done in
viewtopic, do not include the topic_id when linking to a post

PHPBB3-11103
This commit is contained in:
Nathan Guse 2012-09-12 22:29:48 -05:00
parent e145956212
commit 3d1549d43f
4 changed files with 105 additions and 3 deletions

View file

@ -65,6 +65,7 @@ class phpbb_notifications_service
), $options); ), $options);
$notifications = $user_ids = array(); $notifications = $user_ids = array();
$load_special = array();
$sql = 'SELECT * $sql = 'SELECT *
FROM ' . NOTIFICATIONS_TABLE . ' FROM ' . NOTIFICATIONS_TABLE . '
@ -78,14 +79,30 @@ class phpbb_notifications_service
$notification = new $item_type_class_name($this->phpbb_container, $row); $notification = new $item_type_class_name($this->phpbb_container, $row);
// Array of user_ids to query all at once
$user_ids = array_merge($user_ids, $notification->users_to_query()); $user_ids = array_merge($user_ids, $notification->users_to_query());
// Some notification types also require querying additional tables themselves
if (!isset($load_special[$row['item_type']]))
{
$load_special[$row['item_type']] = array();
}
$load_special[$row['item_type']] = array_merge($load_special[$row['item_type']], $notification->get_load_special());
$notifications[] = $notification; $notifications[] = $notification;
} }
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);
$this->load_users($user_ids); $this->load_users($user_ids);
// Allow each type to load it's own special items
foreach ($load_special as $item_type => $data)
{
$item_type_class_name = $this->get_item_type_class_name($item_type, true);
$item_type_class_name::load_special($this->phpbb_container, $data, $notifications);
}
return $notifications; return $notifications;
} }

View file

@ -123,6 +123,55 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type
); );
} }
/**
* Mark this item read
*
* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
* @return string
*/
public function mark_read($return = true)
{
return $this->mark(false, $return);
}
/**
* Mark this item unread
*
* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
* @return string
*/
public function mark_unread($return = true)
{
return $this->mark(true, $return);
}
/**
* Mark this item read/unread
*
* @param bool $unread Unread (True/False) (Default: False)
* @param bool $return True to return a string containing the SQL code to update this item, False to execute it (Default: False)
* @return string
*/
protected function mark($unread = true, $return = false)
{
$where = array(
'item_type = ' . $this->db->sql_escape($this->item_type),
'item_id = ' . (int) $this->item_id,
'user_id = ' . (int) $this->user_id,
);
$where = implode(' AND ' . $where);
if ($return)
{
return $where;
}
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . '
SET unread = ' . (bool) $unread . '
WHERE ' . $where;
$this->db->sql_query($sql);
}
/** /**
* Function for preparing the data for insertion in an SQL query * Function for preparing the data for insertion in an SQL query
* (The service handles insertion) * (The service handles insertion)
@ -206,6 +255,11 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type
{ {
$user = $this->service->get_user($user_id); $user = $this->service->get_user($user_id);
if (!function_exists('get_user_avatar'))
{
include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
}
return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height'], $user['username'], false, 'notifications-avatar'); return get_user_avatar($user['user_avatar'], $user['user_avatar_type'], $user['user_avatar_width'], $user['user_avatar_height'], $user['username'], false, 'notifications-avatar');
} }
@ -236,4 +290,20 @@ abstract class phpbb_notifications_type_base implements phpbb_notifications_type
{ {
return ''; return '';
} }
/**
* Get the special items to load (fall-back)
*/
public function get_load_special()
{
return array();
}
/**
* Load the special items (fall-back)
*/
public static function load_special(ContainerBuilder $phpbb_container, $data, $notifications)
{
return;
}
} }

View file

@ -37,5 +37,13 @@ interface phpbb_notifications_type_interface
public function get_unsubscribe_url($method); public function get_unsubscribe_url($method);
public function mark_read($return);
public function mark_unread($return);
public function create_insert_array($type_data); public function create_insert_array($type_data);
public function get_load_special();
public static function load_special(ContainerBuilder $phpbb_container, $data, $notifications);
} }

View file

@ -60,12 +60,19 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base
$db = $phpbb_container->get('dbal.conn'); $db = $phpbb_container->get('dbal.conn');
/* todo
* find what type of notification they'd like to receive
* make sure not to send duplicate notifications
*/
$sql = 'SELECT user_id $sql = 'SELECT user_id
FROM ' . TOPICS_WATCH_TABLE . ' FROM ' . TOPICS_WATCH_TABLE . '
WHERE topic_id = ' . (int) $post['topic_id'] . ' WHERE topic_id = ' . (int) $post['topic_id'] . '
AND notify_status = ' . NOTIFY_YES; AND notify_status = ' . NOTIFY_YES;
$result = $db->sql_query($sql); $result = $db->sql_query($sql);
$users = $db->sql_fetchrowset($result); while ($row = $db->sql_fetchrow($result))
{
$users[$row['user_id']] = array('');
}
$db->sql_freeresult($result); $db->sql_freeresult($result);
if (empty($users)) if (empty($users))
@ -155,7 +162,7 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base
*/ */
public function get_url() public function get_url()
{ {
return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t={$this->get_data('topic_id')}&p={$this->item_id}#p{$this->item_id}"); return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "p={$this->item_id}#p{$this->item_id}");
} }
/** /**
@ -165,7 +172,7 @@ class phpbb_notifications_type_post extends phpbb_notifications_type_base
*/ */
public function get_full_url() public function get_full_url()
{ {
return generate_board_url() . "/viewtopic.{$this->php_ext}?t={$this->get_data('topic_id')}&p={$this->item_id}#p{$this->item_id}"; return generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}";
} }
/** /**