Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
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 public static $notification_option = array(
054 'lang' => 'NOTIFICATION_TYPE_POST',
055 'group' => 'NOTIFICATION_GROUP_POSTING',
056 );
057
058 /**
059 * Is available
060 */
061 public function is_available()
062 {
063 return $this->config['allow_topic_notify'];
064 }
065
066 /**
067 * Get the id of the item
068 *
069 * @param array $post The data from the post
070 */
071 public static function get_item_id($post)
072 {
073 return (int) $post['post_id'];
074 }
075
076 /**
077 * Get the id of the parent
078 *
079 * @param array $post The data from the post
080 */
081 public static function get_item_parent_id($post)
082 {
083 return (int) $post['topic_id'];
084 }
085
086 /**
087 * Find the users who want to receive notifications
088 *
089 * @param array $post Data from submit_post
090 * @param array $options Options for finding users for notification
091 *
092 * @return array
093 */
094 public function find_users_for_notification($post, $options = array())
095 {
096 $options = array_merge(array(
097 'ignore_users' => array(),
098 ), $options);
099
100 $users = array();
101
102 $sql = 'SELECT user_id
103 FROM ' . TOPICS_WATCH_TABLE . '
104 WHERE topic_id = ' . (int) $post['topic_id'] . '
105 AND notify_status = ' . NOTIFY_YES . '
106 AND user_id <> ' . (int) $post['poster_id'];
107 $result = $this->db->sql_query($sql);
108 while ($row = $this->db->sql_fetchrow($result))
109 {
110 $users[] = (int) $row['user_id'];
111 }
112 $this->db->sql_freeresult($result);
113
114 $sql = 'SELECT user_id
115 FROM ' . FORUMS_WATCH_TABLE . '
116 WHERE forum_id = ' . (int) $post['forum_id'] . '
117 AND notify_status = ' . NOTIFY_YES . '
118 AND user_id <> ' . (int) $post['poster_id'];
119 $result = $this->db->sql_query($sql);
120 while ($row = $this->db->sql_fetchrow($result))
121 {
122 $users[] = (int) $row['user_id'];
123 }
124 $this->db->sql_freeresult($result);
125
126 $notify_users = $this->get_authorised_recipients($users, $post['forum_id'], $options, true);
127
128 if (empty($notify_users))
129 {
130 return array();
131 }
132
133 // Try to find the users who already have been notified about replies and have not read the topic since and just update their notifications
134 $update_notifications = array();
135 $sql = 'SELECT n.*
136 FROM ' . $this->notifications_table . ' n, ' . $this->notification_types_table . ' nt
137 WHERE n.notification_type_id = ' . (int) $this->notification_type_id . '
138 AND n.item_parent_id = ' . (int) self::get_item_parent_id($post) . '
139 AND n.notification_read = 0
140 AND nt.notification_type_id = n.notification_type_id
141 AND nt.notification_type_enabled = 1';
142 $result = $this->db->sql_query($sql);
143 while ($row = $this->db->sql_fetchrow($result))
144 {
145 // Do not create a new notification
146 unset($notify_users[$row['user_id']]);
147
148 $notification = $this->notification_manager->get_item_type_class($this->get_type(), $row);
149 $update_responders = $notification->add_responders($post);
150 if (!empty($update_responders))
151 {
152 $sql = 'UPDATE ' . $this->notifications_table . '
153 SET ' . $this->db->sql_build_array('UPDATE', $update_responders) . '
154 WHERE notification_id = ' . $row['notification_id'];
155 $this->db->sql_query($sql);
156 }
157 }
158 $this->db->sql_freeresult($result);
159
160 return $notify_users;
161 }
162
163 /**
164 * Get the user's avatar
165 */
166 public function get_avatar()
167 {
168 return $this->user_loader->get_avatar($this->get_data('poster_id'));
169 }
170
171 /**
172 * Get the HTML formatted title of this notification
173 *
174 * @return string
175 */
176 public function get_title()
177 {
178 $responders = $this->get_data('responders');
179 $usernames = array();
180
181 if (!is_array($responders))
182 {
183 $responders = array();
184 }
185
186 $responders = array_merge(array(array(
187 'poster_id' => $this->get_data('poster_id'),
188 'username' => $this->get_data('post_username'),
189 )), $responders);
190
191 $responders_cnt = sizeof($responders);
192 $responders = $this->trim_user_ary($responders);
193 $trimmed_responders_cnt = $responders_cnt - sizeof($responders);
194
195 foreach ($responders as $responder)
196 {
197 if ($responder['username'])
198 {
199 $usernames[] = $responder['username'];
200 }
201 else
202 {
203 $usernames[] = $this->user_loader->get_username($responder['poster_id'], 'no_profile');
204 }
205 }
206
207 if ($trimmed_responders_cnt > 20)
208 {
209 $usernames[] = $this->user->lang('NOTIFICATION_MANY_OTHERS');
210 }
211 else if ($trimmed_responders_cnt)
212 {
213 $usernames[] = $this->user->lang('NOTIFICATION_X_OTHERS', $trimmed_responders_cnt);
214 }
215
216 return $this->user->lang(
217 $this->language_key,
218 phpbb_generate_string_list($usernames, $this->user),
219 $responders_cnt
220 );
221 }
222
223 /**
224 * Get the HTML formatted reference of the notification
225 *
226 * @return string
227 */
228 public function get_reference()
229 {
230 return $this->user->lang(
231 'NOTIFICATION_REFERENCE',
232 censor_text($this->get_data('topic_title'))
233 );
234 }
235
236 /**
237 * Get email template
238 *
239 * @return string|bool
240 */
241 public function get_email_template()
242 {
243 return 'topic_notify';
244 }
245
246 /**
247 * Get email template variables
248 *
249 * @return array
250 */
251 public function get_email_template_variables()
252 {
253 if ($this->get_data('post_username'))
254 {
255 $username = $this->get_data('post_username');
256 }
257 else
258 {
259 $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username');
260 }
261
262 return array(
263 'AUTHOR_NAME' => htmlspecialchars_decode($username),
264 'POST_SUBJECT' => htmlspecialchars_decode(censor_text($this->get_data('post_subject'))),
265 'TOPIC_TITLE' => htmlspecialchars_decode(censor_text($this->get_data('topic_title'))),
266
267 'U_VIEW_POST' => generate_board_url() . "/viewtopic.{$this->php_ext}?p={$this->item_id}#p{$this->item_id}",
268 '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",
269 'U_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
270 'U_VIEW_TOPIC' => generate_board_url() . "/viewtopic.{$this->php_ext}?f={$this->get_data('forum_id')}&t={$this->item_parent_id}",
271 'U_FORUM' => generate_board_url() . "/viewforum.{$this->php_ext}?f={$this->get_data('forum_id')}",
272 '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",
273 );
274 }
275
276 /**
277 * Get the url to this item
278 *
279 * @return string URL
280 */
281 public function get_url()
282 {
283 return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "p={$this->item_id}#p{$this->item_id}");
284 }
285
286 /**
287 * {inheritDoc}
288 */
289 public function get_redirect_url()
290 {
291 return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t={$this->item_parent_id}&view=unread#unread");
292 }
293
294 /**
295 * Users needed to query before this notification can be displayed
296 *
297 * @return array Array of user_ids
298 */
299 public function users_to_query()
300 {
301 $responders = $this->get_data('responders');
302 $users = array(
303 $this->get_data('poster_id'),
304 );
305
306 if (is_array($responders))
307 {
308 foreach ($responders as $responder)
309 {
310 $users[] = $responder['poster_id'];
311 }
312 }
313
314 return $this->trim_user_ary($users);
315 }
316
317 /**
318 * Trim the user array passed down to 3 users if the array contains
319 * more than 4 users.
320 *
321 * @param array $users Array of users
322 * @return array Trimmed array of user_ids
323 */
324 public function trim_user_ary($users)
325 {
326 if (sizeof($users) > 4)
327 {
328 array_splice($users, 3);
329 }
330 return $users;
331 }
332
333 /**
334 * Pre create insert array function
335 * This allows you to perform certain actions, like run a query
336 * and load data, before create_insert_array() is run. The data
337 * returned from this function will be sent to create_insert_array().
338 *
339 * @param array $post Post data from submit_post
340 * @param array $notify_users Notify users list
341 * Formated from find_users_for_notification()
342 * @return array Whatever you want to send to create_insert_array().
343 */
344 public function pre_create_insert_array($post, $notify_users)
345 {
346 if (!sizeof($notify_users) || !$this->inherit_read_status)
347 {
348 return array();
349 }
350
351 $tracking_data = array();
352 $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
353 WHERE topic_id = ' . (int) $post['topic_id'] . '
354 AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
355 $result = $this->db->sql_query($sql);
356 while ($row = $this->db->sql_fetchrow($result))
357 {
358 $tracking_data[$row['user_id']] = $row['mark_time'];
359 }
360
361 return $tracking_data;
362 }
363
364 /**
365 * Function for preparing the data for insertion in an SQL query
366 * (The service handles insertion)
367 *
368 * @param array $post Data from submit_post
369 * @param array $pre_create_data Data from pre_create_insert_array()
370 *
371 * @return array Array of data ready to be inserted into the database
372 */
373 public function create_insert_array($post, $pre_create_data = array())
374 {
375 $this->set_data('poster_id', $post['poster_id']);
376
377 $this->set_data('topic_title', $post['topic_title']);
378
379 $this->set_data('post_subject', $post['post_subject']);
380
381 $this->set_data('post_username', (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''));
382
383 $this->set_data('forum_id', $post['forum_id']);
384
385 $this->set_data('forum_name', $post['forum_name']);
386
387 $this->notification_time = $post['post_time'];
388
389 // Topics can be "read" before they are public (while awaiting approval).
390 // Make sure that if the user has read the topic, it's marked as read in the notification
391 if ($this->inherit_read_status && isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->notification_time)
392 {
393 $this->notification_read = true;
394 }
395
396 return parent::create_insert_array($post, $pre_create_data);
397 }
398
399 /**
400 * Add responders to the notification
401 *
402 * @param mixed $post
403 */
404 public function add_responders($post)
405 {
406 // Do not add them as a responder if they were the original poster that created the notification
407 if ($this->get_data('poster_id') == $post['poster_id'])
408 {
409 return array();
410 }
411
412 $responders = $this->get_data('responders');
413
414 $responders = ($responders === null) ? array() : $responders;
415
416 // Do not add more than 25 responders,
417 // we trim the username list to "a, b, c and x others" anyway
418 // so there is no use to add all of them anyway.
419 if (sizeof($responders) > 25)
420 {
421 return array();
422 }
423
424 foreach ($responders as $responder)
425 {
426 // Do not add them as a responder multiple times
427 if ($responder['poster_id'] == $post['poster_id'])
428 {
429 return array();
430 }
431 }
432
433 $responders[] = array(
434 'poster_id' => $post['poster_id'],
435 'username' => (($post['poster_id'] == ANONYMOUS) ? $post['post_username'] : ''),
436 );
437
438 $this->set_data('responders', $responders);
439
440 $serialized_data = serialize($this->get_data(false));
441
442 // If the data is longer then 4000 characters, it would cause a SQL error.
443 // We don't add the username to the list if this is the case.
444 if (utf8_strlen($serialized_data) >= 4000)
445 {
446 return array();
447 }
448
449 return array('notification_data' => $serialized_data);
450 }
451 }
452