Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.49% |
31 / 39 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
quote | |
79.49% |
31 / 39 |
|
50.00% |
4 / 8 |
13.24 | |
0.00% |
0 / 1 |
get_type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
is_available | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
find_users_for_notification | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
update_notifications | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
3.04 | |||
get_redirect_url | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_email_template | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_email_template_variables | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
set_utils | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
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 | |
14 | namespace phpbb\notification\type; |
15 | |
16 | /** |
17 | * Post quoting notifications class |
18 | * This class handles notifying users when they have been quoted in a post |
19 | */ |
20 | |
21 | class quote extends \phpbb\notification\type\post |
22 | { |
23 | /** |
24 | * @var \phpbb\textformatter\utils_interface |
25 | */ |
26 | protected $utils; |
27 | |
28 | /** |
29 | * Get notification type name |
30 | * |
31 | * @return string |
32 | */ |
33 | public function get_type() |
34 | { |
35 | return 'notification.type.quote'; |
36 | } |
37 | |
38 | /** |
39 | * Language key used to output the text |
40 | * |
41 | * @var string |
42 | */ |
43 | protected $language_key = 'NOTIFICATION_QUOTE'; |
44 | |
45 | /** |
46 | * Notification option data (for outputting to the user) |
47 | * |
48 | * @var bool|array False if the service should use it's default data |
49 | * Array of data (including keys 'id', 'lang', and 'group') |
50 | */ |
51 | public static $notification_option = array( |
52 | 'lang' => 'NOTIFICATION_TYPE_QUOTE', |
53 | 'group' => 'NOTIFICATION_GROUP_POSTING', |
54 | ); |
55 | |
56 | /** |
57 | * Is available |
58 | */ |
59 | public function is_available() |
60 | { |
61 | return true; |
62 | } |
63 | |
64 | /** |
65 | * Find the users who want to receive notifications |
66 | * |
67 | * @param array $type_data Data from submit_post |
68 | * @param array $options Options for finding users for notification |
69 | * |
70 | * @return array |
71 | */ |
72 | public function find_users_for_notification($type_data, $options = array()) |
73 | { |
74 | $options = array_merge(array( |
75 | 'ignore_users' => array(), |
76 | ), $options); |
77 | |
78 | $usernames = $this->utils->get_outermost_quote_authors($type_data['post_text']); |
79 | |
80 | if (empty($usernames)) |
81 | { |
82 | return array(); |
83 | } |
84 | |
85 | $usernames = array_unique($usernames); |
86 | |
87 | $usernames = array_map('utf8_clean_string', $usernames); |
88 | |
89 | $users = array(); |
90 | |
91 | $sql = 'SELECT user_id |
92 | FROM ' . USERS_TABLE . ' |
93 | WHERE ' . $this->db->sql_in_set('username_clean', $usernames) . ' |
94 | AND user_id <> ' . (int) $type_data['poster_id']; |
95 | $result = $this->db->sql_query($sql); |
96 | while ($row = $this->db->sql_fetchrow($result)) |
97 | { |
98 | $users[] = (int) $row['user_id']; |
99 | } |
100 | $this->db->sql_freeresult($result); |
101 | |
102 | return $this->get_authorised_recipients($users, $type_data['forum_id'], $options, true); |
103 | } |
104 | |
105 | /** |
106 | * Update a notification |
107 | * |
108 | * @param array $post Data specific for this type that will be updated |
109 | * @return true |
110 | */ |
111 | public function update_notifications($post) |
112 | { |
113 | $old_notifications = $this->notification_manager->get_notified_users($this->get_type(), array( |
114 | 'item_id' => static::get_item_id($post), |
115 | )); |
116 | |
117 | // Find the new users to notify |
118 | $notifications = $this->find_users_for_notification($post); |
119 | |
120 | // Find the notifications we must delete |
121 | $remove_notifications = array_diff(array_keys($old_notifications), array_keys($notifications)); |
122 | |
123 | // Find the notifications we must add |
124 | $add_notifications = array(); |
125 | foreach (array_diff(array_keys($notifications), array_keys($old_notifications)) as $user_id) |
126 | { |
127 | $add_notifications[$user_id] = $notifications[$user_id]; |
128 | } |
129 | |
130 | // Add the necessary notifications |
131 | $this->notification_manager->add_notifications_for_users($this->get_type(), $post, $add_notifications); |
132 | |
133 | // Remove the necessary notifications |
134 | if (!empty($remove_notifications)) |
135 | { |
136 | $this->notification_manager->delete_notifications($this->get_type(), static::get_item_id($post), false, $remove_notifications); |
137 | } |
138 | |
139 | // return true to continue with the update code in the notifications service (this will update the rest of the notifications) |
140 | return true; |
141 | } |
142 | |
143 | /** |
144 | * {inheritDoc} |
145 | */ |
146 | public function get_redirect_url() |
147 | { |
148 | return $this->get_url(); |
149 | } |
150 | |
151 | /** |
152 | * {@inheritdoc} |
153 | */ |
154 | public function get_email_template() |
155 | { |
156 | return 'quote'; |
157 | } |
158 | |
159 | /** |
160 | * Get email template variables |
161 | * |
162 | * @return array |
163 | */ |
164 | public function get_email_template_variables() |
165 | { |
166 | $user_data = $this->user_loader->get_user($this->get_data('poster_id')); |
167 | |
168 | return array_merge(parent::get_email_template_variables(), array( |
169 | 'AUTHOR_NAME' => html_entity_decode($user_data['username'], ENT_COMPAT), |
170 | )); |
171 | } |
172 | |
173 | /** |
174 | * Set the utils service used to retrieve quote authors |
175 | * |
176 | * @param \phpbb\textformatter\utils_interface $utils |
177 | */ |
178 | public function set_utils(\phpbb\textformatter\utils_interface $utils) |
179 | { |
180 | $this->utils = $utils; |
181 | } |
182 | } |