From 4b31a29c2cfd809b26874cec5b0954ee3ccb88ac Mon Sep 17 00:00:00 2001 From: lavigor Date: Wed, 6 Jun 2018 03:51:52 +0300 Subject: [PATCH] [ticket/13713] Implement colour handling PHPBB3-13713 --- .../container/services_text_formatter.yml | 1 + .../textformatter/s9e/mention_helper.php | 83 ++++++++++++++++++- phpBB/styles/prosilver/template/bbcode.html | 8 +- phpBB/styles/prosilver/theme/content.css | 5 ++ 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/phpBB/config/default/container/services_text_formatter.yml b/phpBB/config/default/container/services_text_formatter.yml index df24d9a080..119e0b2ba4 100644 --- a/phpBB/config/default/container/services_text_formatter.yml +++ b/phpBB/config/default/container/services_text_formatter.yml @@ -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%' diff --git a/phpBB/phpbb/textformatter/s9e/mention_helper.php b/phpBB/phpbb/textformatter/s9e/mention_helper.php index 4f19a9dc5f..96bc896bad 100644 --- a/phpBB/phpbb/textformatter/s9e/mention_helper.php +++ b/phpBB/phpbb/textformatter/s9e/mention_helper.php @@ -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; diff --git a/phpBB/styles/prosilver/template/bbcode.html b/phpBB/styles/prosilver/template/bbcode.html index 71e8d2b0d2..8ee7351486 100644 --- a/phpBB/styles/prosilver/template/bbcode.html +++ b/phpBB/styles/prosilver/template/bbcode.html @@ -9,15 +9,19 @@ +@ - + + + color: #; + - + diff --git a/phpBB/styles/prosilver/theme/content.css b/phpBB/styles/prosilver/theme/content.css index 338016ac2c..701fa5fd94 100644 --- a/phpBB/styles/prosilver/theme/content.css +++ b/phpBB/styles/prosilver/theme/content.css @@ -576,6 +576,11 @@ blockquote .codebox { padding: 5px 3px; } +/* Mention block */ +.mention { + font-weight: bold; +} + /* Attachments ---------------------------------------- */ .attachbox {