Passed
Branch develop (786e4f)
by Daniel
07:32
created

data::get_custom_profile_fields()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 6
nop 2
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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\users;
11
12
class data extends contacts
13
{
14
	/** @var \phpbb\auth\auth */
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
	protected $auth;
16
17
	/** @var \phpbb\config\config */
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	protected $db;
22
23
	/** @var \phpbb\profilefields\manager */
0 ignored issues
show
Bug introduced by
The type phpbb\profilefields\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $profile_fields;
25
26
	/** @var \phpbb\language\language */
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $translator;
28
29
	/** @var \phpbb\user */
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
	protected $user;
31
32
	/** @var \blitze\sitemaker\services\util */
33
	protected $util;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var string */
42
	protected $default_avatar;
43
44
	/** @var array */
45
	protected $user_cache = array();
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\auth\auth					$auth					Auth object
51
	 * @param \phpbb\config\config				$config					Config object
52
	 * @param \phpbb\db\driver\driver_interface	$db     				Database connection
53
	 * @param \phpbb\profilefields\manager      $profile_fields			Profile fields manager
54
	 * @param \phpbb\language\language			$translator				Language object
55
	 * @param \phpbb\user						$user					User Object
56
	 * @param \blitze\sitemaker\services\util	$util					Sitemaker utility Object
57
	 * @param string							$phpbb_root_path		Path to the phpbb includes directory.
58
	 * @param string							$php_ext				php file extension
59
	 */
60 96
	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, \blitze\sitemaker\services\util $util, $phpbb_root_path, $php_ext)
61
	{
62 96
		parent::__construct($auth, $config, $translator, $user, $phpbb_root_path, $php_ext);
63
64 96
		$this->auth = $auth;
65 96
		$this->config = $config;
66 96
		$this->db = $db;
67 96
		$this->profile_fields = $profile_fields;
68 96
		$this->translator = $translator;
69 96
		$this->user = $user;
70 96
		$this->util = $util;
71 96
		$this->phpbb_root_path = $phpbb_root_path;
72 96
		$this->php_ext = $php_ext;
73 96
	}
74
75
	/**
76
	 * @param array $user_ids
77
	 * @param string $sql_where
78
	 * @return array
79
	 */
80 14
	public function get_users(array $user_ids, $sql_where = '')
81
	{
82 14
		$query_ids = array_diff($user_ids, array_keys($this->user_cache));
83
84 14
		if (sizeof($query_ids))
85 14
		{
86 14
			$sql_where .= (($sql_where) ? ' AND ' : '') . $this->db->sql_in_set('user_id', $query_ids);
87 14
			$this->query($sql_where);
88 14
		}
89
90 14
		return array_intersect_key($this->user_cache, array_flip($user_ids));
91
	}
92
93
	/**
94
	 * @param string $sql_where
95
	 * @param string $order_by
96
	 * @param int|bool $limit
97
	 * @return array|bool
98
	 */
99 16
	public function query($sql_where = '', $order_by = '', $limit = false)
100
	{
101 16
		$sql = $this->get_sql_statement($sql_where, $order_by);
102 16
		$result = $this->db->sql_query_limit($sql, $limit);
103
104 16
		$users = array();
105 16
		while ($row = $this->db->sql_fetchrow($result))
106
		{
107 14
			$user_id = $row['user_id'];
108 14
			$users[$user_id] = $row;
109
110 14
			$this->user_cache[$user_id] = $this->get_data($row);
111 14
			$this->user_cache[$user_id] += $this->get_rank($row);
112 14
		}
113 16
		$this->db->sql_freeresult($result);
114
115 16
		if (!sizeof($users))
116 16
		{
117 4
			return false;
118
		}
119
120 14
		$this->get_additional_fields($users);
121 14
		unset($users);
122
123 14
		return $this->user_cache;
124
	}
125
126
	/**
127
	 * @param array $row
128
	 * @return array
129
	 */
130 14
	public function get_data($row)
131
	{
132 14
		$user_id = $row['user_id'];
133 14
		$date_format = $this->translator->lang('DATE_FORMAT');
134
135 14
		$this->default_avatar = $this->util->get_default_avatar();
136
137
		return array(
138 14
			'avatar'			=> $this->get_avatar($row),
139 14
			'username'			=> get_username_string('username', $user_id, $row['username'], $row['user_colour']),
0 ignored issues
show
Bug introduced by
The function get_username_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
			'username'			=> /** @scrutinizer ignore-call */ get_username_string('username', $user_id, $row['username'], $row['user_colour']),
Loading history...
140 14
			'username_full'		=> get_username_string('full', $user_id, $row['username'], $row['user_colour']),
141
142 14
			'joined'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
143 14
			'visited'			=> $this->get_last_visit_date($row['user_lastvisit'], $date_format),
144 14
			'posts'				=> $row['user_posts'],
145 14
			'posts_pct'			=> $this->translator->lang_array('POST_PCT', $this->calculate_percent_posts($row['user_posts'])),
146
147 14
			'contact_user' 		=> $this->translator->lang('CONTACT_USER', get_username_string('username', $user_id, $row['username'], $row['user_colour'], $row['username'])),
148
149 14
			'u_search_posts'	=> $this->get_search_url($user_id),
150 14
			'u_viewprofile'		=> get_username_string('profile', $user_id, $row['username'], $row['user_colour']),
151
152 14
			'contact_fields'	=> array(),
153 14
			'profile_fields'	=> array(),
154 14
		);
155
	}
156
157
	/**
158
	 * @return string[]
159
	 */
160 1
	public function get_profile_fields()
161
	{
162
		$sql = 'SELECT l.lang_name, f.field_ident
163 1
			FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\service...rs\PROFILE_FIELDS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant blitze\sitemaker\services\users\PROFILE_LANG_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
164 1
			WHERE l.lang_id = ' . (int) $this->user->get_iso_lang_id() . '
165
				AND f.field_active = 1
166
				AND f.field_no_view = 0
167
				AND f.field_hide = 0
168
				AND l.field_id = f.field_id
169 1
			ORDER BY f.field_order';
170 1
		$result = $this->db->sql_query($sql);
171
172 1
		$cpf_options = array();
173 1
		while ($row = $this->db->sql_fetchrow($result))
174
		{
175 1
			$cpf_options[$row['field_ident']] = $row['lang_name'];
176 1
		}
177 1
		$this->db->sql_freeresult($result);
178
179 1
		return $cpf_options;
180
	}
181
182
	/**
183
	 * @param array $row
184
	 * @return string
185
	 */
186 14
	protected function get_avatar(array $row)
187
	{
188 14
		$avatar = '';
189 14
		if ($this->user->optionget('viewavatars'))
190 14
		{
191 14
			$avatar = ($row['user_avatar']) ? phpbb_get_user_avatar($row) : $this->default_avatar;
0 ignored issues
show
Bug introduced by
The function phpbb_get_user_avatar was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
			$avatar = ($row['user_avatar']) ? /** @scrutinizer ignore-call */ phpbb_get_user_avatar($row) : $this->default_avatar;
Loading history...
192 14
		}
193
194 14
		return $avatar;
195
	}
196
197
	/**
198
	 * @param int $user_posts
199
	 * @return int
200
	 */
201 14
	protected function calculate_percent_posts($user_posts)
202
	{
203 14
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
204
	}
205
206
	/**
207
	 * @param int $last_visited
208
	 * @param string $date_format
209
	 * @return string
210
	 */
211 14
	protected function get_last_visit_date($last_visited, $date_format)
212
	{
213 14
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
214
	}
215
216
	/**
217
	 * @param int $user_id
218
	 * @return string
219
	 */
220 14
	protected function get_search_url($user_id)
221
	{
222 14
		return ($this->auth->acl_get('u_search')) ? append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id=$user_id&amp;sr=posts") : '';
0 ignored issues
show
Bug introduced by
The function append_sid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

222
		return ($this->auth->acl_get('u_search')) ? /** @scrutinizer ignore-call */ append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id=$user_id&amp;sr=posts") : '';
Loading history...
223
	}
224
225
	/**
226
	 * @param array $row
227
	 * @return array
228
	 */
229 14
	public function get_rank(array $row)
230
	{
231 14
		if (!function_exists('phpbb_get_user_rank'))
232 14
		{
233 1
			include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
234 1
		}
235
236 14
		$user_rank_data = phpbb_get_user_rank($row, $row['user_posts']);
237
238
		return array(
239 14
			'RANK_TITLE'		=> $user_rank_data['title'],
240 14
			'RANK_IMAGE'		=> $user_rank_data['img'],
241 14
			'RANK_IMAGE_SRC'	=> $user_rank_data['img_src'],
242 14
		);
243
	}
244
245
	/**
246
	 * @param array $users
247
	 */
248 14
	protected function get_additional_fields(array $users)
249
	{
250 14
		$user_ids = array_keys($users);
251
252 14
		$can_receive_pm_list = $this->get_can_receive_pm_list($user_ids);
253 14
		$permanently_banned_users = $this->get_banned_users_list($user_ids);
254
255
		// Grab all profile fields from users in id cache for later use - similar to the poster cache
256 14
		$profile_fields_cache = $this->profile_fields->grab_profile_fields_data($user_ids);
257
258 14
		for ($i = 0, $size = sizeof($user_ids); $i < $size; $i++)
259
		{
260 14
			$id = $user_ids[$i];
261 14
			$row = $users[$id];
262
263 14
			$this->user_cache[$id]['contact_fields'] = array_filter(array(
264 14
				'pm'		=> $this->get_pm_contact($row, $can_receive_pm_list, $permanently_banned_users),
265 14
				'email'		=> $this->get_email_contact($row),
266 14
				'jabber'	=> $this->get_jabber_contact($row),
267 14
			));
268 14
			$this->get_custom_profile_fields($id, $profile_fields_cache);
269 14
		}
270 14
	}
271
272
	/**
273
	 * @param int $user_id
274
	 * @param array $profile_fields_cache
275
	 */
276 14
	protected function get_custom_profile_fields($user_id, array $profile_fields_cache)
277
	{
278 14
		$cp_row = (isset($profile_fields_cache[$user_id])) ? $this->profile_fields->generate_profile_fields_template_data($profile_fields_cache[$user_id]) : array('blockrow' => array());
279
280 14
		foreach ($cp_row['blockrow'] as $field_data)
281
		{
282 10
			$field = $field_data['PROFILE_FIELD_IDENT'];
283
284 10
			if ($field_data['S_PROFILE_CONTACT'])
285 10
			{
286 10
				$this->user_cache[$user_id]['contact_fields'][$field] = array(
287 10
					'ID'		=> $field,
288 10
					'NAME'		=> $field_data['PROFILE_FIELD_NAME'],
289 10
					'U_CONTACT'	=> $field_data['PROFILE_FIELD_CONTACT'],
290
				);
291 10
			}
292
			else
293
			{
294 10
				$this->user_cache[$user_id]['profile_fields'][$field] = $field_data;
295
			}
296 14
		}
297 14
	}
298
299
	/**
300
	 * @param string $sql_where
301
	 * @param string $order_by
302
	 * @return string
303
	 */
304 16
	protected function get_sql_statement($sql_where = '', $order_by = '')
305
	{
306
		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_email, user_allow_viewemail, user_allow_pm, user_jabber, user_inactive_reason
307 16
			FROM ' . USERS_TABLE .
0 ignored issues
show
Bug introduced by
The constant blitze\sitemaker\services\users\USERS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
308 16
			(($sql_where) ? ' WHERE ' . $sql_where : '') .
309 16
			(($order_by) ? ' ORDER BY ' . $order_by : '');
310
	}
311
}
312