[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: text_formatter.s9e.mention_helper:
class: phpbb\textformatter\s9e\mention_helper class: phpbb\textformatter\s9e\mention_helper
arguments: arguments:
- '@dbal.conn'
- '%core.root_path%' - '%core.root_path%'
- '%core.php_ext%' - '%core.php_ext%'

View file

@ -13,8 +13,15 @@
namespace phpbb\textformatter\s9e; namespace phpbb\textformatter\s9e;
use s9e\TextFormatter\Utils;
class mention_helper 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 * @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; protected $group_profile_url;
/**
* @var array Array of users' and groups' colors for each cached ID
*/
protected $cached_colors = [];
/** /**
* Constructor * Constructor
* *
* @param \phpbb\db\driver\driver_interface $db
* @param string $root_path * @param string $root_path
* @param string $php_ext * @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->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); $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 * Inject dynamic metadata into MENTION tags in given XML
* *
@ -48,7 +111,13 @@ class mention_helper
$user_profile_url = $this->user_profile_url; $user_profile_url = $this->user_profile_url;
$group_profile_url = $this->group_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, $xml,
'MENTION', 'MENTION',
function ($attributes) use ($user_profile_url, $group_profile_url) function ($attributes) use ($user_profile_url, $group_profile_url)
@ -56,10 +125,20 @@ class mention_helper
if (isset($attributes['user_id'])) if (isset($attributes['user_id']))
{ {
$attributes['profile_url'] = str_replace('{USER_ID}', $attributes['user_id'], $user_profile_url); $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'])) else if (isset($attributes['group_id']))
{ {
$attributes['profile_url'] = str_replace('{GROUP_ID}', $attributes['group_id'], $group_profile_url); $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; return $attributes;

View file

@ -9,15 +9,19 @@
<!-- BEGIN listitem_close --></li><!-- END listitem_close --> <!-- BEGIN listitem_close --></li><!-- END listitem_close -->
<!-- BEGIN mention --> <!-- BEGIN mention -->
<xsl:text>@</xsl:text>
<xsl:choose> <xsl:choose>
<xsl:when test="@profile_url"> <xsl:when test="@profile_url">
<a> <a class="mention">
<xsl:attribute name="href"><xsl:value-of select="@profile_url"/></xsl:attribute> <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/> <xsl:apply-templates/>
</a> </a>
</xsl:when> </xsl:when>
<xsl:otherwise> <xsl:otherwise>
<span> <span class="mention">
<xsl:apply-templates/> <xsl:apply-templates/>
</span> </span>
</xsl:otherwise> </xsl:otherwise>

View file

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