Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/discussions/start.php (14 issues)

1
<?php
2
/**
3
 * Discussion plugin
4
 */
5
6
/**
7
 * Initialize the discussion component
8
 *
9
 * @return void
10
 */
11
function discussion_init() {
12
13 31
	elgg_register_page_handler('discussion', 'discussion_page_handler');
14
15 31
	elgg_register_plugin_hook_handler('entity:url', 'object', 'discussion_set_topic_url');
16
17
	// prevent comments on closed discussions
18 31
	elgg_register_plugin_hook_handler('permissions_check:comment', 'object', 'discussion_comment_permissions');
19
20
	// add link to owner block
21 31
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'discussion_owner_block_menu');
22
23
	// add the forum tool option
24 31
	add_group_tool_option('forum', elgg_echo('groups:enableforum'), true);
25 31
	elgg_extend_view('groups/tool_latest', 'discussion/group_module');
26
27
	// notifications
28 31
	elgg_register_plugin_hook_handler('get', 'subscriptions', 'discussion_get_subscriptions');
29 31
	elgg_register_notification_event('object', 'discussion');
30 31
	elgg_register_plugin_hook_handler('prepare', 'notification:create:object:discussion', 'discussion_prepare_notification');
31 31
	elgg_register_plugin_hook_handler('prepare', 'notification:create:object:comment', 'discussion_prepare_comment_notification');
32
33
	// allow to be liked
34 31
	elgg_register_plugin_hook_handler('likes:is_likable', 'object:discussion', 'Elgg\Values::getTrue');
35
36
	// Add latest discussions tab to /groups/all page
37 31
	elgg_register_plugin_hook_handler('register', 'menu:filter:groups/all', 'discussion_setup_groups_filter_tabs');
38 31
}
39
40
/**
41
 * Discussion page handler
42
 *
43
 * URLs take the form of
44
 *  All topics in site:    discussion/all
45
 *  List topics in forum:  discussion/owner/<guid>
46
 *  View discussion topic: discussion/view/<guid>
47
 *  Add discussion topic:  discussion/add/<guid>
48
 *  Edit discussion topic: discussion/edit/<guid>
49
 *
50
 * @param array $page Array of url segments for routing
51
 * @return bool
52
 */
53
function discussion_page_handler($page) {
54
55
	if (!isset($page[0])) {
56
		$page[0] = 'all';
57
	}
58
59
	elgg_push_breadcrumb(elgg_echo('discussion'), 'discussion/all');
60
61
	switch ($page[0]) {
62
		case 'all':
63
			echo elgg_view_resource('discussion/all');
64
			break;
65
		case 'owner':
66
			echo elgg_view_resource('discussion/owner', [
67
				'guid' => elgg_extract(1, $page),
68
			]);
69
			break;
70
		case 'group':
71
			echo elgg_view_resource('discussion/group', [
72
				'guid' => elgg_extract(1, $page),
73
			]);
74
			break;
75
		case 'add':
76
			echo elgg_view_resource('discussion/add', [
77
				'guid' => elgg_extract(1, $page),
78
			]);
79
			break;
80
		case 'edit':
81
			echo elgg_view_resource('discussion/edit', [
82
				'guid' => elgg_extract(1, $page),
83
			]);
84
			break;
85
		case 'view':
86
			echo elgg_view_resource('discussion/view', [
87
				'guid' => elgg_extract(1, $page),
88
			]);
89
			break;
90
		default:
91
			return false;
92
	}
93
	return true;
94
}
95
96
/**
97
 * Override the url for discussion topics
98
 *
99
 * @param string $hook   'entity:url'
100
 * @param string $type   'object'
101
 * @param string $url    current value
102
 * @param array  $params supplied params
103
 *
104
 * @return void|string
105
 */
106
function discussion_set_topic_url($hook, $type, $url, $params) {
2 ignored issues
show
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
function discussion_set_topic_url(/** @scrutinizer ignore-unused */ $hook, $type, $url, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
function discussion_set_topic_url($hook, /** @scrutinizer ignore-unused */ $type, $url, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $url is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

106
function discussion_set_topic_url($hook, $type, /** @scrutinizer ignore-unused */ $url, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
	
108 3
	$entity = elgg_extract('entity', $params);
109 3
	if (!$entity instanceof ElggDiscussion) {
110 3
		return;
111
	}
112
113
	
114
	$title = elgg_get_friendly_title($entity->getDisplayName());
115
	return "discussion/view/{$entity->guid}/{$title}";
116
}
117
118
/**
119
 * Add owner block link for groups
120
 *
121
 * @param string         $hook   'register'
122
 * @param string         $type   'menu:owner_block'
123
 * @param ElggMenuItem[] $return current return value
124
 * @param array          $params supplied params
125
 *
126
 * @return void|ElggMenuItem[]
127
 */
128
function discussion_owner_block_menu($hook, $type, $return, $params) {
2 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
function discussion_owner_block_menu($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

128
function discussion_owner_block_menu(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
	
130
	$entity = elgg_extract('entity', $params);
131
	if (!$entity instanceof ElggGroup) {
132
		return;
133
	}
134
	
135
	if (!$entity->isToolEnabled('forum')) {
136
		return;
137
	}
138
	
139
	$url = "discussion/group/{$entity->guid}";
140
	$item = new ElggMenuItem('discussion', elgg_echo('discussion:group'), $url);
141
	$return[] = $item;
142
	
143
	return $return;
144
}
145
146
/**
147
 * Prepare a notification message about a new discussion topic
148
 *
149
 * @param string                          $hook         Hook name
150
 * @param string                          $type         Hook type
151
 * @param Elgg\Notifications\Notification $notification The notification to prepare
152
 * @param array                           $params       Hook parameters
153
 *
154
 * @return Elgg\Notifications\Notification
155
 */
156
function discussion_prepare_notification($hook, $type, $notification, $params) {
2 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
function discussion_prepare_notification($hook, /** @scrutinizer ignore-unused */ $type, $notification, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

156
function discussion_prepare_notification(/** @scrutinizer ignore-unused */ $hook, $type, $notification, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
	$entity = $params['event']->getObject();
158
	$owner = $params['event']->getActor();
159
	$language = $params['language'];
160
161
	$descr = $entity->description;
162
	$title = $entity->title;
163
164
	$notification->subject = elgg_echo('discussion:topic:notify:subject', [$title], $language);
165
	$notification->body = elgg_echo('discussion:topic:notify:body', [
166
		$owner->name,
167
		$title,
168
		$descr,
169
		$entity->getURL()
170
	], $language);
171
	$notification->summary = elgg_echo('discussion:topic:notify:summary', [$entity->title], $language);
172
	$notification->url = $entity->getURL();
173
	
174
	return $notification;
175
}
176
177
/**
178
 * Prepare a notification message about a new comment on a discussion
179
 *
180
 * @param string                          $hook         Hook name
181
 * @param string                          $type         Hook type
182
 * @param Elgg\Notifications\Notification $notification The notification to prepare
183
 * @param array                           $params       Hook parameters
184
 *
185
 * @return void|Elgg\Notifications\Notification
186
 */
187
function discussion_prepare_comment_notification($hook, $type, $notification, $params) {
2 ignored issues
show
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

187
function discussion_prepare_comment_notification(/** @scrutinizer ignore-unused */ $hook, $type, $notification, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

187
function discussion_prepare_comment_notification($hook, /** @scrutinizer ignore-unused */ $type, $notification, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
	
189
	$event = elgg_extract('event', $params);
190
	if (!$event instanceof Elgg\Notifications\NotificationEvent) {
191
		return;
192
	}
193
	
194
	$comment = $event->getObject();
195
	if (!$comment instanceof ElggComment) {
196
		return;
197
	}
198
	
199
	$discussion = $comment->getContainerEntity();
200
	if (!$discussion instanceof ElggDiscussion) {
201
		return;
202
	}
203
	
204
	$language = elgg_extract('language', $params);
205
	
206
	$poster = $comment->getOwnerEntity();
207
	
208
	$notification->subject = elgg_echo('discussion:comment:notify:subject', [$discussion->getDisplayName()], $language);
209
	$notification->summary = elgg_echo('discussion:comment:notify:summary', [$discussion->getDisplayName()], $language);
210
	$notification->body = elgg_echo('discussion:comment:notify:body', [
211
		$poster->getDisplayName(),
212
		$discussion->getDisplayName(),
213
		$comment->description,
214
		$comment->getURL(),
215
	], $language);
216
	$notification->url = $comment->getURL();
217
	
218
	return $notification;
219
}
220
221
/**
222
 * Add group members to the comment subscriber on a discussion
223
 *
224
 * @param string $hook          'get'
225
 * @param string $type          'subscriptions'
226
 * @param array  $subscriptions Array containing subscriptions in the form
227
 *                              <user guid> => array('email', 'site', etc.)
228
 * @param array  $params        Hook parameters
229
 *
230
 * @return void|array
231
 */
232
function discussion_get_subscriptions($hook, $type, $subscriptions, $params) {
2 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

232
function discussion_get_subscriptions($hook, /** @scrutinizer ignore-unused */ $type, $subscriptions, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

232
function discussion_get_subscriptions(/** @scrutinizer ignore-unused */ $hook, $type, $subscriptions, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
233
	
234 2
	$event = elgg_extract('event', $params);
235 2
	if (!$event instanceof Elgg\Notifications\NotificationEvent) {
236
		return;
237
	}
238
	
239 2
	$comment = $event->getObject();
240 2
	if (!$comment instanceof ElggComment) {
241 2
		return;
242
	}
243
	
244
	$discussion = $comment->getContainerEntity();
245
	if (!$discussion instanceof ElggDiscussion) {
246
		return;
247
	}
248
	
249
	$container = $discussion->getContainerEntity();
250
	if (!$container instanceof ElggGroup) {
251
		return;
252
	}
253
	
254
	$group_subscriptions = elgg_get_subscriptions_for_container($container->guid);
255
	
256
	return ($subscriptions + $group_subscriptions);
257
}
258
259
/**
260
 * Make sure that discussion comments can not be written to a discussion after it has been closed
261
 *
262
 * @param string $hook   'container_logic_check'
263
 * @param string $type   'object'
264
 * @param array  $return Allowed or not
265
 * @param array  $params Hook params
266
 *
267
 * @return void|false
268
 */
269
function discussion_comment_permissions($hook, $type, $return, $params) {
3 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

269
function discussion_comment_permissions($hook, /** @scrutinizer ignore-unused */ $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

269
function discussion_comment_permissions(/** @scrutinizer ignore-unused */ $hook, $type, $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $return is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

269
function discussion_comment_permissions($hook, $type, /** @scrutinizer ignore-unused */ $return, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
270
	
271 1
	$discussion = elgg_extract('entity', $params);
272 1
	if (!$discussion instanceof ElggDiscussion) {
273 1
		return;
274
	}
275
276
	if ($discussion->status == 'closed') {
277
		// do not allow new comments in closed discussions
278
		return false;
279
	}
280
}
281
282
/**
283
 * Prepare discussion topic form variables
284
 *
285
 * @param ElggObject $topic Topic object if editing
286
 * @return array
287
 */
288
function discussion_prepare_form_vars($topic = null) {
289
	// input names => defaults
290
	$values = [
291
		'title' => '',
292
		'description' => '',
293
		'status' => '',
294
		'access_id' => ACCESS_DEFAULT,
295
		'tags' => '',
296
		'container_guid' => elgg_get_page_owner_guid(),
297
		'guid' => null,
298
		'topic' => $topic,
299
		'entity' => $topic,
300
	];
301
302
	if ($topic) {
303
		foreach (array_keys($values) as $field) {
304
			if (isset($topic->$field)) {
305
				$values[$field] = $topic->$field;
306
			}
307
		}
308
	}
309
310
	if (elgg_is_sticky_form('topic')) {
311
		$sticky_values = elgg_get_sticky_values('topic');
312
		foreach ($sticky_values as $key => $value) {
313
			$values[$key] = $value;
314
		}
315
	}
316
317
	elgg_clear_sticky_form('topic');
318
319
	return $values;
320
}
321
322
/**
323
 * Add latest discussions tab to /groups/all page
324
 *
325
 * @param string         $hook   "register"
326
 * @param string         $type   "menu:filter:groups/all"
327
 * @param ElggMenuItem[] $return Menu
328
 * @param array          $params Hook params
329
 * @return ElggMenuItem[]
330
 */
331
function discussion_setup_groups_filter_tabs($hook, $type, $return, $params) {
332
333
	$filter_value = elgg_extract('filter_value', $params);
334
335
	$return[] = ElggMenuItem::factory([
336
		'name' => 'discussion',
337
		'text' => elgg_echo('discussion:latest'),
338
		'href' => 'groups/all?filter=discussion',
339
		'priority' => 500,
340
		'selected' => $filter_value == 'discussion',
341
	]);
342
343
	return $return;
344
}
345
346
return function() {
347 18
	elgg_register_event_handler('init', 'system', 'discussion_init');
348
};
349