Completed
Pull Request — gcconnex (#1509)
by Nick
18:12
created

wire.php ➔ reply_wire()   C

Complexity

Conditions 8
Paths 28

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 28
nop 4
dl 0
loc 27
rs 5.3846
c 0
b 0
f 0
1
<?php
2
/*
3
 * Exposes API endpoints for Wire entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.wirepost",
8
	"get_wirepost",
9
	array(
10
		"user" => array('type' => 'string', 'required' => true),
11
		"guid" => array('type' => 'int', 'required' => true),
12
		"thread" => array('type' => 'int', 'required' => false, 'default' => 0),
13
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
14
	),
15
	'Retrieves a wire post & all replies based on user id and wire post id',
16
	'POST',
17
	true,
18
	false
19
);
20
21
elgg_ws_expose_function(
22
	"get.wireposts",
23
	"get_wireposts",
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 a user\'s wire posts based on user id',
32
	'POST',
33
	true,
34
	false
35
);
36
37
elgg_ws_expose_function(
38
	"get.wirepostsbycolleagues",
39
	"get_wirepostsbycolleagues",
40
	array(
41
		"user" => array('type' => 'string', 'required' => true),
42
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
43
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
44
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
45
	),
46
	'Retrieves a user\'s colleague\'s wire posts based on user id',
47
	'POST',
48
	true,
49
	false
50
);
51
52
elgg_ws_expose_function(
53
	"get.wirepostsbyuser",
54
	"get_wirepostsbyuser",
55
	array(
56
		"profileemail" => array('type' => 'string', 'required' => true),
57
		"user" => array('type' => 'string', 'required' => true),
58
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
59
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
60
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
61
	),
62
	'Retrieves a user\'s wire posts based on user id',
63
	'POST',
64
	true,
65
	false
66
);
67
68
elgg_ws_expose_function(
69
	"reply.wire",
70
	"reply_wire",
71
	array(
72
		"user" => array('type' => 'string', 'required' => true),
73
		"message" => array('type' => 'string', 'required' => true),
74
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
75
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
76
	),
77
	'Submits a reply to a wire post based on user id and wire post id',
78
	'POST',
79
	true,
80
	false
81
);
82
83
function get_wirepost($user, $guid, $thread, $lang)
84
{
85
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
86
	if (!$user_entity) {
87
		return "User was not found. Please try a different GUID, username, or email address";
88
	}
89
	if (!$user_entity instanceof ElggUser) {
90
		return "Invalid user. Please try a different GUID, username, or email address";
91
	}
92
93
	$entity = get_entity($guid);
94
	if (!$entity) {
95
		return "Wire was not found. Please try a different GUID";
96
	}
97
	if (!$entity instanceof ElggWire) {
98
		return "Invalid wire. Please try a different GUID";
99
	}
100
101
	$thread_id = $entity->wire_thread;
102
103
	if (!elgg_is_logged_in()) {
104
		login($user_entity);
105
	}
106
107
	if ($thread) {
108
		$all_wire_posts = elgg_list_entities_from_metadata(array(
109
			"metadata_name" => "wire_thread",
110
			"metadata_value" => $thread_id,
111
			"type" => "object",
112
			"subtype" => "thewire",
113
			"limit" => 0,
114
			"preload_owners" => true
115
		));
116
		$wire_posts = json_decode($all_wire_posts);
117
118 View Code Duplication
		foreach ($wire_posts as $wire_post) {
119
			$wire_post_obj = get_entity($wire_post->guid);
120
			$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
121
122
			$url = "";
123
			if (!empty($reshare)) {
124
				$url = $reshare->getURL();
125
			}
126
127
			$text = "";
128
			if (!empty($reshare->title)) {
129
				$text = $reshare->title;
130
			} elseif (!empty($reshare->name)) {
131
				$text = $reshare->name;
132
			} elseif (!empty($reshare->description)) {
133
				$text = elgg_get_excerpt($reshare->description, 140);
134
			}
135
136
			$wire_post->shareURL = $url;
137
			$wire_post->shareText = gc_explode_translation($text, $lang);
138
139
			$likes = elgg_get_annotations(array(
140
				'guid' => $wire_post->guid,
141
				'annotation_name' => 'likes'
142
			));
143
			$wire_post->likes = count($likes);
144
145
			$liked = elgg_get_annotations(array(
146
				'guid' => $wire_post->guid,
147
				'annotation_owner_guid' => $user_entity->guid,
148
				'annotation_name' => 'likes'
149
			));
150
			$wire_post->liked = count($liked) > 0;
151
152
			$replied = elgg_get_entities_from_metadata(array(
153
				"metadata_name" => "wire_thread",
154
				"metadata_value" => $thread_id,
155
				"type" => "object",
156
				"subtype" => "thewire",
157
				"owner_guid" => $user_entity->guid
158
			));
159
			$wire_post->replied = count($replied) > 0;
160
161
			$wire_post->thread_id = $thread_id;
162
163
			$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
164
			$wire_post->description = wire_filter($wire_post->description);
165
		}
166
	} else {
167
		$wire_posts = elgg_list_entities(array(
168
			"type" => "object",
169
			"subtype" => "thewire",
170
			"guid" => $guid
171
		));
172
		$wire_post = json_decode($wire_posts)[0];
173
174
		$wire_post_obj = get_entity($wire_post->guid);
175
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
176
177
		$url = "";
178
		if (!empty($reshare)) {
179
			$url = $reshare->getURL();
180
		}
181
182
		$text = "";
183
		if (!empty($reshare->title)) {
184
			$text = $reshare->title;
185
		} elseif (!empty($reshare->name)) {
186
			$text = $reshare->name;
187
		} elseif (!empty($reshare->description)) {
188
			$text = elgg_get_excerpt($reshare->description, 140);
189
		}
190
191
		$wire_post->shareURL = $url;
192
		$wire_post->shareText = gc_explode_translation($text, $lang);
193
194
		$likes = elgg_get_annotations(array(
195
			'guid' => $wire_post->guid,
196
			'annotation_name' => 'likes'
197
		));
198
		$wire_post->likes = count($likes);
199
200
		$liked = elgg_get_annotations(array(
201
			'guid' => $wire_post->guid,
202
			'annotation_owner_guid' => $user_entity->guid,
203
			'annotation_name' => 'likes'
204
		));
205
		$wire_post->liked = count($liked) > 0;
206
207
		$replied = elgg_get_entities_from_metadata(array(
208
			"metadata_name" => "wire_thread",
209
			"metadata_value" => $thread_id,
210
			"type" => "object",
211
			"subtype" => "thewire",
212
			"owner_guid" => $user_entity->guid
213
		));
214
		$wire_post->replied = count($replied) > 0;
215
216
		$wire_post->thread_id = $thread_id;
217
218
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
219
		$wire_post->description = wire_filter($wire_post->description);
220
221
		$wire_posts = $wire_post;
222
	}
223
224
	return $wire_posts;
225
}
226
227
function get_wireposts($user, $limit, $filters, $offset, $lang)
228
{
229
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
230
	if (!$user_entity) {
231
		return "User was not found. Please try a different GUID, username, or email address";
232
	}
233
	if (!$user_entity instanceof ElggUser) {
234
		return "Invalid user. Please try a different GUID, username, or email address";
235
	}
236
237
	if (!elgg_is_logged_in()) {
238
		login($user_entity);
239
	}
240
241
	$filter_data = json_decode($filters);
242
	if (!empty($filter_data)) {
243
		$params = array(
244
			'type' => 'object',
245
			'subtype' => 'thewire',
246
			'limit' => $limit,
247
			'offset' => $offset
248
		);
249
250
		if ($filter_data->mine) {
251
			$all_wire_posts = elgg_list_entities_from_relationship($params);
252
		} else {
253
			$all_wire_posts = elgg_list_entities_from_metadata($params);
254
		}
255
	} else {
256
		$all_wire_posts = elgg_list_entities(array(
257
			'type' => 'object',
258
			'subtype' => 'thewire',
259
			'limit' => $limit,
260
			'offset' => $offset
261
		));
262
	}
263
264
	$wire_posts = json_decode($all_wire_posts);
265
266 View Code Duplication
	foreach ($wire_posts as $wire_post) {
267
		$wire_post_obj = get_entity($wire_post->guid);
268
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
269
270
		$url = "";
271
		if (!empty($reshare)) {
272
			$url = $reshare->getURL();
273
		}
274
275
		$text = "";
276
		if (!empty($reshare->title)) {
277
			$text = $reshare->title;
278
		} elseif (!empty($reshare->name)) {
279
			$text = $reshare->name;
280
		} elseif (!empty($reshare->description)) {
281
			$text = elgg_get_excerpt($reshare->description, 140);
282
		}
283
284
		$wire_post->shareURL = $url;
285
		$wire_post->shareText = gc_explode_translation($text, $lang);
286
287
		$likes = elgg_get_annotations(array(
288
			'guid' => $wire_post->guid,
289
			'annotation_name' => 'likes'
290
		));
291
		$wire_post->likes = count($likes);
292
293
		$liked = elgg_get_annotations(array(
294
			'guid' => $wire_post->guid,
295
			'annotation_owner_guid' => $user_entity->guid,
296
			'annotation_name' => 'likes'
297
		));
298
		$wire_post->liked = count($liked) > 0;
299
300
		$replied = elgg_get_entities_from_metadata(array(
301
			"metadata_name" => "wire_thread",
302
			"metadata_value" => $wire_post->wire_thread,
303
			"type" => "object",
304
			"subtype" => "thewire",
305
			"owner_guid" => $user_entity->guid
306
		));
307
		$wire_post->replied = count($replied) > 0;
308
309
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
310
		$wire_post->description = wire_filter($wire_post->description);
311
	}
312
313
	return $wire_posts;
314
}
315
316
function get_wirepostsbycolleagues($user, $limit, $offset, $lang)
317
{
318
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
319
	if (!$user_entity) {
320
		return "User was not found. Please try a different GUID, username, or email address";
321
	}
322
	if (!$user_entity instanceof ElggUser) {
323
		return "Invalid user. Please try a different GUID, username, or email address";
324
	}
325
326
	if (!elgg_is_logged_in()) {
327
		login($user_entity);
328
	}
329
330
	$all_wire_posts = elgg_list_entities_from_relationship(array(
331
		'type' => 'object',
332
		'subtype' => 'thewire',
333
		'relationship' => 'friend',
334
		'relationship_guid' => $user_entity->guid,
335
		'relationship_join_on' => 'container_guid',
336
		'limit' => $limit,
337
		'offset' => $offset
338
	));
339
	$wire_posts = json_decode($all_wire_posts);
340
341 View Code Duplication
	foreach ($wire_posts as $wire_post) {
342
		$wire_post_obj = get_entity($wire_post->guid);
343
344
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
345
346
		$url = "";
347
		if (!empty($reshare)) {
348
			$url = $reshare->getURL();
349
		}
350
351
		$text = "";
352
		if (!empty($reshare->title)) {
353
			$text = $reshare->title;
354
		} elseif (!empty($reshare->name)) {
355
			$text = $reshare->name;
356
		} elseif (!empty($reshare->description)) {
357
			$text = elgg_get_excerpt($reshare->description, 140);
358
		}
359
360
		$wire_post->shareURL = $url;
361
		$wire_post->shareText = gc_explode_translation($text, $lang);
362
363
		$likes = elgg_get_annotations(array(
364
			'guid' => $wire_post->guid,
365
			'annotation_name' => 'likes'
366
		));
367
		$wire_post->likes = count($likes);
368
369
		$liked = elgg_get_annotations(array(
370
			'guid' => $wire_post->guid,
371
			'annotation_owner_guid' => $user_entity->guid,
372
			'annotation_name' => 'likes'
373
		));
374
		$wire_post->liked = count($liked) > 0;
375
376
		$replied = elgg_get_entities_from_metadata(array(
377
			"metadata_name" => "wire_thread",
378
			"metadata_value" => $wire_post->wire_thread,
379
			"type" => "object",
380
			"subtype" => "thewire",
381
			'owner_guid' => $user_entity->guid
382
		));
383
		$wire_post->replied = count($replied) > 0;
384
385
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
386
		$wire_post->description = wire_filter($wire_post->description);
387
	}
388
389
	return $wire_posts;
390
}
391
392
function get_wirepostsbyuser($profileemail, $user, $limit, $offset, $lang)
393
{
394
	$user_entity = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
395
	if (!$user_entity) {
396
		return "User was not found. Please try a different GUID, username, or email address";
397
	}
398
	if (!$user_entity instanceof ElggUser) {
399
		return "Invalid user. Please try a different GUID, username, or email address";
400
	}
401
402
	$viewer = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
403
	if (!$viewer) {
404
		return "Viewer user was not found. Please try a different GUID, username, or email address";
405
	}
406
	if (!$viewer instanceof ElggUser) {
407
		return "Invalid viewer user. Please try a different GUID, username, or email address";
408
	}
409
410
	if (!elgg_is_logged_in()) {
411
		login($user_entity);
412
	}
413
414
	$all_wire_posts = elgg_list_entities(array(
415
		'type' => 'object',
416
		'subtype' => 'thewire',
417
		'owner_guid' => $user_entity->guid,
418
		'limit' => $limit,
419
		'offset' => $offset
420
	));
421
	$wire_posts = json_decode($all_wire_posts);
422
423 View Code Duplication
	foreach ($wire_posts as $wire_post) {
424
		$wire_post_obj = get_entity($wire_post->guid);
425
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
426
427
		$url = "";
428
		if (!empty($reshare)) {
429
			$url = $reshare->getURL();
430
		}
431
432
		$text = "";
433
		if (!empty($reshare->title)) {
434
			$text = $reshare->title;
435
		} elseif (!empty($reshare->name)) {
436
			$text = $reshare->name;
437
		} elseif (!empty($reshare->description)) {
438
			$text = elgg_get_excerpt($reshare->description, 140);
439
		}
440
441
		$wire_post->shareURL = $url;
442
		$wire_post->shareText = gc_explode_translation($text, $lang);
443
444
		$likes = elgg_get_annotations(array(
445
			'guid' => $wire_post->guid,
446
			'annotation_name' => 'likes'
447
		));
448
		$wire_post->likes = count($likes);
449
450
		$liked = elgg_get_annotations(array(
451
			'guid' => $wire_post->guid,
452
			'annotation_owner_guid' => $viewer->guid,
453
			'annotation_name' => 'likes'
454
		));
455
		$wire_post->liked = count($liked) > 0;
456
457
		$replied = elgg_get_entities_from_metadata(array(
458
			"metadata_name" => "wire_thread",
459
			"metadata_value" => $wire_post->wire_thread,
460
			"type" => "object",
461
			"subtype" => "thewire",
462
			"owner_guid" => $viewer->guid
463
		));
464
		$wire_post->replied = count($replied) > 0;
465
466
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
467
		$wire_post->description = wire_filter($wire_post->description);
468
	}
469
470
	return $wire_posts;
471
}
472
473
function reply_wire($user, $message, $guid, $lang)
474
{
475
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
476
	if (!$user_entity) {
477
		return "User was not found. Please try a different GUID, username, or email address";
478
	}
479
	if (!$user_entity instanceof ElggUser) {
480
		return "Invalid user. Please try a different GUID, username, or email address";
481
	}
482
483
	if (trim($message) == "") {
484
		return elgg_echo("thewire:blank");
485
	}
486
487
	if (!elgg_is_logged_in()) {
488
		login($user_entity);
489
	}
490
491
	$message = utf8_encode($message);
492
493
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, $guid);
494
	if (!$new_wire) {
495
		return elgg_echo("thewire:notsaved");
496
	}
497
498
	return elgg_echo("thewire:posted");
499
}
500