Completed
Push — master ( 1f1439...c96b51 )
by Daniel
08:29
created

featured_member::_get_cpf_fields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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