Completed
Push — develop ( 29678f...b18c7a )
by Daniel
14:18
created

featured_member   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 14
Bugs 4 Features 1
Metric Value
wmc 27
c 14
b 4
f 1
lcom 1
cbo 4
dl 0
loc 252
ccs 98
cts 98
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A get_config() 0 18 1
B display() 0 28 3
A get_user_data() 0 22 3
A change_user() 0 11 3
A get_settings() 0 8 3
A save_settings() 0 13 3
A display_user() 0 23 2
A get_block_title() 0 5 2
A explain_view() 0 10 4
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\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\language\language */
27
	protected $translator;
28
29
	/** @var \blitze\sitemaker\services\user_data */
30
	protected $user_data;
31
32
	/** @var string */
33
	protected $blocks_table;
34
35
	/** @var array */
36
	private $settings;
37
38
	/** @var array */
39
	private static $rotations = array(
40
		'hourly'	=> 'hour',
41
		'daily'		=> 'day',
42
		'weekly'	=> 'week',
43
		'monthly'	=> 'month'
44
	);
45
46
	/**
47
	 * Constructor
48
	 *
49
	 * @param \phpbb\cache\driver\driver_interface		$cache					Cache driver interface
50
	 * @param \phpbb\db\driver\driver_interface			$db	 					Database connection
51
	 * @param \phpbb\language\language					$translator				Language object
52
	 * @param \blitze\sitemaker\services\user_data		$user_data				Sitemaker User data object
53
	 * @param string									$blocks_table			Name of blocks database table
54
	 */
55
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $translator, \blitze\sitemaker\services\user_data $user_data, $blocks_table)
56
	{
57
		$this->cache = $cache;
58
		$this->db = $db;
59
		$this->translator = $translator;
60
		$this->user_data = $user_data;
61
		$this->blocks_table = $blocks_table;
62
	}
63
64
	/**
65
	 * {@inheritdoc}
66
	 */
67
	public function get_config(array $settings)
68
	{
69
		$rotation_options = $this->get_rotation_frequencies();
70
		$qtype_options = $this->get_query_types();
71
		$cpf_options = $this->user_data->get_profile_fields();
72
73
		return array(
74
			'legend1'	=> 'SETTINGS',
75 11
			'qtype'			=> array('lang' => 'QUERY_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $qtype_options, 'default' => 'recent', 'explain' => false),
76
			'rotation'		=> array('lang' => 'FREQUENCY', 'validate' => 'string', 'type' => 'select', 'options' => $rotation_options, 'default' => 'daily', 'explain' => false),
77 11
			'userlist'		=> array('lang' => 'FEATURED_MEMBER_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'default' => '', 'explain' => true),
78 11
79 11
			'legend2'	=> 'CUSTOM_PROFILE_FIELDS',
80 11
			'show_cpf'		=> array('lang' => 'SELECT_PROFILE_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $cpf_options, 'default' => array(), 'explain' => true),
81 11
			'last_changed'	=> array('type' => 'hidden', 'default' => 0),
82 11
			'current_user'	=> array('type' => 'hidden', 'default' => 0),
83 11
		);
84 11
	}
85 11
86 11
	/**
87 11
	 * {@inheritdoc}
88
	 */
89
	public function display(array $bdata, $edit_mode = false, $loop_count = 0)
90
	{
91
		
92 1
		$this->settings = $this->get_settings($bdata);
93
94 1
		$change_user = $this->change_user();
95 1
		$block_title = $this->get_block_title($this->settings['qtype']);
96 1
97
		if (($row = $this->get_user_data($change_user)) === false)
98
		{
99 1
			userlist::update($this->settings);
100 1
101 1
			$bdata['settings'] = $this->settings;
102 1
			$bdata['hash'] = 0;
103
104 1
			// Prevent endless loop looking for valid user
105 1
			if ($loop_count < 3)
106 1
			{
107 1
				return $this->display($bdata, $edit_mode, ++$loop_count);
108 1
			}
109
			$row = array();
110
		}
111
112
		return array(
113
			'title'		=> $block_title,
114 10
			'content'	=> $this->display_user($bdata['bid'], $row, $change_user),
115
		);
116 10
	}
117
118 10
	/**
119 10
	 * @param bool $change_user
120
	 * @return array|false
121 10
	 */
122 10
	protected function get_user_data($change_user)
123 3
	{
124
		$sql_where = 'user_posts > 0
125 3
			AND ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER));
126 3
127
		$sort_keys = array(
128
			'recent'	=> 'user_regdate DESC',
129 3
			'posts'		=> 'user_posts DESC',
130 3
		);
131 3
132
		if (isset($sort_keys[$this->settings['qtype']]))
133 1
		{
134 1
			return $this->user_data->query($sql_where, $sort_keys[$this->settings['qtype']], 1);
135
		}
136
		else
137 10
		{
138 10
			$user_id = (int) userlist::get_user_id($this->settings, $change_user);
139 10
			$data = $this->user_data->get_users(array($user_id), $sql_where);
140
141
			return (sizeof($data)) ? $data : false;
142
		}
143
	}
144
145
	/**
146 10
	 * @return bool
147
	 */
148
	private function change_user()
149 10
	{
150 10
		$change = false;
151
		if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']]))
152 10
		{
153
			$this->settings['last_changed'] = time();
154 10
			$change = true;
155 10
		}
156 9
157 9
		return $change;
158
	}
159
160 1
	/**
161
	 * @param array $bdata
162
	 * @return array
163 9
	 */
164 9
	private function get_settings(array $bdata)
165 9
	{
166
		$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']);
167 9
		$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings'];
168
		$settings['hash'] = $bdata['hash'];
169
170
		return $settings;
171
	}
172
173
	/**
174 7
	 * @param int $bid
175
	 * @param bool $change_user
176 7
	 */
177 7
	private function save_settings($bid, $change_user)
178
	{
179
		if ($change_user && $this->settings['qtype'] === 'featured')
180
		{
181
			$settings = $this->settings;
182 1
			unset($settings['hash']);
183
			$sql_data = array(
184 1
				'settings'	=> json_encode($settings)
185 1
			);
186
			$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid);
187
			$this->cache->put('pt_block_data_' . $bid, $this->settings);
188
		}
189
	}
190 1
191
	/**
192 1
	 * @param int   $block_id
193 1
	 * @param array $row
194
	 * @param bool  $change_user
195
	 * @return string
196
	 */
197
	private function display_user($block_id, array $row, $change_user)
198
	{
199
		$this->save_settings($block_id, $change_user);
200 9
201
		$html = '';
202 9
		if (sizeof($row))
203
		{
204
			$this->explain_view();
205
206
			$row = array_shift($row);
207
			$allowed_fields = array_flip(array_merge(array('pm', 'email', 'jabber'), $this->settings['show_cpf']));
208 10
209
			$this->ptemplate->assign_block_vars_array('contact_field', array_intersect_key($row['contact_fields'], $allowed_fields));
210 10
			$this->ptemplate->assign_block_vars_array('profile_field', array_intersect_key($row['profile_fields'], $allowed_fields));
211 10
			unset($row['contact_fields'], $row['profile_fields']);
212 10
213 9
			$this->ptemplate->assign_vars($row);
214 9
215 9
			$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block');
216
		}
217 10
218
		return $html;
219
	}
220
221
	/**
222 8
	 * @param string $qtype
223
	 * @return string
224 8
	 */
225 8
	private function get_block_title($qtype)
226
	{
227 8
		$qtypes = $this->get_query_types();
228 8
		return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER';
229 8
	}
230 8
231 8
	/**
232
	 */
233
	private function explain_view()
234
	{
235
		$query_type = $this->settings['qtype'];
236
		$rotation = $this->settings['rotation'];
237 10
238
		$this->ptemplate->assign_vars(array(
239 10
			'QTYPE_EXPLAIN'		=> ($query_type == 'posts' || $query_type == 'recent') ? $this->translator->lang('QTYPE_' . strtoupper($query_type)) : '',
240 10
			'TITLE_EXPLAIN'		=> ($rotation != 'pageload') ? $this->translator->lang(strtoupper($rotation) . '_MEMBER') : '',
241 10
		));
242
	}
243 10
244
	/**
245
	 * @return array
246
	 */
247
	private function get_rotation_frequencies()
248
	{
249
		return array(
250 10
			'pageload'	=> 'ROTATE_PAGELOAD',
251
			'hourly'	=> 'ROTATE_HOURLY',
252 10
			'daily'		=> 'ROTATE_DAILY',
253 10
			'weekly'	=> 'ROTATE_WEEKLY',
254 6
			'monthly'	=> 'ROTATE_MONTHLY',
255 6
		);
256
	}
257 6
258 6
	/**
259 6
	 * @return array
260 6
	 */
261 6
	private function get_query_types()
262 10
	{
263
		return array(
264
			'recent'	=> 'RECENT_MEMBER',
265
			'posts'		=> 'POSTS_MEMBER',
266
			'featured'	=> 'FEATURED_MEMBER',
267
		);
268
	}
269
}
270