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

mod/messageboard/start.php (1 issue)

1
<?php
2
/**
3
 * Elgg Message board
4
 * This plugin allows users and groups to attach a message board to their profile for other users
5
 * to post comments.
6
 *
7
 * @package MessageBoard
8
 */
9
10
/**
11
 * MessageBoard initialisation
12
 *
13
 * @return void
14
 */
15
function messageboard_init() {
16 31
	elgg_register_page_handler('messageboard', 'messageboard_page_handler');
17
18
	// delete annotations for posts
19 31
	elgg_register_plugin_hook_handler('register', 'menu:annotation', 'messageboard_annotation_menu_setup');
20 31
}
21
22
/**
23
 * Messageboard dispatcher for flat message board.
24
 * Profile (and eventually group) widgets handle their own.
25
 *
26
 * URLs take the form of
27
 *  User's messageboard:               messageboard/owner/<username>
28
 *  Y's history of posts on X's board: messageboard/owner/<X>/history/<Y>
29
 *  New post:                          messageboard/add/<guid> (container: user or group)
30
 *  Group messageboard:                messageboard/group/<guid>/all (not implemented)
31
 *
32
 * @param array $page Array of page elements
33
 *
34
 * @return bool
35
 */
36
function messageboard_page_handler($page) {
37
38
	$vars = [];
39
	switch ($page[0]) {
40
		case 'owner':
41
			//@todo if they have the widget disabled, don't allow this.
42
			$owner_name = elgg_extract(1, $page);
43
			$owner = get_user_by_username($owner_name);
44
			$vars['page_owner_guid'] = $owner->guid;
45
			$history = elgg_extract(2, $page);
46
			$username = elgg_extract(3, $page);
47
48
			if ($history && $username) {
49
				$vars['history_username'] = $username;
50
			}
51
52
			echo elgg_view_resource('messageboard/owner', $vars);
53
			break;
54
55
		case 'group':
56
			elgg_group_gatekeeper();
57
			$owner_guid = elgg_extract(1, $page);
58
			$vars['page_owner_guid'] = $owner_guid;
59
			echo elgg_view_resource('messageboard/owner', $vars);
60
			break;
61
62
		default:
63
			return false;
64
	}
65
	return true;
66
}
67
68
/**
69
 * Add messageboard post
70
 *
71
 * @param ElggUser $poster    User posting the message
72
 * @param ElggUser $owner     User who owns the message board
73
 * @param string   $message   The posted message
74
 * @param int      $access_id Access level (see defines in constants.php)
75
 *
76
 * @return false|int
77
 */
78
function messageboard_add($poster, $owner, $message, $access_id = ACCESS_PUBLIC) {
79
	
80
	if (!$poster instanceof ElggUser || !$owner instanceof ElggUser || empty($message)) {
81
		return false;
82
	}
83
	
84
	$access_id = (int) $access_id;
85
	
86
	$result_id = $owner->annotate('messageboard', $message, $access_id, $poster->guid);
87
	if (!$result_id) {
88
		return false;
89
	}
90
91
	elgg_create_river_item([
92
		'view' => 'river/object/messageboard/create',
93
		'action_type' => 'messageboard',
94
		'subject_guid' => $poster->guid,
95
		'object_guid' => $owner->guid,
96
		'access_id' => $access_id,
97
		'annotation_id' => $result_id,
98
	]);
99
100
	// Send notification only if poster isn't the owner
101
	if ($poster->guid != $owner->guid) {
102
		$subject = elgg_echo('messageboard:email:subject', [], $owner->language);
103
		$url = elgg_normalize_url("messageboard/owner/{$owner->username}");
104
105
		$body = elgg_echo('messageboard:email:body', [
106
			$poster->name,
107
			$message,
108
			$url,
109
			$poster->name,
110
			$poster->getURL(),
111
		], $owner->language);
112
113
		$params = [
114
			'action' => 'create',
115
			'object' => elgg_get_annotation_from_id($result_id),
116
			'url' => $url,
117
		];
118
		notify_user($owner->guid, $poster->guid, $subject, $body, $params);
119
	}
120
121
	return $result_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result_id also could return the type true which is incompatible with the documented return type integer|false.
Loading history...
122
}
123
124
/**
125
 * Add edit and delete links for forum replies
126
 *
127
 * @param string         $hook   'register'
128
 * @param string         $type   'menu:annotation'
129
 * @param ElggMenuItem[] $return current return value
130
 * @param array          $params supplied params
131
 *
132
 * @return void|ElggMenuItem[]
133
 */
134
function messageboard_annotation_menu_setup($hook, $type, $return, $params) {
135
	$annotation = elgg_extract('annotation', $params);
136
	if (!$annotation instanceof ElggAnnotation) {
137
		return;
138
	}
139
	
140
	if ($annotation->name !== 'messageboard') {
141
		return;
142
	}
143
144
	if (!$annotation->canEdit()) {
145
		return;
146
	}
147
	
148
	$url = elgg_http_add_url_query_elements('action/messageboard/delete', [
149
		'annotation_id' => $annotation->id,
150
	]);
151
152
	$return[] = ElggMenuItem::factory([
153
		'name' => 'delete',
154
		'href' => $url,
155
		'text' => elgg_view_icon('delete'),
156
		'confirm' => elgg_echo('deleteconfirm'),
157
		'encode_text' => false,
158
	]);
159
	
160
	return $return;
161
}
162
163
return function() {
164 18
	elgg_register_event_handler('init', 'system', 'messageboard_init');
165
};
166