Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
post.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\notification\type;
015
016 /**
017 * Post notifications class
018 * This class handles notifications for replies to a topic
019 */
020
021 class post extends \phpbb\notification\type\base
022 {
023 /**
024 * Get notification type name
025 *
026 * @return string
027 */
028 public function get_type()
029 {
030 return 'notification.type.post';
031 }
032
033 /**
034 * Language key used to output the text
035 *
036 * @var string
037 */
038 protected $language_key = 'NOTIFICATION_POST';
039
040 /**
041 * Inherit notification read status from post.
042 *
043 * @var bool
044 */
045 protected $inherit_read_status = true;
046
047 /**
048 * Notification option data (for outputting to the user)
049 *
050 * @var bool|array False if the service should use it's default data
051 * Array of data (including keys 'id', 'lang', and 'group')
052 */
053 static public $notification_option = array(
054 'lang' => 'NOTIFICATION_TYPE_POST',
055 'group' => 'NOTIFICATION_GROUP_POSTING',
056 );
057
058 /** @var \phpbb\user_loader */
059 protected $user_loader;
060
061 /** @var \phpbb\config\config */
062 protected $config;
063
064 public function set_config(\phpbb\config\config $config)
065 {
066 $this->config = $config;
067 }
068
069 public function set_user_loader(\phpbb\user_loader $user_loader)
070 {
071 $this->user_loader = $user_loader;
072 }
073
074 /**
075 * Is available
076 */
077 public function is_available()
078 {
079 return $this->config['allow_topic_notify'];
080 }
081
082 /**
083 * Get the id of the item
084 *
085 * @param array $post The data from the post
086 * @return int The post id
087 */
088 static public function get_item_id($post)
089 {
090 return (int) $post['post_id'];
091 }
092
093 /**
094 * Get the id of the parent
095 *
096 * @param array $post The data from the post
097 * @return int The topic id
098 */
099 static public function get_item_parent_id($post)
100 {
101 return (int) $post['topic_id'];
102 }
103
104 /**
105 * Find the users who want to receive notifications
106 *
107 * @param array $post Data from submit_post
108 * @param array $options Options for finding users for notification
109 *
110 * @return array
111 */
112 public function find_users_for_notification($post, $options = array())
113 {
114 $options = array_merge(array(
115 'ignore_users' => array(),
116 ), $options);
117
118 $users = array();
119
120 $sql = 'SELECT user_id
121 FROM ' . TOPICS_WATCH_TABLE . '
122 WHERE topic_id = ' . (int) $post['topic_id'] . '
123 AND notify_status = ' . NOTIFY_YES . '
124 AND user_id <> ' . (int) $post['poster_id'];
125 $result = $this->db->sql_query($sql);
126 while ($row = $this->db->sql_fetchrow($result))
127 {
128 $users[] = (int) $row['user_id'];
129 }
130 $this->db->sql_freeresult($result);
131
132 $sql = 'SELECT user_id
133 FROM ' . FORUMS_WATCH_TABLE . '
134 WHERE forum_id = ' . (int) $post['forum_id'] . '
135 AND notify_status = ' . NOTIFY_YES . '
136 AND user_id <> ' . (int) $post['poster_id'];
137 $result = $this->db->sql_query($sql);
138 while ($row = $this->db->sql_fetchrow($result))
139 {
140 $users[] = (int) $row['user_id'];
141 }
142 $this->db->sql_freeresult($result);
143
144 $notify_users = $this->get_authorised_recipients($users, $post['forum_id'], $options, true);
145
146 if (empty($notify_users))
147 {
148 return array();
149 }
150
151 // Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
152 $notified_users = $this->notification_manager->get_notified_users($this->get_type(), array(
153 'item_parent_id' => static::get_item_parent_id($post),
154 'read' => 0,
155 ));
156
157 foreach ($notified_users as $user => $notification_data)
158 {
159 unset($notify_users[$user]);
160
161 /** @var post $notification */
162 $notification = $this->notification_manager->get_item_type_class($this->get_type(), $notification_data);
163 $update_responders = $notification->add_responders($post);
164 if (!empty($update_responders))
165 {
166 $this->notification_manager->update_notification($notification, $update_responders, array(
167 'item_parent_id' => self::get_item_parent_id($post),
168 'read' => 0,
169 'user_id' => $user,
170 ));
171 }
172 }
173
174 return $notify_users;
175 }
176
177 /**
178 * Get the user's avatar
179 */
180 public function get_avatar()
181 {
182 return $this->user_loader->get_avatar($this->get_data('poster_id'), false, true);
183 }
184
185 /**
186 * Get the HTML formatted title of this notification
187 *
188 * @return string
189 */
190 public function get_title()
191 {
192 $responders = $this->get_data('responders');
193 $usernames = array();
194
195 if (!is_array($responders))
196 {
197 $responders = array();
198 }
199
200 $responders = array_merge(array(array(
201 'poster_id' => $this->get_data('poster_id'),
202 'username' => $this->get_data('post_username'),
203 )), $responders);
204
205 $responders_cnt = sizeof($responders);
206 $responders = $this->trim_user_ary($responders);
207 $trimmed_responders_cnt = $responders_cnt - sizeof($responders);
208
209 foreach ($responders as $responder)
210 {
211 if ($responder['username'])
212 {
213 $usernames[] = $responder['username'];
214 }
215 else
216 {
217 $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
218 }
219 }
220
221 if ($trimmed_responders_cnt > 20)
222 {
223 $usernames[] = $this->language->lang('NOTIFICATION_MANY_OTHERS');
224 }
225 else if ($trimmed_responders_cnt)
226 {
227 $usernames[] = $this->language->lang('NOTIFICATION_X_OTHERS', $trimmed_responders_cnt);
228 }
229
230 return $this->language->lang(
231 $this->language_key,
232 phpbb_generate_string_list($usernames, $this->user),
233 $responders_cnt
234 );
235 }
236
237 /**
238 * Get the HTML formatted reference of the notification
239 *
240 * @return string
241 */
242 public function get_reference()
243 {
244 return $this->language->lang(
245 'NOTIFICATION_REFERENCE',
246 censor_text($this->get_data('topic_title'))
247 );
248 }
249
250 /**
251 * Get email template
252 *
253 * @return string|bool
254 */
255 public function get_email_template()
256 {
257 return 'topic_notify';
258 }
259
260 /**
261 * Get email template variables
262 *
263 * @return array
264 */
265 public function get_email_template_variables()
266 {
267 if ($this->get_data('post_username'))
268 {
269 $username = $this->get_data('post_username');
270 }
271 else
272 {
273 $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username');
274 }
275
276 return array(
277 'AUTHOR_NAME' => htmlspecialchars_decode($username),
278 'POST_SUBJECT' => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))),
279 'TOPIC_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))),
280
281 'U_VIEW_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}",
282 'U_NEWEST_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}&e=1&view=unread#unread",
283 'U_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
284 'U_VIEW_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
285 'U_FORUM' => generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}",
286 'U_STOP_WATCHING_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?uid={$this->user_id}&f={$this->get_data('forum_id')}&t={$this->item_parent_id}&unwatch=topic",
287 );
288 }
289
290 /**
291 * Get the url to this item
292 *
293 * @return string URL
294 */
295 public function get_url()
296 {
297 return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "p={$this->item_id}#p{$this->item_id}");
298 }
299
300 /**
301 * {inheritDoc}
302 */
303 public function get_redirect_url()
304 {
305 return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t={$this->item_parent_id}&view=unread#unread");
306 }
307
308 /**
309 * Users needed to query before this notification can be displayed
310 *
311 * @return array Array of user_ids
312 */
313 public function users_to_query()
314 {
315 $responders = $this->get_data('responders');
316 $users = array(
317 $this->get_data('poster_id'),
318 );
319
320 if (is_array($responders))
321 {
322 foreach ($responders as $responder)
323 {
324 $users[] = $responder['poster_id'];
325 }
326 }
327
328 return $this->trim_user_ary($users);
329 }
330
331 /**
332 * Trim the user array passed down to 3 users if the array contains
333 * more than 4 users.
334 *
335 * @param array $users Array of users
336 * @return array Trimmed array of user_ids
337 */
338 public function trim_user_ary($users)
339 {
340 if (sizeof($users) > 4)
341 {
342 array_splice($users, 3);
343 }
344 return $users;
345 }
346
347 /**
348 * Pre create insert array function
349 * This allows you to perform certain actions, like run a query
350 * and load data, before create_insert_array() is run. The data
351 * returned from this function will be sent to create_insert_array().
352 *
353 * @param array $post Post data from submit_post
354 * @param array $notify_users Notify users list
355 * Formated from find_users_for_notification()
356 * @return array Whatever you want to send to create_insert_array().
357 */
358 public function pre_create_insert_array($post, $notify_users)
359 {
360 if (!sizeof($notify_users) || !$this->inherit_read_status)
361 {
362 return array();
363 }
364
365 $tracking_data = array();
366 $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
367 WHERE topic_id = ' . (int) $post['topic_id'] . '
368 AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
369 $result = $this->db->sql_query($sql);
370 while ($row = $this->db->sql_fetchrow($result))
371 {
372 $tracking_data[$row['user_id']] = $row['mark_time'];
373 }
374 $this->db->sql_freeresult($result);
375
376 return $tracking_data;
377 }
378
379 /**
380 * {@inheritdoc}
381 */
382 public function create_insert_array($post, $pre_create_data = array())
383 {
384 $this->set_data('poster_id', $post['poster_id']);
385
386 $this->set_data('topic_title', $post['topic_title']);
387
388 $this->set_data('post_subject', $post['post_subject']);
389
390 $this->set_data('post_username', (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''));
391
392 $this->set_data('forum_id', $post['forum_id']);
393
394 $this->set_data('forum_name', $post['forum_name']);
395
396 $this->notification_time = $post['post_time'];
397
398 // Topics can be "read" before they are public (while awaiting approval).
399 // Make sure that if the user has read the topic, it's marked as read in the notification
400 if ($this->inherit_read_status && isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->notification_time)
401 {
402 $this->notification_read = true;
403 }
404
405 parent::create_insert_array($post, $pre_create_data);
406 }
407
408 /**
409 * Add responders to the notification
410 *
411 * @param mixed $post
412 * @return array Array of responder data
413 */
414 public function add_responders($post)
415 {
416 // Do not add them as a responder if they were the original poster that created the notification
417 if ($this->get_data('poster_id') == $post['poster_id'])
418 {
419 return array();
420 }
421
422 $responders = $this->get_data('responders');
423
424 $responders = ($responders === null) ? array() : $responders;
425
426 // Do not add more than 25 responders,
427 // we trim the username list to "a, b, c and x others" anyway
428 // so there is no use to add all of them anyway.
429 if (sizeof($responders) > 25)
430 {
431 return array();
432 }
433
434 foreach ($responders as $responder)
435 {
436 // Do not add them as a responder multiple times
437 if ($responder['poster_id'] == $post['poster_id'])
438 {
439 return array();
440 }
441 }
442
443 $responders[] = array(
444 'poster_id' => $post['poster_id'],
445 'username' => (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''),
446 );
447
448 $this->set_data('responders', $responders);
449
450 $serialized_data = serialize($this->get_data(false));
451
452 // If the data is longer then 4000 characters, it would cause a SQL error.
453 // We don't add the username to the list if this is the case.
454 if (utf8_strlen($serialized_data) >= 4000)
455 {
456 return array();
457 }
458
459 $data_array = array_merge(array(
460 'post_time' => $post['post_time'],
461 'post_id' => $post['post_id'],
462 'topic_id' => $post['topic_id']
463 ), $this->get_data(false));
464
465 return $data_array;
466 }
467 }
468