Completed
Push — api_discussion ( e68ea7...a50bfe )
by
unknown
18:28
created

group.php ➔ get_group_discussions()   D

Complexity

Conditions 9
Paths 32

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
nc 32
nop 5
dl 0
loc 43
rs 4.909
c 0
b 0
f 0
1
<?php
2
/*
3
 * Exposes API endpoints for Group entities
4
 */
5
6
elgg_ws_expose_function(
7
	"get.group",
8
	"get_group",
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
	'Retrieves a group based on user id and group id',
15
	'POST',
16
	true,
17
	false
18
);
19
20
elgg_ws_expose_function(
21
	"get.groups",
22
	"get_groups",
23
	array(
24
		"user" => array('type' => 'string', 'required' => true),
25
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
26
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
27
		"filters" => array('type' => 'string', 'required' => false, 'default' => ""),
28
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
29
	),
30
	'Retrieves groups based on user id',
31
	'POST',
32
	true,
33
	false
34
);
35
36
elgg_ws_expose_function(
37
	"get.groupactivity",
38
	"get_group_activity",
39
	array(
40
		"user" => array('type' => 'string', 'required' => true),
41
		"guid" => array('type' => 'int', '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 group\'s activity based on user id and group id',
47
	'POST',
48
	true,
49
	false
50
);
51
52
elgg_ws_expose_function(
53
	"get.groupblogs",
54
	"get_group_blogs",
55
	array(
56
		"user" => array('type' => 'string', 'required' => true),
57
		"guid" => array('type' => 'int', '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 group\'s blogs based on user id and group id',
63
	'POST',
64
	true,
65
	false
66
);
67
68
elgg_ws_expose_function(
69
	"get.groupdiscussions",
70
	"get_group_discussions",
71
	array(
72
		"user" => array('type' => 'string', 'required' => true),
73
		"guid" => array('type' => 'int', 'required' => true),
74
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
75
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
76
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
77
	),
78
	'Retrieves a group\'s discussions based on user id and group id',
79
	'POST',
80
	true,
81
	false
82
);
83
84
elgg_ws_expose_function(
85
	"get.groupdocs",
86
	"get_group_docs",
87
	array(
88
		"user" => array('type' => 'string', 'required' => true),
89
		"guid" => array('type' => 'int', 'required' => true),
90
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
91
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
92
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
93
	),
94
	'Retrieves a group\'s docs based on user id and group id',
95
	'POST',
96
	true,
97
	false
98
);
99
100
elgg_ws_expose_function(
101
	"get.groupevents",
102
	"get_group_events",
103
	array(
104
		"user" => array('type' => 'string', 'required' => true),
105
		"guid" => array('type' => 'int', 'required' => true),
106
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
107
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
108
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
109
	),
110
	'Retrieves a group\'s events based on user id and group id',
111
	'POST',
112
	true,
113
	false
114
);
115
116
elgg_ws_expose_function(
117
	"get.groupfiles",
118
	"get_group_files",
119
	array(
120
		"user" => array('type' => 'string', 'required' => true),
121
		"guid" => array('type' => 'int', 'required' => true),
122
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
123
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
124
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
125
	),
126
	'Retrieves a group\'s files based on user id and group id',
127
	'POST',
128
	true,
129
	false
130
);
131
132
elgg_ws_expose_function(
133
	"group.members",
134
	"get_groups_members",
135
	array(
136
		"user" => array('type' => 'string', 'required' => true),
137
		"guid" => array('type' => 'int', 'required' => true),
138
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
139
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
140
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
141
	),
142
	'Retrieves a group\'s members based on user id and group id',
143
	'POST',
144
	true,
145
	false
146
);
147
148
elgg_ws_expose_function(
149
	"group.join",
150
	"join_group_function",
151
	array(
152
		"user" => array('type' => 'string', 'required' => true),
153
		"guid" => array('type' => 'int', 'required' => true),
154
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
155
	),
156
	'Joins a group based on user id and group id',
157
	'POST',
158
	true,
159
	false
160
);
161
162
elgg_ws_expose_function(
163
	"group.leave",
164
	"leave_group_function",
165
	array(
166
		"user" => array('type' => 'string', 'required' => true),
167
		"guid" => array('type' => 'int', 'required' => true),
168
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
169
	),
170
	'Leaves a group based on user id and group id',
171
	'POST',
172
	true,
173
	false
174
);
175
176
elgg_ws_expose_function(
177
	"group.invite",
178
	"invite_group_member",
179
	array(
180
		"profileemail" => array('type' => 'string', 'required' => true),
181
		"user" => array('type' => 'string', 'required' => true),
182
		"guid" => array('type' => 'int', 'required' => true),
183
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
184
	),
185
	'Invites a member to a group based on user id and group id',
186
	'POST',
187
	true,
188
	false
189
);
190
191
elgg_ws_expose_function(
192
	"group.invitemembers",
193
	"invite_group_members",
194
	array(
195
		"profileemail" => array('type' => 'string', 'required' => true),
196
		"user" => array('type' => 'string', 'required' => true),
197
		"guid" => array('type' => 'int', 'required' => true),
198
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
199
	),
200
	'Invites members to a group based on user ids and group id',
201
	'POST',
202
	true,
203
	false
204
);
205
206
elgg_ws_expose_function(
207
	"group.decline",
208
	"decline_group_invite",
209
	array(
210
		"user" => array('type' => 'string', 'required' => true),
211
		"guid" => array('type' => 'int', 'required' => true),
212
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
213
	),
214
	'Declines a group invite to a group based on user id and group id',
215
	'POST',
216
	true,
217
	false
218
);
219
220
function get_group($user, $guid, $lang)
221
{
222
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
223
	if (!$user_entity) {
224
		return "User was not found. Please try a different GUID, username, or email address";
225
	}
226
	if (!$user_entity instanceof ElggUser) {
227
		return "Invalid user. Please try a different GUID, username, or email address";
228
	}
229
230
	$ia = elgg_set_ignore_access(true);
231
	$entity = get_entity($guid);
232
	elgg_set_ignore_access($ia);
233
234
	if (!$entity) {
235
		return "Group was not found. Please try a different GUID";
236
	}
237
	if (!$entity instanceof ElggGroup) {
238
		return "Invalid group. Please try a different GUID";
239
	}
240
241
	if (!elgg_is_logged_in()) {
242
		login($user_entity);
243
	}
244
245
	$groups = elgg_list_entities(array(
246
		'type' => 'group',
247
		'guid' => $guid
248
	));
249
	$group = json_decode($groups)[0];
250
251
	$group->name = gc_explode_translation($group->name, $lang);
252
253
	$likes = elgg_get_annotations(array(
254
		'guid' => $group->guid,
255
		'annotation_name' => 'likes'
256
	));
257
	$group->likes = count($likes);
258
259
	$liked = elgg_get_annotations(array(
260
		'guid' => $group->guid,
261
		'annotation_owner_guid' => $user_entity->guid,
262
		'annotation_name' => 'likes'
263
	));
264
	$group->liked = count($liked) > 0;
265
266
	$groupObj = get_entity($group->guid);
267
	$group->member = $groupObj->isMember($user_entity);
268
	$group->owner = ($groupObj->getOwnerEntity() == $user_entity);
269
	$group->iconURL = $groupObj->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
270
	$group->count = $groupObj->getMembers(array('count' => true));
271
272
	$group->comments = get_entity_comments($group->guid);
273
	$group->tags = $groupObj->interests;
274
275
	$group->userDetails = get_user_block($group->owner_guid, $lang);
276
	$group->description = gc_explode_translation($group->description, $lang);
277
278
	return $group;
279
}
280
281
function get_groups($user, $limit, $offset, $filters, $lang)
282
{
283
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
284
	if (!$user_entity) {
285
		return "User was not found. Please try a different GUID, username, or email address";
286
	}
287
	if (!$user_entity instanceof ElggUser) {
288
		return "Invalid user. Please try a different GUID, username, or email address";
289
	}
290
291
	if (!elgg_is_logged_in()) {
292
		login($user_entity);
293
	}
294
295
	$filter_data = json_decode($filters);
296
	if (!empty($filter_data)) {
297
		$params = array(
298
			'type' => 'group',
299
			'limit' => $limit,
300
			'offset' => $offset
301
		);
302
303
		if ($filter_data->mine) {
304
			$params['relationship'] = 'member';
305
			$params['relationship_guid'] = $user_entity->guid;
306
			$params['inverse_relationship'] = false;
307
		}
308
309
		if ($filter_data->name) {
310
			$db_prefix = elgg_get_config('dbprefix');
311
			$params['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
312
			$params['wheres'] = array("(ge.name LIKE '%" . $filter_data->name . "%' OR ge.description LIKE '%" . $filter_data->name . "%')");
313
		}
314
315
		if ($filter_data->mine) {
316
			$all_groups = elgg_list_entities_from_relationship($params);
317
		} else {
318
			$all_groups = elgg_list_entities_from_metadata($params);
319
		}
320
	} else {
321
		$all_groups = elgg_list_entities(array(
322
			'type' => 'group',
323
			'limit' => $limit,
324
			'offset' => $offset
325
		));
326
	}
327
328
	$groups = json_decode($all_groups);
329
330
	foreach ($groups as $group) {
331
		$group->name = gc_explode_translation($group->name, $lang);
332
333
		$likes = elgg_get_annotations(array(
334
			'guid' => $group->guid,
335
			'annotation_name' => 'likes'
336
		));
337
		$group->likes = count($likes);
338
339
		$liked = elgg_get_annotations(array(
340
			'guid' => $group->guid,
341
			'annotation_owner_guid' => $user_entity->guid,
342
			'annotation_name' => 'likes'
343
		));
344
		$group->liked = count($liked) > 0;
345
346
		$groupObj = get_entity($group->guid);
347
		$group->member = $groupObj->isMember($user_entity);
348
		$group->owner = ($groupObj->getOwnerEntity() == $user_entity);
349
		$group->iconURL = $groupObj->geticon();
0 ignored issues
show
Deprecated Code introduced by
The method ElggEntity::getIcon() has been deprecated with message: 1.8 Use getIconURL()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
350
		$group->count = $groupObj->getMembers(array('count' => true));
351
352
		$group->comments = get_entity_comments($group->guid);
353
		$group->tags = $groupObj->interests;
354
355
		$group->userDetails = get_user_block($group->owner_guid, $lang);
356
		$group->description = gc_explode_translation($group->description, $lang);
357
	}
358
359
	return $groups;
360
}
361
362
function get_group_activity($user, $guid, $limit, $offset, $lang)
363
{
364
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
365
	if (!$user_entity) {
366
		return "User was not found. Please try a different GUID, username, or email address";
367
	}
368
	if (!$user_entity instanceof ElggUser) {
369
		return "Invalid user. Please try a different GUID, username, or email address";
370
	}
371
372
	$group = get_entity($guid);
373
	if (!$group) {
374
		return "Group was not found. Please try a different GUID";
375
	}
376
	if (!$group instanceof ElggGroup) {
377
		return "Invalid group. Please try a different GUID";
378
	}
379
380
	if (!elgg_is_logged_in()) {
381
		login($user_entity);
382
	}
383
384
	$all_activity = elgg_list_group_river(array(
385
		'limit' => $limit,
386
		'offset' => $offset,
387
		'wheres1' => array(
388
			"oe.container_guid = $group->guid",
389
		),
390
		'wheres2' => array(
391
			"te.container_guid = $group->guid",
392
		),
393
	));
394
395
	$activity = json_decode($all_activity);
396 View Code Duplication
	foreach ($activity as $event) {
397
		$subject = get_user($event->subject_guid);
398
		$object = get_entity($event->object_guid);
399
		$event->userDetails = get_user_block($event->subject_guid, $lang);
400
401
		if ($object instanceof ElggUser) {
402
			$event->object = get_user_block($event->object_guid, $lang);
403
			$event->object['type'] = 'user';
404
		} elseif ($object instanceof ElggWire) {
405
			$event->object['type'] = 'wire';
406
			$event->object['wire'] = wire_filter($object->description);
407
408
			$thread_id = $object->wire_thread;
409
			$reshare = $object->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
410
411
			$url = "";
412
			if (!empty($reshare)) {
413
				$url = $reshare->getURL();
414
			}
415
416
			$text = "";
417
			if (!empty($reshare->title)) {
418
				$text = $reshare->title;
419
			} elseif (!empty($reshare->name)) {
420
				$text = $reshare->name;
421
			} elseif (!empty($reshare->description)) {
422
				$text = elgg_get_excerpt($reshare->description, 140);
423
			}
424
425
			$event->shareURL = $url;
426
			$event->shareText = gc_explode_translation($text, $lang);
427
		} elseif ($object instanceof ElggGroup) {
428
			$event->object['type'] = 'group';
429
			$event->object['name'] = gc_explode_translation($object->name, $lang);
430
		} elseif ($object instanceof ElggDiscussionReply) {
431
			$event->object['type'] = 'discussion-reply';
432
			$original_discussion = get_entity($object->container_guid);
433
			$event->object['name'] = $original_discussion->title;
434
			$event->object['description'] = $object->description;
435
		} elseif ($object instanceof ElggFile) {
436
			$event->object['type'] = 'file';
437
			$event->object['name'] = $object->title;
438
			$event->object['description'] = $object->description;
439
		} elseif ($object instanceof ElggObject) {
440
			$event->object['type'] = 'discussion-add';
441
442
			if($object->title){
443
				if (strpos($object->title, '"en":') !== false) {
444
					$event->object['name'] = gc_explode_translation($object->title, $lang);
445
				} else {
446
					$event->object['name'] = $object->title;
447
				}
448
			} else if($object->name){
449
				if (strpos($object->name, '"en":') !== false) {
450
					$event->object['name'] = gc_explode_translation($object->name, $lang);
451
				} else {
452
					$event->object['name'] = $object->name;
453
				}
454
			}
455
456
			if (strpos($object->description, '"en":') !== false) {
457
				$event->object['description'] = gc_explode_translation($object->description, $lang);
458
			} else {
459
				$event->object['description'] = $object->description;
460
			}
461
462
			$other = get_entity($event->object_guid);
463
			$parent = get_entity($other->container_guid);
464
			if ($parent instanceof ElggGroup) {
465
				if (!isset($event->object['name'])) {
466
					$event->object['name'] = ($parent->title) ? $parent->title : $parent->name;
467
				}
468
			} else {
469
				if (!isset($event->object['name'])) {
470
					$event->object['name'] = ($parent->title) ? $parent->title : $parent->name;
471
				}
472
			}
473
		} else {
474
			//@TODO handle any unknown events
475
			if (strpos($object->title, '"en":') !== false) {
476
				$event->object['name'] = gc_explode_translation($object->title, $lang);
477
			} else {
478
				$event->object['name'] = $object->title;
479
			}
480
481
			if (strpos($object->description, '"en":') !== false) {
482
				$event->object['description'] = gc_explode_translation($object->description, $lang);
483
			} else {
484
				$event->object['description'] = $object->description;
485
			}
486
		}
487
	}
488
489
	return $activity;
490
}
491
492 View Code Duplication
function get_group_blogs($user, $guid, $limit, $offset, $lang)
493
{
494
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
495
	if (!$user_entity) {
496
		return "User was not found. Please try a different GUID, username, or email address";
497
	}
498
	if (!$user_entity instanceof ElggUser) {
499
		return "Invalid user. Please try a different GUID, username, or email address";
500
	}
501
502
	$group = get_entity($guid);
503
	if (!$group) {
504
		return "Group was not found. Please try a different GUID";
505
	}
506
	if (!$group instanceof ElggGroup) {
507
		return "Invalid group. Please try a different GUID";
508
	}
509
510
	if (!elgg_is_logged_in()) {
511
		login($user_entity);
512
	}
513
514
	$blogs = elgg_list_entities(array(
515
		'type' => 'object',
516
		'subtype' => 'blog',
517
		'container_guid' => $guid,
518
		'limit' => $limit,
519
		'offset' => $offset,
520
		'order_by' => 'e.last_action desc'
521
	));
522
523
	return json_decode($blogs);
524
}
525
526
function get_group_discussions($user, $guid, $limit, $offset, $lang)
527
{
528
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
529
	if (!$user_entity) {
530
		return "User was not found. Please try a different GUID, username, or email address";
531
	}
532
	if (!$user_entity instanceof ElggUser) {
533
		return "Invalid user. Please try a different GUID, username, or email address";
534
	}
535
536
	$group = get_entity($guid);
537
	if (!$group) {
538
		return "Group was not found. Please try a different GUID";
539
	}
540
	if (!$group instanceof ElggGroup) {
541
		return "Invalid group. Please try a different GUID";
542
	}
543
544
	if (!elgg_is_logged_in()) {
545
		login($user_entity);
546
	}
547
548
	$discussions = elgg_list_entities(array(
549
		'type' => 'object',
550
		'subtype' => 'groupforumtopic',
551
		'container_guid' => $guid,
552
		'limit' => $limit,
553
		'offset' => $offset,
554
		'order_by' => 'e.last_action desc'
555
	));
556
557
	$discussions = json_decode($discussions);
558
	foreach ($discussions as $discussion) {
559
		
560
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
561
		$discussion->title = gc_explode_translation($discussion->title, $lang);
562
		$discussion->description = gc_explode_translation($discussion->description, $lang);
563
		error_log(print_r($discussion->userDetails,true));
564
		
565
	}
566
567
	return $discussions;
568
}
569
570 View Code Duplication
function get_group_docs($user, $guid, $limit, $offset, $lang)
571
{
572
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
573
	if (!$user_entity) {
574
		return "User was not found. Please try a different GUID, username, or email address";
575
	}
576
	if (!$user_entity instanceof ElggUser) {
577
		return "Invalid user. Please try a different GUID, username, or email address";
578
	}
579
580
	$group = get_entity($guid);
581
	if (!$group) {
582
		return "Group was not found. Please try a different GUID";
583
	}
584
	if (!$group instanceof ElggGroup) {
585
		return "Invalid group. Please try a different GUID";
586
	}
587
588
	if (!elgg_is_logged_in()) {
589
		login($user_entity);
590
	}
591
592
	$docs = elgg_list_entities(array(
593
		'type' => 'object',
594
		'subtype' => 'etherpad',
595
		'container_guid' => $guid,
596
		'limit' => $limit,
597
		'offset' => $offset,
598
		'order_by' => 'e.last_action desc'
599
	));
600
601
	return json_decode($docs);
602
}
603
604 View Code Duplication
function get_group_events($user, $guid, $limit, $offset, $lang)
605
{
606
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
607
	if (!$user_entity) {
608
		return "User was not found. Please try a different GUID, username, or email address";
609
	}
610
	if (!$user_entity instanceof ElggUser) {
611
		return "Invalid user. Please try a different GUID, username, or email address";
612
	}
613
614
	$group = get_entity($guid);
615
	if (!$group) {
616
		return "Group was not found. Please try a different GUID";
617
	}
618
	if (!$group instanceof ElggGroup) {
619
		return "Invalid group. Please try a different GUID";
620
	}
621
622
	if (!elgg_is_logged_in()) {
623
		login($user_entity);
624
	}
625
626
	$events = elgg_list_entities(array(
627
		'type' => 'object',
628
		'subtype' => 'event_calendar',
629
		'container_guid' => $guid,
630
		'limit' => $limit,
631
		'offset' => $offset,
632
		'order_by' => 'e.last_action desc'
633
	));
634
635
	return json_decode($events);
636
}
637
638 View Code Duplication
function get_group_files($user, $guid, $limit, $offset, $lang)
639
{
640
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
641
	if (!$user_entity) {
642
		return "User was not found. Please try a different GUID, username, or email address";
643
	}
644
	if (!$user_entity instanceof ElggUser) {
645
		return "Invalid user. Please try a different GUID, username, or email address";
646
	}
647
648
	$group = get_entity($guid);
649
	if (!$group) {
650
		return "Group was not found. Please try a different GUID";
651
	}
652
	if (!$group instanceof ElggGroup) {
653
		return "Invalid group. Please try a different GUID";
654
	}
655
656
	if (!elgg_is_logged_in()) {
657
		login($user_entity);
658
	}
659
660
	$files = elgg_list_entities(array(
661
		'type' => 'object',
662
		'subtype' => 'file',
663
		'container_guid' => $guid,
664
		'limit' => $limit,
665
		'offset' => $offset,
666
		'order_by' => 'e.last_action desc'
667
	));
668
669
	return json_decode($files);
670
}
671
672
function get_groups_members($user, $guid, $limit, $offset, $lang)
673
{
674
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
675
	if (!$user_entity) {
676
		return "User was not found. Please try a different GUID, username, or email address";
677
	}
678
	if (!$user_entity instanceof ElggUser) {
679
		return "Invalid user. Please try a different GUID, username, or email address";
680
	}
681
682
	$group = get_entity($guid);
683
	if (!$group) {
684
		return "Group was not found. Please try a different GUID";
685
	}
686
	if (!$group instanceof ElggGroup) {
687
		return "Invalid group. Please try a different GUID";
688
	}
689
690
	if (!elgg_is_logged_in()) {
691
		login($user_entity);
692
	}
693
694
	$db_prefix = elgg_get_config('dbprefix');
695
	$members = elgg_list_entities_from_relationship(array(
696
		'type' => 'user',
697
		'limit' => $limit,
698
		'offset' => $offset,
699
		'relationship' => 'member',
700
		'relationship_guid' => $guid,
701
		'inverse_relationship' => true,
702
		'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
703
		'order_by' => 'u.name ASC'
704
	));
705
	$members = json_decode($members);
706
707
	$data = array();
708 View Code Duplication
	foreach ($members as $member) {
709
		$member_obj = get_user($member->guid);
710
		$member_data = get_user_block($member->guid, $lang);
711
712
		$about = "";
713
		if ($member_obj->description) {
714
			$about = strip_tags($member_obj->description, '<p>');
715
			$about = str_replace("<p>&nbsp;</p>", '', $about);
716
		}
717
718
		$member_data['about'] = $about;
719
		$data[] = $member_data;
720
	}
721
722
	return $data;
723
}
724
725
function join_group_function($user, $guid, $lang)
726
{
727
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
728
	if (!$user_entity) {
729
		return "User was not found. Please try a different GUID, username, or email address";
730
	}
731
	if (!$user_entity instanceof ElggUser) {
732
		return "Invalid user. Please try a different GUID, username, or email address";
733
	}
734
735
	$group = get_entity($guid);
736
	if (!$group) {
737
		return "Group was not found. Please try a different GUID";
738
	}
739
	if (!$group instanceof ElggGroup) {
740
		return "Invalid group. Please try a different GUID";
741
	}
742
743
	if (!elgg_is_logged_in()) {
744
		login($user_entity);
745
	}
746
747
	// access bypass for getting invisible group
748
	$ia = elgg_set_ignore_access(true);
749
	$group = get_entity($guid);
750
	elgg_set_ignore_access($ia);
751
752
	if ($user_entity && ($group instanceof ElggGroup)) {
753
754
		// join or request
755
		$join = false;
756 View Code Duplication
		if ($group->isPublicMembership() || $group->canEdit($user_entity->guid)) {
757
			// anyone can join public groups and admins can join any group
758
			$join = true;
759
		} else {
760
			if (check_entity_relationship($group->guid, 'invited', $user_entity->guid)) {
761
				// user has invite to closed group
762
				$join = true;
763
			}
764
		}
765
766
		if ($join) {
767
			if (groups_join_group($group, $user_entity)) {
768
769
770
				// cyu - 05/12/2016: modified to comform to the business requirements documentation
771 View Code Duplication
				if (elgg_is_active_plugin('cp_notifications')) {
772
					$user_entity = elgg_get_logged_in_user_entity();
773
					add_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $group->getGUID());
774
					add_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $group->getGUID());
775
				}
776
777
				return elgg_echo("groups:joined");
778
			} else {
779
				return elgg_echo("groups:cantjoin");
780
			}
781
		} else {
782
			add_entity_relationship($user_entity->guid, 'membership_request', $group->guid);
783
784
			$owner = $group->getOwnerEntity();
785
786
			$url = "{$CONFIG->url}groups/requests/$group->guid";
0 ignored issues
show
Bug introduced by
The variable $CONFIG does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
787
788
			$subject = elgg_echo('groups:request:subject', array(
789
				$user_entity->name,
790
				$group->name,
791
			), $owner->language);
792
793
			$body = elgg_echo('groups:request:body', array(
794
				$group->getOwnerEntity()->name,
795
				$user_entity->name,
796
				$group->name,
797
				$user_entity->getURL(),
798
				$url,
799
			), $owner->language);
800
801
			$params = [
802
				'action' => 'membership_request',
803
				'object' => $group,
804
			];
805
			
806
			// Notify group owner
807
			if (notify_user($owner->guid, $user_entity->getGUID(), $subject, $body, $params)) {
808
				return elgg_echo("groups:joinrequestmade");
809
			} else {
810
				return elgg_echo("groups:joinrequestnotmade");
811
			}
812
		}
813
	} else {
814
		return elgg_echo("groups:cantjoin");
815
	}
816
}
817
818
function leave_group_function($user, $guid, $lang)
819
{
820
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
821
	if (!$user_entity) {
822
		return "User was not found. Please try a different GUID, username, or email address";
823
	}
824
	if (!$user_entity instanceof ElggUser) {
825
		return "Invalid user. Please try a different GUID, username, or email address";
826
	}
827
828
	$group = get_entity($guid);
829
	if (!$group) {
830
		return "Group was not found. Please try a different GUID";
831
	}
832
	if (!$group instanceof ElggGroup) {
833
		return "Invalid group. Please try a different GUID";
834
	}
835
836
	if (!elgg_is_logged_in()) {
837
		login($user_entity);
838
	}
839
840
	elgg_set_page_owner_guid($group->guid);
841
842
	if ($user_entity && ($group instanceof ElggGroup)) {
843
		if ($group->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
844
			if ($group->leave($user_entity)) {
845
846
				// cyu - remove all the relationships when a user leaves a group
847
				if (elgg_is_active_plugin('cp_notifications')) {
848
849
					$group_content_arr = array('blog','bookmark','groupforumtopic','event_calendar','file',/*'hjforumtopic','hjforum',*/'photo','album','task','page','page_top','task_top','idea');
850
					$dbprefix = elgg_get_config('dbprefix');
851
852
					$query = "SELECT o.guid as content_id, o.title FROM {$dbprefix}entity_relationships r, {$dbprefix}objects_entity o, {$dbprefix}entities e, {$dbprefix}entity_subtypes es WHERE r.guid_one = {$user_entity->getGUID()} AND r.guid_two = o.guid AND o.title <> '' AND o.guid = e.guid AND e.container_guid = {$guid} AND es.id = e.subtype AND ( es.subtype = 'poll'";
853
					foreach ($group_content_arr as $grp_content_subtype)
854
						$query .= " OR es.subtype = '{$grp_content_subtype}'";
855
					$query .= " )";
856
857
					$group_contents = get_data($query);
858
859
					// unsubscribe to the group
860
					remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $guid);
861
					remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $guid);
862
					// unsubscribe to group content if not already
863
					foreach ($group_contents as $group_content) {
864
						remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $group_content->content_id);
865
						remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $group_content->content_id);
866
					}
867
868
				}
869
870
				//check if user is a group operator
871
				if(check_entity_relationship($user_entity->getGUID(), 'operator', $guid)){
872
					//remove operator rights
873
					remove_entity_relationship($user_entity->getGUID(), 'operator', $guid);
874
				}
875
876
				return elgg_echo("groups:left");
877
			} else {
878
				return elgg_echo("groups:cantleave");
879
			}
880
		} else {
881
			return elgg_echo("groups:cantleave");
882
		}
883
	} else {
884
		return elgg_echo("groups:cantleave");
885
	}
886
}
887
888
function invite_group_member($profileemail, $user, $guid, $lang)
889
{
890
	$invitee = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
891
	if (!$invitee) {
892
		return "User was not found. Please try a different GUID, username, or email address";
893
	}
894
	if (!$invitee instanceof ElggUser) {
895
		return "Invalid user. Please try a different GUID, username, or email address";
896
	}
897
898
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
899
	if (!$user_entity) {
900
		return "Viewer user was not found. Please try a different GUID, username, or email address";
901
	}
902
	if (!$user_entity instanceof ElggUser) {
903
		return "Invalid viewer user. Please try a different GUID, username, or email address";
904
	}
905
906
	$group = get_entity($guid);
907
	if (!$group) {
908
		return "Group was not found. Please try a different GUID";
909
	}
910
	if (!$group instanceof ElggGroup) {
911
		return "Invalid group. Please try a different GUID";
912
	}
913
914
	if (!elgg_is_logged_in()) {
915
		login($user_entity);
916
	}
917
918
	if (check_entity_relationship($group->guid, 'invited', $invitee->guid)) {
919
		return elgg_echo("groups:useralreadyinvited");
920
		continue;
921
	}
922
923
	if (check_entity_relationship($invitee->guid, 'member', $group->guid)) {
924
		// @todo add error message
925
		continue;
926
	}
927
928
	// Create relationship
929
	add_entity_relationship($group->guid, 'invited', $invitee->guid);
930
931
	$url = elgg_normalize_url("groups/invitations/$invitee->username");
932
933
	$subject = elgg_echo('groups:invite:subject', array(
934
		$invitee->name,
935
		$group->name
936
	), $invitee->language);
937
938
	$body = elgg_echo('groups:invite:body', array(
939
		$invitee->name,
940
		$user_entity->name,
941
		$group->name,
942
		$url,
943
	), $invitee->language);
944
	
945
	$params = [
946
		'action' => 'invite',
947
		'object' => $group,
948
	];
949
950
	// Send notification
951
	$result = notify_user($invitee->getGUID(), $group->owner_guid, $subject, $body, $params);
952
953
	if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
954
		return elgg_echo("groups:userinvited");
955
	} else {
956
		return elgg_echo("groups:usernotinvited");
957
	}
958
}
959
960
function invite_group_members($profileemail, $user, $guid, $lang)
961
{
962
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
963
	if (!$user_entity) {
964
		return "Viewer user was not found. Please try a different GUID, username, or email address";
965
	}
966
	if (!$user_entity instanceof ElggUser) {
967
		return "Invalid viewer user. Please try a different GUID, username, or email address";
968
	}
969
970
	$group = get_entity($guid);
971
	if (!$group) {
972
		return "Group was not found. Please try a different GUID";
973
	}
974
	if (!$group instanceof ElggGroup) {
975
		return "Invalid group. Please try a different GUID";
976
	}
977
978
	if (!elgg_is_logged_in()) {
979
		login($user_entity);
980
	}
981
982
	$user_guids = explode(',', $profileemail);
983
	if (count($user_guids) > 0 && elgg_instanceof($group, 'group') && $group->canEdit()) {
984
		foreach ($user_guids as $guid) {
985
			$invitee = is_numeric($guid) ? get_user($guid) : (strpos($guid, '@') !== false ? get_user_by_email($guid)[0] : get_user_by_username($guid));
986
			if (!$invitee) {
987
				continue;
988
			}
989
			if (!$invitee instanceof ElggUser) {
990
				continue;
991
			}
992
993
			if (check_entity_relationship($group->guid, 'invited', $invitee->guid)) {
994
				return elgg_echo("groups:useralreadyinvited");
995
				continue;
0 ignored issues
show
Unused Code introduced by
continue; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
996
			}
997
998
			if (check_entity_relationship($invitee->guid, 'member', $group->guid)) {
999
				// @todo add error message
1000
				continue;
1001
			}
1002
1003
			// Create relationship
1004
			add_entity_relationship($group->guid, 'invited', $invitee->guid);
1005
1006
			$url = elgg_normalize_url("groups/invitations/$invitee->username");
1007
1008
			$subject = elgg_echo('groups:invite:subject', array(
1009
				$invitee->name,
1010
				$group->name
1011
			), $invitee->language);
1012
1013
			$body = elgg_echo('groups:invite:body', array(
1014
				$invitee->name,
1015
				$user_entity->name,
1016
				$group->name,
1017
				$url,
1018
			), $invitee->language);
1019
			
1020
			$params = [
1021
				'action' => 'invite',
1022
				'object' => $group,
1023
			];
1024
1025
			// Send notification
1026
			$result = notify_user($invitee->getGUID(), $group->owner_guid, $subject, $body, $params);
1027
1028
			if ($result) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1029
				return elgg_echo("groups:userinvited");
1030
			} else {
1031
				return elgg_echo("groups:usernotinvited");
1032
			}
1033
		}
1034
	}
1035
}
1036
1037
function decline_group_invite($user, $guid, $lang)
1038
{
1039
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
1040
	if (!$user_entity) {
1041
		return "Viewer user was not found. Please try a different GUID, username, or email address";
1042
	}
1043
	if (!$user_entity instanceof ElggUser) {
1044
		return "Invalid viewer user. Please try a different GUID, username, or email address";
1045
	}
1046
1047
	$group = get_entity($guid);
1048
	if (!$group) {
1049
		return "Group was not found. Please try a different GUID";
1050
	}
1051
	if (!$group instanceof ElggGroup) {
1052
		return "Invalid group. Please try a different GUID";
1053
	}
1054
1055
	if (!elgg_is_logged_in()) {
1056
		login($user_entity);
1057
	}
1058
1059
	// invisible groups require overriding access to delete invite
1060
	$old_access = elgg_set_ignore_access(true);
1061
	$group = get_entity($guid);
1062
	elgg_set_ignore_access($old_access);
1063
1064
	// If join request made
1065 View Code Duplication
	if (check_entity_relationship($group->guid, 'invited', $user_entity->guid)) {
1066
		remove_entity_relationship($group->guid, 'invited', $user_entity->guid);
1067
		return elgg_echo("groups:invitekilled");
1068
	}
1069
1070
	return false;
1071
}
1072