Completed
Push — develop ( 29678f...b18c7a )
by Daniel
14:18
created

user_data::get_rank()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 14
nc 4
nop 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services;
11
12
class user_data
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\profilefields\manager */
24
	protected $profile_fields;
25
26
	/** @var \phpbb\language\language */
27
	protected $translator;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var string */
33
	protected $phpbb_root_path;
34
35
	/** @var string */
36
	protected $php_ext;
37
38
	/** @var array */
39
	protected $user_cache = array();
40
41
	/**
42
	 * Constructor
43
	 
44
	 * 
45
	 * @param \phpbb\auth\auth					$auth					Auth object
46
	 * @param \phpbb\config\config				$config					Config object
47
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
48
	 * @param \phpbb\profilefields\manager      $profile_fields			Profile fields manager
49
	 * @param \phpbb\language\language			$translator				Language object
50
	 * @param \phpbb\user						$user					User Object
51
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
52
	 * @param string							$php_ext				php file extension
53
	 */
54
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\profilefields\manager $profile_fields, \phpbb\language\language $translator, \phpbb\user $user, $phpbb_root_path, $php_ext)
55
	{
56
		$this->auth = $auth;
57
		$this->config = $config;
58
		$this->db = $db;
59
		$this->profile_fields = $profile_fields;
60
		$this->translator = $translator;
61
		$this->user = $user;
62
		$this->phpbb_root_path = $phpbb_root_path;
63
		$this->php_ext = $php_ext;
64
	}
65
66
	/**
67
	 * @param array $user_ids
68
	 * @param string $sql_where
69
	 * @return array
70
	 */
71
	public function get_users(array $user_ids, $sql_where = '')
72
	{
73
		$query_ids = array_diff($user_ids, array_keys($this->user_cache));
74
75
		if (sizeof($query_ids))
76
		{
77
			$sql_where .= (($sql_where) ? ' AND ' : '') . $this->db->sql_in_set('user_id', $query_ids);
78
			$this->query($sql_where);
79
		}
80
81
		return array_intersect_key($this->user_cache, array_flip($user_ids));
82
	}
83
84
	/**
85
	 * @param string $sql_where
86
	 * @param string $order_by
87
	 * @param int|bool $limit
88
	 * @return array|false
89
	 */
90
	public function query($sql_where = '', $order_by = '', $limit = false)
91
	{
92
		$sql = $this->get_sql_statement($sql_where, $order_by);
93
		$result = $this->db->sql_query_limit($sql, $limit);
94
95
		$users = array();
96
		while ($row = $this->db->sql_fetchrow($result))
97
		{
98
			$user_id = $row['user_id'];
99
			$users[$user_id] = $row;
100
101
			$this->user_cache[$user_id] = $this->get_data($row);
102
			$this->user_cache[$user_id] += $this->get_rank($row);
103
		}
104
		$this->db->sql_freeresult($result);
105
106
		if (!sizeof($users))
107
		{
108
			return false;
109
		}
110
111
		$this->get_additional_fields($users);
112
		unset($users);
113
114
		return $this->user_cache;
115
	}
116
117
	/**
118
	 * @param array $row
119
	 * @return array
120
	 */
121
	public function get_data($row)
122
	{
123
		$user_id = $row['user_id'];
124
		$date_format = $this->translator->lang('DATE_FORMAT');
125
126
		return array(
127
			'AVATAR'			=> $this->get_avatar($row),
128
			'USERNAME'			=> get_username_string('username', $user_id, $row['username'], $row['user_colour']),
129
			'USERNAME_FULL'		=> get_username_string('full', $user_id, $row['username'], $row['user_colour']),
130
131
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
132
			'VISITED'			=> $this->get_last_visit_date($row['user_lastvisit'], $date_format),
133
			'POSTS'				=> $row['user_posts'],
134
			'POSTS_PCT'			=> $this->translator->lang_array('POST_PCT', $this->calculate_percent_posts($row['user_posts'])),
135
136
			'CONTACT_USER' 		=> $this->translator->lang('CONTACT_USER', get_username_string('username', $user_id, $row['username'], $row['user_colour'], $row['username'])),
137
138
			'U_SEARCH_POSTS'	=> $this->get_search_url($user_id),
139
			'U_VIEWPROFILE'		=> get_username_string('profile', $user_id, $row['username'], $row['user_colour']),
140
141
			'contact_fields'	=> array(),
142
			'profile_fields'	=> array(),
143
		);
144
	}
145
146
	/**
147
	 * @return array
148
	 */
149
	public function get_profile_fields()
150
	{
151
		$sql = 'SELECT l.lang_name, f.field_ident
152
			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
153
			WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . '
154
				AND f.field_active = 1
155
				AND f.field_no_view = 0
156
				AND f.field_hide = 0
157
				AND l.field_id = f.field_id
158
			ORDER BY f.field_order';
159
		$result = $this->db->sql_query($sql);
160
161
		$cpf_options = false;
162
		while ($row = $this->db->sql_fetchrow($result))
163
		{
164
			$cpf_options[$row['field_ident']] = $row['lang_name'];
165
		}
166
		$this->db->sql_freeresult($result);
167
168
		return $cpf_options;
169
	}
170
171
	/**
172
	 * @param array $row
173
	 * @return string
174
	 */
175
	protected function get_avatar(array $row)
176
	{
177
		return ($this->user->optionget('viewavatars')) ? phpbb_get_user_avatar($row) : '';
178
	}
179
180
	/**
181
	 * @param int $user_posts
182
	 * @return int|mixed
183
	 */
184
	protected function calculate_percent_posts($user_posts)
185
	{
186
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
187
	}
188
189
	/**
190
	 * @param int $last_visited
191
	 * @param string $date_format
192
	 * @return string
193
	 */
194
	protected function get_last_visit_date($last_visited, $date_format)
195
	{
196
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
197
	}
198
199
	/**
200
	 * @param int $user_id
201
	 * @return string
202
	 */
203
	protected function get_search_url($user_id)
204
	{
205
		return ($this->auth->acl_get('u_search')) ? append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id=$user_id&amp;sr=posts") : '';
206
	}
207
208
	/**
209
	 * @param array $row
210
	 * @return array
211
	 */
212
	public function get_rank(array $row)
213
	{
214
		if (!function_exists('phpbb_get_user_rank'))
215
		{
216
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
217
		}
218
219
		$user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
220
221
		if (!empty($user_rank_data))
222
		{
223
			return array(
224
				'RANK_TITLE'		=> $user_rank_data['title'],
225
				'RANK_IMAGE'		=> $user_rank_data['img'],
226
				'RANK_IMAGE_SRC'	=> $user_rank_data['img_src'],
227
			);
228
		}
229
		else
230
		{
231
			return array(
232
				'RANK_TITLE'		=> '',
233
				'RANK_IMAGE'		=> '',
234
				'RANK_IMAGE_SRC'	=> '',
235
			);
236
		}
237
	}
238
239
	/**
240
	 * @param array $users
241
	 */
242
	protected function get_additional_fields(array $users)
243
	{
244
		$user_ids = array_keys($users);
245
246
		$can_receive_pm_list = $this->get_can_receive_pm_list($user_ids);
247
		$permanently_banned_users = $this->get_banned_users_list($user_ids);
248
249
		// Grab all profile fields from users in id cache for later use - similar to the poster cache
250
		$profile_fields_cache = $this->profile_fields->grab_profile_fields_data($user_ids);
251
252
		for ($i = 0, $size = sizeof($user_ids); $i < $size; $i++)
253
		{
254
			$id = $user_ids[$i];
255
			$row = $users[$id];
256
257
			$this->user_cache[$id]['contact_fields'] = array_filter(array(
258
				'pm'		=> $this->get_pm_contact($row, $can_receive_pm_list, $permanently_banned_users),
259
				'email'		=> $this->get_email_contact($row),
260
				'jabber'	=> $this->get_jabber_contact($row),
261
			));
262
			$this->get_custom_profile_fields($id, $profile_fields_cache);
263
		}
264
	}
265
266
	/**
267
	 * @param array $row
268
	 * @return array
269
	 */
270
	protected function get_email_contact(array &$row)
271
	{
272
		$email = array();
273
		if ((!empty($row['user_allow_viewemail']) && $this->auth->acl_get('u_sendemail')) || $this->auth->acl_get('a_email'))
274
		{
275
			$email = array(
276
				'ID'		=> 'email',
277
				'NAME'		=> $this->translator->lang('SEND_EMAIL'),
278
				'U_CONTACT'	=> ($this->config['board_email_form'] && $this->config['email_enable']) ? append_sid("{$this->phpbb_root_path}memberlist.$this->php_ext", 'mode=email&amp;u=' . $row['user_id']) : (($this->config['board_hide_emails'] && !$this->auth->acl_get('a_email')) ? '' : 'mailto:' . $row['user_email']),
279
			);
280
		}
281
282
		return $email;
283
	}
284
285
	/**
286
	 * @param array $row
287
	 * @param array $can_receive_pm_list
288
	 * @param array $permanently_banned_users
289
	 * @return array
290
	 */
291
	protected function get_pm_contact(array &$row, array $can_receive_pm_list, array $permanently_banned_users)
292
	{
293
		$pm = array();
294
		if ($this->user_can_pm() && $this->can_receive_pm($row, $can_receive_pm_list, $permanently_banned_users))
295
		{
296
			$pm = array(
297
				'ID'		=> 'pm',
298
				'NAME' 		=> $this->translator->lang('SEND_PRIVATE_MESSAGE'),
299
				'U_CONTACT'	=> append_sid("{$this->phpbb_root_path}ucp.{$this->php_ext}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']),
300
			);
301
		}
302
303
		return $pm;
304
	}
305
306
	/**
307
	 * @param array $row
308
	 * @return array
309
	 */
310
	protected function get_jabber_contact(array &$row)
311
	{
312
		$jabber = array();
313
		if ($this->user_can_jabber($row))
314
		{
315
			$jabber = array(
316
				'ID'		=> 'jabber',
317
				'NAME' 		=> $this->translator->lang('JABBER'),
318
				'U_CONTACT'	=> append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=contact&amp;action=jabber&amp;u=' . $row['user_id']),
319
			);
320
		}
321
322
		return $jabber;
323
	}
324
325
	/**
326
	 * @return bool
327
	 */
328
	protected function user_can_pm()
329
	{
330
		return ($this->config['allow_privmsg'] && $this->auth->acl_get('u_sendpm')) ? true : false;
331
	}
332
333
	/**
334
	 * @param array $row
335
	 * @return bool
336
	 */
337
	protected function user_can_jabber(array $row)
338
	{
339
		return ($this->config['jab_enable'] && $row['user_jabber'] && $this->auth->acl_get('u_sendim')) ? true : false;
340
	}
341
342
	/**
343
	 * @param array $row
344
	 * @param array $can_receive_pm_list
345
	 * @param array $permanently_banned_users
346
	 * @return bool
347
	 */
348
	protected function can_receive_pm(array $row, array $can_receive_pm_list, array $permanently_banned_users)
349
	{
350
		return (
351
			// They must be a "normal" user
352
			$row['user_type'] != USER_IGNORE &&
353
354
			// They must not be deactivated by the administrator
355
			($row['user_type'] != USER_INACTIVE || $row['user_inactive_reason'] != INACTIVE_MANUAL) &&
356
357
			// They must be able to read PMs
358
			in_array($row['user_id'], $can_receive_pm_list) &&
359
360
			// They must not be permanently banned
361
			!in_array($row['user_id'], $permanently_banned_users) &&
362
363
			// They must allow users to contact via PM
364
			(($this->auth->acl_gets('a_', 'm_') || $this->auth->acl_getf_global('m_')) || $row['allow_pm'])
365
		);
366
	}
367
368
	/**
369
	 * @param int $user_id
370
	 * @param array $profile_fields_cache
371
	 */
372
	protected function get_custom_profile_fields($user_id, array $profile_fields_cache)
373
	{
374
		$cp_row = (isset($profile_fields_cache[$user_id])) ? $this->profile_fields->generate_profile_fields_template_data($profile_fields_cache[$user_id]) : array('blockrow' => array());
375
376
		foreach ($cp_row['blockrow'] as $field_data)
377
		{
378
			$field = $field_data['PROFILE_FIELD_IDENT'];
379
380
			if ($field_data['S_PROFILE_CONTACT'])
381
			{
382
				$this->user_cache[$user_id]['contact_fields'][$field] = array(
383
					'ID'		=> $field,
384
					'NAME'		=> $field_data['PROFILE_FIELD_NAME'],
385
					'U_CONTACT'	=> $field_data['PROFILE_FIELD_CONTACT'],
386
				);
387
			}
388
			else
389
			{
390
				$this->user_cache[$user_id]['profile_fields'][$field] = $field_data;
391
			}
392
		}
393
	}
394
395
	/**
396
	 * Get the list of users who can receive private messages
397
	 *
398
	 * @param array $user_ids
399
	 * @return array
400
	 */
401
	protected function get_can_receive_pm_list(array $user_ids)
402
	{
403
		$can_receive_pm_list = $this->auth->acl_get_list($user_ids, 'u_readpm');
404
		return (empty($can_receive_pm_list) || !isset($can_receive_pm_list[0]['u_readpm'])) ? array() : $can_receive_pm_list[0]['u_readpm'];
405
	}
406
407
	/**
408
	 * Get the list of permanently banned users
409
	 *
410
	 * @param array $user_ids
411
	 * @return array
412
	 */
413
	protected function get_banned_users_list(array $user_ids)
414
	{
415
		if (!function_exists('phpbb_get_banned_user_ids'))
416
		{
417
			include($this->phpbb_root_path . 'includes/functions_user.' . $this->php_ext);
418
		}
419
420
		return phpbb_get_banned_user_ids($user_ids, false);
421
	}
422
423
	/**
424
	 * @param array $ids
0 ignored issues
show
Bug introduced by
There is no parameter named $ids. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
425
	 * @param string $sql_where
426
	 * @param string $order_by
427
	 * @return string
428
	 */
429
	protected function get_sql_statement($sql_where = '', $order_by = '')
430
	{
431
		return 'SELECT user_id, username, user_type, user_colour, user_avatar, user_avatar_type, user_avatar_height, user_avatar_width, user_regdate, user_lastvisit, user_birthday, user_posts, user_rank, user_allow_viewemail, user_allow_pm, user_jabber, user_inactive_reason
432
			FROM ' . USERS_TABLE .
433
			(($sql_where) ? ' WHERE ' . $sql_where : '') .
434
			(($order_by) ? ' ORDER BY ' . $order_by : '');
435
	}
436
}
437