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