-
Notifications
You must be signed in to change notification settings - Fork 669
/
start.php
627 lines (535 loc) · 17.5 KB
/
start.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* Discussion plugin
*/
elgg_register_event_handler('init', 'system', 'discussion_init');
/**
* Initialize the discussion component
*/
function discussion_init() {
elgg_register_library('elgg:discussion', __DIR__ . '/lib/discussion.php');
elgg_register_page_handler('discussion', 'discussion_page_handler');
elgg_register_plugin_hook_handler('entity:url', 'object', 'discussion_set_topic_url');
// commenting not allowed on discussion topics (use a different annotation)
elgg_register_plugin_hook_handler('permissions_check:comment', 'object', 'discussion_comment_override');
elgg_register_plugin_hook_handler('permissions_check', 'object', 'discussion_can_edit_reply');
// discussion reply menu
elgg_register_plugin_hook_handler('register', 'menu:entity', 'discussion_reply_menu_setup');
// allow non-owners to add replies to discussion
elgg_register_plugin_hook_handler('container_permissions_check', 'object', 'discussion_reply_container_permissions_override');
elgg_register_event_handler('update:after', 'object', 'discussion_update_reply_access_ids');
$action_base = __DIR__ . '/actions/discussion';
elgg_register_action('discussion/save', "$action_base/save.php");
elgg_register_action('discussion/delete', "$action_base/delete.php");
elgg_register_action('discussion/reply/save', "$action_base/reply/save.php");
elgg_register_action('discussion/reply/delete', "$action_base/reply/delete.php");
// add link to owner block
elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'discussion_owner_block_menu');
// Register for search.
elgg_register_entity_type('object', 'discussion');
elgg_register_plugin_hook_handler('search', 'object:discussion', 'discussion_search_discussion');
// because replies are not comments, need of our menu item
elgg_register_plugin_hook_handler('register', 'menu:river', 'discussion_add_to_river_menu');
// add the forum tool option
add_group_tool_option('forum', elgg_echo('groups:enableforum'), true);
elgg_extend_view('groups/tool_latest', 'discussion/group_module');
elgg_register_js('elgg.discussion', elgg_get_simplecache_url('discussion/discussion.js'));
elgg_register_ajax_view('ajax/discussion/reply/edit');
// notifications
elgg_register_plugin_hook_handler('get', 'subscriptions', 'discussion_get_subscriptions');
elgg_register_notification_event('object', 'discussion');
elgg_register_plugin_hook_handler('prepare', 'notification:create:object:discussion', 'discussion_prepare_notification');
elgg_register_notification_event('object', 'discussion_reply');
elgg_register_plugin_hook_handler('prepare', 'notification:create:object:discussion_reply', 'discussion_prepare_reply_notification');
// allow ecml in discussion
elgg_register_plugin_hook_handler('get_views', 'ecml', 'discussion_ecml_views_hook');
// allow to be liked
elgg_register_plugin_hook_handler('likes:is_likable', 'object:discussion', 'Elgg\Values::getTrue');
elgg_register_plugin_hook_handler('likes:is_likable', 'object:discussion_reply', 'Elgg\Values::getTrue');
}
/**
* Discussion page handler
*
* URLs take the form of
* All topics in site: discussion/all
* List topics in forum: discussion/owner/<guid>
* View discussion topic: discussion/view/<guid>
* Add discussion topic: discussion/add/<guid>
* Edit discussion topic: discussion/edit/<guid>
*
* @param array $page Array of url segments for routing
* @return bool
*/
function discussion_page_handler($page) {
if (!isset($page[0])) {
$page[0] = 'all';
}
elgg_push_breadcrumb(elgg_echo('discussion'), 'discussion/all');
switch ($page[0]) {
case 'all':
echo elgg_view_resource('discussion/all');
break;
case 'owner':
echo elgg_view_resource('discussion/owner', [
'guid' => elgg_extract(1, $page),
]);
break;
case 'group':
echo elgg_view_resource('discussion/group', [
'guid' => elgg_extract(1, $page),
]);
break;
case 'add':
echo elgg_view_resource('discussion/add', [
'guid' => elgg_extract(1, $page),
]);
break;
case 'reply':
switch (elgg_extract(1, $page)) {
case 'edit':
echo elgg_view_resource('discussion/reply/edit', [
'guid' => elgg_extract(2, $page),
]);
break;
case 'view':
discussion_redirect_to_reply(elgg_extract(2, $page), elgg_extract(3, $page));
break;
default:
return false;
}
break;
case 'edit':
echo elgg_view_resource('discussion/edit', [
'guid' => elgg_extract(1, $page),
]);
break;
case 'view':
echo elgg_view_resource('discussion/view', [
'guid' => elgg_extract(1, $page),
]);
break;
default:
return false;
}
return true;
}
/**
* Redirect to the reply in context of the containing topic
*
* @param int $reply_guid GUID of the reply
* @param int $fallback_guid GUID of the topic
*
* @return void
* @access private
*/
function discussion_redirect_to_reply($reply_guid, $fallback_guid) {
$fail = function () {
register_error(elgg_echo('discussion:reply:error:notfound'));
forward(REFERER);
};
$reply = get_entity($reply_guid);
if (!$reply) {
// try fallback
$fallback = get_entity($fallback_guid);
if (!elgg_instanceof($fallback, 'object', 'discussion')) {
$fail();
}
register_error(elgg_echo('discussion:reply:error:notfound_fallback'));
forward($fallback->getURL());
}
if (!elgg_instanceof($reply, 'object', 'discussion_reply')) {
$fail();
}
// start with topic URL
$topic = $reply->getContainerEntity();
// this won't work with threaded comments, but core doesn't support that yet
$count = elgg_get_entities([
'type' => 'object',
'subtype' => $reply->getSubtype(),
'container_guid' => $topic->guid,
'count' => true,
'wheres' => ["e.guid < " . (int)$reply->guid],
]);
$limit = (int)get_input('limit', 0);
if (!$limit) {
$limit = _elgg_services()->config->get('default_limit');
}
$offset = floor($count / $limit) * $limit;
if (!$offset) {
$offset = null;
}
$url = elgg_http_add_url_query_elements($topic->getURL(), [
'offset' => $offset,
]) . "#elgg-object-{$reply->guid}";
forward($url);
}
/**
* Override the url for discussion topics and replies
*
* Discussion replies do not have their own page so their url is
* the same as the topic url.
*
* @param string $hook
* @param string $type
* @param string $url
* @param array $params
* @return string
*/
function discussion_set_topic_url($hook, $type, $url, $params) {
$entity = $params['entity'];
if (!$entity instanceof ElggObject) {
return;
}
if ($entity->getSubtype() === 'discussion') {
$title = elgg_get_friendly_title($entity->title);
return "discussion/view/{$entity->guid}/{$title}";
}
if ($entity->getSubtype() === 'discussion_reply') {
$topic = $entity->getContainerEntity();
return "discussion/reply/view/{$entity->guid}/{$topic->guid}";
}
}
/**
* We don't want people commenting on topics in the river
*
* @param string $hook
* @param string $type
* @param string $return
* @param array $params
* @return bool
*/
function discussion_comment_override($hook, $type, $return, $params) {
if (elgg_instanceof($params['entity'], 'object', 'discussion')) {
return false;
}
}
/**
* Add owner block link for groups
*
* @param string $hook 'register'
* @param string $type 'menu:owner_block'
* @param ElggMenuItem[] $return
* @param array $params
* @return ElggMenuItem[] $return
*/
function discussion_owner_block_menu($hook, $type, $return, $params) {
if (elgg_instanceof($params['entity'], 'group')) {
if ($params['entity']->forum_enable != "no") {
$url = "discussion/group/{$params['entity']->guid}";
$item = new ElggMenuItem('discussion', elgg_echo('discussion:group'), $url);
$return[] = $item;
}
}
return $return;
}
/**
* Set up menu items for river items
*
* Add reply button for discussion topic. Remove the possibility
* to comment on a discussion reply.
*
* @param string $hook 'register'
* @param string $type 'menu:river'
* @param ElggMenuItem[] $return
* @param array $params
* @return ElggMenuItem[] $return
*/
function discussion_add_to_river_menu($hook, $type, $return, $params) {
if (!elgg_is_logged_in() || elgg_in_context('widgets')) {
return $return;
}
$item = $params['item'];
$object = $item->getObjectEntity();
if (elgg_instanceof($object, 'object', 'discussion')) {
$container = $object->getContainerEntity();
if ($container && ($container->canWriteToContainer() || elgg_is_admin_logged_in())) {
$options = array(
'name' => 'reply',
'href' => "#discussion-reply-{$object->guid}",
'text' => elgg_view_icon('speech-bubble'),
'title' => elgg_echo('reply:this'),
'rel' => 'toggle',
'priority' => 50,
);
$return[] = ElggMenuItem::factory($options);
}
} else {
if (elgg_instanceof($object, 'object', 'discussion_reply')) {
// Discussion replies cannot be commented
foreach ($return as $key => $item) {
if ($item->getName() === 'comment') {
unset($return[$key]);
}
}
}
}
return $return;
}
/**
* Prepare a notification message about a new discussion topic
*
* @param string $hook Hook name
* @param string $type Hook type
* @param Elgg\Notifications\Notification $notification The notification to prepare
* @param array $params Hook parameters
* @return Elgg\Notifications\Notification
*/
function discussion_prepare_notification($hook, $type, $notification, $params) {
$entity = $params['event']->getObject();
$owner = $params['event']->getActor();
$language = $params['language'];
$descr = $entity->description;
$title = $entity->title;
$notification->subject = elgg_echo('discussion:topic:notify:subject', array($title), $language);
$notification->body = elgg_echo('discussion:topic:notify:body', array(
$owner->name,
$title,
$descr,
$entity->getURL()
), $language);
$notification->summary = elgg_echo('discussion:topic:notify:summary', array($entity->title), $language);
return $notification;
}
/**
* Prepare a notification message about a new discussion reply
*
* @param string $hook Hook name
* @param string $type Hook type
* @param Elgg\Notifications\Notification $notification The notification to prepare
* @param array $params Hook parameters
* @return Elgg\Notifications\Notification
*/
function discussion_prepare_reply_notification($hook, $type, $notification, $params) {
$reply = $params['event']->getObject();
$topic = $reply->getContainerEntity();
$poster = $reply->getOwnerEntity();
$language = elgg_extract('language', $params);
$notification->subject = elgg_echo('discussion:reply:notify:subject', array($topic->title), $language);
$notification->body = elgg_echo('discussion:reply:notify:body', array(
$poster->name,
$topic->title,
$reply->description,
$reply->getURL(),
), $language);
$notification->summary = elgg_echo('discussion:reply:notify:summary', array($topic->title), $language);
return $notification;
}
/**
* Get subscriptions for notifications
*
* @param string $hook 'get'
* @param string $type 'subscriptions'
* @param array $subscriptions Array containing subscriptions in the form
* <user guid> => array('email', 'site', etc.)
* @param array $params Hook parameters
* @return array
*/
function discussion_get_subscriptions($hook, $type, $subscriptions, $params) {
$reply = $params['event']->getObject();
if (!elgg_instanceof($reply, 'object', 'discussion_reply')) {
return $subscriptions;
}
$container_guid = $reply->getContainerEntity()->container_guid;
$container_subscriptions = elgg_get_subscriptions_for_container($container_guid);
return ($subscriptions + $container_subscriptions);
}
/**
* Parse ECML on discussion views
*/
function discussion_ecml_views_hook($hook, $type, $return_value, $params) {
$return_value['forum/viewposts'] = elgg_echo('discussion:ecml:discussion');
return $return_value;
}
/**
* Allow group owner and discussion owner to edit discussion replies.
*
* @param string $hook 'permissions_check'
* @param string $type 'object'
* @param boolean $return
* @param array $params Array('entity' => ElggEntity, 'user' => ElggUser)
* @return boolean True if user is discussion or group owner
*/
function discussion_can_edit_reply($hook, $type, $return, $params) {
/** @var $reply ElggEntity */
$reply = $params['entity'];
$user = $params['user'];
if (!elgg_instanceof($reply, 'object', 'discussion_reply')) {
return $return;
}
if ($reply->owner_guid == $user->guid) {
return true;
}
$discussion = $reply->getContainerEntity();
if ($discussion->owner_guid == $user->guid) {
return true;
}
$container = $discussion->getContainerEntity();
if (elgg_instanceof($container, 'group') && $container->owner_guid == $user->guid) {
return true;
}
return false;
}
/**
* Make sure that only group members can post to a group discussion
*
* @param string $hook 'container_permissions_check'
* @param string $type 'object'
* @param array $return
* @param array $params Array with container, user and subtype
* @return boolean $return
*/
function discussion_reply_container_permissions_override($hook, $type, $return, $params) {
if ($params['subtype'] !== 'discussion_reply') {
return $return;
}
/** @var $discussion ElggEntity */
$discussion = $params['container'];
$user = $params['user'];
$container = $discussion->getContainerEntity();
if (elgg_instanceof($container, 'group')) {
// Only group members are allowed to reply
// to a discussion within a group
if (!$container->canWriteToContainer($user->guid)) {
return false;
}
}
return true;
}
/**
* Update access_id of discussion replies when topic access_id is updated.
*
* @param string $event 'update'
* @param string $type 'object'
* @param ElggObject $object ElggObject
*/
function discussion_update_reply_access_ids($event, $type, $object) {
if (!elgg_instanceof($object, 'object', 'discussion')) {
return;
}
$ia = elgg_set_ignore_access(true);
$options = array(
'type' => 'object',
'subtype' => 'discussion_reply',
'container_guid' => $object->getGUID(),
'limit' => 0,
);
$batch = new ElggBatch('elgg_get_entities', $options);
foreach ($batch as $reply) {
if ($reply->access_id == $object->access_id) {
// Assume access_id of the replies is up-to-date
break;
}
// Update reply access_id
$reply->access_id = $object->access_id;
$reply->save();
}
elgg_set_ignore_access($ia);
}
/**
* Set up discussion reply entity menu
*
* @param string $hook 'register'
* @param string $type 'menu:entity'
* @param ElggMenuItem[] $return
* @param array $params
* @return ElggMenuItem[] $return
*/
function discussion_reply_menu_setup($hook, $type, $return, $params) {
/** @var $reply ElggEntity */
$reply = elgg_extract('entity', $params);
if (empty($reply) || !elgg_instanceof($reply, 'object', 'discussion_reply')) {
return $return;
}
if (!elgg_is_logged_in()) {
return $return;
}
if (elgg_in_context('widgets')) {
return $return;
}
// Reply has the same access as the topic so no need to view it
$remove = array('access');
$user = elgg_get_logged_in_user_entity();
// Allow discussion topic owner, group owner and admins to edit and delete
if ($reply->canEdit() && !elgg_in_context('activity')) {
$return[] = ElggMenuItem::factory(array(
'name' => 'edit',
'text' => elgg_echo('edit'),
'href' => "discussion/reply/edit/{$reply->guid}",
'priority' => 150,
));
$return[] = ElggMenuItem::factory(array(
'name' => 'delete',
'text' => elgg_view_icon('delete'),
'href' => "action/discussion/reply/delete?guid={$reply->guid}",
'priority' => 150,
'is_action' => true,
'confirm' => elgg_echo('deleteconfirm'),
));
} else {
// Edit and delete links can be removed from all other users
$remove[] = 'edit';
$remove[] = 'delete';
}
// Remove unneeded menu items
foreach ($return as $key => $item) {
if (in_array($item->getName(), $remove)) {
unset($return[$key]);
}
}
return $return;
}
/**
* Search in both discussion topics and replies
*
* @param string $hook the name of the hook
* @param string $type the type of the hook
* @param mixed $value the current return value
* @param array $params supplied params
*/
function discussion_search_discussion($hook, $type, $value, $params) {
if (empty($params) || !is_array($params)) {
return $value;
}
$subtype = elgg_extract("subtype", $params);
if (empty($subtype) || ($subtype !== "discussion")) {
return $value;
}
unset($params["subtype"]);
$params["subtypes"] = array("discussion", "discussion_reply");
// trigger the 'normal' object search as it can handle the added options
return elgg_trigger_plugin_hook('search', 'object', $params, array());
}
/**
* Prepare discussion topic form variables
*
* @param ElggObject $topic Topic object if editing
* @return array
*/
function discussion_prepare_form_vars($topic = NULL) {
// input names => defaults
$values = array(
'title' => '',
'description' => '',
'status' => '',
'access_id' => ACCESS_DEFAULT,
'tags' => '',
'container_guid' => elgg_get_page_owner_guid(),
'guid' => null,
'topic' => $topic,
);
if ($topic) {
foreach (array_keys($values) as $field) {
if (isset($topic->$field)) {
$values[$field] = $topic->$field;
}
}
}
if (elgg_is_sticky_form('topic')) {
$sticky_values = elgg_get_sticky_values('topic');
foreach ($sticky_values as $key => $value) {
$values[$key] = $value;
}
}
elgg_clear_sticky_form('topic');
return $values;
}