Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

featured_member::get_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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