Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.11% covered (warning)
52.11%
37 / 71
47.06% covered (danger)
47.06%
8 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
topic
52.11% covered (warning)
52.11%
37 / 71
47.06% covered (danger)
47.06%
8 / 17
107.06
0.00% covered (danger)
0.00%
0 / 1
 get_type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_config
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_user_loader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_item_id
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_item_parent_id
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 find_users_for_notification
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 get_avatar
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_title
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_reference
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get_forum
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 get_email_template
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_email_template_variables
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 get_url
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 users_to_query
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 pre_create_insert_array
81.82% covered (warning)
81.82%
9 / 11
0.00% covered (danger)
0.00%
0 / 1
4.10
 create_insert_array
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\notification\type;
15
16/**
17* Topic notifications class
18* This class handles notifications for new topics
19*/
20
21class topic extends \phpbb\notification\type\base
22{
23    /**
24    * Get notification type name
25    *
26    * @return string
27    */
28    public function get_type()
29    {
30        return 'notification.type.topic';
31    }
32
33    /**
34    * Language key used to output the text
35    *
36    * @var string
37    */
38    protected $language_key = 'NOTIFICATION_TOPIC';
39
40    /**
41    * Inherit notification read status from topic.
42    *
43    * @var bool
44    */
45    protected $inherit_read_status = true;
46
47    /**
48    * Notification option data (for outputting to the user)
49    *
50    * @var bool|array False if the service should use it's default data
51    *                     Array of data (including keys 'id', 'lang', and 'group')
52    */
53    public static $notification_option = array(
54        'lang'    => 'NOTIFICATION_TYPE_TOPIC',
55        'group'    => 'NOTIFICATION_GROUP_POSTING',
56    );
57
58    /** @var \phpbb\user_loader */
59    protected $user_loader;
60
61    /** @var \phpbb\config\config */
62    protected $config;
63
64    public function set_config(\phpbb\config\config $config)
65    {
66        $this->config = $config;
67    }
68
69    public function set_user_loader(\phpbb\user_loader $user_loader)
70    {
71        $this->user_loader = $user_loader;
72    }
73
74    /**
75    * Is available
76    */
77    public function is_available()
78    {
79        return (bool) $this->config['allow_forum_notify'];
80    }
81
82    /**
83    * Get the id of the item
84    *
85    * @param array $type_data The data from the post
86    *
87    * @return int The topic id
88    */
89    public static function get_item_id($type_data)
90    {
91        return (int) $type_data['topic_id'];
92    }
93
94    /**
95    * Get the id of the parent
96    *
97    * @param array $type_data The data from the post
98    *
99    * @return int The forum id
100    */
101    public static function get_item_parent_id($type_data)
102    {
103        return (int) $type_data['forum_id'];
104    }
105
106    /**
107    * Find the users who want to receive notifications
108    *
109    * @param array $type_data Data from the topic
110    * @param array $options Options for finding users for notification
111    *
112    * @return array
113    */
114    public function find_users_for_notification($type_data, $options = array())
115    {
116        $options = array_merge(array(
117            'ignore_users'            => array(),
118        ), $options);
119
120        $users = array();
121
122        $sql = 'SELECT user_id
123            FROM ' . FORUMS_WATCH_TABLE . '
124            WHERE forum_id = ' . (int) $type_data['forum_id'] . '
125                AND notify_status = ' . NOTIFY_YES . '
126                AND user_id <> ' . (int) $type_data['poster_id'];
127        $result = $this->db->sql_query($sql);
128        while ($row = $this->db->sql_fetchrow($result))
129        {
130            $users[] = (int) $row['user_id'];
131        }
132        $this->db->sql_freeresult($result);
133
134        return $this->get_authorised_recipients($users, $type_data['forum_id'], $options);
135    }
136
137    /**
138    * Get the user's avatar
139    */
140    public function get_avatar()
141    {
142        return $this->user_loader->get_avatar($this->get_data('poster_id'), false, true);
143    }
144
145    /**
146    * Get the HTML formatted title of this notification
147    *
148    * @return string
149    */
150    public function get_title()
151    {
152        if ($this->get_data('post_username'))
153        {
154            $username = $this->get_data('post_username');
155        }
156        else
157        {
158            $username = $this->user_loader->get_username($this->get_data('poster_id'), 'no_profile');
159        }
160
161        return $this->language->lang(
162            $this->language_key,
163            $username
164        );
165    }
166
167    /**
168    * Get the HTML formatted reference of the notification
169    *
170    * @return string
171    */
172    public function get_reference()
173    {
174        return $this->language->lang(
175            'NOTIFICATION_REFERENCE',
176            censor_text($this->get_data('topic_title'))
177        );
178    }
179
180    /**
181    * Get the forum of the notification reference
182    *
183    * @return string
184    */
185    public function get_forum()
186    {
187        return $this->language->lang(
188            'NOTIFICATION_FORUM',
189            $this->get_data('forum_name')
190        );
191    }
192
193    /**
194    * {@inheritdoc}
195    */
196    public function get_email_template()
197    {
198        return 'newtopic_notify';
199    }
200
201    /**
202    * Get email template variables
203    *
204    * @return array
205    */
206    public function get_email_template_variables()
207    {
208        $board_url = generate_board_url();
209
210        if ($this->get_data('post_username'))
211        {
212            $username = $this->get_data('post_username');
213        }
214        else
215        {
216            $username = $this->user_loader->get_username($this->get_data('poster_id'), 'username');
217        }
218
219        return array(
220            'AUTHOR_NAME'                => html_entity_decode($username, ENT_COMPAT),
221            'FORUM_NAME'                => html_entity_decode($this->get_data('forum_name'), ENT_COMPAT),
222            'TOPIC_TITLE'                => html_entity_decode(censor_text($this->get_data('topic_title')), ENT_COMPAT),
223
224            'U_TOPIC'                    => "{$board_url}/viewtopic.{$this->php_ext}?t={$this->item_id}",
225            'U_VIEW_TOPIC'                => "{$board_url}/viewtopic.{$this->php_ext}?t={$this->item_id}",
226            'U_FORUM'                    => "{$board_url}/viewforum.{$this->php_ext}?f={$this->item_parent_id}",
227            'U_STOP_WATCHING_FORUM'        => "{$board_url}/viewforum.{$this->php_ext}?uid={$this->user_id}&f={$this->item_parent_id}&unwatch=forum",
228        );
229    }
230
231    /**
232    * Get the url to this item
233    *
234    * @return string URL
235    */
236    public function get_url()
237    {
238        return append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "t={$this->item_id}");
239    }
240
241    /**
242    * Users needed to query before this notification can be displayed
243    *
244    * @return array Array of user_ids
245    */
246    public function users_to_query()
247    {
248        return array($this->get_data('poster_id'));
249    }
250
251    /**
252    * Pre create insert array function
253    * This allows you to perform certain actions, like run a query
254    * and load data, before create_insert_array() is run. The data
255    * returned from this function will be sent to create_insert_array().
256    *
257    * @param array $type_data Post data from submit_post
258    * @param array $notify_users Notify users list
259    *         Formatted from find_users_for_notification()
260    *
261    * @return array Whatever you want to send to create_insert_array().
262    */
263    public function pre_create_insert_array($type_data, $notify_users)
264    {
265        if (!count($notify_users) || !$this->inherit_read_status)
266        {
267            return array();
268        }
269
270        $tracking_data = array();
271        $sql = 'SELECT user_id, mark_time FROM ' . TOPICS_TRACK_TABLE . '
272            WHERE topic_id = ' . (int) $type_data['topic_id'] . '
273                AND ' . $this->db->sql_in_set('user_id', array_keys($notify_users));
274        $result = $this->db->sql_query($sql);
275        while ($row = $this->db->sql_fetchrow($result))
276        {
277            $tracking_data[$row['user_id']] = $row['mark_time'];
278        }
279        $this->db->sql_freeresult($result);
280
281        return $tracking_data;
282    }
283
284    /**
285    * {@inheritdoc}
286    */
287    public function create_insert_array($type_data, $pre_create_data = array())
288    {
289        $this->set_data('poster_id', $type_data['poster_id']);
290
291        $this->set_data('topic_title', $type_data['topic_title']);
292
293        $this->set_data('post_username', (($type_data['poster_id'] == ANONYMOUS) ? $type_data['post_username'] : ''));
294
295        $this->set_data('forum_name', $type_data['forum_name']);
296
297        $this->notification_time = $type_data['post_time'];
298
299        // Topics can be "read" before they are public (while awaiting approval).
300        // Make sure that if the user has read the topic, it's marked as read in the notification
301        if ($this->inherit_read_status && isset($pre_create_data[$this->user_id]) && $pre_create_data[$this->user_id] >= $this->notification_time)
302        {
303            $this->notification_read = true;
304        }
305
306        parent::create_insert_array($type_data, $pre_create_data);
307    }
308}