Completed
Push — develop ( b18c7a...273208 )
by Daniel
11:54 queued 09:05
created

featured_member::get_block_title()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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