Completed
Push — develop ( 1f2a5d...e6d06f )
by Daniel
21:42 queued 08:26
created

featured_member::display()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 11
Bugs 2 Features 0
Metric Value
c 11
b 2
f 0
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 8.8571
cc 3
eloc 13
nc 3
nop 3
crap 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 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\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
use blitze\sitemaker\services\userlist;
14
15
/**
16
 * Featured Member Block
17
 */
18
class featured_member extends block
19
{
20
	/** @var \phpbb\cache\driver\driver_interface */
21
	protected $cache;
22
23
	/** @var \phpbb\config\config */
24
	protected $config;
25
26
	/** @var \phpbb\db\driver\driver_interface */
27
	protected $db;
28
29
	/** @var \phpbb\language\language */
30
	protected $translator;
31
32
	/** @var \phpbb\user */
33
	protected $user;
34
35
	/** @var \blitze\sitemaker\services\profilefields */
36
	protected $profilefields;
37
38
	/** @var string */
39
	protected $phpbb_root_path;
40
41
	/** @var string */
42
	protected $php_ext;
43
44
	/** @var string */
45
	protected $blocks_table;
46
47
	/** @var int */
48
	protected $cache_time;
49
50
	/** @var array */
51
	private $settings;
52
53
	/** @var array */
54
	private static $rotations = array(
55
		'hourly'	=> 'hour',
56
		'daily'		=> 'day',
57
		'weekly'	=> 'week',
58
		'monthly'	=> 'month'
59
	);
60
61
	/**
62
	 * Constructor
63
	 *
64
	 * @param \phpbb\cache\driver\driver_interface		$cache					Cache driver interface
65
	 * @param \phpbb\config\config						$config					Config object
66
	 * @param \phpbb\db\driver\driver_interface			$db	 					Database connection
67
	 * @param \phpbb\language\language					$translator				Language object
68
	 * @param \phpbb\user								$user					User object
69
	 * @param \blitze\sitemaker\services\profilefields	$profilefields			Profile fields manager object
70
	 * @param string									$phpbb_root_path		Path to the phpbb includes directory.
71
	 * @param string									$php_ext				php file extension
72
	 * @param string									$blocks_table			Name of blocks database table
73
	 * @param int										$cache_time
74
	 */
75 10
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\profilefields $profilefields, $phpbb_root_path, $php_ext, $blocks_table, $cache_time = 3600)
76
	{
77 10
		$this->cache = $cache;
78 10
		$this->config = $config;
79 10
		$this->db = $db;
80 10
		$this->translator = $translator;
81 10
		$this->user = $user;
82 10
		$this->profilefields = $profilefields;
83 10
		$this->phpbb_root_path = $phpbb_root_path;
84 10
		$this->php_ext = $php_ext;
85 10
		$this->blocks_table = $blocks_table;
86 10
		$this->cache_time = $cache_time;
87 10
	}
88
89
	/**
90
	 * {@inheritdoc}
91
	 */
92 1
	public function get_config(array $settings)
93
	{
94 1
		$rotation_options = $this->_get_rotation_frequencies();
95 1
		$qtype_options = $this->_get_query_types();
96 1
		$cpf_options = $this->profilefields->get_all_fields();
97
98
		return array(
99 1
			'legend1'	=> 'SETTINGS',
100 1
			'qtype'			=> array('lang' => 'QUERY_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $qtype_options, 'default' => 'recent', 'explain' => false),
101 1
			'rotation'		=> array('lang' => 'FREQUENCY', 'validate' => 'string', 'type' => 'select', 'options' => $rotation_options, 'default' => 'daily', 'explain' => false),
102 1
			'userlist'		=> array('lang' => 'FEATURED_MEMBER_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'default' => '', 'explain' => true),
103
104 1
			'legend2'	=> 'CUSTOM_PROFILE_FIELDS',
105 1
			'show_cpf'		=> array('lang' => 'SELECT_PROFILE_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $cpf_options, 'default' => array(), 'explain' => true),
106 1
			'last_changed'	=> array('type' => 'hidden', 'default' => 0),
107 1
			'current_user'	=> array('type' => 'hidden', 'default' => 0),
108 1
		);
109
	}
110
111
	/**
112
	 * {@inheritdoc}
113
	 */
114 9
	public function display(array $bdata, $edit_mode = false, $loop_count = 0)
115
	{
116 9
		$this->settings = $this->_get_settings($bdata);
117
118 9
		$change_user = $this->_change_user();
119 9
		$block_title = $this->_get_block_title($this->settings['qtype']);
120
121 9
		if (($row = $this->_get_user_data($change_user)) === false)
122 9
		{
123 3
			userlist::update($this->settings);
124
125 3
			$bdata['settings'] = $this->settings;
126 3
			$bdata['hash'] = 0;
127
128
			// Prevent endless loop looking for valid user
129 3
			if ($loop_count < 3)
130 3
			{
131 3
				return $this->display($bdata, $edit_mode, ++$loop_count);
132
			}
133 1
		}
134
135
		return array(
136 9
			'title'		=> $block_title,
137 9
			'content'	=> $this->_display_user($bdata['bid'], $row, $change_user),
138 9
		);
139
	}
140
141
	/**
142
	 * @param bool $change_user
143
	 * @return array|false
144
	 */
145 9
	private function _get_user_data($change_user)
146
	{
147
		$sql = 'SELECT user_id, username, user_colour, user_avatar, user_avatar_type, user_avatar_height, user_avatar_width, user_regdate, user_lastvisit, user_birthday, user_posts, user_rank
148 9
			FROM ' . USERS_TABLE . '
149 9
			WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER));
150
151 9
		$method = '_query_' . $this->settings['qtype'];
152
153 9
		if (is_callable(array($this, $method)))
154 9
		{
155 8
			call_user_func_array(array($this, $method), array(&$sql, $change_user));
156 8
		}
157
		else
158
		{
159 1
			return array();
160
		}
161
162 8
		$result = $this->db->sql_query_limit($sql, 1, 0, $this->_get_cache_time($this->settings['qtype'], $this->settings['rotation']));
163 8
		$row = $this->db->sql_fetchrow($result);
164 8
		$this->db->sql_freeresult($result);
165
166 8
		return $row;
167
	}
168
169
	/**
170
	 * @param string $sql
171
	 * @param bool $change_user
172
	 */
173 6
	private function _query_featured(&$sql, $change_user)
174
	{
175 6
		$sql .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user);
176 6
	}
177
178
	/**
179
	 * @param string $sql
180
	 */
181 1
	private function _query_recent(&$sql)
182
	{
183 1
		$sql .= ' AND user_posts > 0 ORDER BY user_regdate DESC';
184 1
	}
185
186
	/**
187
	 * @param string $sql
188
	 */
189 1
	private function _query_posts(&$sql)
190
	{
191 1
		$sql .= ' AND user_posts > 0 ORDER BY user_posts DESC';
192 1
	}
193
194
	/**
195
	 * @param string $query_type
196
	 * @param string $rotation
197
	 * @return int
198
	 */
199 8
	private function _get_cache_time($query_type, $rotation)
200
	{
201 8
		return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0;
202
	}
203
204
	/**
205
	 * @return bool
206
	 */
207 9
	private function _change_user()
208
	{
209 9
		$change = false;
210 9
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
211 9
		{
212 9
			$this->settings['last_changed'] = time();
213 9
			$change = true;
214 9
		}
215
216 9
		return $change;
217
	}
218
219
	/**
220
	 */
221 7
	private function _explain_view()
222
	{
223 7
		$query_type = $this->settings['qtype'];
224 7
		$rotation = $this->settings['rotation'];
225
226 7
		$this->ptemplate->assign_vars(array(
227 7
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->translator->lang('QTYPE_' . strtoupper($query_type)) : '',
228 7
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->translator->lang(strtoupper($rotation) . '_MEMBER') : '',
229 7
		));
230 7
	}
231
232
	/**
233
	 * @param array $bdata
234
	 * @return array
235
	 */
236 9
	private function _get_settings(array $bdata)
237
	{
238 9
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
239 9
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
240 9
		$settings['hash'] = $bdata['hash'];
241
242 9
		return $settings;
243
	}
244
245
	/**
246
	 * @param $bid
247
	 * @param bool $change_user
248
	 */
249 9
	private function _save_settings($bid, $change_user)
250
	{
251 9
		if ($change_user && ($this->settings['rotation'] !== 'pageload' || $this->settings['qtype'] === 'featured'))
252 9
		{
253 8
			$settings = $this->settings;
254 8
			unset($settings['hash']);
255
			$sql_data = array(
256 8
				'settings'	=> serialize($settings)
257 8
			);
258 8
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
259 8
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
260 8
		}
261 9
	}
262
263
	/**
264
	 * @param int $block_id
265
	 * @param array|bool $row
266
	 * @param bool $change_user
267
	 */
268 9
	private function _display_user($block_id, $row, $change_user)
269
	{
270 9
		$this->_save_settings($block_id, $change_user);
271
272 9
		$html = '';
273
		if ($row)
274 9
		{
275 7
			$this->_explain_view();
276
277 7
			$tpl_data = $this->_get_template_data($row);
0 ignored issues
show
Bug introduced by
It seems like $row defined by parameter $row on line 268 can also be of type boolean; however, blitze\sitemaker\blocks\...r::_get_template_data() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
278 7
			$this->ptemplate->assign_vars($tpl_data['row']);
279 7
			$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']);
280 7
			unset($tpl_data);
281
282 7
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
283 7
		}
284
285 9
		return $html;
286
	}
287
288
	/**
289
	 * @param array $row
290
	 * @return array
291
	 */
292 7
	private function _get_template_data(array $row)
293
	{
294 7
		$date_format = $this->user->date_format;
295 7
		$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']);
296 7
		$rank = phpbb_get_user_rank($row, $row['user_posts']);
297
298 7
		$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']);
299
300 7
		$tpl_data['row'] = array_merge($tpl_data['row'], array(
301 7
			'USERNAME'			=> $username,
302 7
			'AVATAR_IMG'		=> phpbb_get_user_avatar($row),
303 7
			'POSTS_PCT'			=> sprintf($this->translator->lang('POST_PCT'), $this->_calculate_percent_posts($row['user_posts'])),
304 7
			'L_VIEW_PROFILE'	=> sprintf($this->translator->lang('VIEW_USER_PROFILE'), $username),
305 7
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
306 7
			'VISITED'			=> $this->_get_last_visit_date($row['user_lastvisit'], $date_format),
307 7
			'POSTS'				=> $row['user_posts'],
308 7
			'RANK_TITLE'		=> $rank['title'],
309 7
			'RANK_IMG'			=> $rank['img'],
310 7
			'U_PROFILE'			=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
311 7
			'U_SEARCH_USER'		=> append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts"),
312 7
		));
313
314 7
		return $tpl_data;
315
	}
316
317
	/**
318
	 * @param int $user_posts
319
	 * @return int|mixed
320
	 */
321 7
	private function _calculate_percent_posts($user_posts)
322
	{
323 7
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
324
	}
325
326
	/**
327
	 * @param int $last_visited
328
	 * @param string $date_format
329
	 * @return string
330
	 */
331 7
	private function _get_last_visit_date($last_visited, $date_format)
332
	{
333 7
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
334
	}
335
336
	/**
337
	 * @param string $qtype
338
	 * @return string
339
	 */
340 9
	private function _get_block_title($qtype)
341
	{
342 9
		$qtypes = $this->_get_query_types();
343 9
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
344
	}
345
346
	/**
347
	 * @return array
348
	 */
349 1
	private function _get_rotation_frequencies()
350
	{
351
		return array(
352 1
			'pageload'	=> 'ROTATE_PAGELOAD',
353 1
			'hourly'	=> 'ROTATE_HOURLY',
354 1
			'daily'		=> 'ROTATE_DAILY',
355 1
			'weekly'	=> 'ROTATE_WEEKLY',
356 1
			'monthly'	=> 'ROTATE_MONTHLY',
357 1
		);
358
	}
359
360
	/**
361
	 * @return array
362
	 */
363 10
	private function _get_query_types()
364
	{
365
		return array(
366 10
			'recent'	=> 'RECENT_MEMBER',
367 10
			'posts'		=> 'POSTS_MEMBER',
368 10
			'featured'	=> 'FEATURED_MEMBER',
369 10
		);
370
	}
371
}
372