Completed
Push — master ( 4f5aad...a0199d )
by Daniel
09:18
created

featured_member   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 342
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 4 Features 0
Metric Value
wmc 37
c 17
b 4
f 0
lcom 1
cbo 4
dl 0
loc 342
ccs 144
cts 144
cp 1
rs 8.6

19 Methods

Rating   Name   Duplication   Size   Complexity  
A display() 0 22 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 _explain_view() 0 10 4
A _save_settings() 0 13 4
A _calculate_percent_posts() 0 4 2
A _get_last_visit_date() 0 4 2
A __construct() 0 12 1
A get_config() 0 18 1
A _get_user_data() 0 23 2
A _change_user() 0 11 3
A _get_settings() 0 8 3
A _display_user() 0 18 2
A _get_block_title() 0 5 2
A _get_rotation_frequencies() 0 10 1
A _get_query_types() 0 8 1
B _get_template_data() 0 24 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 9
	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 9
		$this->cache = $cache;
74 9
		$this->config = $config;
75 9
		$this->db = $db;
76 9
		$this->user = $user;
77 9
		$this->profilefields = $profilefields;
78 9
		$this->phpbb_root_path = $phpbb_root_path;
79 9
		$this->php_ext = $php_ext;
80 9
		$this->blocks_table = $blocks_table;
81 9
		$this->cache_time = $cache_time;
82 9
	}
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 8
	public function display(array $bdata, $edit_mode = false)
110
	{
111 8
		$this->settings = $this->_get_settings($bdata);
112
113 8
		$change_user = $this->_change_user();
114 8
		$block_title = $this->_get_block_title($this->settings['qtype']);
115
116 8
		if (($row = $this->_get_user_data($change_user)) === false)
117 8
		{
118 2
			userlist::update($this->settings);
119
120 2
			$bdata['settings'] = $this->settings;
121 2
			$bdata['hash'] = 0;
122
123 2
			return $this->display($bdata, $edit_mode);
124
		}
125
126
		return array(
127 8
			'title'		=> $block_title,
128 8
			'content'	=> $this->_display_user($bdata['bid'], $row, $change_user),
129 8
		);
130
	}
131
132
	/**
133
	 * @param bool $change_user
134
	 * @return array|false
135
	 */
136 8
	private function _get_user_data($change_user)
137
	{
138
		$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
139 8
			FROM ' . USERS_TABLE . '
140 8
			WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER));
141
142 8
		$method = '_query_' . $this->settings['qtype'];
143
144 8
		if (is_callable(array($this, $method)))
145 8
		{
146 7
			call_user_func_array(array($this, $method), array(&$sql, $change_user));
147 7
		}
148
		else
149
		{
150 1
			return array();
151
		}
152
153 7
		$result = $this->db->sql_query_limit($sql, 1, 0, $this->_get_cache_time($this->settings['qtype'], $this->settings['rotation']));
154 7
		$row = $this->db->sql_fetchrow($result);
155 7
		$this->db->sql_freeresult($result);
156
157 7
		return $row;
158
	}
159
160
	/**
161
	 * @param string $sql
162
	 * @param bool $change_user
163
	 */
164 5
	private function _query_featured(&$sql, $change_user)
165
	{
166 5
		$sql .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user);
167 5
	}
168
169
	/**
170
	 * @param string $sql
171
	 */
172 1
	private function _query_recent(&$sql)
173
	{
174 1
		$sql .= ' AND user_posts > 0 ORDER BY user_regdate DESC';
175 1
	}
176
177
	/**
178
	 * @param string $sql
179
	 */
180 1
	private function _query_posts(&$sql)
181
	{
182 1
		$sql .= ' AND user_posts > 0 ORDER BY user_posts DESC';
183 1
	}
184
185
	/**
186
	 * @param string $query_type
187
	 * @param string $rotation
188
	 * @return int
189
	 */
190 7
	private function _get_cache_time($query_type, $rotation)
191
	{
192 7
		return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0;
193
	}
194
195
	/**
196
	 * @return bool
197
	 */
198 8
	private function _change_user()
199
	{
200 8
		$change = false;
201 8
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
202 8
		{
203 8
			$this->settings['last_changed'] = time();
204 8
			$change = true;
205 8
		}
206
207 8
		return $change;
208
	}
209
210
	/**
211
	 */
212 7
	private function _explain_view()
213
	{
214 7
		$query_type = $this->settings['qtype'];
215 7
		$rotation = $this->settings['rotation'];
216
217 7
		$this->ptemplate->assign_vars(array(
218 7
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->user->lang('QTYPE_' . strtoupper($query_type)) : '',
219 7
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->user->lang(strtoupper($rotation) . '_MEMBER') : '',
220 7
		));
221 7
	}
222
223
	/**
224
	 * @param array $bdata
225
	 * @return array
226
	 */
227 8
	private function _get_settings(array $bdata)
228
	{
229 8
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
230 8
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
231 8
		$settings['hash'] = $bdata['hash'];
232
233 8
		return $settings;
234
	}
235
236
	/**
237
	 * @param $bid
238
	 * @param bool $change_user
239
	 */
240 7
	private function _save_settings($bid, $change_user)
241
	{
242 7
		if ($change_user && ($this->settings['rotation'] !== 'pageload' || $this->settings['qtype'] === 'featured'))
243 7
		{
244 6
			$settings = $this->settings;
245 6
			unset($settings['hash']);
246
			$sql_data = array(
247 6
				'settings'	=> serialize($settings)
248 6
			);
249 6
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
250 6
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
251 6
		}
252 7
	}
253
254
	/**
255
	 * @param array $row
256
	 */
257 8
	private function _display_user($block_id, array $row, $change_user)
258
	{
259 8
		$html = '';
260 8
		if (sizeof($row))
261 8
		{
262 7
			$this->_save_settings($block_id, $change_user);
263 7
			$this->_explain_view();
264
265 7
			$tpl_data = $this->_get_template_data($row);
266 7
			$this->ptemplate->assign_vars($tpl_data['row']);
267 7
			$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']);
268 7
			unset($tpl_data);
269
270 7
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
271 7
		}
272
273 8
		return $html;
274
	}
275
276
	/**
277
	 * @param array $row
278
	 * @return array
279
	 */
280 7
	private function _get_template_data(array $row)
281
	{
282 7
		$date_format = $this->user->lang('DATE_FORMAT');
283 7
		$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']);
284 7
		$rank = phpbb_get_user_rank($row, $row['user_posts']);
285
286 7
		$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']);
287
288 7
		$tpl_data['row'] = array_merge($tpl_data['row'], array(
289 7
			'USERNAME'			=> $username,
290 7
			'AVATAR_IMG'		=> phpbb_get_user_avatar($row),
291 7
			'POSTS_PCT'			=> sprintf($this->user->lang('POST_PCT'), $this->_calculate_percent_posts($row['user_posts'])),
292 7
			'L_VIEW_PROFILE'	=> sprintf($this->user->lang('VIEW_USER_PROFILE'), $username),
293 7
			'JOINED'			=> $this->user->format_date($row['user_regdate'], "|$date_format|"),
294 7
			'VISITED'			=> $this->_get_last_visit_date($row['user_lastvisit'], $date_format),
295 7
			'POSTS'				=> $row['user_posts'],
296 7
			'RANK_TITLE'		=> $rank['title'],
297 7
			'RANK_IMG'			=> $rank['img'],
298 7
			'U_PROFILE'			=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
299 7
			'U_SEARCH_USER'		=> append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&amp;sr=posts"),
300 7
		));
301
302 7
		return $tpl_data;
303
	}
304
305
	/**
306
	 * @param int $user_posts
307
	 * @return int|mixed
308
	 */
309 7
	private function _calculate_percent_posts($user_posts)
310
	{
311 7
		return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0;
312
	}
313
314
	/**
315
	 * @param int $last_visited
316
	 * @param string $date_format
317
	 * @return string
318
	 */
319 7
	private function _get_last_visit_date($last_visited, $date_format)
320
	{
321 7
		return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : '';
322
	}
323
324
	/**
325
	 * @param string $qtype
326
	 * @return string
327
	 */
328 8
	private function _get_block_title($qtype)
329
	{
330 8
		$qtypes = $this->_get_query_types();
331 8
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
332
	}
333
334
	/**
335
	 * @return array
336
	 */
337 1
	private function _get_rotation_frequencies()
338
	{
339
		return array(
340 1
			'pageload'	=> 'ROTATE_PAGELOAD',
341 1
			'hourly'	=> 'ROTATE_HOURLY',
342 1
			'daily'		=> 'ROTATE_DAILY',
343 1
			'weekly'	=> 'ROTATE_WEEKLY',
344 1
			'monthly'	=> 'ROTATE_MONTHLY',
345 1
		);
346
	}
347
348
	/**
349
	 * @return array
350
	 */
351 9
	private function _get_query_types()
352
	{
353
		return array(
354 9
			'recent'	=> 'RECENT_MEMBER',
355 9
			'posts'		=> 'POSTS_MEMBER',
356 9
			'featured'	=> 'FEATURED_MEMBER',
357 9
		);
358
	}
359
}
360