Completed
Push — master ( bc7e6e...4d3cdd )
by Daniel
09:46
created

featured_member   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 18
Bugs 5 Features 0
Metric Value
wmc 38
c 18
b 5
f 0
lcom 1
cbo 4
dl 0
loc 350
ccs 148
cts 148
cp 1
rs 8.3999

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A get_config() 0 18 1
A _get_user_data() 0 23 2
A _query_featured() 0 4 1
A _query_recent() 0 4 1
A _query_posts() 0 4 1
A _get_cache_time() 0 4 3
A _change_user() 0 11 3
A _get_settings() 0 8 3
B display() 0 27 3
A _explain_view() 0 10 4
A _save_settings() 0 13 4
A _display_user() 0 19 2
B _get_template_data() 0 24 1
A _calculate_percent_posts() 0 4 2
A _get_last_visit_date() 0 4 2
A _get_block_title() 0 5 2
A _get_rotation_frequencies() 0 10 1
A _get_query_types() 0 8 1
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\user */
30
	protected $user;
31
32
	/** @var \blitze\sitemaker\services\profilefields */
33
	protected $profilefields;
34
35
	/** @var string */
36
	protected $phpbb_root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var string */
42
	protected $blocks_table;
43
44
	/** @var int */
45
	protected $cache_time;
46
47
	/** @var array */
48
	private $settings;
49
50
	/** @var array */
51
	private static $rotations = array(
52
		'hourly'	=> 'hour',
53
		'daily'		=> 'day',
54
		'weekly'	=> 'week',
55
		'monthly'	=> 'month'
56
	);
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param \phpbb\cache\driver\driver_interface		$cache					Cache driver interface
62
	 * @param \phpbb\config\config						$config					Config object
63
	 * @param \phpbb\db\driver\driver_interface			$db	 					Database connection
64
	 * @param \blitze\sitemaker\services\profilefields	$profilefields			Profile fields manager object
65
	 * @param \phpbb\user								$user					User object
66
	 * @param string									$phpbb_root_path		Path to the phpbb includes directory.
67
	 * @param string									$php_ext				php file extension
68
	 * @param string									$blocks_table			Name of blocks database table
69
	 * @param int										$cache_time
70
	 */
71 11
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\user $user, \blitze\sitemaker\services\profilefields $profilefields, $phpbb_root_path, $php_ext, $blocks_table, $cache_time = 3600)
72
	{
73 11
		$this->cache = $cache;
74 11
		$this->config = $config;
75 11
		$this->db = $db;
76 11
		$this->user = $user;
77 11
		$this->profilefields = $profilefields;
78 11
		$this->phpbb_root_path = $phpbb_root_path;
79 11
		$this->php_ext = $php_ext;
80 11
		$this->blocks_table = $blocks_table;
81 11
		$this->cache_time = $cache_time;
82 11
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87 1
	public function get_config(array $settings)
88
	{
89 1
		$rotation_options = $this->_get_rotation_frequencies();
90 1
		$qtype_options = $this->_get_query_types();
91 1
		$cpf_options = $this->profilefields->get_all_fields();
92
93
		return array(
94 1
			'legend1'		=> 'SETTINGS',
95 1
			'qtype'			=> array('lang' => 'QUERY_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $qtype_options, 'default' => 'recent', 'explain' => false),
96 1
			'rotation'		=> array('lang' => 'FREQUENCY', 'validate' => 'string', 'type' => 'select', 'options' => $rotation_options, 'default' => 'daily', 'explain' => false),
97 1
			'userlist'		=> array('lang' => 'FEATURED_MEMBER_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'default' => '', 'explain' => true),
98
99 1
			'legend2'		=> 'CUSTOM_PROFILE_FIELDS',
100 1
			'show_cpf'		=> array('lang' => 'SELECT_PROFILE_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $cpf_options, 'default' => array(), 'explain' => true),
101 1
			'last_changed'	=> array('type' => 'hidden', 'default' => 0),
102 1
			'current_user'	=> array('type' => 'hidden', 'default' => 0),
103 1
		);
104
	}
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109 10
	public function display(array $bdata, $edit_mode = false, $loop_count = 0)
110
	{
111 10
		$this->settings = $this->_get_settings($bdata);
112
113 10
		$change_user = $this->_change_user();
114 10
		$block_title = $this->_get_block_title($this->settings['qtype']);
115
116 10
		if (($row = $this->_get_user_data($change_user)) === false)
117 10
		{
118 3
			userlist::update($this->settings);
119
120 3
			$bdata['settings'] = $this->settings;
121 3
			$bdata['hash'] = 0;
122
123
			// Prevent endless loop looking for valid user
124 3
			if ($loop_count < 3)
125 3
			{
126 3
				return $this->display($bdata, $edit_mode, ++$loop_count);
127
			}
128 1
			$row = array();
129 1
		}
130
131
		return array(
132 10
			'title'		=> $block_title,
133 10
			'content'	=> $this->_display_user($bdata['bid'], $row, $change_user),
134 10
		);
135
	}
136
137
	/**
138
	 * @param bool $change_user
139
	 * @return array|false
140
	 */
141 10
	private function _get_user_data($change_user)
142
	{
143
		$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
144 10
			FROM ' . USERS_TABLE . '
145 10
			WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER));
146
147 10
		$method = '_query_' . $this->settings['qtype'];
148
149 10
		if (is_callable(array($this, $method)))
150 10
		{
151 9
			call_user_func_array(array($this, $method), array(&$sql, $change_user));
152 9
		}
153
		else
154
		{
155 1
			return array();
156
		}
157
158 9
		$result = $this->db->sql_query_limit($sql, 1, 0, $this->_get_cache_time($this->settings['qtype'], $this->settings['rotation']));
159 9
		$row = $this->db->sql_fetchrow($result);
160 9
		$this->db->sql_freeresult($result);
161
162 9
		return $row;
163
	}
164
165
	/**
166
	 * @param string $sql
167
	 * @param bool $change_user
168
	 */
169 7
	private function _query_featured(&$sql, $change_user)
170
	{
171 7
		$sql .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user);
172 7
	}
173
174
	/**
175
	 * @param string $sql
176
	 */
177 1
	private function _query_recent(&$sql)
178
	{
179 1
		$sql .= ' AND user_posts > 0 ORDER BY user_regdate DESC';
180 1
	}
181
182
	/**
183
	 * @param string $sql
184
	 */
185 1
	private function _query_posts(&$sql)
186
	{
187 1
		$sql .= ' AND user_posts > 0 ORDER BY user_posts DESC';
188 1
	}
189
190
	/**
191
	 * @param string $query_type
192
	 * @param string $rotation
193
	 * @return int
194
	 */
195 9
	private function _get_cache_time($query_type, $rotation)
196
	{
197 9
		return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0;
198
	}
199
200
	/**
201
	 * @return bool
202
	 */
203 10
	private function _change_user()
204
	{
205 10
		$change = false;
206 10
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
207 10
		{
208 9
			$this->settings['last_changed'] = time();
209 9
			$change = true;
210 9
		}
211
212 10
		return $change;
213
	}
214
215
	/**
216
	 */
217 8
	private function _explain_view()
218
	{
219 8
		$query_type = $this->settings['qtype'];
220 8
		$rotation = $this->settings['rotation'];
221
222 8
		$this->ptemplate->assign_vars(array(
223 8
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->user->lang('QTYPE_' . strtoupper($query_type)) : '',
224 8
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->user->lang(strtoupper($rotation) . '_MEMBER') : '',
225 8
		));
226 8
	}
227
228
	/**
229
	 * @param array $bdata
230
	 * @return array
231
	 */
232 10
	private function _get_settings(array $bdata)
233
	{
234 10
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
235 10
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
236 10
		$settings['hash'] = $bdata['hash'];
237
238 10
		return $settings;
239
	}
240
241
	/**
242
	 * @param int $bid
243
	 * @param bool $change_user
244
	 */
245 10
	private function _save_settings($bid, $change_user)
246
	{
247 10
		if ($change_user && ($this->settings['rotation'] !== 'pageload' || $this->settings['qtype'] === 'featured'))
248 10
		{
249 8
			$settings = $this->settings;
250 8
			unset($settings['hash']);
251
			$sql_data = array(
252 8
				'settings'	=> serialize($settings)
253 8
			);
254 8
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
255 8
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
256 8
		}
257 10
	}
258
259
	/**
260
	 * @param int $block_id
261
	 * @param array $row
262
	 * @param bool $change_user
263
	 */
264 10
	private function _display_user($block_id, array $row, $change_user)
265
	{
266 10
		$this->_save_settings($block_id, $change_user);
267
268 10
		$html = '';
269 10
		if (sizeof($row))
270 10
		{
271 8
			$this->_explain_view();
272
273 8
			$tpl_data = $this->_get_template_data($row);
274 8
			$this->ptemplate->assign_vars($tpl_data['row']);
275 8
			$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']);
276 8
			unset($tpl_data);
277
278 8
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
279 8
		}
280
281 10
		return $html;
282
	}
283
284
	/**
285
	 * @param array $row
286
	 * @return array
287
	 */
288 8
	private function _get_template_data(array $row)
289
	{
290 8
		$date_format = $this->user->lang('DATE_FORMAT');
291 8
		$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']);
292 8
		$rank = phpbb_get_user_rank($row, $row['user_posts']);
293
294 8
		$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']);
295
296 8
		$tpl_data['row'] = array_merge($tpl_data['row'], array(
297 8
			'USERNAME'			=> $username,
298 8
			'AVATAR_IMG'		=> phpbb_get_user_avatar($row),
299 8
			'POSTS_PCT'			=> sprintf($this->user->lang('POST_PCT'), $this->_calculate_percent_posts($row['user_posts'])),
300 8
			'L_VIEW_PROFILE'	=> sprintf($this->user->lang('VIEW_USER_PROFILE'), $username),
301 8
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
302 8
			'VISITED'			=> $this->_get_last_visit_date($row['user_lastvisit'], $date_format),
303 8
			'POSTS'				=> $row['user_posts'],
304 8
			'RANK_TITLE'		=> $rank['title'],
305 8
			'RANK_IMG'			=> $rank['img'],
306 8
			'U_PROFILE'			=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
307 8
			'U_SEARCH_USER'		=> append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts"),
308 8
		));
309
310 8
		return $tpl_data;
311
	}
312
313
	/**
314
	 * @param int $user_posts
315
	 * @return int|mixed
316
	 */
317 8
	private function _calculate_percent_posts($user_posts)
318
	{
319 8
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
320
	}
321
322
	/**
323
	 * @param int $last_visited
324
	 * @param string $date_format
325
	 * @return string
326
	 */
327 8
	private function _get_last_visit_date($last_visited, $date_format)
328
	{
329 8
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
330
	}
331
332
	/**
333
	 * @param string $qtype
334
	 * @return string
335
	 */
336 10
	private function _get_block_title($qtype)
337
	{
338 10
		$qtypes = $this->_get_query_types();
339 10
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
340
	}
341
342
	/**
343
	 * @return array
344
	 */
345 1
	private function _get_rotation_frequencies()
346
	{
347
		return array(
348 1
			'pageload'	=> 'ROTATE_PAGELOAD',
349 1
			'hourly'	=> 'ROTATE_HOURLY',
350 1
			'daily'		=> 'ROTATE_DAILY',
351 1
			'weekly'	=> 'ROTATE_WEEKLY',
352 1
			'monthly'	=> 'ROTATE_MONTHLY',
353 1
		);
354
	}
355
356
	/**
357
	 * @return array
358
	 */
359 11
	private function _get_query_types()
360
	{
361
		return array(
362 11
			'recent'	=> 'RECENT_MEMBER',
363 11
			'posts'		=> 'POSTS_MEMBER',
364 11
			'featured'	=> 'FEATURED_MEMBER',
365 11
		);
366
	}
367
}
368