These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Elgg notifications plugin |
||
4 | * |
||
5 | * @package ElggNotifications |
||
6 | */ |
||
7 | |||
8 | elgg_register_event_handler('init', 'system', 'notifications_plugin_init'); |
||
9 | |||
10 | function notifications_plugin_init() { |
||
11 | |||
12 | elgg_extend_view('elgg.css', 'notifications.css'); |
||
13 | |||
14 | elgg_register_page_handler('notifications', 'notifications_page_handler'); |
||
15 | |||
16 | elgg_register_plugin_hook_handler('register', 'menu:page', '_notifications_page_menu'); |
||
17 | elgg_register_plugin_hook_handler('register', 'menu:page', '_notifications_groups_subscription_page_menu'); |
||
18 | |||
19 | // Unset the default notification settings |
||
20 | elgg_unregister_plugin_hook_handler('usersettings:save', 'user', '_elgg_save_notification_user_settings'); |
||
21 | elgg_unextend_view('forms/account/settings', 'core/settings/account/notifications'); |
||
22 | |||
23 | // update notifications based on relationships changing |
||
24 | elgg_register_event_handler('delete', 'relationship', 'notifications_relationship_remove'); |
||
25 | |||
26 | // update notifications when new friend or access collection membership |
||
27 | elgg_register_event_handler('create', 'relationship', 'notifications_update_friend_notify'); |
||
28 | elgg_register_plugin_hook_handler('access:collections:add_user', 'collection', 'notifications_update_collection_notify'); |
||
29 | |||
30 | // register unit tests |
||
31 | elgg_register_plugin_hook_handler('unit_test', 'system', 'notifications_register_tests'); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Route page requests |
||
36 | * |
||
37 | * @param array $page Array of url parameters |
||
38 | * @return bool |
||
39 | */ |
||
40 | function notifications_page_handler($page) { |
||
41 | |||
42 | elgg_gatekeeper(); |
||
43 | |||
44 | // Set the context to settings |
||
45 | elgg_set_context('settings'); |
||
46 | |||
47 | $current_user = elgg_get_logged_in_user_entity(); |
||
48 | |||
49 | // default to personal notifications |
||
50 | if (!isset($page[0])) { |
||
51 | $page[0] = 'personal'; |
||
52 | } |
||
53 | if (!isset($page[1])) { |
||
54 | forward("notifications/{$page[0]}/{$current_user->username}"); |
||
55 | } |
||
56 | |||
57 | $vars['username'] = $page[1]; |
||
58 | |||
59 | // note: $user passed in |
||
60 | switch ($page[0]) { |
||
61 | case 'group': |
||
62 | echo elgg_view_resource('notifications/groups', $vars); |
||
63 | break; |
||
64 | case 'personal': |
||
65 | echo elgg_view_resource('notifications/index', $vars); |
||
66 | break; |
||
67 | default: |
||
68 | return false; |
||
69 | } |
||
70 | return true; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Register menu items for the page menu |
||
75 | * |
||
76 | * @param string $hook |
||
77 | * @param string $type |
||
78 | * @param array $return |
||
79 | * @param array $params |
||
80 | * @return array |
||
81 | * |
||
82 | * @access private |
||
83 | * |
||
84 | * @since 3.0 |
||
85 | */ |
||
86 | function _notifications_page_menu($hook, $type, $return, $params) { |
||
87 | |||
88 | if (!elgg_in_context('settings') || !elgg_get_logged_in_user_guid()) { |
||
89 | return; |
||
90 | } |
||
91 | |||
92 | $user = elgg_get_page_owner_entity(); |
||
93 | if (!$user) { |
||
94 | $user = elgg_get_logged_in_user_entity(); |
||
95 | } |
||
96 | |||
97 | $return[] = \ElggMenuItem::factory([ |
||
98 | 'name' => '2_a_user_notify', |
||
99 | 'text' => elgg_echo('notifications:subscriptions:changesettings'), |
||
100 | 'href' => "notifications/personal/{$user->username}", |
||
101 | 'section' => 'notifications', |
||
102 | ]); |
||
103 | |||
104 | if (elgg_is_active_plugin('groups')) { |
||
105 | $return[] = \ElggMenuItem::factory([ |
||
106 | 'name' => '2_group_notify', |
||
107 | 'text' => elgg_echo('notifications:subscriptions:changesettings:groups'), |
||
108 | 'href' => "notifications/group/{$user->username}", |
||
109 | 'section' => 'notifications', |
||
110 | ]); |
||
111 | } |
||
112 | |||
113 | return $return; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Register menu items for the page menu on group profiles |
||
118 | * |
||
119 | * @param \Elgg\Hook $hook hook |
||
120 | * |
||
121 | * @return array |
||
122 | * |
||
123 | * @access private |
||
124 | * |
||
125 | * @since 3.0 |
||
126 | */ |
||
127 | function _notifications_groups_subscription_page_menu(\Elgg\Hook $hook) { |
||
128 | |||
129 | if (!elgg_is_active_plugin('groups')) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | if (!elgg_in_context('group_profile') || !elgg_get_logged_in_user_guid()) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $user = elgg_get_logged_in_user_entity(); |
||
138 | $group = elgg_get_page_owner_entity(); |
||
139 | if (!($group instanceof \ElggGroup)) { |
||
140 | return; |
||
141 | } |
||
142 | |||
143 | $subscribed = false; |
||
144 | $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal(); |
||
145 | foreach ($NOTIFICATION_HANDLERS as $method => $foo) { |
||
146 | $relationship = check_entity_relationship($user->guid, 'notify' . $method, $group->guid); |
||
147 | |||
148 | if ($relationship) { |
||
149 | $subscribed = true; |
||
150 | break; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | $return = $hook->getValue(); |
||
155 | |||
156 | View Code Duplication | if ($subscribed) { |
|
1 ignored issue
–
show
|
|||
157 | $return[] = \ElggMenuItem::factory([ |
||
158 | 'name' => 'subscription_status', |
||
159 | 'text' => elgg_echo('notifications:groups:subscribed'), |
||
160 | 'href' => "notifications/group/$user->username", |
||
161 | ]); |
||
162 | } else { |
||
163 | $return[] = \ElggMenuItem::factory([ |
||
164 | 'name' => 'subscription_status', |
||
165 | 'text' => elgg_echo('notifications:groups:unsubscribed'), |
||
166 | 'href' => "notifications/group/$user->username" |
||
167 | ]); |
||
168 | } |
||
169 | |||
170 | return $return; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Update notifications when a relationship is deleted |
||
175 | * |
||
176 | * @param string $event "delete" |
||
177 | * @param string $object_type "relationship" |
||
178 | * @param \ElggRelationship $relationship Relationship obj |
||
179 | * @return void |
||
180 | */ |
||
181 | function notifications_relationship_remove($event, $object_type, $relationship) { |
||
182 | |||
183 | if (!in_array($relationship->relationship, ['member', 'friend'])) { |
||
184 | return; |
||
185 | } |
||
186 | |||
187 | $methods = array_keys(_elgg_services()->notifications->getMethodsAsDeprecatedGlobal()); |
||
188 | foreach ($methods as $method) { |
||
189 | elgg_remove_subscription($relationship->guid_one, $method, $relationship->guid_two); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Turn on notifications for new friends if all friend notifications is on |
||
195 | * |
||
196 | * @param string $event |
||
197 | * @param string $object_type |
||
198 | * @param object $relationship |
||
199 | */ |
||
200 | function notifications_update_friend_notify($event, $object_type, $relationship) { |
||
201 | // The handler gets triggered regardless of which relationship was |
||
202 | // created, so proceed only if dealing with a 'friend' relationship. |
||
203 | if ($relationship->relationship != 'friend') { |
||
204 | return true; |
||
205 | } |
||
206 | |||
207 | $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal(); |
||
208 | |||
209 | $user_guid = $relationship->guid_one; |
||
210 | $friend_guid = $relationship->guid_two; |
||
211 | |||
212 | $user = get_entity($user_guid); |
||
213 | |||
214 | // loop through all notification types |
||
215 | foreach ($NOTIFICATION_HANDLERS as $method => $foo) { |
||
216 | $metaname = 'collections_notifications_preferences_' . $method; |
||
217 | $collections_preferences = $user->$metaname; |
||
218 | if ($collections_preferences) { |
||
219 | if (!empty($collections_preferences) && !is_array($collections_preferences)) { |
||
220 | $collections_preferences = [$collections_preferences]; |
||
221 | } |
||
222 | if (is_array($collections_preferences)) { |
||
223 | // -1 means all friends is on - should be a define |
||
224 | if (in_array(-1, $collections_preferences)) { |
||
225 | add_entity_relationship($user_guid, 'notify' . $method, $friend_guid); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Update notifications for changes in access collection membership. |
||
234 | * |
||
235 | * This function assumes that only friends can belong to access collections. |
||
236 | * |
||
237 | * @param string $event |
||
238 | * @param string $object_type |
||
239 | * @param bool $returnvalue |
||
240 | * @param array $params |
||
241 | */ |
||
242 | function notifications_update_collection_notify($event, $object_type, $returnvalue, $params) { |
||
243 | $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal(); |
||
244 | |||
245 | // only update notifications for user owned collections |
||
246 | $collection_id = $params['collection_id']; |
||
247 | $collection = get_access_collection($collection_id); |
||
248 | $user = get_entity($collection->owner_guid); |
||
249 | if (!($user instanceof ElggUser)) { |
||
250 | return $returnvalue; |
||
251 | } |
||
252 | |||
253 | $member_guid = $params['user_guid']; |
||
254 | |||
255 | // loop through all notification types |
||
256 | foreach ($NOTIFICATION_HANDLERS as $method => $foo) { |
||
257 | $metaname = 'collections_notifications_preferences_' . $method; |
||
258 | $collections_preferences = $user->$metaname; |
||
259 | if (!$collections_preferences) { |
||
260 | continue; |
||
261 | } |
||
262 | if (!is_array($collections_preferences)) { |
||
263 | $collections_preferences = [$collections_preferences]; |
||
264 | } |
||
265 | if (in_array(-1, $collections_preferences)) { |
||
266 | // if "all friends" notify is on, we don't change any notifications |
||
267 | // since must be a friend to be in an access collection |
||
268 | continue; |
||
269 | } |
||
270 | if (in_array($collection_id, $collections_preferences)) { |
||
271 | // notifications are on for this collection so we add/remove |
||
272 | if ($event == 'access:collections:add_user') { |
||
273 | add_entity_relationship($user->guid, "notify$method", $member_guid); |
||
274 | } elseif ($event == 'access:collections:remove_user') { |
||
275 | // removing someone from an access collection is not a guarantee |
||
276 | // that they should be removed from notifications |
||
277 | //remove_entity_relationship($user->guid, "notify$method", $member_guid); |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Register unit tests |
||
285 | * |
||
286 | * @param string $hook "unit_test" |
||
287 | * @param string $type "system" |
||
288 | * @param string[] $tests Tests |
||
289 | * @return string[] |
||
290 | */ |
||
291 | function notifications_register_tests($hook, $type, $tests) { |
||
292 | $tests[] = __DIR__ . '/tests/ElggNotificationsPluginUnitTest.php'; |
||
293 | return $tests; |
||
294 | } |
||
295 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.