Completed
Pull Request — gcconnex (#1558)
by
unknown
15:36
created

wire.php ➔ post_wire()   C

Complexity

Conditions 8
Paths 28

Size

Total Lines 25
Code Lines 14

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 14
nc 28
nop 3
dl 25
loc 25
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
	"post.wire",
70
	"post_wire",
71
	array(
72
		"user" => array('type' => 'string', 'required' => true),
73
		"message" => array('type' => 'string', 'required' => true),
74
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
75
	),
76
	'Posts a new wire post based on user id',
77
	'POST',
78
	true,
79
	false
80
);
81
82
elgg_ws_expose_function(
83
	"reply.wire",
84
	"reply_wire",
85
	array(
86
		"user" => array('type' => 'string', 'required' => true),
87
		"message" => array('type' => 'string', 'required' => true),
88
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
89
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
90
	),
91
	'Submits a reply to a wire post based on user id and wire post id',
92
	'POST',
93
	true,
94
	false
95
);
96
97
elgg_ws_expose_function(
98
	"edit.wire",
99
	"edit_wire",
100
	array(
101
		"user" => array('type' => 'string', 'required' => true),
102
		"message" => array('type' => 'string', 'required' => true),
103
		"guid" => array('type' => 'int', 'required' => false, 'default' => 0),
104
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
105
	),
106
	'Edits a wire post based on user id and wire post id',
107
	'POST',
108
	true,
109
	false
110
);
111
112
function get_wirepost($user, $guid, $thread, $lang)
113
{
114
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
115
	if (!$user_entity) {
116
		return "User was not found. Please try a different GUID, username, or email address";
117
	}
118
	if (!$user_entity instanceof ElggUser) {
119
		return "Invalid user. Please try a different GUID, username, or email address";
120
	}
121
122
	$entity = get_entity($guid);
123
	if (!$entity) {
124
		return "Wire was not found. Please try a different GUID";
125
	}
126
	if (!$entity instanceof ElggWire) {
127
		return "Invalid wire. Please try a different GUID";
128
	}
129
130
	$thread_id = $entity->wire_thread;
131
132
	if (!elgg_is_logged_in()) {
133
		login($user_entity);
134
	}
135
136
	if ($thread) {
137
		$all_wire_posts = elgg_list_entities_from_metadata(array(
138
			"metadata_name" => "wire_thread",
139
			"metadata_value" => $thread_id,
140
			"type" => "object",
141
			"subtype" => "thewire",
142
			"limit" => 0,
143
			"preload_owners" => true
144
		));
145
		$wire_posts = json_decode($all_wire_posts);
146
147 View Code Duplication
		foreach ($wire_posts as $wire_post) {
148
			$wire_post_obj = get_entity($wire_post->guid);
149
			$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
150
151
			$url = "";
152
			if (!empty($reshare)) {
153
				$url = $reshare->getURL();
154
			}
155
156
			$text = "";
157
			if (!empty($reshare->title)) {
158
				$text = $reshare->title;
159
			} elseif (!empty($reshare->name)) {
160
				$text = $reshare->name;
161
			} elseif (!empty($reshare->description)) {
162
				$text = elgg_get_excerpt($reshare->description, 140);
163
			}
164
165
			$wire_post->shareURL = $url;
166
			$wire_post->shareText = gc_explode_translation($text, $lang);
167
168
			$likes = elgg_get_annotations(array(
169
				'guid' => $wire_post->guid,
170
				'annotation_name' => 'likes'
171
			));
172
			$wire_post->likes = count($likes);
173
174
			$liked = elgg_get_annotations(array(
175
				'guid' => $wire_post->guid,
176
				'annotation_owner_guid' => $user_entity->guid,
177
				'annotation_name' => 'likes'
178
			));
179
			$wire_post->liked = count($liked) > 0;
180
181
			$replied = elgg_get_entities_from_metadata(array(
182
				"metadata_name" => "wire_thread",
183
				"metadata_value" => $thread_id,
184
				"type" => "object",
185
				"subtype" => "thewire",
186
				"owner_guid" => $user_entity->guid
187
			));
188
			$wire_post->replied = count($replied) > 0;
189
190
			$wire_post->thread_id = $thread_id;
191
192
			$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
193
			$wire_post->description = wire_filter($wire_post->description);
194
		}
195
	} else {
196
		$wire_posts = elgg_list_entities(array(
197
			"type" => "object",
198
			"subtype" => "thewire",
199
			"guid" => $guid
200
		));
201
		$wire_post = json_decode($wire_posts)[0];
202
203
		$wire_post_obj = get_entity($wire_post->guid);
204
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
205
206
		$url = "";
207
		if (!empty($reshare)) {
208
			$url = $reshare->getURL();
209
		}
210
211
		$text = "";
212
		if (!empty($reshare->title)) {
213
			$text = $reshare->title;
214
		} elseif (!empty($reshare->name)) {
215
			$text = $reshare->name;
216
		} elseif (!empty($reshare->description)) {
217
			$text = elgg_get_excerpt($reshare->description, 140);
218
		}
219
220
		$wire_post->shareURL = $url;
221
		$wire_post->shareText = gc_explode_translation($text, $lang);
222
223
		$likes = elgg_get_annotations(array(
224
			'guid' => $wire_post->guid,
225
			'annotation_name' => 'likes'
226
		));
227
		$wire_post->likes = count($likes);
228
229
		$liked = elgg_get_annotations(array(
230
			'guid' => $wire_post->guid,
231
			'annotation_owner_guid' => $user_entity->guid,
232
			'annotation_name' => 'likes'
233
		));
234
		$wire_post->liked = count($liked) > 0;
235
236
		$replied = elgg_get_entities_from_metadata(array(
237
			"metadata_name" => "wire_thread",
238
			"metadata_value" => $thread_id,
239
			"type" => "object",
240
			"subtype" => "thewire",
241
			"owner_guid" => $user_entity->guid
242
		));
243
		$wire_post->replied = count($replied) > 0;
244
245
		$wire_post->thread_id = $thread_id;
246
247
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
248
		$wire_post->description = wire_filter($wire_post->description);
249
250
		$wire_posts = $wire_post;
251
	}
252
253
	return $wire_posts;
254
}
255
256
function get_wireposts($user, $limit, $offset, $filters, $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
266
	if (!elgg_is_logged_in()) {
267
		login($user_entity);
268
	}
269
270
	$filter_data = json_decode($filters);
271
	if (!empty($filter_data)) {
272
		$params = array(
273
			'type' => 'object',
274
			'subtype' => 'thewire',
275
			'limit' => $limit,
276
			'offset' => $offset
277
		);
278
279
		if ($filter_data->mine) {
280
			$all_wire_posts = elgg_list_entities_from_relationship($params);
281
		} else {
282
			$all_wire_posts = elgg_list_entities_from_metadata($params);
283
		}
284
	} else {
285
		$all_wire_posts = elgg_list_entities(array(
286
			'type' => 'object',
287
			'subtype' => 'thewire',
288
			'limit' => $limit,
289
			'offset' => $offset
290
		));
291
	}
292
293
	$wire_posts = json_decode($all_wire_posts);
294
295 View Code Duplication
	foreach ($wire_posts as $wire_post) {
296
		$wire_post_obj = get_entity($wire_post->guid);
297
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
298
299
		$url = "";
300
		if (!empty($reshare)) {
301
			$url = $reshare->getURL();
302
		}
303
304
		$text = "";
305
		if (!empty($reshare->title)) {
306
			$text = $reshare->title;
307
		} elseif (!empty($reshare->name)) {
308
			$text = $reshare->name;
309
		} elseif (!empty($reshare->description)) {
310
			$text = elgg_get_excerpt($reshare->description, 140);
311
		}
312
313
		$wire_post->shareURL = $url;
314
		$wire_post->shareText = gc_explode_translation($text, $lang);
315
316
		$likes = elgg_get_annotations(array(
317
			'guid' => $wire_post->guid,
318
			'annotation_name' => 'likes'
319
		));
320
		$wire_post->likes = count($likes);
321
322
		$liked = elgg_get_annotations(array(
323
			'guid' => $wire_post->guid,
324
			'annotation_owner_guid' => $user_entity->guid,
325
			'annotation_name' => 'likes'
326
		));
327
		$wire_post->liked = count($liked) > 0;
328
329
		$replied = elgg_get_entities_from_metadata(array(
330
			"metadata_name" => "wire_thread",
331
			"metadata_value" => $wire_post->wire_thread,
332
			"type" => "object",
333
			"subtype" => "thewire",
334
			"owner_guid" => $user_entity->guid
335
		));
336
		$wire_post->replied = count($replied) > 0;
337
338
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
339
		$wire_post->description = wire_filter($wire_post->description);
340
	}
341
342
	return $wire_posts;
343
}
344
345
function get_wirepostsbycolleagues($user, $limit, $offset, $lang)
346
{
347
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
348
	if (!$user_entity) {
349
		return "User was not found. Please try a different GUID, username, or email address";
350
	}
351
	if (!$user_entity instanceof ElggUser) {
352
		return "Invalid user. Please try a different GUID, username, or email address";
353
	}
354
355
	if (!elgg_is_logged_in()) {
356
		login($user_entity);
357
	}
358
359
	$all_wire_posts = elgg_list_entities_from_relationship(array(
360
		'type' => 'object',
361
		'subtype' => 'thewire',
362
		'relationship' => 'friend',
363
		'relationship_guid' => $user_entity->guid,
364
		'relationship_join_on' => 'container_guid',
365
		'limit' => $limit,
366
		'offset' => $offset
367
	));
368
	$wire_posts = json_decode($all_wire_posts);
369
370 View Code Duplication
	foreach ($wire_posts as $wire_post) {
371
		$wire_post_obj = get_entity($wire_post->guid);
372
373
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
374
375
		$url = "";
376
		if (!empty($reshare)) {
377
			$url = $reshare->getURL();
378
		}
379
380
		$text = "";
381
		if (!empty($reshare->title)) {
382
			$text = $reshare->title;
383
		} elseif (!empty($reshare->name)) {
384
			$text = $reshare->name;
385
		} elseif (!empty($reshare->description)) {
386
			$text = elgg_get_excerpt($reshare->description, 140);
387
		}
388
389
		$wire_post->shareURL = $url;
390
		$wire_post->shareText = gc_explode_translation($text, $lang);
391
392
		$likes = elgg_get_annotations(array(
393
			'guid' => $wire_post->guid,
394
			'annotation_name' => 'likes'
395
		));
396
		$wire_post->likes = count($likes);
397
398
		$liked = elgg_get_annotations(array(
399
			'guid' => $wire_post->guid,
400
			'annotation_owner_guid' => $user_entity->guid,
401
			'annotation_name' => 'likes'
402
		));
403
		$wire_post->liked = count($liked) > 0;
404
405
		$replied = elgg_get_entities_from_metadata(array(
406
			"metadata_name" => "wire_thread",
407
			"metadata_value" => $wire_post->wire_thread,
408
			"type" => "object",
409
			"subtype" => "thewire",
410
			'owner_guid' => $user_entity->guid
411
		));
412
		$wire_post->replied = count($replied) > 0;
413
414
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
415
		$wire_post->description = wire_filter($wire_post->description);
416
	}
417
418
	return $wire_posts;
419
}
420
421
function get_wirepostsbyuser($profileemail, $user, $limit, $offset, $lang)
422
{
423
	$user_entity = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
424
	if (!$user_entity) {
425
		return "User was not found. Please try a different GUID, username, or email address";
426
	}
427
	if (!$user_entity instanceof ElggUser) {
428
		return "Invalid user. Please try a different GUID, username, or email address";
429
	}
430
431
	$viewer = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
432
	if (!$viewer) {
433
		return "Viewer user was not found. Please try a different GUID, username, or email address";
434
	}
435
	if (!$viewer instanceof ElggUser) {
436
		return "Invalid viewer user. Please try a different GUID, username, or email address";
437
	}
438
439
	if (!elgg_is_logged_in()) {
440
		login($user_entity);
441
	}
442
443
	$all_wire_posts = elgg_list_entities(array(
444
		'type' => 'object',
445
		'subtype' => 'thewire',
446
		'owner_guid' => $user_entity->guid,
447
		'limit' => $limit,
448
		'offset' => $offset
449
	));
450
	$wire_posts = json_decode($all_wire_posts);
451
452 View Code Duplication
	foreach ($wire_posts as $wire_post) {
453
		$wire_post_obj = get_entity($wire_post->guid);
454
		$reshare = $wire_post_obj->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
455
456
		$url = "";
457
		if (!empty($reshare)) {
458
			$url = $reshare->getURL();
459
		}
460
461
		$text = "";
462
		if (!empty($reshare->title)) {
463
			$text = $reshare->title;
464
		} elseif (!empty($reshare->name)) {
465
			$text = $reshare->name;
466
		} elseif (!empty($reshare->description)) {
467
			$text = elgg_get_excerpt($reshare->description, 140);
468
		}
469
470
		$wire_post->shareURL = $url;
471
		$wire_post->shareText = gc_explode_translation($text, $lang);
472
473
		$likes = elgg_get_annotations(array(
474
			'guid' => $wire_post->guid,
475
			'annotation_name' => 'likes'
476
		));
477
		$wire_post->likes = count($likes);
478
479
		$liked = elgg_get_annotations(array(
480
			'guid' => $wire_post->guid,
481
			'annotation_owner_guid' => $viewer->guid,
482
			'annotation_name' => 'likes'
483
		));
484
		$wire_post->liked = count($liked) > 0;
485
486
		$replied = elgg_get_entities_from_metadata(array(
487
			"metadata_name" => "wire_thread",
488
			"metadata_value" => $wire_post->wire_thread,
489
			"type" => "object",
490
			"subtype" => "thewire",
491
			"owner_guid" => $viewer->guid
492
		));
493
		$wire_post->replied = count($replied) > 0;
494
495
		$wire_post->userDetails = get_user_block($wire_post->owner_guid, $lang);
496
		$wire_post->description = wire_filter($wire_post->description);
497
	}
498
499
	return $wire_posts;
500
}
501
502 View Code Duplication
function post_wire($user, $message, $lang)
503
{
504
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
505
	if (!$user_entity) {
506
		return "User was not found. Please try a different GUID, username, or email address";
507
	}
508
	if (!$user_entity instanceof ElggUser) {
509
		return "Invalid user. Please try a different GUID, username, or email address";
510
	}
511
512
	if (trim($message) == "") {
513
		return elgg_echo("thewire:blank");
514
	}
515
516
	if (!elgg_is_logged_in()) {
517
		login($user_entity);
518
	}
519
520
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, 0);
521
	if (!$new_wire) {
522
		return elgg_echo("thewire:notsaved");
523
	}
524
525
	return elgg_echo("thewire:posted");
526
}
527
528 View Code Duplication
function reply_wire($user, $message, $guid, $lang)
529
{
530
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
531
	if (!$user_entity) {
532
		return "User was not found. Please try a different GUID, username, or email address";
533
	}
534
	if (!$user_entity instanceof ElggUser) {
535
		return "Invalid user. Please try a different GUID, username, or email address";
536
	}
537
538
	if (trim($message) == "") {
539
		return elgg_echo("thewire:blank");
540
	}
541
542
	if (!elgg_is_logged_in()) {
543
		login($user_entity);
544
	}
545
546
	$new_wire = thewire_save_post($message, $user_entity->guid, ACCESS_PUBLIC, $guid);
547
	if (!$new_wire) {
548
		return elgg_echo("thewire:notsaved");
549
	}
550
551
	return elgg_echo("thewire:posted");
552
}
553
554
function edit_wire($user, $message, $guid, $lang)
555
{
556
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
557
	if (!$user_entity) {
558
		return "User was not found. Please try a different GUID, username, or email address";
559
	}
560
	if (!$user_entity instanceof ElggUser) {
561
		return "Invalid user. Please try a different GUID, username, or email address";
562
	}
563
564
	$entity = get_entity($guid);
565
	if (!$entity) {
566
		return "Wire was not found. Please try a different GUID";
567
	}
568
	if (!$entity instanceof ElggWire) {
569
		return "Invalid wire. Please try a different GUID";
570
	}
571
572
	if (trim($message) == "") {
573
		return elgg_echo("thewire:blank");
574
	}
575
576
	if (!elgg_is_logged_in()) {
577
		login($user_entity);
578
	}
579
580
	$message = htmlspecialchars($message, ENT_NOQUOTES, 'UTF-8');
581
582
	$result = elgg_echo("thewire:notsaved");
583
	if ($entity->canEdit()) {
584
		$entity->description = $message;
585
		if ($entity->save()) {
586
			$result = elgg_echo("thewire:posted");
587
		}
588
	}
589
590
	return $result;
591
}