1 | <?php |
||
2 | /** |
||
3 | * Discussion plugin |
||
4 | */ |
||
5 | |||
6 | /** |
||
7 | * Initialize the discussion component |
||
8 | * |
||
9 | * @return void |
||
10 | */ |
||
11 | function discussion_init() { |
||
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
12 | |||
13 | // prevent comments on closed discussions |
||
14 | 77 | elgg_register_plugin_hook_handler('permissions_check:comment', 'object', 'discussion_comment_permissions'); |
|
15 | |||
16 | // add link to owner block |
||
17 | 77 | elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'discussion_owner_block_menu'); |
|
18 | |||
19 | // add the forum tool option |
||
20 | 77 | elgg()->group_tools->register('forum'); |
|
21 | |||
22 | // notifications |
||
23 | 77 | elgg_register_plugin_hook_handler('get', 'subscriptions', 'discussion_get_subscriptions'); |
|
24 | 77 | elgg_register_notification_event('object', 'discussion'); |
|
25 | 77 | elgg_register_plugin_hook_handler('prepare', 'notification:create:object:discussion', 'discussion_prepare_notification'); |
|
26 | 77 | elgg_register_plugin_hook_handler('prepare', 'notification:create:object:comment', 'discussion_prepare_comment_notification'); |
|
27 | |||
28 | // allow to be liked |
||
29 | 77 | elgg_register_plugin_hook_handler('likes:is_likable', 'object:discussion', 'Elgg\Values::getTrue'); |
|
30 | |||
31 | // Add latest discussions tab to /groups/all page |
||
32 | 77 | elgg_register_plugin_hook_handler('register', 'menu:filter:groups/all', 'discussion_setup_groups_filter_tabs'); |
|
33 | |||
34 | // register database seed |
||
35 | 77 | elgg_register_plugin_hook_handler('seeds', 'database', 'discussion_register_db_seeds'); |
|
36 | 77 | } |
|
37 | |||
38 | /** |
||
39 | * Add owner block link for groups |
||
40 | * |
||
41 | * @param \Elgg\Hook $hook 'register', 'menu:owner_block' |
||
42 | * |
||
43 | * @return void|ElggMenuItem[] |
||
44 | */ |
||
45 | function discussion_owner_block_menu(\Elgg\Hook $hook) { |
||
46 | |||
47 | $entity = $hook->getEntityParam(); |
||
48 | if (!$entity instanceof ElggGroup) { |
||
49 | return; |
||
50 | } |
||
51 | |||
52 | if (!$entity->isToolEnabled('forum')) { |
||
53 | return; |
||
54 | } |
||
55 | |||
56 | $url = elgg_generate_url('collection:object:discussion:group', [ |
||
57 | 'guid' => $entity->guid, |
||
58 | ]); |
||
59 | |||
60 | $return = $hook->getValue(); |
||
61 | $return[] = new ElggMenuItem('discussion', elgg_echo('collection:object:discussion:group'), $url); |
||
62 | |||
63 | return $return; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Prepare a notification message about a new discussion topic |
||
68 | * |
||
69 | * @param \Elgg\Hook $hook 'prepare', 'notification:create:object:discussion' |
||
70 | * |
||
71 | * @return Elgg\Notifications\Notification |
||
72 | */ |
||
73 | function discussion_prepare_notification(\Elgg\Hook $hook) { |
||
74 | $entity = $hook->getParam('event')->getObject(); |
||
75 | $owner = $hook->getParam('event')->getActor(); |
||
76 | $language = $hook->getParam('language'); |
||
77 | |||
78 | $title = $entity->getDisplayName(); |
||
79 | |||
80 | $notification = $hook->getValue(); |
||
81 | $notification->subject = elgg_echo('discussion:topic:notify:subject', [$title], $language); |
||
82 | $notification->body = elgg_echo('discussion:topic:notify:body', [ |
||
83 | $owner->getDisplayName(), |
||
84 | $title, |
||
85 | $entity->description, |
||
86 | $entity->getURL() |
||
87 | ], $language); |
||
88 | $notification->summary = elgg_echo('discussion:topic:notify:summary', [$title], $language); |
||
89 | $notification->url = $entity->getURL(); |
||
90 | |||
91 | return $notification; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Prepare a notification message about a new comment on a discussion |
||
96 | * |
||
97 | * @param \Elgg\Hook $hook 'prepare', 'notification:create:object:comment' |
||
98 | * |
||
99 | * @return void|Elgg\Notifications\Notification |
||
100 | */ |
||
101 | function discussion_prepare_comment_notification(\Elgg\Hook $hook) { |
||
102 | |||
103 | $event = $hook->getParam('event'); |
||
104 | if (!$event instanceof Elgg\Notifications\NotificationEvent) { |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | $comment = $event->getObject(); |
||
109 | if (!$comment instanceof ElggComment) { |
||
110 | return; |
||
111 | } |
||
112 | |||
113 | $discussion = $comment->getContainerEntity(); |
||
114 | if (!$discussion instanceof ElggDiscussion) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | $language = $hook->getParam('language'); |
||
119 | |||
120 | $poster = $comment->getOwnerEntity(); |
||
121 | |||
122 | $notification = $hook->getValue(); |
||
123 | $notification->subject = elgg_echo('discussion:comment:notify:subject', [$discussion->getDisplayName()], $language); |
||
124 | $notification->summary = elgg_echo('discussion:comment:notify:summary', [$discussion->getDisplayName()], $language); |
||
125 | $notification->body = elgg_echo('discussion:comment:notify:body', [ |
||
126 | $poster->getDisplayName(), |
||
127 | $discussion->getDisplayName(), |
||
128 | $comment->description, |
||
129 | $comment->getURL(), |
||
130 | ], $language); |
||
131 | $notification->url = $comment->getURL(); |
||
132 | |||
133 | return $notification; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Add group members to the comment subscriber on a discussion |
||
138 | * |
||
139 | * @param \Elgg\Hook $hook 'get', 'subscriptions' |
||
140 | * |
||
141 | * @return void|array |
||
142 | */ |
||
143 | function discussion_get_subscriptions(\Elgg\Hook $hook) { |
||
144 | |||
145 | 3 | $event = $hook->getParam('event'); |
|
146 | 3 | if (!$event instanceof \Elgg\Notifications\SubscriptionNotificationEvent) { |
|
147 | 3 | return; |
|
148 | } |
||
149 | |||
150 | if ($event->getAction() !== 'create') { |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | $comment = $event->getObject(); |
||
155 | if (!$comment instanceof ElggComment) { |
||
156 | return; |
||
157 | } |
||
158 | |||
159 | $discussion = $comment->getContainerEntity(); |
||
160 | if (!$discussion instanceof ElggDiscussion) { |
||
161 | return; |
||
162 | } |
||
163 | |||
164 | $container = $discussion->getContainerEntity(); |
||
165 | if (!$container instanceof ElggGroup) { |
||
166 | return; |
||
167 | } |
||
168 | |||
169 | $subscriptions = $hook->getValue(); |
||
170 | $group_subscriptions = elgg_get_subscriptions_for_container($container->guid); |
||
171 | |||
172 | return ($subscriptions + $group_subscriptions); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Make sure that discussion comments can not be written to a discussion after it has been closed |
||
177 | * |
||
178 | * @param \Elgg\Hook $hook 'container_logic_check', 'object' |
||
179 | * |
||
180 | * @return void|false |
||
181 | */ |
||
182 | function discussion_comment_permissions(\Elgg\Hook $hook) { |
||
183 | |||
184 | $discussion = $hook->getEntityParam(); |
||
185 | if (!$discussion instanceof ElggDiscussion) { |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | if ($discussion->status == 'closed') { |
||
190 | // do not allow new comments in closed discussions |
||
191 | return false; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Prepare discussion topic form variables |
||
197 | * |
||
198 | * @param ElggObject $topic Topic object if editing |
||
199 | * @return array |
||
200 | */ |
||
201 | function discussion_prepare_form_vars($topic = null) { |
||
202 | // input names => defaults |
||
203 | $values = [ |
||
204 | 'title' => '', |
||
205 | 'description' => '', |
||
206 | 'status' => '', |
||
207 | 'access_id' => ACCESS_DEFAULT, |
||
208 | 'tags' => '', |
||
209 | 'container_guid' => elgg_get_page_owner_guid(), |
||
210 | 'guid' => null, |
||
211 | 'topic' => $topic, |
||
212 | 'entity' => $topic, |
||
213 | ]; |
||
214 | |||
215 | if ($topic) { |
||
216 | foreach (array_keys($values) as $field) { |
||
217 | if (isset($topic->$field)) { |
||
218 | $values[$field] = $topic->$field; |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if (elgg_is_sticky_form('topic')) { |
||
224 | $sticky_values = elgg_get_sticky_values('topic'); |
||
225 | foreach ($sticky_values as $key => $value) { |
||
226 | $values[$key] = $value; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | elgg_clear_sticky_form('topic'); |
||
231 | |||
232 | return $values; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Add latest discussions tab to /groups/all page |
||
237 | * |
||
238 | * @param \Elgg\Hook $hook "register", "menu:filter:groups/all" |
||
239 | * |
||
240 | * @return ElggMenuItem[] |
||
241 | */ |
||
242 | function discussion_setup_groups_filter_tabs(\Elgg\Hook $hook) { |
||
243 | $return = $hook->getValue(); |
||
244 | $return[] = ElggMenuItem::factory([ |
||
245 | 'name' => 'discussion', |
||
246 | 'text' => elgg_echo('discussion:latest'), |
||
247 | 'href' => elgg_generate_url('collection:group:group:all', [ |
||
248 | 'filter' => 'discussion', |
||
249 | ]), |
||
250 | 'priority' => 500, |
||
251 | ]); |
||
252 | |||
253 | return $return; |
||
254 | } |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Register database seed |
||
259 | * |
||
260 | * @elgg_plugin_hook seeds database |
||
261 | * |
||
262 | * @param \Elgg\Hook $hook Hook |
||
263 | * @return array |
||
264 | */ |
||
265 | function discussion_register_db_seeds(\Elgg\Hook $hook) { |
||
266 | |||
267 | $seeds = $hook->getValue(); |
||
268 | |||
269 | $seeds[] = \Elgg\Discussions\Seeder::class; |
||
270 | |||
271 | return $seeds; |
||
272 | } |
||
273 | |||
274 | return function() { |
||
275 | 80 | elgg_register_event_handler('init', 'system', 'discussion_init'); |
|
276 | }; |
||
277 |