1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Exposes API endpoints for Discussion entities |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
elgg_ws_expose_function( |
7
|
|
|
"get.discussion", |
8
|
|
|
"get_discussion", |
9
|
|
|
array( |
10
|
|
|
"user" => array('type' => 'string', 'required' => true), |
11
|
|
|
"guid" => array('type' => 'int', 'required' => true), |
12
|
|
|
"thread" => array('type' => 'int', 'required' => false, 'default' => 1), |
13
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
14
|
|
|
), |
15
|
|
|
'Retrieves a discussion & all replies based on user id and discussion id', |
16
|
|
|
'POST', |
17
|
|
|
true, |
18
|
|
|
false |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
elgg_ws_expose_function( |
22
|
|
|
"get.discussions", |
23
|
|
|
"get_discussions", |
24
|
|
|
array( |
25
|
|
|
"user" => array('type' => 'string', 'required' => true), |
26
|
|
|
"limit" => array('type' => 'int', 'required' => false, 'default' => 10), |
27
|
|
|
"offset" => array('type' => 'int', 'required' => false, 'default' => 0), |
28
|
|
|
"filters" => array('type' => 'string', 'required' => false, 'default' => ""), |
29
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
30
|
|
|
), |
31
|
|
|
'Retrieves discussions based on user id', |
32
|
|
|
'POST', |
33
|
|
|
true, |
34
|
|
|
false |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
elgg_ws_expose_function( |
38
|
|
|
"get.discussionedit", |
39
|
|
|
"get_discussion_edit", |
40
|
|
|
array( |
41
|
|
|
"user" => array('type' => 'string', 'required' => true), |
42
|
|
|
"guid" => array('type' => 'int', 'required' => true), |
43
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
44
|
|
|
), |
45
|
|
|
'Retrieves a discussion based on guid, returns only information required for edit', |
46
|
|
|
'POST', |
47
|
|
|
true, |
48
|
|
|
false |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
elgg_ws_expose_function( |
52
|
|
|
"post.discussion", |
53
|
|
|
"post_discussion", |
54
|
|
|
array( |
55
|
|
|
"user" => array('type' => 'string', 'required' => true), |
56
|
|
|
"title" => array('type' => 'string', 'required' => true), |
57
|
|
|
"message" => array('type' =>'string', 'required' => true), |
58
|
|
|
"container_guid" => array('type' =>'string', 'required' => false, 'default' => ''), |
59
|
|
|
"access" => array('type' =>'int', 'required' => false, 'default' => 2), |
60
|
|
|
"open" => array('type' =>'int', 'required' => false, 'default' => 1), |
61
|
|
|
"topic_guid" => array('type' =>'int', 'required' => false, 'default' => 0), |
62
|
|
|
"lang" => array('type' => 'string', 'required' => false, 'default' => "en") |
63
|
|
|
), |
64
|
|
|
'Posts/Saves a new discussion topic', |
65
|
|
|
'POST', |
66
|
|
|
true, |
67
|
|
|
false |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
function get_discussion($user, $guid, $thread, $lang) |
71
|
|
|
{ |
72
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
73
|
|
|
if (!$user_entity) { |
74
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
75
|
|
|
} |
76
|
|
|
if (!$user_entity instanceof ElggUser) { |
77
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (!elgg_is_logged_in()) { |
81
|
|
|
login($user_entity); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$entity = get_entity($guid); |
85
|
|
|
if (!$entity) { |
86
|
|
|
return "Discussion was not found. Please try a different GUID"; |
87
|
|
|
} |
88
|
|
|
if (!elgg_instanceof($entity, "object", "groupforumtopic")) { |
89
|
|
|
return "Invalid discussion. Please try a different GUID"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
$discussions = elgg_list_entities(array( |
95
|
|
|
'type' => 'object', |
96
|
|
|
'subtype' => 'groupforumtopic', |
97
|
|
|
'guid' => $guid |
98
|
|
|
)); |
99
|
|
|
$discussion = json_decode($discussions)[0]; |
100
|
|
|
|
101
|
|
|
$discussion->title = gc_explode_translation($discussion->title, $lang); |
102
|
|
|
|
103
|
|
|
$likes = elgg_get_annotations(array( |
104
|
|
|
'guid' => $discussion->guid, |
105
|
|
|
'annotation_name' => 'likes' |
106
|
|
|
)); |
107
|
|
|
$discussion->likes = count($likes); |
108
|
|
|
|
109
|
|
|
$liked = elgg_get_annotations(array( |
110
|
|
|
'guid' => $discussion->guid, |
111
|
|
|
'annotation_owner_guid' => $user_entity->guid, |
112
|
|
|
'annotation_name' => 'likes' |
113
|
|
|
)); |
114
|
|
|
$discussion->liked = count($liked) > 0; |
115
|
|
|
|
116
|
|
|
$discussion->userDetails = get_user_block($discussion->owner_guid, $lang); |
117
|
|
|
$discussion->description = gc_explode_translation($discussion->description, $lang); |
118
|
|
|
|
119
|
|
|
$discussionsArray = array(); |
120
|
|
|
$discussionsArray[] = $discussion; |
121
|
|
|
|
122
|
|
|
if ($thread) { |
123
|
|
|
$all_replies = elgg_list_entities_from_metadata(array( |
124
|
|
|
'type' => 'object', |
125
|
|
|
'subtype' => 'discussion_reply', |
126
|
|
|
'container_guid' => $guid |
127
|
|
|
)); |
128
|
|
|
$replies = json_decode($all_replies); |
129
|
|
|
$replies = array_reverse($replies); |
130
|
|
|
|
131
|
|
|
foreach ($replies as $reply) { |
132
|
|
|
$discussionsArray[] = $reply; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $discussionsArray; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function get_discussions($user, $limit, $offset, $filters, $lang) |
140
|
|
|
{ |
141
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
142
|
|
|
if (!$user_entity) { |
143
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
144
|
|
|
} |
145
|
|
|
if (!$user_entity instanceof ElggUser) { |
146
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (!elgg_is_logged_in()) { |
150
|
|
|
login($user_entity); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$filter_data = json_decode($filters); |
154
|
|
|
if (!empty($filter_data)) { |
155
|
|
|
$params = array( |
156
|
|
|
'type' => 'object', |
157
|
|
|
'subtype' => 'groupforumtopic', |
158
|
|
|
'limit' => $limit, |
159
|
|
|
'offset' => $offset |
160
|
|
|
); |
161
|
|
|
|
162
|
|
|
if ($filter_data->mine) { |
163
|
|
|
$params['owner_guid'] = $user_entity->guid; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
if ($filter_data->name) { |
167
|
|
|
$db_prefix = elgg_get_config('dbprefix'); |
168
|
|
|
$params['joins'] = array("JOIN {$db_prefix}objects_entity oe ON e.guid = oe.guid"); |
169
|
|
|
$params['wheres'] = array("(oe.title LIKE '%" . $filter_data->name . "%' OR oe.description LIKE '%" . $filter_data->name . "%')"); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($filter_data->mine) { |
173
|
|
|
$all_discussions = elgg_list_entities_from_relationship($params); |
174
|
|
|
} else { |
175
|
|
|
$all_discussions = elgg_list_entities_from_metadata($params); |
176
|
|
|
} |
177
|
|
|
} else { |
178
|
|
|
$all_discussions = elgg_list_entities(array( |
179
|
|
|
'type' => 'object', |
180
|
|
|
'subtype' => 'groupforumtopic', |
181
|
|
|
'limit' => $limit, |
182
|
|
|
'offset' => $offset |
183
|
|
|
)); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$discussions = json_decode($all_discussions); |
187
|
|
|
|
188
|
|
|
foreach ($discussions as $discussion) { |
189
|
|
|
$discussion->name = gc_explode_translation($discussion->name, $lang); |
190
|
|
|
|
191
|
|
|
$likes = elgg_get_annotations(array( |
192
|
|
|
'guid' => $discussion->guid, |
193
|
|
|
'annotation_name' => 'likes' |
194
|
|
|
)); |
195
|
|
|
$discussion->likes = count($likes); |
196
|
|
|
|
197
|
|
|
$liked = elgg_get_annotations(array( |
198
|
|
|
'guid' => $discussion->guid, |
199
|
|
|
'annotation_owner_guid' => $user_entity->guid, |
200
|
|
|
'annotation_name' => 'likes' |
201
|
|
|
)); |
202
|
|
|
$discussion->liked = count($liked) > 0; |
203
|
|
|
|
204
|
|
|
$discussion->userDetails = get_user_block($discussion->owner_guid, $lang); |
205
|
|
|
$discussion->description = gc_explode_translation($discussion->description, $lang); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $discussions; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
function get_discussion_edit($user, $guid, $lang) |
212
|
|
|
{ |
213
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
214
|
|
|
if (!$user_entity) { |
215
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
216
|
|
|
} |
217
|
|
|
if (!$user_entity instanceof ElggUser) { |
218
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if (!elgg_is_logged_in()) { |
222
|
|
|
login($user_entity); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$entity = get_entity($guid); |
226
|
|
|
if (!$entity) { |
227
|
|
|
return "Discussion was not found. Please try a different GUID"; |
228
|
|
|
} |
229
|
|
|
if (!elgg_instanceof($entity, "object", "groupforumtopic")) { |
230
|
|
|
return "Invalid discussion. Please try a different GUID"; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
|
235
|
|
|
$discussions = elgg_list_entities(array( |
236
|
|
|
'type' => 'object', |
237
|
|
|
'subtype' => 'groupforumtopic', |
238
|
|
|
'guid' => $guid |
239
|
|
|
)); |
240
|
|
|
$discussion = json_decode($discussions)[0]; |
241
|
|
|
if ($discussion->owner_guid != $user_entity->getGUID()){ |
242
|
|
|
return elgg_echo('discussion:error:permissions'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$discussion->title = json_decode($discussion->title); |
246
|
|
|
$discussion->description = json_decode($discussion->description); |
247
|
|
|
$container = get_entity($discussion->container_guid); |
248
|
|
|
$discussion->group->public = $container->isPublicMembership(); |
249
|
|
|
if (!$discussion->group->public && !$container->isMember($user_entity)){ |
250
|
|
|
return elgg_echo('discussion:error:permissions'); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $discussion; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
function post_discussion($user, $title, $message, $container_guid, $access, $open, $topic_guid, $lang) |
257
|
|
|
{ |
258
|
|
|
$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user)); |
259
|
|
|
if (!$user_entity) { |
260
|
|
|
return "User was not found. Please try a different GUID, username, or email address"; |
261
|
|
|
} |
262
|
|
|
if (!$user_entity instanceof ElggUser) { |
263
|
|
|
return "Invalid user. Please try a different GUID, username, or email address"; |
264
|
|
|
} |
265
|
|
|
if (!elgg_is_logged_in()) { |
266
|
|
|
login($user_entity); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
//check required fields |
270
|
|
|
$titles = json_decode($title); |
271
|
|
|
$message = json_decode($message); |
272
|
|
|
if (!$titles->en && !$titles->fr) { return elgg_echo("discussion:error:missing"); } |
273
|
|
|
if (!$message->en && !$message->fr) { return elgg_echo("discussion:error:missing"); } |
274
|
|
View Code Duplication |
if (!($titles->en && $message->en) && !($titles->fr && $message->fr)) { return "require-same-lang"; } |
275
|
|
|
|
276
|
|
|
$container = get_entity($container_guid); |
277
|
|
|
if (!$container || !$container->canWriteToContainer(0, 'object', 'groupforumtopic')) { |
278
|
|
|
return elgg_echo('discussion:error:permissions'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
//Check if new topic or edit |
282
|
|
|
$new_topic = true; |
283
|
|
|
if ($topic_guid > 0) { |
284
|
|
|
$new_topic = false; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
if ($new_topic) { |
288
|
|
|
$topic = new ElggObject(); |
289
|
|
|
$topic->subtype = 'groupforumtopic'; |
290
|
|
|
} else { |
291
|
|
|
$topic = get_entity($topic_guid); |
292
|
|
|
if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) { |
293
|
|
|
return elgg_echo('discussion:topic:notfound'); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
//french english setup |
298
|
|
|
$title1 = htmlspecialchars($titles->en, ENT_QUOTES, 'UTF-8'); |
299
|
|
|
$title2 = htmlspecialchars($titles->fr, ENT_QUOTES, 'UTF-8'); |
300
|
|
|
$title = gc_implode_translation($title1, $title2); |
301
|
|
|
$desc = gc_implode_translation($message->en, $message->fr); |
302
|
|
|
|
303
|
|
|
if ($access == 1 && !$container->isPublicMembership()){ |
304
|
|
|
$access = 2; //Access cannot be public if group is not public. Default to group only. |
305
|
|
|
} |
306
|
|
|
$access_id = $access; |
307
|
|
|
if ($access_id === 2){ |
308
|
|
|
$access_id = $container->group_acl; //Sets access id to match group only id. |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
$topic->title = $title; |
313
|
|
|
$topic->title2 = $title2; |
314
|
|
|
$topic->description = $desc; |
315
|
|
|
$topic->description2 = $message->fr; |
316
|
|
|
$topic->status = ($open == 1) ? "open" : "closed"; |
317
|
|
|
$topic->access_id = $access_id; |
318
|
|
|
$topic->container_guid = $container_guid; |
319
|
|
|
|
320
|
|
|
$result = $topic->save(); |
321
|
|
|
if (!$result) { |
322
|
|
|
return elgg_echo('discussion:error:notsaved'); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
//handle results differently for new topics and topic edits |
326
|
|
View Code Duplication |
if ($new_topic) { |
327
|
|
|
system_message(elgg_echo('discussion:topic:created')); |
328
|
|
|
elgg_create_river_item(array( |
329
|
|
|
'view' => 'river/object/groupforumtopic/create', |
330
|
|
|
'action_type' => 'create', |
331
|
|
|
'subject_guid' => elgg_get_logged_in_user_guid(), |
332
|
|
|
'object_guid' => $topic->guid, |
333
|
|
|
)); |
334
|
|
|
} else { |
335
|
|
|
system_message(elgg_echo('discussion:topic:updated')); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $topic->getURL(); |
339
|
|
|
} |
340
|
|
|
|