Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.95% covered (warning)
78.95%
75 / 95
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
helper
78.95% covered (warning)
78.95%
75 / 95
83.33% covered (warning)
83.33%
5 / 6
51.47
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 get_name
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_name_string
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
22
 get_rank
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
5
 get_avatar
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 display_legend
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\group;
15
16use phpbb\auth\auth;
17use phpbb\avatar\helper as avatar_helper;
18use phpbb\cache\service as cache;
19use phpbb\config\config;
20use phpbb\db\driver\driver_interface;
21use phpbb\language\language;
22use phpbb\event\dispatcher_interface;
23use phpbb\path_helper;
24use phpbb\template\template;
25use phpbb\user;
26
27class helper
28{
29    /** @var auth */
30    protected $auth;
31
32    /** @var avatar_helper */
33    protected $avatar_helper;
34
35    /** @var cache */
36    protected $cache;
37
38    /** @var config */
39    protected $config;
40
41    /** @var language */
42    protected $language;
43
44    /** @var dispatcher_interface */
45    protected $dispatcher;
46
47    /** @var path_helper */
48    protected $path_helper;
49
50    /** @var user */
51    protected $user;
52
53    /** @var string phpBB root path */
54    protected $phpbb_root_path;
55
56    /** @var array Return templates for a group name string */
57    protected $name_strings;
58
59    /**
60     * Constructor
61     *
62     * @param auth                    $auth            Authentication object
63     * @param avatar_helper            $avatar_helper    Avatar helper object
64     * @param cache                    $cache            Cache service object
65     * @param config                $config            Configuration object
66     * @param language                $language        Language object
67     * @param dispatcher_interface    $dispatcher        Event dispatcher object
68     * @param path_helper            $path_helper    Path helper object
69     * @param user                    $user            User object
70     */
71    public function __construct(auth $auth, avatar_helper $avatar_helper, cache $cache, config $config, language $language, dispatcher_interface $dispatcher, path_helper $path_helper, user $user)
72    {
73        $this->auth = $auth;
74        $this->avatar_helper = $avatar_helper;
75        $this->cache = $cache;
76        $this->config = $config;
77        $this->language = $language;
78        $this->dispatcher = $dispatcher;
79        $this->path_helper = $path_helper;
80        $this->user = $user;
81
82        $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
83
84        /** @html Group name spans and links for usage in the template */
85        $this->name_strings = array(
86            'base_url'                => "{$path_helper->get_phpbb_root_path()}memberlist.{$path_helper->get_php_ext()}?mode=group&amp;g={GROUP_ID}",
87            'tpl_noprofile'            => '<span class="username">{GROUP_NAME}</span>',
88            'tpl_noprofile_colour'    => '<span class="username-coloured" style="color: {GROUP_COLOUR};">{GROUP_NAME}</span>',
89            'tpl_profile'            => '<a class="username" href="{PROFILE_URL}">{GROUP_NAME}</a>',
90            'tpl_profile_colour'    => '<a class="username-coloured" href="{PROFILE_URL}" style="color: {GROUP_COLOUR};">{GROUP_NAME}</a>',
91        );
92    }
93
94    /**
95     * @param    string    $group_name    The stored group name
96     *
97     * @return string        Group name or translated group name if it exists
98     */
99    public function get_name($group_name)
100    {
101        return $this->language->is_set('G_' . utf8_strtoupper($group_name)) ? $this->language->lang('G_' . utf8_strtoupper($group_name)) : $group_name;
102    }
103
104    /**
105     * Get group name details for placing into templates.
106     *
107     * @html Group name spans and links
108     *
109     * @param string    $mode                Profile (for getting an url to the profile),
110     *                                            group_name (for obtaining the group name),
111     *                                            colour (for obtaining the group colour),
112     *                                            full (for obtaining a coloured group name link to the group's profile),
113     *                                            no_profile (the same as full but forcing no profile link)
114     * @param int        $group_id            The group id
115     * @param string    $group_name            The group name
116     * @param string    $group_colour        The group colour
117     * @param mixed        $custom_profile_url    optional parameter to specify a profile url. The group id gets appended to this url as &amp;g={group_id}
118     *
119     * @return string A string consisting of what is wanted based on $mode.
120     */
121    public function get_name_string($mode, $group_id, $group_name, $group_colour = '', $custom_profile_url = false)
122    {
123        $s_is_bots = ($group_name === 'BOTS');
124        $group_name_string = null;
125
126        // This switch makes sure we only run code required for the mode
127        switch ($mode)
128        {
129            case 'full':
130            case 'no_profile':
131            case 'colour':
132
133                // Build correct group colour
134                $group_colour = $group_colour ? '#' . $group_colour : '';
135
136                // Return colour
137                if ($mode === 'colour')
138                {
139                    $group_name_string = $group_colour;
140                    break;
141                }
142
143            // no break;
144
145            case 'group_name':
146
147                // Build correct group name
148                $group_name = $this->get_name($group_name);
149
150                // Return group name
151                if ($mode === 'group_name')
152                {
153                    $group_name_string = $group_name;
154                    break;
155                }
156
157            // no break;
158
159            case 'profile':
160
161                // Build correct profile url - only show if not anonymous and permission to view profile if registered user
162                // For anonymous the link leads to a login page.
163                if ($group_id && !$s_is_bots && ($this->user->data['user_id'] == ANONYMOUS || $this->auth->acl_get('u_viewprofile')))
164                {
165                    $profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&amp;g=' . (int) $group_id : str_replace(array('={GROUP_ID}', '=%7BGROUP_ID%7D'), '=' . (int) $group_id, append_sid($this->name_strings['base_url']));
166                }
167                else
168                {
169                    $profile_url = '';
170                }
171
172                // Return profile
173                if ($mode === 'profile')
174                {
175                    $group_name_string = $profile_url;
176                    break;
177                }
178
179            // no break;
180        }
181
182        if (!isset($group_name_string))
183        {
184            if (($mode === 'full' && empty($profile_url)) || $mode === 'no_profile' || $s_is_bots)
185            {
186                $group_name_string = str_replace(array('{GROUP_COLOUR}', '{GROUP_NAME}'), array($group_colour, $group_name), (!$group_colour) ? $this->name_strings['tpl_noprofile'] : $this->name_strings['tpl_noprofile_colour']);
187            }
188            else
189            {
190                $group_name_string = str_replace(array('{PROFILE_URL}', '{GROUP_COLOUR}', '{GROUP_NAME}'), array($profile_url, $group_colour, $group_name), (!$group_colour) ? $this->name_strings['tpl_profile'] : $this->name_strings['tpl_profile_colour']);
191            }
192        }
193
194        $name_strings = $this->name_strings;
195
196        /**
197         * Use this event to change the output of the group name
198         *
199         * @event core.modify_group_name_string
200         * @var string    mode                profile|group_name|colour|full|no_profile
201         * @var int        group_id            The group identifier
202         * @var string    group_name            The group name
203         * @var string    group_colour        The group colour
204         * @var string    custom_profile_url    Optional parameter to specify a profile url.
205         * @var string    group_name_string    The string that has been generated
206         * @var array    name_strings        Array of original return templates
207         * @since 3.2.8-RC1
208         */
209        $vars = array(
210            'mode',
211            'group_id',
212            'group_name',
213            'group_colour',
214            'custom_profile_url',
215            'group_name_string',
216            'name_strings',
217        );
218        extract($this->dispatcher->trigger_event('core.modify_group_name_string', compact($vars)));
219
220        return $group_name_string;
221    }
222
223    /**
224     * Get group rank title and image
225     *
226     * @html Group rank image element
227     *
228     * @param array        $group_data        The current stored group data
229     *
230     * @return array                    An associative array containing the rank title (title),
231     *                                     the rank image as full img tag (img) and the rank image source (img_src)
232     */
233    public function get_rank($group_data)
234    {
235        $group_rank_data = array(
236            'title'        => null,
237            'img'        => null,
238            'img_src'    => null,
239        );
240
241        /**
242         * Preparing a group's rank before displaying
243         *
244         * @event core.get_group_rank_before
245         * @var    array    group_data        Array with group's data
246         * @since 3.2.8-RC1
247         */
248
249        $vars = array('group_data');
250        extract($this->dispatcher->trigger_event('core.get_group_rank_before', compact($vars)));
251
252        if (!empty($group_data['group_rank']))
253        {
254            // Only obtain ranks if group rank is set
255            $ranks = $this->cache->obtain_ranks();
256
257            if (isset($ranks['special'][$group_data['group_rank']]))
258            {
259                $rank = $ranks['special'][$group_data['group_rank']];
260
261                $group_rank_data['title'] = $rank['rank_title'];
262
263                $group_rank_data['img_src'] = (!empty($rank['rank_image'])) ? $this->path_helper->update_web_root_path($this->phpbb_root_path . $this->config['ranks_path'] . '/' . $rank['rank_image']) : '';
264
265                /** @html Group rank image element for usage in the template */
266                $group_rank_data['img'] = (!empty($rank['rank_image'])) ? '<img src="' . $group_rank_data['img_src'] . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" />' : '';
267            }
268        }
269
270        /**
271         * Modify a group's rank before displaying
272         *
273         * @event core.get_group_rank_after
274         * @var    array    group_data        Array with group's data
275         * @var    array    group_rank_data    Group rank data
276         * @since 3.2.8-RC1
277         */
278
279        $vars = array(
280            'group_data',
281            'group_rank_data',
282        );
283        extract($this->dispatcher->trigger_event('core.get_group_rank_after', compact($vars)));
284
285        return $group_rank_data;
286    }
287
288    /**
289     * Get group avatar.
290     * Wrapper function for \phpbb\avatar\helper::get_group_avatar()
291     *
292     * @param array        $group_row        Row from the groups table
293     * @param string    $alt            Optional language string for alt tag within image, can be a language key or text
294     * @param bool        $ignore_config    Ignores the config-setting, to be still able to view the avatar in the UCP
295     * @param bool        $lazy            If true, will be lazy loaded (requires JS)
296     *
297     * @return array                     Avatar data
298     */
299    public function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
300    {
301        return $this->avatar_helper->get_group_avatar($group_row, $alt, $ignore_config, $lazy);
302    }
303
304    /**
305     * Display groups legend
306     *
307     * @param driver_interface $db
308     * @param template $template
309     * @return void
310     */
311    public function display_legend(driver_interface $db, template $template): void
312    {
313        $order_legend = $this->config['legend_sort_groupname'] ? 'group_name' : 'group_legend';
314
315        // Grab group details for legend display
316        if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
317        {
318            $sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend
319                FROM ' . GROUPS_TABLE . '
320                WHERE group_legend > 0
321                ORDER BY ' . $order_legend . ' ASC';
322        }
323        else
324        {
325            $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend
326                FROM ' . GROUPS_TABLE . ' g
327                LEFT JOIN ' . USER_GROUP_TABLE . ' ug
328                    ON (
329                        g.group_id = ug.group_id
330                        AND ug.user_id = ' . $this->user->data['user_id'] . '
331                        AND ug.user_pending = 0
332                    )
333                WHERE g.group_legend > 0
334                    AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ')
335                ORDER BY g.' . $order_legend . ' ASC';
336        }
337        $result = $db->sql_query($sql);
338
339        while ($row = $db->sql_fetchrow($result))
340        {
341            $show_group_url = $row['group_name'] != 'BOTS' && $this->auth->acl_get('u_viewprofile');
342
343            $template->assign_block_vars('LEGEND', [
344                'GROUP_COLOR'        => $row['group_colour'] ?: '',
345                'GROUP_NAME'        => $this->get_name($row['group_name']),
346                'GROUP_URL'            => $show_group_url ? append_sid("{$this->path_helper->get_phpbb_root_path()}memberlist.{$this->path_helper->get_php_ext()}", 'mode=group&amp;g=' . $row['group_id']) : '',
347            ]);
348        }
349        $db->sql_freeresult($result);
350    }
351}