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

elgg_delete_annotations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
1
<?php
2
/**
3
 * Elgg annotations
4
 * Functions to manage object annotations.
5
 */
6
7
/**
8
 * Get a specific annotation by its id.
9
 * If you want multiple annotation objects, use
10
 * {@link elgg_get_annotations()}.
11
 *
12
 * @param int $id The id of the annotation object being retrieved.
13
 *
14
 * @return \ElggAnnotation|false
15
 */
16
function elgg_get_annotation_from_id($id) {
17 14
	return _elgg_services()->annotationsTable->get($id);
18
}
19
20
/**
21
 * Deletes an annotation using its ID.
22
 *
23
 * @param int $id The annotation ID to delete.
24
 *
25
 * @return bool
26
 */
27
function elgg_delete_annotation_by_id($id) {
28
	$annotation = elgg_get_annotation_from_id($id);
29
	if (!$annotation) {
30
		return false;
31
	}
32
33
	return $annotation->delete();
34
}
35
36
/**
37
 * Fetch annotations or perform a calculation on them
38
 *
39
 * Accepts all options supported by {@link elgg_get_entities()}
40
 *
41
 * The default 'order_by' is 'n_table.time_created, n_table.id',
42
 *
43
 * @see   elgg_get_entities()
44
 *
45
 * @param array $options Options
46
 *
47
 * @return \ElggAnnotation[]|mixed
48
 *
49
 * @see   elgg_get_entities()
50
 * @since 1.8.0
51
 */
52
function elgg_get_annotations(array $options = []) {
53 20
	return _elgg_services()->annotationsTable->find($options);
54
}
55
56
/**
57
 * Returns a rendered list of annotations with pagination.
58
 *
59
 * @param array $options Annotation getter and display options.
60
 *                       {@link elgg_get_annotations()} and {@link elgg_list_entities()}.
61
 *
62
 * @return string The list of entities
63
 * @since 1.8.0
64
 */
65
function elgg_list_annotations($options) {
66
	$defaults = [
67 1
		'limit' => 25,
68 1
		'offset' => (int) max(get_input('annoff', 0), 0),
69 1
		'no_results' => '',
70
	];
71
72 1
	$options = array_merge($defaults, $options);
73
74 1
	return elgg_list_entities($options, 'elgg_get_annotations', 'elgg_view_annotation_list');
75
}
76
77
/**
78
 * Deletes annotations based on $options.
79
 *
80
 * @warning Unlike elgg_get_annotations() this will not accept an empty options array!
81
 *          This requires at least one constraint: annotation_owner_guid(s),
82
 *          annotation_name(s), annotation_value(s), or guid(s) must be set.
83
 *
84
 * @param array $options An options array. {@link elgg_get_annotations()}
85
 * @return bool|null true on success, false on failure, null if no annotations to delete.
86
 * @since 1.8.0
87
 */
88
function elgg_delete_annotations(array $options) {
89 215
	return _elgg_services()->annotationsTable->deleteAll($options);
90
}
91
92
/**
93
 * Disables annotations based on $options.
94
 *
95
 * @warning Unlike elgg_get_annotations() this will not accept an empty options array!
96
 *
97
 * @param array $options An options array. {@link elgg_get_annotations()}
98
 * @return bool|null true on success, false on failure, null if no annotations disabled.
99
 * @since 1.8.0
100
 */
101
function elgg_disable_annotations(array $options) {
102 9
	return _elgg_services()->annotationsTable->disableAll($options);
103
}
104
105
/**
106
 * Enables annotations based on $options.
107
 *
108
 * @warning Unlike elgg_get_annotations() this will not accept an empty options array!
109
 *
110
 * @warning In order to enable annotations, you must first use
111
 * {@link access_show_hidden_entities()}.
112
 *
113
 * @param array $options An options array. {@link elgg_get_annotations()}
114
 * @return bool|null true on success, false on failure, null if no metadata enabled.
115
 * @since 1.8.0
116
 */
117
function elgg_enable_annotations(array $options) {
118 4
	return _elgg_services()->annotationsTable->enableAll($options);
119
}
120
121
/**
122
 * Check to see if a user has already created an annotation on an object
123
 *
124
 * @param int    $entity_guid Entity guid
125
 * @param string $name        Annotation name
126
 * @param int    $owner_guid  Defaults to logged in user.
127
 *
128
 * @return bool
129
 * @since 1.8.0
130
 */
131
function elgg_annotation_exists($entity_guid, $name, $owner_guid = null) {
132 1
	if (!$owner_guid) {
0 ignored issues
show
Bug Best Practice introduced by Ismayil Khayredinov
The expression $owner_guid of type null|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
133 1
		$owner_guid = elgg_get_logged_in_user_guid();
134
	}
135
136 1
	return _elgg_services()->annotationsTable->exists($entity_guid, $name, $owner_guid);
137
}
138
139
/**
140
 * Set the URL for a comment when called from a plugin hook
141
 *
142
 * @param string $hook   Hook name
143
 * @param string $type   Hook type
144
 * @param string $url    URL string
145
 * @param array  $params Parameters of the hook
146
 * @return string
147
 * @access private
148
 */
149
function _elgg_set_comment_url($hook, $type, $url, $params) {
2 ignored issues
show
Unused Code introduced by Cash Costello
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

149
function _elgg_set_comment_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...
Unused Code introduced by Cash Costello
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

149
function _elgg_set_comment_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 Cash Costello
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

149
function _elgg_set_comment_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...
150
	$annotation = $params['extender'];
151
	/* @var \ElggExtender $annotation */
152
	if ($annotation->getSubtype() == 'generic_comment') {
153
		$entity = $annotation->getEntity();
154
		if ($entity) {
155
			return $entity->getURL() . '#item-annotation-' . $annotation->id;
156
		}
157
	}
158
}
159
160
/**
161
 * Register annotation unit tests
162
 *
163
 * @param string $hook  'unit_test'
164
 * @param string $type  'system'
165
 * @param array  $tests current return value
166
 *
167
 * @return array
168
 *
169
 * @access private
170
 * @codeCoverageIgnore
171
 */
172
function _elgg_annotations_test($hook, $type, $tests) {
2 ignored issues
show
Unused Code introduced by Cash Costello
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

172
function _elgg_annotations_test(/** @scrutinizer ignore-unused */ $hook, $type, $tests) {

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 Cash Costello
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

172
function _elgg_annotations_test($hook, /** @scrutinizer ignore-unused */ $type, $tests) {

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...
173
	$tests[] = ElggCoreAnnotationAPITest::class;
174
	return $tests;
175
}
176
177
/**
178
 * Initialize the annotation library
179
 *
180
 * @return void
181
 *
182
 * @access private
183
 */
184
function _elgg_annotations_init() {
185 31
	elgg_register_plugin_hook_handler('extender:url', 'annotation', '_elgg_set_comment_url');
186 31
	elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_annotations_test');
187 31
}
188
189
/**
190
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
191
 */
192
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
Unused Code introduced by Steve Clay
The parameter $hooks 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

192
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

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...
193 18
	$events->registerHandler('init', 'system', '_elgg_annotations_init');
194
};
195