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

bookmarks_prepare_form_vars()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 4
nop 1
dl 0
loc 31
ccs 0
cts 16
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg Bookmarks plugin
4
 *
5
 * @package ElggBookmarks
6
 */
7
8
/**
9
 * Bookmark init
10
 *
11
 * @return void
12
 */
13
function bookmarks_init() {
14
15
	// menus
16 31
	elgg_register_menu_item('site', [
17 31
		'name' => 'bookmarks',
18 31
		'text' => elgg_echo('bookmarks'),
19 31
		'href' => 'bookmarks/all',
20
	]);
21
22 31
	elgg_register_plugin_hook_handler('register', 'menu:page', 'bookmarks_page_menu');
23 31
	elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'bookmarks_owner_block_menu');
24
25 31
	elgg_register_page_handler('bookmarks', 'bookmarks_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

25
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('bookmarks', 'bookmarks_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...
26
27 31
	elgg_extend_view('elgg.js', 'bookmarks.js');
28
29
	// Register for notifications
30 31
	elgg_register_notification_event('object', 'bookmarks', ['create']);
31 31
	elgg_register_plugin_hook_handler('prepare', 'notification:create:object:bookmarks', 'bookmarks_prepare_notification');
32
33
	// Register bookmarks view for ecml parsing
34 31
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'bookmarks_ecml_views_hook');
35
36
	// Register a URL handler for bookmarks
37 31
	elgg_register_plugin_hook_handler('entity:url', 'object', 'bookmark_set_url');
38
39
	// Groups
40 31
	add_group_tool_option('bookmarks', elgg_echo('bookmarks:enablebookmarks'), true);
41 31
	elgg_extend_view('groups/tool_latest', 'bookmarks/group_module');
42
43
	// allow to be liked
44 31
	elgg_register_plugin_hook_handler('likes:is_likable', 'object:bookmarks', 'Elgg\Values::getTrue');
45 31
}
46
47
/**
48
 * Dispatcher for bookmarks.
49
 *
50
 * URLs take the form of
51
 *  All bookmarks:        bookmarks/all
52
 *  User's bookmarks:     bookmarks/owner/<username>
53
 *  Friends' bookmarks:   bookmarks/friends/<username>
54
 *  View bookmark:        bookmarks/view/<guid>/<title>
55
 *  New bookmark:         bookmarks/add/<guid> (container: user, group, parent)
56
 *  Edit bookmark:        bookmarks/edit/<guid>
57
 *  Group bookmarks:      bookmarks/group/<guid>/all
58
 *  Bookmarklet:          bookmarks/bookmarklet/<guid> (user)
59
 *
60
 * Title is ignored
61
 *
62
 * @param array $page URL segments
63
 * @return bool
64
 */
65
function bookmarks_page_handler($page) {
66
67
	if (!isset($page[0])) {
68
		$page[0] = 'all';
69
	}
70
71
	elgg_push_breadcrumb(elgg_echo('bookmarks'), 'bookmarks/all');
72
73
	switch ($page[0]) {
74
		case "all":
75
			echo elgg_view_resource('bookmarks/all');
76
			break;
77
78
		case "owner":
79
			echo elgg_view_resource('bookmarks/owner');
80
			break;
81
82
		case "friends":
83
			echo elgg_view_resource('bookmarks/friends');
84
			break;
85
86
		case "view":
87
			echo elgg_view_resource('bookmarks/view', [
88
				'guid' => $page[1],
89
			]);
90
			break;
91
92
		case "add":
93
			echo elgg_view_resource('bookmarks/add');
94
			break;
95
96
		case "edit":
97
			echo elgg_view_resource('bookmarks/edit', [
98
				'guid' => $page[1],
99
			]);
100
			break;
101
102
		case 'group':
103
			echo elgg_view_resource('bookmarks/owner');
104
			break;
105
106
		case "bookmarklet":
107
			echo elgg_view_resource('bookmarks/bookmarklet', [
108
				'container_guid' => $page[1],
109
			]);
110
			break;
111
112
		default:
113
			return false;
114
	}
115
116
	elgg_pop_context();
117
	return true;
118
}
119
120
/**
121
 * Populates the ->getUrl() method for bookmarked objects
122
 *
123
 * @param string $hook   'entity:url'
124
 * @param string $type   'object'
125
 * @param string $url    current return value
126
 * @param array  $params supplied params
127
 *
128
 * @return void|string bookmarked item URL
129
 */
130
function bookmark_set_url($hook, $type, $url, $params) {
2 ignored issues
show
Unused Code introduced by
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

130
function bookmark_set_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...
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

130
function bookmark_set_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...
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

130
function bookmark_set_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...
131 3
	$entity = elgg_extract('entity', $params);
132 3
	if (!$entity instanceof ElggBookmark) {
133 3
		return;
134
	}
135
	
136
	$title = elgg_get_friendly_title($entity->title);
137
	return "bookmarks/view/{$entity->guid}/{$title}";
138
}
139
140
/**
141
 * Add a menu item to an ownerblock
142
 *
143
 * @param string         $hook   'register'
144
 * @param string         $type   'menu:owner_block'
145
 * @param ElggMenuItem[] $return current return value
146
 * @param array          $params supplied params
147
 *
148
 * @return ElggMenuItem[]
149
 */
150
function bookmarks_owner_block_menu($hook, $type, $return, $params) {
2 ignored issues
show
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

150
function bookmarks_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...
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

150
function bookmarks_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...
151
152
	$entity = elgg_extract('entity', $params);
153
	
154
	if ($entity instanceof ElggUser) {
155
		$url = "bookmarks/owner/{$entity->username}";
156
		$item = new ElggMenuItem('bookmarks', elgg_echo('bookmarks'), $url);
157
		$return[] = $item;
158
	} elseif ($entity instanceof ElggGroup) {
159
		if ($entity->isToolEnabled('bookmarks')) {
160
			$url = "bookmarks/group/{$entity->guid}/all";
161
			$item = new ElggMenuItem('bookmarks', elgg_echo('bookmarks:group'), $url);
162
			$return[] = $item;
163
		}
164
	}
165
	
166
	return $return;
167
}
168
169
/**
170
 * Prepare a notification message about a new bookmark
171
 *
172
 * @param string                          $hook         Hook name
173
 * @param string                          $type         Hook type
174
 * @param Elgg\Notifications\Notification $notification The notification to prepare
175
 * @param array                           $params       Hook parameters
176
 * @return Elgg\Notifications\Notification
177
 */
178
function bookmarks_prepare_notification($hook, $type, $notification, $params) {
2 ignored issues
show
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

178
function bookmarks_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...
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

178
function bookmarks_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...
179
	$entity = $params['event']->getObject();
180
	$owner = $params['event']->getActor();
181
	$recipient = $params['recipient'];
0 ignored issues
show
Unused Code introduced by
The assignment to $recipient is dead and can be removed.
Loading history...
182
	$language = $params['language'];
183
	$method = $params['method'];
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
184
185
	$descr = $entity->description;
186
	$title = $entity->title;
187
188
	$notification->subject = elgg_echo('bookmarks:notify:subject', [$title], $language);
189
	$notification->body = elgg_echo('bookmarks:notify:body', [
190
		$owner->name,
191
		$title,
192
		$entity->address,
193
		$descr,
194
		$entity->getURL()
195
	], $language);
196
	$notification->summary = elgg_echo('bookmarks:notify:summary', [$entity->title], $language);
197
	$notification->url = $entity->getURL();
198
	return $notification;
199
}
200
201
/**
202
 * Add a page menu menu
203
 *
204
 * @param string         $hook   'register'
205
 * @param string         $type   'menu:page'
206
 * @param ElggMenuItem[] $return current return value
207
 * @param array          $params supplied params
208
 *
209
 * @return void|ElggMenuItem[]
210
 */
211
function bookmarks_page_menu($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
The parameter $params 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

211
function bookmarks_page_menu($hook, $type, $return, /** @scrutinizer ignore-unused */ $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 $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

211
function bookmarks_page_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...
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

211
function bookmarks_page_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...
212 1
	if (!elgg_is_logged_in()) {
213 1
		return;
214
	}
215
	// only show bookmarklet in bookmark pages
216
	if (!elgg_in_context('bookmarks')) {
217
		return;
218
	}
219
	
220
	$page_owner = elgg_get_page_owner_entity();
221
	if (!$page_owner) {
222
		$page_owner = elgg_get_logged_in_user_entity();
223
	}
224
	
225
	if ($page_owner instanceof ElggGroup) {
226
		$title = elgg_echo('bookmarks:bookmarklet:group');
227
	} else {
228
		$title = elgg_echo('bookmarks:bookmarklet');
229
	}
230
231
	$return[] = ElggMenuItem::factory([
232
		'name' => 'bookmarklet',
233
		'text' => $title,
234
		'href' => 'bookmarks/bookmarklet/' . $page_owner->getGUID(),
235
	]);
236
237
	return $return;
238
}
239
240
/**
241
 * Return bookmarks views to parse for ecml
242
 *
243
 * @param string $hook   'get_views'
244
 * @param string $type   'ecml'
245
 * @param array  $return current return value
246
 * @param array  $params supplied params
247
 *
248
 * @return array
249
 */
250
function bookmarks_ecml_views_hook($hook, $type, $return, $params) {
3 ignored issues
show
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

250
function bookmarks_ecml_views_hook(/** @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...
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

250
function bookmarks_ecml_views_hook($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 $params 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

250
function bookmarks_ecml_views_hook($hook, $type, $return, /** @scrutinizer ignore-unused */ $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...
251
	$return['object/bookmarks'] = elgg_echo('item:object:bookmarks');
252
	return $return;
253
}
254
255
/**
256
 * Prepare the add/edit form variables
257
 *
258
 * @param ElggObject $bookmark A bookmark object.
259
 * @return array
260
 */
261
function bookmarks_prepare_form_vars($bookmark = null) {
262
	// input names => defaults
263
	$values = [
264
		'title' => get_input('title', ''), // bookmarklet support
265
		'address' => get_input('address', ''),
266
		'description' => '',
267
		'access_id' => ACCESS_DEFAULT,
268
		'tags' => '',
269
		'container_guid' => elgg_get_page_owner_guid(),
270
		'guid' => null,
271
		'entity' => $bookmark,
272
	];
273
274
	if ($bookmark) {
275
		foreach (array_keys($values) as $field) {
276
			if (isset($bookmark->$field)) {
277
				$values[$field] = $bookmark->$field;
278
			}
279
		}
280
	}
281
282
	if (elgg_is_sticky_form('bookmarks')) {
283
		$sticky_values = elgg_get_sticky_values('bookmarks');
284
		foreach ($sticky_values as $key => $value) {
285
			$values[$key] = $value;
286
		}
287
	}
288
289
	elgg_clear_sticky_form('bookmarks');
290
291
	return $values;
292
}
293
294
return function() {
295 18
	elgg_register_event_handler('init', 'system', 'bookmarks_init');
296
};
297