aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/group/helper.php
blob: 14c7891c8297a5e1afb0978c359060807507cd02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\group;

class helper
{
	/** @var  \phpbb\auth\auth */
	protected $auth;

	/** @var  \phpbb\cache\service */
	protected $cache;

	/** @var  \phpbb\config\config */
	protected $config;

	/** @var  \phpbb\language\language */
	protected $language;

	/** @var  \phpbb\event\dispatcher_interface */
	protected $phpbb_dispatcher;

	/** @var  \phpbb\path_helper */
	protected $phpbb_path_helper;

	/** @var  \phpbb\user */
	protected $user;

	/** @var string phpBB root path */
	protected $phpbb_root_path;

	/** @var string PHP file extension */
	protected $php_ext;

	/** @var array Default group name string templates */
	protected $default_templates = array();

	/**
	 * Constructor
	 *
	 * @param \phpbb\auth\auth $auth Authentication object
	 * @param \phpbb\cache\service $cache Cache object
	 * @param \phpbb\config\config $config Configuration object
	 * @param \phpbb\language\language $language Language object
	 * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object
	 * @param \phpbb\path_helper $phpbb_path_helper Path helper object
	 * @param \phpbb\user $user User object
	 * @param string $phpbb_root_path phpBB root path
	 * @param string $php_ext PHP file extension
	 */
	public function __construct(\phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\config\config $config, \phpbb\language\language $language, \phpbb\event\dispatcher_interface $phpbb_dispatcher,  \phpbb\path_helper $phpbb_path_helper, \phpbb\user $user, $phpbb_root_path, $php_ext)
	{
		$this->auth = $auth;
		$this->cache = $cache;
		$this->config = $config;
		$this->language = $language;
		$this->phpbb_dispatcher = $phpbb_dispatcher;
		$this->phpbb_path_helper = $phpbb_path_helper;
		$this->user = $user;

		$this->phpbb_root_path = $phpbb_root_path;

		$this->default_templates = array(
			'base_url'				=> append_sid("{$phpbb_root_path}memberlist.$php_ext", 'mode=group&amp;g={GROUP_ID}'),
			'tpl_noprofile'			=> '<span class="username">{GROUP_NAME}</span>',
			'tpl_noprofile_colour'	=> '<span style="color: {GROUP_COLOUR};" class="username-coloured">{GROUP_NAME}</span>',
			'tpl_profile'			=> '<a href="{PROFILE_URL}" class="username">{GROUP_NAME}</a>',
			'tpl_profile_colour'	=> '<a href="{PROFILE_URL}" style="color: {GROUP_COLOUR};" class="username-coloured">{GROUP_NAME}</a>',
		);
	}

	/**
	 * @param $group_name string	The stored group name
	 *
	 * @return string		Group name or translated group name if it exists
	 */
	public function get_name($group_name)
	{
		return $this->language->is_set('G_' . utf8_strtoupper($group_name)) ? $this->language->lang('G_' . utf8_strtoupper($group_name)) : $group_name;
	}

	/**
	 * Get group name details for placing into templates.
	 *
	 * @param string	$mode				Can be profile (for getting an url to the profile), group_name (for obtaining the group name), colour (for obtaining the group colour), full (for obtaining a html string representing a coloured link to the group's profile) or no_profile (the same as full but forcing no profile link)
	 * @param int		$group_id			The group id
	 * @param string	$group_name			The group name
	 * @param string	$group_colour		The group colour
	 * @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}
	 *
	 * @return string A string consisting of what is wanted based on $mode.
	 */
	public function get_name_string($mode, $group_id, $group_name, $group_colour = '', $custom_profile_url = false)
	{
		// This switch makes sure we only run code required for the mode
		switch ($mode)
		{
			case 'full':
			case 'no_profile':
			case 'colour':

				// Build correct group colour
				$group_colour = ($group_colour) ? '#' . $group_colour : '';

				// Return colour
				if ($mode === 'colour')
				{
					$group_name_string = $group_colour;
					break;
				}

			// no break;

			case 'group_name':

				// Build correct group name
				$group_name = $this->get_name($group_name);

				// Return group name
				if ($mode === 'group_name')
				{
					$group_name_string = $group_name;
					break;
				}

			// no break;

			case 'profile':

				// Build correct profile url - only show if not anonymous and permission to view profile if registered user
				// For anonymous the link leads to a login page.
				if ($group_id && ($this->user->data['user_id'] == ANONYMOUS || $this->auth->acl_get('u_viewprofile')))
				{
					$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, $this->default_templates['base_url']);
				}
				else
				{
					$profile_url = '';
				}

				// Return profile
				if ($mode === 'profile')
				{
					$group_name_string = $profile_url;
					break;
				}

			// no break;
		}

		if (!isset($group_name_string))
		{
			if (($mode === 'full' && !$profile_url) || $mode === 'no_profile')
			{
				$group_name_string = str_replace(array('{GROUP_COLOUR}', '{GROUP_NAME}'), array($group_colour, $group_name), (!$group_colour) ? $this->default_templates['tpl_noprofile'] : $this->default_templates['tpl_noprofile_colour']);
			}
			else
			{
				$group_name_string = str_replace(array('{PROFILE_URL}', '{GROUP_COLOUR}', '{GROUP_NAME}'), array($profile_url, $group_colour, $group_name), (!$group_colour) ? $this->default_templates['tpl_profile'] : $this->default_templates['tpl_profile_colour']);
			}
		}

		/**
		 * Use this event to change the output of the group name
		 *
		 * @event core.modify_group_name_string
		 * @var string	mode				profile|group_name|colour|full|no_profile
		 * @var int		group_id			The group identifier
		 * @var string	group_name			The group name
		 * @var string	group_colour		The group colour
		 * @var string	custom_profile_url	Optional parameter to specify a profile url.
		 * @var string	group_name_string	The string that has been generated
		 * @var array	_profile_cache		Array of original return templates
		 * @since 3.2.5-RC1
		 */
		$vars = array(
			'mode',
			'group_id',
			'group_name',
			'group_colour',
			'custom_profile_url',
			'group_name_string',
			'_profile_cache',
		);
		extract($this->phpbb_dispatcher->trigger_event('core.modify_group_name_string', compact($vars)));

		return $group_name_string;
	}

	/**
	 * Get group rank title and image
	 *
	 * @param array		$group_data		the current stored group data
	 *
	 * @return array                    An associative array containing the rank title (title),
	 * 									the rank image as full img tag (img) and the rank image source (img_src)
	 */
	public function get_rank($group_data)
	{
		$group_rank_data = array(
			'title'		=> null,
			'img'		=> null,
			'img_src'	=> null,
		);

		/**
		 * Preparing a group's rank before displaying
		 *
		 * @event core.get_group_rank_before
		 * @var	array	group_data		Array with group's data
		 * @since 3.2.5-RC1
		 */

		$vars = array('group_data');
		extract($this->phpbb_dispatcher->trigger_event('core.get_group_rank_before', compact($vars)));

		if (!empty($group_data['group_rank']))
		{
			// Only obtain ranks if group rank is set
			$ranks = $this->cache->obtain_ranks();

			if (isset($ranks['special'][$group_data['group_rank']]))
			{
				$rank = $ranks['special'][$group_data['group_rank']];

				$group_rank_data['title'] = $rank['rank_title'];

				$group_rank_data['img_src'] = (!empty($rank['rank_image'])) ? $this->phpbb_path_helper->update_web_root_path($this->phpbb_root_path . $this->config['ranks_path'] . '/' . $rank['rank_image']) : '';

				$group_rank_data['img'] = (!empty($rank['rank_image'])) ? '<img src="' . $group_rank_data['img_src'] . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" />' : '';
			}
		}

		/**
		 * Modify a group's rank before displaying
		 *
		 * @event core.get_group_rank_after
		 * @var	array	group_data		Array with group's data
		 * @var	array	group_rank_data	Group rank data
		 * @since 3.2.5-RC1
		 */

		$vars = array(
			'group_data',
			'group_rank_data',
		);
		extract($this->phpbb_dispatcher->trigger_event('core.get_group_rank_after', compact($vars)));

		return $group_rank_data;
	}

	/**
	 * Get group avatar.
	 * Wrapper function for phpbb_get_group_avatar()
	 *
	 * @param array		$group_row		Row from the groups table
	 * @param string	$alt			Optional language string for alt tag within image, can be a language key or text
	 * @param bool		$ignore_config	Ignores the config-setting, to be still able to view the avatar in the UCP
	 * @param bool		$lazy			If true, will be lazy loaded (requires JS)
	 *
	 * @return string 					Avatar html
	 */
	function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
	{
		return phpbb_get_group_avatar($group_row, $alt, $ignore_config, $lazy);
	}
}