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\language\language */ |
30
|
|
|
protected $translator; |
31
|
|
|
|
32
|
|
|
/** @var \phpbb\user */ |
33
|
|
|
protected $user; |
34
|
|
|
|
35
|
|
|
/** @var \blitze\sitemaker\services\profilefields */ |
36
|
|
|
protected $profilefields; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
protected $phpbb_root_path; |
40
|
|
|
|
41
|
|
|
/** @var string */ |
42
|
|
|
protected $php_ext; |
43
|
|
|
|
44
|
|
|
/** @var string */ |
45
|
|
|
protected $blocks_table; |
46
|
|
|
|
47
|
|
|
/** @var int */ |
48
|
|
|
protected $cache_time; |
49
|
|
|
|
50
|
|
|
/** @var array */ |
51
|
|
|
private $settings; |
52
|
|
|
|
53
|
|
|
/** @var array */ |
54
|
|
|
private static $rotations = array( |
55
|
|
|
'hourly' => 'hour', |
56
|
|
|
'daily' => 'day', |
57
|
|
|
'weekly' => 'week', |
58
|
|
|
'monthly' => 'month' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor |
63
|
|
|
* |
64
|
|
|
* @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
65
|
|
|
* @param \phpbb\config\config $config Config object |
66
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database connection |
67
|
|
|
* @param \phpbb\language\language $translator Language object |
68
|
|
|
* @param \phpbb\user $user User object |
69
|
|
|
* @param \blitze\sitemaker\services\profilefields $profilefields Profile fields manager object |
70
|
|
|
* @param string $phpbb_root_path Path to the phpbb includes directory. |
71
|
|
|
* @param string $php_ext php file extension |
72
|
|
|
* @param string $blocks_table Name of blocks database table |
73
|
|
|
* @param int $cache_time |
74
|
|
|
*/ |
75
|
11 |
|
public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\profilefields $profilefields, $phpbb_root_path, $php_ext, $blocks_table, $cache_time = 3600) |
76
|
|
|
{ |
77
|
11 |
|
$this->cache = $cache; |
78
|
11 |
|
$this->config = $config; |
79
|
11 |
|
$this->db = $db; |
80
|
11 |
|
$this->translator = $translator; |
81
|
11 |
|
$this->user = $user; |
82
|
11 |
|
$this->profilefields = $profilefields; |
83
|
11 |
|
$this->phpbb_root_path = $phpbb_root_path; |
84
|
11 |
|
$this->php_ext = $php_ext; |
85
|
11 |
|
$this->blocks_table = $blocks_table; |
86
|
11 |
|
$this->cache_time = $cache_time; |
87
|
11 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function get_config(array $settings) |
93
|
|
|
{ |
94
|
1 |
|
$rotation_options = $this->get_rotation_frequencies(); |
95
|
1 |
|
$qtype_options = $this->get_query_types(); |
96
|
1 |
|
$cpf_options = $this->profilefields->get_all_fields(); |
97
|
|
|
|
98
|
|
|
return array( |
99
|
1 |
|
'legend1' => 'SETTINGS', |
100
|
1 |
|
'qtype' => array('lang' => 'QUERY_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $qtype_options, 'default' => 'recent', 'explain' => false), |
101
|
1 |
|
'rotation' => array('lang' => 'FREQUENCY', 'validate' => 'string', 'type' => 'select', 'options' => $rotation_options, 'default' => 'daily', 'explain' => false), |
102
|
1 |
|
'userlist' => array('lang' => 'FEATURED_MEMBER_IDS', 'validate' => 'string', 'type' => 'textarea:3:40', 'default' => '', 'explain' => true), |
103
|
|
|
|
104
|
1 |
|
'legend2' => 'CUSTOM_PROFILE_FIELDS', |
105
|
1 |
|
'show_cpf' => array('lang' => 'SELECT_PROFILE_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $cpf_options, 'default' => array(), 'explain' => true), |
106
|
1 |
|
'last_changed' => array('type' => 'hidden', 'default' => 0), |
107
|
1 |
|
'current_user' => array('type' => 'hidden', 'default' => 0), |
108
|
1 |
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
10 |
|
public function display(array $bdata, $edit_mode = false, $loop_count = 0) |
115
|
|
|
{ |
116
|
10 |
|
$this->settings = $this->get_settings($bdata); |
117
|
|
|
|
118
|
10 |
|
$change_user = $this->change_user(); |
119
|
10 |
|
$block_title = $this->get_block_title($this->settings['qtype']); |
120
|
|
|
|
121
|
10 |
|
if (($row = $this->get_user_data($change_user)) === false) |
122
|
10 |
|
{ |
123
|
3 |
|
userlist::update($this->settings); |
124
|
|
|
|
125
|
3 |
|
$bdata['settings'] = $this->settings; |
126
|
3 |
|
$bdata['hash'] = 0; |
127
|
|
|
|
128
|
|
|
// Prevent endless loop looking for valid user |
129
|
3 |
|
if ($loop_count < 3) |
130
|
3 |
|
{ |
131
|
3 |
|
return $this->display($bdata, $edit_mode, ++$loop_count); |
132
|
|
|
} |
133
|
1 |
|
$row = array(); |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
return array( |
137
|
10 |
|
'title' => $block_title, |
138
|
10 |
|
'content' => $this->display_user($bdata['bid'], $row, $change_user), |
139
|
10 |
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param bool $change_user |
144
|
|
|
* @return array|false |
145
|
|
|
*/ |
146
|
10 |
|
private function get_user_data($change_user) |
147
|
|
|
{ |
148
|
|
|
$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 |
149
|
10 |
|
FROM ' . USERS_TABLE . ' |
150
|
10 |
|
WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER)); |
151
|
|
|
|
152
|
10 |
|
$method = 'query_' . $this->settings['qtype']; |
153
|
|
|
|
154
|
10 |
|
if (is_callable(array($this, $method))) |
155
|
10 |
|
{ |
156
|
9 |
|
call_user_func_array(array($this, $method), array(&$sql, $change_user)); |
157
|
9 |
|
} |
158
|
|
|
else |
159
|
|
|
{ |
160
|
1 |
|
return array(); |
161
|
|
|
} |
162
|
|
|
|
163
|
9 |
|
$result = $this->db->sql_query_limit($sql, 1, 0, $this->get_cache_time($this->settings['qtype'], $this->settings['rotation'])); |
164
|
9 |
|
$row = $this->db->sql_fetchrow($result); |
165
|
9 |
|
$this->db->sql_freeresult($result); |
166
|
|
|
|
167
|
9 |
|
return $row; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $sql |
172
|
|
|
* @param bool $change_user |
173
|
|
|
*/ |
174
|
7 |
|
private function query_featured(&$sql, $change_user) |
175
|
|
|
{ |
176
|
7 |
|
$sql .= ' AND user_id = ' . userlist::get_user_id($this->settings, $change_user); |
177
|
7 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $sql |
181
|
|
|
*/ |
182
|
1 |
|
private function query_recent(&$sql) |
183
|
|
|
{ |
184
|
1 |
|
$sql .= ' AND user_posts > 0 ORDER BY user_regdate DESC'; |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $sql |
189
|
|
|
*/ |
190
|
1 |
|
private function query_posts(&$sql) |
191
|
|
|
{ |
192
|
1 |
|
$sql .= ' AND user_posts > 0 ORDER BY user_posts DESC'; |
193
|
1 |
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $query_type |
197
|
|
|
* @param string $rotation |
198
|
|
|
* @return int |
199
|
|
|
*/ |
200
|
9 |
|
private function get_cache_time($query_type, $rotation) |
201
|
|
|
{ |
202
|
9 |
|
return ($rotation !== 'pageload' || in_array($query_type, array('posts', 'recent'))) ? $this->cache_time : 0; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
10 |
|
private function change_user() |
209
|
|
|
{ |
210
|
10 |
|
$change = false; |
211
|
10 |
|
if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']])) |
212
|
10 |
|
{ |
213
|
9 |
|
$this->settings['last_changed'] = time(); |
214
|
9 |
|
$change = true; |
215
|
9 |
|
} |
216
|
|
|
|
217
|
10 |
|
return $change; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
*/ |
222
|
8 |
|
private function explain_view() |
223
|
|
|
{ |
224
|
8 |
|
$query_type = $this->settings['qtype']; |
225
|
8 |
|
$rotation = $this->settings['rotation']; |
226
|
|
|
|
227
|
8 |
|
$this->ptemplate->assign_vars(array( |
228
|
8 |
|
'QTYPE_EXPLAIN' => ($query_type == 'posts' || $query_type == 'recent') ? $this->translator->lang('QTYPE_' . strtoupper($query_type)) : '', |
229
|
8 |
|
'TITLE_EXPLAIN' => ($rotation != 'pageload') ? $this->translator->lang(strtoupper($rotation) . '_MEMBER') : '', |
230
|
8 |
|
)); |
231
|
8 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $bdata |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
10 |
|
private function get_settings(array $bdata) |
238
|
|
|
{ |
239
|
10 |
|
$cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']); |
240
|
10 |
|
$settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings']; |
241
|
10 |
|
$settings['hash'] = $bdata['hash']; |
242
|
|
|
|
243
|
10 |
|
return $settings; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param int $bid |
248
|
|
|
* @param bool $change_user |
249
|
|
|
*/ |
250
|
10 |
|
private function save_settings($bid, $change_user) |
251
|
|
|
{ |
252
|
10 |
|
if ($change_user && $this->settings['qtype'] === 'featured') |
253
|
10 |
|
{ |
254
|
6 |
|
$settings = $this->settings; |
255
|
6 |
|
unset($settings['hash']); |
256
|
|
|
$sql_data = array( |
257
|
6 |
|
'settings' => json_encode($settings) |
258
|
6 |
|
); |
259
|
6 |
|
$this->db->sql_query('UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_data) . ' WHERE bid = ' . (int) $bid); |
260
|
6 |
|
$this->cache->put('pt_block_data_' . $bid, $this->settings); |
261
|
6 |
|
} |
262
|
10 |
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param int $block_id |
266
|
|
|
* @param array $row |
267
|
|
|
* @param bool $change_user |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
10 |
|
private function display_user($block_id, array $row, $change_user) |
271
|
|
|
{ |
272
|
10 |
|
$this->save_settings($block_id, $change_user); |
273
|
|
|
|
274
|
10 |
|
$html = ''; |
275
|
10 |
|
if (sizeof($row)) |
276
|
10 |
|
{ |
277
|
8 |
|
$this->explain_view(); |
278
|
|
|
|
279
|
8 |
|
$tpl_data = $this->get_template_data($row); |
280
|
8 |
|
$this->ptemplate->assign_vars($tpl_data['row']); |
281
|
8 |
|
$this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']); |
282
|
8 |
|
unset($tpl_data); |
283
|
|
|
|
284
|
8 |
|
$html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block'); |
285
|
8 |
|
} |
286
|
|
|
|
287
|
10 |
|
return $html; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param array $row |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
8 |
|
private function get_template_data(array $row) |
295
|
|
|
{ |
296
|
8 |
|
$date_format = $this->user->date_format; |
297
|
8 |
|
$username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']); |
298
|
8 |
|
$rank = phpbb_get_user_rank($row, $row['user_posts']); |
299
|
|
|
|
300
|
8 |
|
$tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']); |
301
|
|
|
|
302
|
8 |
|
$tpl_data['row'] = array_merge($tpl_data['row'], array( |
303
|
8 |
|
'USERNAME' => $username, |
304
|
8 |
|
'AVATAR_IMG' => phpbb_get_user_avatar($row), |
305
|
8 |
|
'POSTS_PCT' => sprintf($this->translator->lang('POST_PCT'), $this->calculate_percent_posts($row['user_posts'])), |
306
|
8 |
|
'L_VIEW_PROFILE' => sprintf($this->translator->lang('VIEW_USER_PROFILE'), $username), |
307
|
8 |
|
'JOINED' => $this->user->format_date($row['user_regdate'], "|$date_format|"), |
308
|
8 |
|
'VISITED' => $this->get_last_visit_date($row['user_lastvisit'], $date_format), |
309
|
8 |
|
'POSTS' => $row['user_posts'], |
310
|
8 |
|
'RANK_TITLE' => $rank['title'], |
311
|
8 |
|
'RANK_IMG' => $rank['img'], |
312
|
8 |
|
'U_PROFILE' => get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']), |
313
|
8 |
|
'U_SEARCH_USER' => append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&sr=posts"), |
314
|
8 |
|
)); |
315
|
|
|
|
316
|
8 |
|
return $tpl_data; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param int $user_posts |
321
|
|
|
* @return int|mixed |
322
|
|
|
*/ |
323
|
8 |
|
private function calculate_percent_posts($user_posts) |
324
|
|
|
{ |
325
|
8 |
|
return ($this->config['num_posts']) ? min(100, ($user_posts / $this->config['num_posts']) * 100) : 0; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param int $last_visited |
330
|
|
|
* @param string $date_format |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
8 |
|
private function get_last_visit_date($last_visited, $date_format) |
334
|
|
|
{ |
335
|
8 |
|
return ($last_visited) ? $this->user->format_date($last_visited, "|$date_format|") : ''; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param string $qtype |
340
|
|
|
* @return string |
341
|
|
|
*/ |
342
|
10 |
|
private function get_block_title($qtype) |
343
|
|
|
{ |
344
|
10 |
|
$qtypes = $this->get_query_types(); |
345
|
10 |
|
return isset($qtypes[$qtype]) ? $qtypes[$qtype] : 'FEATURED_MEMBER'; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @return array |
350
|
|
|
*/ |
351
|
1 |
|
private function get_rotation_frequencies() |
352
|
|
|
{ |
353
|
|
|
return array( |
354
|
1 |
|
'pageload' => 'ROTATE_PAGELOAD', |
355
|
1 |
|
'hourly' => 'ROTATE_HOURLY', |
356
|
1 |
|
'daily' => 'ROTATE_DAILY', |
357
|
1 |
|
'weekly' => 'ROTATE_WEEKLY', |
358
|
1 |
|
'monthly' => 'ROTATE_MONTHLY', |
359
|
1 |
|
); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @return array |
364
|
|
|
*/ |
365
|
11 |
|
private function get_query_types() |
366
|
|
|
{ |
367
|
|
|
return array( |
368
|
11 |
|
'recent' => 'RECENT_MEMBER', |
369
|
11 |
|
'posts' => 'POSTS_MEMBER', |
370
|
11 |
|
'featured' => 'FEATURED_MEMBER', |
371
|
11 |
|
); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|