|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Exposes API endpoints for Like annotations |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
elgg_ws_expose_function( |
|
7
|
|
|
"like.item", |
|
8
|
|
|
"like_item", |
|
9
|
|
|
array( |
|
10
|
|
|
"user" => array('type' => 'string', 'required' => true), |
|
11
|
|
|
"guid" => array('type' => 'int', 'required' => true), |
|
12
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
|
13
|
|
|
), |
|
14
|
|
|
'Submits a like/unlike on an entity based on user id and entity id', |
|
15
|
|
|
'POST', |
|
16
|
|
|
true, |
|
17
|
|
|
false |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
elgg_ws_expose_function( |
|
21
|
|
|
"like.count", |
|
22
|
|
|
"like_count", |
|
23
|
|
|
array( |
|
24
|
|
|
"guid" => array('type' => 'int', 'required' => true), |
|
25
|
|
|
"user" => array('type' => 'string', 'required' => false), |
|
26
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
|
27
|
|
|
), |
|
28
|
|
|
'Retrieves a like count on an entity based on user id and entity id', |
|
29
|
|
|
'POST', |
|
30
|
|
|
true, |
|
31
|
|
|
false |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
elgg_ws_expose_function( |
|
35
|
|
|
"like.users", |
|
36
|
|
|
"like_users", |
|
37
|
|
|
array( |
|
38
|
|
|
"guid" => array('type' => 'int', 'required' => true), |
|
39
|
|
|
"user" => array('type' => 'string', 'required' => false), |
|
40
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
|
41
|
|
|
), |
|
42
|
|
|
'Retrieves all users who liked an entity based on user id and entity id', |
|
43
|
|
|
'POST', |
|
44
|
|
|
true, |
|
45
|
|
|
false |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
function like_item($user, $guid, $lang) |
|
49
|
|
|
{ |
|
50
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
|
51
|
|
|
if (!$user_entity) { |
|
52
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
|
53
|
|
|
} |
|
54
|
|
|
if (!$user_entity instanceof ElggUser) { |
|
55
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!elgg_is_logged_in()) { |
|
59
|
|
|
login($user_entity); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$entity = get_entity($guid); |
|
63
|
|
|
if (!$entity) { |
|
64
|
|
|
return "Object was not found. Please try a different GUID"; |
|
65
|
|
|
} |
|
66
|
|
|
if (!$entity instanceof ElggObject) { |
|
67
|
|
|
return "Invalid object. Please try a different GUID"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$likes = elgg_get_annotations(array( |
|
71
|
|
|
'guid' => $guid, |
|
72
|
|
|
'annotation_owner_guid' => $user_entity->guid, |
|
73
|
|
|
'annotation_name' => 'likes' |
|
74
|
|
|
)); |
|
75
|
|
|
|
|
76
|
|
|
$liked = false; |
|
77
|
|
|
|
|
78
|
|
|
// check to see if the user has already liked the item |
|
79
|
|
|
if (!empty($likes)) { |
|
80
|
|
|
$like = $likes[0]; |
|
81
|
|
|
|
|
82
|
|
|
if ($like && $like->canEdit()) { |
|
83
|
|
|
$like->delete(); |
|
84
|
|
|
$data['message'] = elgg_echo("likes:deleted"); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
} else { |
|
87
|
|
|
$annotation_id = create_annotation($entity->guid, 'likes', "likes", "", $user_entity->guid, $entity->access_id); |
|
88
|
|
|
$liked = true; |
|
89
|
|
|
|
|
90
|
|
|
// notify if poster wasn't owner |
|
91
|
|
View Code Duplication |
if ($entity->owner_guid != $user_entity->guid) { |
|
92
|
|
|
$owner = $entity->getOwnerEntity(); |
|
93
|
|
|
|
|
94
|
|
|
$annotation = elgg_get_annotation_from_id($annotation_id); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$title_str = $entity->getDisplayName(); |
|
97
|
|
|
if (!$title_str) { |
|
98
|
|
|
$title_str = elgg_get_excerpt($entity->description); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$site = elgg_get_site_entity(); |
|
102
|
|
|
|
|
103
|
|
|
$subject = elgg_echo( |
|
104
|
|
|
'likes:notifications:subject', |
|
105
|
|
|
array( |
|
106
|
|
|
$user_entity->name, |
|
107
|
|
|
$title_str |
|
108
|
|
|
), |
|
109
|
|
|
$owner->language |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$body = elgg_echo( |
|
113
|
|
|
'likes:notifications:body', |
|
114
|
|
|
array( |
|
115
|
|
|
$owner->name, |
|
116
|
|
|
$user_entity->name, |
|
117
|
|
|
$title_str, |
|
118
|
|
|
$site->name, |
|
119
|
|
|
$entity->getURL(), |
|
120
|
|
|
$user_entity->getURL() |
|
121
|
|
|
), |
|
122
|
|
|
$owner->language |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
notify_user( |
|
126
|
|
|
$entity->owner_guid, |
|
127
|
|
|
$user_entity->guid, |
|
128
|
|
|
$subject, |
|
129
|
|
|
$body, |
|
130
|
|
|
array( |
|
131
|
|
|
'action' => 'create', |
|
132
|
|
|
'object' => $annotation, |
|
133
|
|
|
) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$data['message'] = elgg_echo("likes:likes"); |
|
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$likes = elgg_get_annotations(array( |
|
141
|
|
|
'guid' => $guid, |
|
142
|
|
|
'annotation_name' => 'likes' |
|
143
|
|
|
)); |
|
144
|
|
|
$data['count'] = count($likes); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$data['liked'] = $liked; |
|
147
|
|
|
|
|
148
|
|
|
return $data; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
function like_count($guid, $user, $lang) |
|
152
|
|
|
{ |
|
153
|
|
|
$entity = get_entity($guid); |
|
154
|
|
|
if (!$entity) { |
|
155
|
|
|
return "Object was not found. Please try a different GUID"; |
|
156
|
|
|
} |
|
157
|
|
|
if (!$entity instanceof ElggObject) { |
|
158
|
|
|
return "Invalid object. Please try a different GUID"; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$likes = elgg_get_annotations(array( |
|
162
|
|
|
'guid' => $guid, |
|
163
|
|
|
'annotation_name' => 'likes' |
|
164
|
|
|
)); |
|
165
|
|
|
$data['count'] = count($likes); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
View Code Duplication |
if ($user) { |
|
168
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
|
169
|
|
|
if (!$user_entity) { |
|
170
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
|
171
|
|
|
} |
|
172
|
|
|
if (!$user_entity instanceof ElggUser) { |
|
173
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($user_entity) { |
|
177
|
|
|
$likes = elgg_get_annotations(array( |
|
178
|
|
|
'guid' => $guid, |
|
179
|
|
|
'annotation_owner_guid' => $user_entity->guid, |
|
180
|
|
|
'annotation_name' => 'likes' |
|
181
|
|
|
)); |
|
182
|
|
|
$data['liked'] = count($likes) > 0; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $data; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
function like_users($guid, $user, $lang) |
|
190
|
|
|
{ |
|
191
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
|
192
|
|
|
if (!$user_entity) { |
|
193
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
|
194
|
|
|
} |
|
195
|
|
|
if (!$user_entity instanceof ElggUser) { |
|
196
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (!elgg_is_logged_in()) { |
|
200
|
|
|
login($user_entity); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$entity = get_entity($guid); |
|
204
|
|
|
if (!$entity) { |
|
205
|
|
|
return "Object was not found. Please try a different GUID"; |
|
206
|
|
|
} |
|
207
|
|
|
if (!$entity instanceof ElggObject) { |
|
208
|
|
|
return "Invalid object. Please try a different GUID"; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$likes = elgg_get_annotations(array( |
|
212
|
|
|
'guid' => $guid, |
|
213
|
|
|
'annotation_name' => 'likes' |
|
214
|
|
|
)); |
|
215
|
|
|
$data = array(); |
|
216
|
|
|
|
|
217
|
|
|
foreach ($likes as $key => $like) { |
|
218
|
|
|
$item = get_user_block($like->owner_guid, $lang); |
|
219
|
|
|
$item['time_created'] = date("Y-m-d H:i:s", $like->time_created); |
|
220
|
|
|
$data['users'][] = $item; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$data['count'] = count($likes); |
|
224
|
|
|
|
|
225
|
|
View Code Duplication |
if ($user) { |
|
226
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
|
227
|
|
|
if (!$user_entity) { |
|
228
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
|
229
|
|
|
} |
|
230
|
|
|
if (!$user_entity instanceof ElggUser) { |
|
231
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if ($user_entity) { |
|
235
|
|
|
$likes = elgg_get_annotations(array( |
|
236
|
|
|
'guid' => $guid, |
|
237
|
|
|
'annotation_owner_guid' => $user_entity->guid, |
|
238
|
|
|
'annotation_name' => 'likes' |
|
239
|
|
|
)); |
|
240
|
|
|
$data['liked'] = count($likes) > 0; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $data; |
|
245
|
|
|
} |
|
246
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.