1 | <?php |
||
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 | 9 | 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 | 9 | $this->cache = $cache; |
|
74 | 9 | $this->config = $config; |
|
75 | 9 | $this->db = $db; |
|
76 | 9 | $this->user = $user; |
|
77 | 9 | $this->profilefields = $profilefields; |
|
78 | 9 | $this->phpbb_root_path = $phpbb_root_path; |
|
79 | 9 | $this->php_ext = $php_ext; |
|
80 | 9 | $this->blocks_table = $blocks_table; |
|
81 | 9 | $this->cache_time = $cache_time; |
|
82 | 9 | } |
|
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 | 8 | public function display(array $bdata, $edit_mode = false) |
|
131 | |||
132 | /** |
||
133 | * @param bool $change_user |
||
134 | * @return array|false |
||
135 | */ |
||
136 | 8 | private function _get_user_data($change_user) |
|
137 | { |
||
138 | $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 |
||
139 | 8 | FROM ' . USERS_TABLE . ' |
|
140 | 8 | WHERE ' . $this->db->sql_in_set('user_type', array(USER_NORMAL, USER_FOUNDER)); |
|
141 | |||
142 | 8 | $method = '_query_' . $this->settings['qtype']; |
|
143 | |||
144 | 8 | if (is_callable(array($this, $method))) |
|
145 | 8 | { |
|
146 | 7 | call_user_func_array(array($this, $method), array(&$sql, $change_user)); |
|
147 | 7 | } |
|
148 | else |
||
149 | { |
||
150 | 1 | return array(); |
|
151 | } |
||
152 | |||
153 | 7 | $result = $this->db->sql_query_limit($sql, 1, 0, $this->_get_cache_time($this->settings['qtype'], $this->settings['rotation'])); |
|
154 | 7 | $row = $this->db->sql_fetchrow($result); |
|
155 | 7 | $this->db->sql_freeresult($result); |
|
156 | |||
157 | 7 | return $row; |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param string $sql |
||
162 | * @param bool $change_user |
||
163 | */ |
||
164 | 5 | private function _query_featured(&$sql, $change_user) |
|
168 | |||
169 | /** |
||
170 | * @param string $sql |
||
171 | */ |
||
172 | 1 | private function _query_recent(&$sql) |
|
176 | |||
177 | /** |
||
178 | * @param string $sql |
||
179 | */ |
||
180 | 1 | private function _query_posts(&$sql) |
|
184 | |||
185 | /** |
||
186 | * @param string $query_type |
||
187 | * @param string $rotation |
||
188 | * @return int |
||
189 | */ |
||
190 | 7 | private function _get_cache_time($query_type, $rotation) |
|
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | 8 | private function _change_user() |
|
199 | { |
||
200 | 8 | $change = false; |
|
201 | 8 | if ($this->settings['rotation'] == 'pageload' || $this->settings['last_changed'] < strtotime('-1 ' . self::$rotations[$this->settings['rotation']])) |
|
202 | 8 | { |
|
203 | 8 | $this->settings['last_changed'] = time(); |
|
204 | 8 | $change = true; |
|
205 | 8 | } |
|
206 | |||
207 | 8 | return $change; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | */ |
||
212 | 7 | private function _explain_view() |
|
222 | |||
223 | /** |
||
224 | * @param array $bdata |
||
225 | * @return array |
||
226 | */ |
||
227 | 8 | private function _get_settings(array $bdata) |
|
228 | { |
||
229 | 8 | $cached_settings = $this->cache->get('pt_block_data_' . $bdata['bid']); |
|
230 | 8 | $settings = ($cached_settings && $cached_settings['hash'] === $bdata['hash']) ? $cached_settings : $bdata['settings']; |
|
231 | 8 | $settings['hash'] = $bdata['hash']; |
|
232 | |||
233 | 8 | return $settings; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $bid |
||
238 | * @param bool $change_user |
||
239 | */ |
||
240 | 7 | private function _save_settings($bid, $change_user) |
|
253 | |||
254 | /** |
||
255 | * @param array $row |
||
256 | */ |
||
257 | 8 | private function _display_user($block_id, array $row, $change_user) |
|
258 | { |
||
259 | 8 | $html = ''; |
|
260 | 8 | if (sizeof($row)) |
|
261 | 8 | { |
|
262 | 7 | $this->_save_settings($block_id, $change_user); |
|
263 | 7 | $this->_explain_view(); |
|
264 | |||
265 | 7 | $tpl_data = $this->_get_template_data($row); |
|
266 | 7 | $this->ptemplate->assign_vars($tpl_data['row']); |
|
267 | 7 | $this->ptemplate->assign_block_vars_array('custom_fields', $tpl_data['blockrow']); |
|
268 | 7 | unset($tpl_data); |
|
269 | |||
270 | 7 | $html = $this->ptemplate->render_view('blitze/sitemaker', 'blocks/featured_member.html', 'featured_member_block'); |
|
271 | 7 | } |
|
272 | |||
273 | 8 | return $html; |
|
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param array $row |
||
278 | * @return array |
||
279 | */ |
||
280 | 7 | private function _get_template_data(array $row) |
|
281 | { |
||
282 | 7 | $date_format = $this->user->lang('DATE_FORMAT'); |
|
283 | 7 | $username = get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']); |
|
284 | 7 | $rank = phpbb_get_user_rank($row, $row['user_posts']); |
|
285 | |||
286 | 7 | $tpl_data = $this->profilefields->get_template_data($row['user_id'], $this->settings['show_cpf']); |
|
287 | |||
288 | 7 | $tpl_data['row'] = array_merge($tpl_data['row'], array( |
|
289 | 7 | 'USERNAME' => $username, |
|
290 | 7 | 'AVATAR_IMG' => phpbb_get_user_avatar($row), |
|
291 | 7 | 'POSTS_PCT' => sprintf($this->user->lang('POST_PCT'), $this->_calculate_percent_posts($row['user_posts'])), |
|
292 | 7 | 'L_VIEW_PROFILE' => sprintf($this->user->lang('VIEW_USER_PROFILE'), $username), |
|
293 | 7 | 'JOINED' => $this->user->format_date($row['user_regdate'], "|$date_format|"), |
|
294 | 7 | 'VISITED' => $this->_get_last_visit_date($row['user_lastvisit'], $date_format), |
|
295 | 7 | 'POSTS' => $row['user_posts'], |
|
296 | 7 | 'RANK_TITLE' => $rank['title'], |
|
297 | 7 | 'RANK_IMG' => $rank['img'], |
|
298 | 7 | 'U_PROFILE' => get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']), |
|
299 | 7 | 'U_SEARCH_USER' => append_sid($this->phpbb_root_path . 'search.' . $this->php_ext, "author_id={$row['user_id']}&sr=posts"), |
|
300 | 7 | )); |
|
301 | |||
302 | 7 | return $tpl_data; |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param int $user_posts |
||
307 | * @return int|mixed |
||
308 | */ |
||
309 | 7 | private function _calculate_percent_posts($user_posts) |
|
313 | |||
314 | /** |
||
315 | * @param int $last_visited |
||
316 | * @param string $date_format |
||
317 | * @return string |
||
318 | */ |
||
319 | 7 | private function _get_last_visit_date($last_visited, $date_format) |
|
323 | |||
324 | /** |
||
325 | * @param string $qtype |
||
326 | * @return string |
||
327 | */ |
||
328 | 8 | private function _get_block_title($qtype) |
|
329 | { |
||
333 | |||
334 | /** |
||
335 | * @return array |
||
336 | */ |
||
337 | 1 | private function _get_rotation_frequencies() |
|
347 | |||
348 | /** |
||
349 | * @return array |
||
350 | */ |
||
351 | 9 | private function _get_query_types() |
|
359 | } |
||
360 |