1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-10 16:35:35 +02:00

[ticket/13713] Implement colour handling

PHPBB3-13713
This commit is contained in:
lavigor 2018-06-06 03:51:52 +03:00 committed by Marc Alexander
parent f775c1e79d
commit 4b31a29c2c
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
4 changed files with 93 additions and 4 deletions

View File

@ -55,6 +55,7 @@ services:
text_formatter.s9e.mention_helper:
class: phpbb\textformatter\s9e\mention_helper
arguments:
- '@dbal.conn'
- '%core.root_path%'
- '%core.php_ext%'

View File

@ -13,8 +13,15 @@
namespace phpbb\textformatter\s9e;
use s9e\TextFormatter\Utils;
class mention_helper
{
/**
* @var \phpbb\db\driver\driver_interface
*/
protected $db;
/**
* @var string Base URL for a user profile link, uses {USER_ID} as placeholder
*/
@ -25,18 +32,74 @@ class mention_helper
*/
protected $group_profile_url;
/**
* @var array Array of users' and groups' colors for each cached ID
*/
protected $cached_colors = [];
/**
* Constructor
*
* @param \phpbb\db\driver\driver_interface $db
* @param string $root_path
* @param string $php_ext
*/
public function __construct($root_path, $php_ext)
public function __construct($db, $root_path, $php_ext)
{
$this->db = $db;
$this->user_profile_url = append_sid($root_path . 'memberlist.' . $php_ext, 'mode=viewprofile&u={USER_ID}', false);
$this->group_profile_url = append_sid($root_path . 'memberlist.' . $php_ext, 'mode=group&g={GROUP_ID}', false);
}
/**
* Caches colors for specified user IDs and group IDs
*
* @param array $user_ids
* @param array $group_ids
*/
protected function get_colors($user_ids, $group_ids)
{
$this->cached_colors = [];
$this->cached_colors['users'] = [];
$this->cached_colors['groups'] = [];
if (!empty($user_ids))
{
$query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'u.user_colour, u.user_id',
'FROM' => [
USERS_TABLE => 'u',
],
'WHERE' => 'u.user_id <> ' . ANONYMOUS . '
AND ' . $this->db->sql_in_set('u.user_type', [USER_NORMAL, USER_FOUNDER]) . '
AND ' . $this->db->sql_in_set('u.user_id', $user_ids),
]);
$res = $this->db->sql_query($query);
while ($row = $this->db->sql_fetchrow($res))
{
$this->cached_colors['users'][$row['user_id']] = $row['user_colour'];
}
}
if (!empty($group_ids))
{
$query = $this->db->sql_build_query('SELECT', [
'SELECT' => 'g.group_colour, g.group_id',
'FROM' => [
GROUPS_TABLE => 'g',
],
'WHERE' => $this->db->sql_in_set('g.group_id', $group_ids),
]);
$res = $this->db->sql_query($query);
while ($row = $this->db->sql_fetchrow($res))
{
$this->cached_colors['groups'][$row['group_id']] = $row['group_colour'];
}
}
}
/**
* Inject dynamic metadata into MENTION tags in given XML
*
@ -48,7 +111,13 @@ class mention_helper
$user_profile_url = $this->user_profile_url;
$group_profile_url = $this->group_profile_url;
return \s9e\TextFormatter\Utils::replaceAttributes(
// TODO: think about optimization for caching colors.
$this->get_colors(
Utils::getAttributeValues($xml, 'MENTION', 'user_id'),
Utils::getAttributeValues($xml, 'MENTION', 'group_id')
);
return Utils::replaceAttributes(
$xml,
'MENTION',
function ($attributes) use ($user_profile_url, $group_profile_url)
@ -56,10 +125,20 @@ class mention_helper
if (isset($attributes['user_id']))
{
$attributes['profile_url'] = str_replace('{USER_ID}', $attributes['user_id'], $user_profile_url);
if (isset($this->cached_colors['users'][$attributes['user_id']]))
{
$attributes['color'] = $this->cached_colors['users'][$attributes['user_id']];
}
}
else if (isset($attributes['group_id']))
{
$attributes['profile_url'] = str_replace('{GROUP_ID}', $attributes['group_id'], $group_profile_url);
if (isset($this->cached_colors['groups'][$attributes['group_id']]))
{
$attributes['color'] = $this->cached_colors['groups'][$attributes['group_id']];
}
}
return $attributes;

View File

@ -9,15 +9,19 @@
<!-- BEGIN listitem_close --></li><!-- END listitem_close -->
<!-- BEGIN mention -->
<xsl:text>@</xsl:text>
<xsl:choose>
<xsl:when test="@profile_url">
<a>
<a class="mention">
<xsl:attribute name="href"><xsl:value-of select="@profile_url"/></xsl:attribute>
<xsl:if test="@color">
<xsl:attribute name="style">color: #<xsl:value-of select="@color"/>;</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</a>
</xsl:when>
<xsl:otherwise>
<span>
<span class="mention">
<xsl:apply-templates/>
</span>
</xsl:otherwise>

View File

@ -576,6 +576,11 @@ blockquote .codebox {
padding: 5px 3px;
}
/* Mention block */
.mention {
font-weight: bold;
}
/* Attachments
---------------------------------------- */
.attachbox {