team::main()   F
last analyzed

Complexity

Conditions 30
Paths 9060

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 163
Code Lines 86

Importance

Changes 0
Metric Value
dl 0
loc 163
rs 2
c 0
b 0
f 0
cc 30
eloc 86
nc 9060
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\controllers\user;
10
11
/**
12
* The team page
13
*/
14
class team implements team_interface
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \vinabb\web\controllers\cache\service_interface */
20
	protected $cache;
21
22
	/** @var \phpbb\config\config */
23
	protected $config;
24
25
	/** @var \phpbb\db\driver\driver_interface */
26
	protected $db;
27
28
	/** @var \phpbb\language\language */
29
	protected $language;
30
31
	/** @var \phpbb\template\template */
32
	protected $template;
33
34
	/** @var \phpbb\user */
35
	protected $user;
36
37
	/** @var \phpbb\controller\helper */
38
	protected $helper;
39
40
	/** @var \vinabb\web\controllers\helper\helper_interface $ext_helper */
41
	protected $ext_helper;
42
43
	/** @var \phpbb\group\helper */
44
	protected $group_helper;
45
46
	/** @var string */
47
	protected $root_path;
48
49
	/** @var string */
50
	protected $php_ext;
51
52
	/** @var array */
53
	protected $rank_data;
54
55
	/**
56
	* Constructor
57
	*
58
	* @param \phpbb\auth\auth $auth
59
	* @param \vinabb\web\controllers\cache\service_interface $cache
60
	* @param \phpbb\config\config $config
61
	* @param \phpbb\db\driver\driver_interface $db
62
	* @param \phpbb\language\language $language
63
	* @param \phpbb\template\template $template
64
	* @param \phpbb\user $user
65
	* @param \phpbb\controller\helper $helper
66
	* @param \vinabb\web\controllers\helper\helper_interface $ext_helper
67
	* @param \phpbb\group\helper $group_helper
68
	* @param string $root_path
69
	* @param string $php_ext
70
	*/
71
	public function __construct(
72
		\phpbb\auth\auth $auth,
73
		\vinabb\web\controllers\cache\service_interface $cache,
74
		\phpbb\config\config $config,
75
		\phpbb\db\driver\driver_interface $db,
76
		\phpbb\language\language $language,
77
		\phpbb\template\template $template,
78
		\phpbb\user $user,
79
		\phpbb\controller\helper $helper,
80
		\vinabb\web\controllers\helper\helper_interface $ext_helper,
81
		\phpbb\group\helper $group_helper,
82
		$root_path,
83
		$php_ext
84
	)
85
	{
86
		$this->auth = $auth;
87
		$this->cache = $cache;
88
		$this->config = $config;
89
		$this->db = $db;
90
		$this->language = $language;
91
		$this->template = $template;
92
		$this->user = $user;
93
		$this->helper = $helper;
94
		$this->ext_helper = $ext_helper;
95
		$this->group_helper = $group_helper;
96
		$this->root_path = $root_path;
97
		$this->php_ext = $php_ext;
98
99
		$this->rank_data = $this->cache->get_ranks();
100
	}
101
102
	/**
103
	* Main method
104
	*
105
	* @return \Symfony\Component\HttpFoundation\Response
106
	*/
107
	public function main()
108
	{
109
		include "{$this->root_path}includes/functions_display.{$this->php_ext}";
110
		include "{$this->root_path}includes/functions_user.{$this->php_ext}";
111
112
		// Language
113
		$this->language->add_lang('groups');
114
115
		$sql = 'SELECT *
116
			FROM ' . TEAMPAGE_TABLE . '
117
			ORDER BY teampage_position';
118
		$result = $this->db->sql_query($sql, 3600);
119
		$teampage_data = $this->db->sql_fetchrowset($result);
120
		$this->db->sql_freeresult($result);
121
122
		$sql_ary = [
123
			'SELECT'	=> 'g.group_id, g.group_name, g.group_rank, g.group_colour, g.group_type, ug.user_id as ug_user_id, t.teampage_id',
124
			'FROM'		=> [GROUPS_TABLE => 'g'],
125
			'LEFT_JOIN'	=> [
126
				[
127
					'FROM'	=> [TEAMPAGE_TABLE => 't'],
128
					'ON'	=> 't.group_id = g.group_id'
129
				],
130
				[
131
					'FROM'	=> [USER_GROUP_TABLE => 'ug'],
132
					'ON'	=> 'ug.group_id = g.group_id AND ug.user_pending = 0 AND ug.user_id = ' . (int) $this->user->data['user_id']
133
				]
134
			]
135
		];
136
137
		$result = $this->db->sql_query($this->db->sql_build_query('SELECT', $sql_ary));
138
139
		$group_ids = $groups_ary = [];
140
		while ($row = $this->db->sql_fetchrow($result))
141
		{
142
			if ($row['group_type'] == GROUP_HIDDEN && !$this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel') && $row['ug_user_id'] != $this->user->data['user_id'])
143
			{
144
				$row['group_name'] = $this->language->lang('GROUP_UNDISCLOSED');
145
				$row['u_group'] = '';
146
			}
147
			else
148
			{
149
				$row['group_name'] = $this->group_helper->get_name($row['group_name']);
150
				$row['u_group'] = $this->helper->route('vinabb_web_user_group_route', ['group_id' => $row['group_id']]);
151
			}
152
153
			if ($row['teampage_id'])
154
			{
155
				// Only put groups into the array we want to display.
156
				// We are fetching all groups, to ensure we got all data for default groups.
157
				$group_ids[] = (int) $row['group_id'];
158
			}
159
160
			$groups_ary[(int) $row['group_id']] = $row;
161
		}
162
		$this->db->sql_freeresult($result);
163
164
		$sql_ary = [
165
			'SELECT'	=> 'u.user_id, u.group_id as default_group, u.username, u.username_clean, u.user_colour, u.user_type, u.user_rank, u.user_posts, u.user_allow_pm, u.user_avatar_type, u.user_avatar, u.user_avatar_width, u.user_avatar_height, g.group_id',
166
			'FROM'		=> [USER_GROUP_TABLE => 'ug'],
167
			'LEFT_JOIN'	=> [
168
				[
169
					'FROM'	=> [USERS_TABLE => 'u'],
170
					'ON'	=> 'ug.user_id = u.user_id AND ug.user_pending = 0'
171
				],
172
				[
173
					'FROM'	=> [GROUPS_TABLE => 'g'],
174
					'ON'	=> 'ug.group_id = g.group_id'
175
				]
176
			],
177
			'WHERE'		=> $this->db->sql_in_set('g.group_id', $group_ids, false, true),
178
			'ORDER_BY'	=> 'u.username_clean'
179
		];
180
		$result = $this->db->sql_query($this->db->sql_build_query('SELECT', $sql_ary));
181
182
		$user_ary = $group_users = [];
183
		while ($row = $this->db->sql_fetchrow($result))
184
		{
185
			$user_ary[(int) $row['user_id']] = $row;
186
			$group_users[(int) $row['group_id']][] = (int) $row['user_id'];
187
		}
188
		$this->db->sql_freeresult($result);
189
190
		foreach ($teampage_data as $team_data)
191
		{
192
			// If this team entry has no group, it's a category
193
			if (!$team_data['group_id'])
194
			{
195
				$this->template->assign_block_vars('group', [
196
					'GROUP_NAME'  => $team_data['teampage_name']
197
				]);
198
199
				continue;
200
			}
201
202
			$group_data = $groups_ary[(int) $team_data['group_id']];
203
			$group_id = (int) $team_data['group_id'];
204
205
			if (!$team_data['teampage_parent'])
206
			{
207
				// If the group does not have a parent category, we display the groupname as category
208
				$this->template->assign_block_vars('group', [
209
					'GROUP_NAME'		=> $group_data['group_name'],
210
					'GROUP_RANK_RAW'	=> ($group_data['group_rank']) ? $this->rank_data[$group_data['group_rank']]['title'] : '',
211
					'GROUP_RANK'		=> ($group_data['group_rank']) ? (($this->language->is_set(['RANK_TITLES', strtoupper($this->rank_data[$group_data['group_rank']]['title'])])) ? $this->language->lang(['RANK_TITLES', strtoupper($this->rank_data[$group_data['group_rank']]['title'])]) : $this->rank_data[$group_data['group_rank']]['title']) : '',
212
					'GROUP_COLOR'		=> $group_data['group_colour'],
213
					'U_GROUP'			=> $group_data['u_group']
214
				]);
215
			}
216
217
			// Display group members
218
			if (!empty($group_users[$group_id]))
219
			{
220
				foreach ($group_users[$group_id] as $user_id)
221
				{
222
					if (isset($user_ary[$user_id]))
223
					{
224
						$row = $user_ary[$user_id];
225
226
						if ($this->config['teampage_memberships'] == 1 && ($group_id != $groups_ary[$row['default_group']]['group_id']) && $groups_ary[$row['default_group']]['teampage_id'])
227
						{
228
							// Display users in their primary group, instead of the first group, when it is displayed on the teampage.
229
							continue;
230
						}
231
232
						$user_rank_data = ($row['user_rank']) ? $this->rank_data[$row['user_rank']] : phpbb_get_user_rank($row, (($row['user_id'] == ANONYMOUS) ? false : $row['user_posts']));
233
234
						$template_vars = [
235
							'USER_ID'			=> $row['user_id'],
236
							'AVATAR_IMG'		=> ($this->user->optionget('viewavatars')) ? (($row['user_avatar_type'] == 'avatar.driver.gravatar') ? $this->ext_helper->get_gravatar_url($row) : phpbb_get_user_avatar($row)) : '',
237
							'RANK_TITLE_RAW'	=> $user_rank_data['title'],
238
							'RANK_TITLE'		=> ($this->language->is_set(['RANK_TITLES', strtoupper($user_rank_data['title'])])) ? $this->language->lang(['RANK_TITLES', strtoupper($user_rank_data['title'])]) : $user_rank_data['title'],
239
240
							'GROUP_NAME'	=> $groups_ary[$row['default_group']]['group_name'],
241
							'GROUP_COLOR'	=> $groups_ary[$row['default_group']]['group_colour'],
242
							'U_GROUP'		=> $groups_ary[$row['default_group']]['u_group'],
243
244
							'S_INACTIVE'	=> $row['user_type'] == USER_INACTIVE,
245
246
							'U_PM'	=> ($this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_'))) ? $this->helper->route('vinabb_web_ucp_route', ['id' => 'pm', 'mode' => 'compose', 'u' => $row['user_id']]) : '',
247
248
							'USERNAME_FULL'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
249
							'USERNAME'			=> get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
250
							'USER_COLOR'		=> get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
251
							'U_VIEW_PROFILE'	=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour'])
252
						];
253
254
						$this->template->assign_block_vars('group.user', $template_vars);
255
256
						if ($this->config['teampage_memberships'] != 2)
257
						{
258
							unset($user_ary[$user_id]);
259
						}
260
					}
261
				}
262
			}
263
		}
264
265
		// Breadcrumb
266
		$this->ext_helper->set_breadcrumb($this->language->lang('THE_TEAM'));
267
268
		return $this->helper->render('memberlist_team.html', $this->language->lang('THE_TEAM'));
269
	}
270
}
271