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

messageboard_add()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 29
nc 4
nop 4
dl 0
loc 44
ccs 0
cts 28
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
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');
0 ignored issues
show
Deprecated Code introduced by
The function elgg_register_page_handler() has been deprecated: 3.0 ( Ignorable by Annotation )

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

16
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('messageboard', 'messageboard_page_handler');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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),
0 ignored issues
show
Bug introduced by
It seems like $result_id can also be of type true; however, parameter $id of elgg_get_annotation_from_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

115
			'object' => elgg_get_annotation_from_id(/** @scrutinizer ignore-type */ $result_id),
Loading history...
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) {
2 ignored issues
show
Unused Code introduced by
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

134
function messageboard_annotation_menu_setup($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...
Unused Code introduced by
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

134
function messageboard_annotation_menu_setup(/** @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...
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