group.php ➔ get_groups()   F
last analyzed

Complexity

Conditions 14
Paths 368

Size

Total Lines 91

Duplication

Lines 5
Ratio 5.49 %

Importance

Changes 0
Metric Value
cc 14
nc 368
nop 5
dl 5
loc 91
rs 2.863
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		"api_version" => array('type' => 'float', 'required' => false, 'default' => 0)
46
	),
47
	'Retrieves a group\'s activity based on user id and group id',
48
	'POST',
49
	true,
50
	false
51
);
52
53
elgg_ws_expose_function(
54
	"get.groupblogs",
55
	"get_group_blogs",
56
	array(
57
		"user" => array('type' => 'string', 'required' => true),
58
		"guid" => array('type' => 'int', 'required' => true),
59
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
60
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
61
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
62
	),
63
	'Retrieves a group\'s blogs based on user id and group id',
64
	'POST',
65
	true,
66
	false
67
);
68
69
elgg_ws_expose_function(
70
	"get.groupdiscussions",
71
	"get_group_discussions",
72
	array(
73
		"user" => array('type' => 'string', 'required' => true),
74
		"guid" => array('type' => 'int', 'required' => true),
75
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
76
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
77
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
78
	),
79
	'Retrieves a group\'s discussions based on user id and group id',
80
	'POST',
81
	true,
82
	false
83
);
84
85
elgg_ws_expose_function(
86
	"get.groupdocs",
87
	"get_group_docs",
88
	array(
89
		"user" => array('type' => 'string', 'required' => true),
90
		"guid" => array('type' => 'int', 'required' => true),
91
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
92
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
93
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
94
	),
95
	'Retrieves a group\'s docs based on user id and group id',
96
	'POST',
97
	true,
98
	false
99
);
100
101
elgg_ws_expose_function(
102
	"get.groupevents",
103
	"get_group_events",
104
	array(
105
		"user" => array('type' => 'string', 'required' => true),
106
		"guid" => array('type' => 'int', 'required' => true),
107
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
108
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
109
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
110
	),
111
	'Retrieves a group\'s events based on user id and group id',
112
	'POST',
113
	true,
114
	false
115
);
116
117
elgg_ws_expose_function(
118
	"get.groupfiles",
119
	"get_group_files",
120
	array(
121
		"user" => array('type' => 'string', 'required' => true),
122
		"guid" => array('type' => 'int', 'required' => true),
123
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
124
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
125
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
126
	),
127
	'Retrieves a group\'s files based on user id and group id',
128
	'POST',
129
	true,
130
	false
131
);
132
133
elgg_ws_expose_function(
134
	"group.members",
135
	"get_groups_members",
136
	array(
137
		"user" => array('type' => 'string', 'required' => true),
138
		"guid" => array('type' => 'int', 'required' => true),
139
		"limit" => array('type' => 'int', 'required' => false, 'default' => 10),
140
		"offset" => array('type' => 'int', 'required' => false, 'default' => 0),
141
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
142
	),
143
	'Retrieves a group\'s members based on user id and group id',
144
	'POST',
145
	true,
146
	false
147
);
148
149
elgg_ws_expose_function(
150
	"group.join",
151
	"join_group_function",
152
	array(
153
		"user" => array('type' => 'string', 'required' => true),
154
		"guid" => array('type' => 'int', 'required' => true),
155
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
156
	),
157
	'Joins a group based on user id and group id',
158
	'POST',
159
	true,
160
	false
161
);
162
163
elgg_ws_expose_function(
164
	"group.leave",
165
	"leave_group_function",
166
	array(
167
		"user" => array('type' => 'string', 'required' => true),
168
		"guid" => array('type' => 'int', 'required' => true),
169
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
170
	),
171
	'Leaves a group based on user id and group id',
172
	'POST',
173
	true,
174
	false
175
);
176
177
elgg_ws_expose_function(
178
	"group.invite",
179
	"invite_group_member",
180
	array(
181
		"profileemail" => array('type' => 'string', 'required' => true),
182
		"user" => array('type' => 'string', 'required' => true),
183
		"guid" => array('type' => 'int', 'required' => true),
184
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
185
	),
186
	'Invites a member to a group based on user id and group id',
187
	'POST',
188
	true,
189
	false
190
);
191
192
elgg_ws_expose_function(
193
	"group.invitemembers",
194
	"invite_group_members",
195
	array(
196
		"profileemail" => array('type' => 'string', 'required' => true),
197
		"user" => array('type' => 'string', 'required' => true),
198
		"guid" => array('type' => 'int', 'required' => true),
199
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
200
	),
201
	'Invites members to a group based on user ids and group id',
202
	'POST',
203
	true,
204
	false
205
);
206
207
elgg_ws_expose_function(
208
	"group.decline",
209
	"decline_group_invite",
210
	array(
211
		"user" => array('type' => 'string', 'required' => true),
212
		"guid" => array('type' => 'int', 'required' => true),
213
		"lang" => array('type' => 'string', 'required' => false, 'default' => "en")
214
	),
215
	'Declines a group invite to a group based on user id and group id',
216
	'POST',
217
	true,
218
	false
219
);
220
221
function get_group($user, $guid, $lang)
222
{
223
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
224
	if (!$user_entity) {
225
		return "User was not found. Please try a different GUID, username, or email address";
226
	}
227
	if (!$user_entity instanceof ElggUser) {
228
		return "Invalid user. Please try a different GUID, username, or email address";
229
	}
230
231
	$ia = elgg_set_ignore_access(true);
232
	$entity = get_entity($guid);
233
	elgg_set_ignore_access($ia);
234
235
	if (!$entity) {
236
		return "Group was not found. Please try a different GUID";
237
	}
238
	if (!$entity instanceof ElggGroup) {
239
		return "Invalid group. Please try a different GUID";
240
	}
241
242
	if (!elgg_is_logged_in()) {
243
		login($user_entity);
244
	}
245
246
	$groups = elgg_list_entities(array(
247
		'type' => 'group',
248
		'guid' => $guid
249
	));
250
	$group = json_decode($groups)[0];
251
252
	$group->name = gc_explode_translation($group->name, $lang);
253
254
	$likes = elgg_get_annotations(array(
255
		'guid' => $group->guid,
256
		'annotation_name' => 'likes'
257
	));
258
	$group->likes = count($likes);
259
260
	$liked = elgg_get_annotations(array(
261
		'guid' => $group->guid,
262
		'annotation_owner_guid' => $user_entity->guid,
263
		'annotation_name' => 'likes'
264
	));
265
	$group->liked = count($liked) > 0;
266
267
	$groupObj = get_entity($group->guid);
268
	$group->public = $groupObj->isPublicMembership();
269
	$group->member = $groupObj->isMember($user_entity);
270 View Code Duplication
	if (!$group->public && !$group->member){
271
		$group->access = false;
272
	} else {
273
		$group->access = true;
274
	}
275
	//Group 'Tools' that are enabled or not
276
	//Returning info hide anything not activitated
277
	$group->enabled = new stdClass();
278
	$group->enabled->activity = $groupObj->activity_enable;
279
	$group->enabled->bookmarks = $groupObj->bookmarks_enable;
280
	$group->enabled->file_tools_structure_management = $groupObj->file_tools_structure_management_enable;
281
	$group->enabled->etherpad = $groupObj->etherpad_enable;
282
	$group->enabled->blog = $groupObj->blog_enable;
283
	$group->enabled->forum = $groupObj->forum_enable; //discussions
284
	$group->enabled->event_calendar = $groupObj->event_calendar_enable;
285
	$group->enabled->file = $groupObj->file_enable;
286
	$group->enabled->photos = $groupObj->photos_enable; //image albums
287
	$group->enabled->tp_images = $groupObj->tp_images_enable; // group images
288
	$group->enabled->pages = $groupObj->pages_enable;
289
	$group->enabled->ideas = $groupObj->ideas_enable;
290
	$group->enabled->widget_manager = $groupObj->widget_manager_enable;
291
	$group->enabled->polls = $groupObj->polls_enable;
292
	$group->enabled->related_groups = $groupObj->related_groups_enable;
293
	$group->enabled->subgroups = $groupObj->subgroups_enable;
294
	$group->enabled->subgroups_members_create = $groupObj->subgroups_members_create_enable;
295
	// TODO - admin options / whats viewable to non-members, currently access variable can be used to block everything if they dont have access
296
297
	$group->owner = ($groupObj->getOwnerEntity() == $user_entity);
298
	$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...
299
	$group->count = $groupObj->getMembers(array('count' => true));
300
	$group->tags = $groupObj->interests;
301
	$group->userDetails = get_user_block($group->owner_guid, $lang);
302
303
	if ($group->access){
304
		$group->description = gc_explode_translation($group->description, $lang);
305
	} else {
306
		$group->description = elgg_echo("groups:access:private", $lang);
307
	}
308
	return $group;
309
}
310
311
function get_groups($user, $limit, $offset, $filters, $lang)
312
{
313
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
314
	if (!$user_entity) {
315
		return "User was not found. Please try a different GUID, username, or email address";
316
	}
317
	if (!$user_entity instanceof ElggUser) {
318
		return "Invalid user. Please try a different GUID, username, or email address";
319
	}
320
321
	if (!elgg_is_logged_in()) {
322
		login($user_entity);
323
	}
324
325
	$filter_data = json_decode($filters);
326
	if (!empty($filter_data)) {
327
		$params = array(
328
			'type' => 'group',
329
			'limit' => $limit,
330
			'offset' => $offset
331
		);
332
333
		if ($filter_data->mine) {
334
			$params['relationship'] = 'member';
335
			$params['relationship_guid'] = $user_entity->guid;
336
			$params['inverse_relationship'] = false;
337
		}
338
339
		if ($filter_data->name) {
340
			$db_prefix = elgg_get_config('dbprefix');
341
			$params['joins'] = array("JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid");
342
			$params['wheres'] = array("(ge.name LIKE '%" . $filter_data->name . "%' OR ge.description LIKE '%" . $filter_data->name . "%')");
343
		}
344
345
		if ($filter_data->mine) {
346
			$all_groups = elgg_list_entities_from_relationship($params);
347
		} else {
348
			$all_groups = elgg_list_entities_from_metadata($params);
349
		}
350
	} else {
351
		$all_groups = elgg_list_entities(array(
352
			'type' => 'group',
353
			'limit' => $limit,
354
			'offset' => $offset
355
		));
356
	}
357
358
	$groups = json_decode($all_groups);
359
360
	foreach ($groups as $group) {
361
		$group->name = gc_explode_translation($group->name, $lang);
362
363
		$likes = elgg_get_annotations(array(
364
			'guid' => $group->guid,
365
			'annotation_name' => 'likes'
366
		));
367
		$group->likes = count($likes);
368
369
		$liked = elgg_get_annotations(array(
370
			'guid' => $group->guid,
371
			'annotation_owner_guid' => $user_entity->guid,
372
			'annotation_name' => 'likes'
373
		));
374
		$group->liked = count($liked) > 0;
375
376
		$groupObj = get_entity($group->guid);
377
		$group->member = $groupObj->isMember($user_entity);
378
		$group->public = $groupObj->isPublicMembership();
379 View Code Duplication
		if (!$group->public && !$group->member){
380
			$group->access = false;
381
		} else {
382
			$group->access = true;
383
		}
384
		$group->owner = ($groupObj->getOwnerEntity() == $user_entity);
385
		$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...
386
		$group->count = $groupObj->getMembers(array('count' => true));
387
388
		$group->comments = get_entity_comments($group->guid);
389
		$group->tags = $groupObj->interests;
390
391
		$group->userDetails = get_user_block($group->owner_guid, $lang);
392
		if ($group->access){
393
			$group->description = clean_text(gc_explode_translation($group->description, $lang));
394
			$group->description = substr($group->description, 0, 250);
395
		} else {
396
			$group->description = elgg_echo("groups:access:private", $lang);
397
		}
398
	}
399
400
	return $groups;
401
}
402
403
function get_group_activity($user, $guid, $limit, $offset, $lang, $api_version)
404
{
405
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
406
	if (!$user_entity) {
407
		return "User was not found. Please try a different GUID, username, or email address";
408
	}
409
	if (!$user_entity instanceof ElggUser) {
410
		return "Invalid user. Please try a different GUID, username, or email address";
411
	}
412
	if (!elgg_is_logged_in()) {
413
		login($user_entity);
414
	}
415
416
	$group = get_entity($guid);
417
	if (!$group) {
418
		return "Group was not found. Please try a different GUID";
419
	}
420
	if (!$group instanceof ElggGroup) {
421
		return "Invalid group. Please try a different GUID";
422
	}
423
424
	$all_activity = elgg_list_group_river(array(
425
		'limit' => $limit,
426
		'offset' => $offset,
427
		'wheres1' => array(
428
			"oe.container_guid = $group->guid",
429
		),
430
		'wheres2' => array(
431
			"te.container_guid = $group->guid",
432
		),
433
	));
434
435
	$activity = json_decode($all_activity);
436
	foreach ($activity as $event) {
437
		$subject = get_user($event->subject_guid);
438
		$object = get_entity($event->object_guid);
439
		$event->userDetails = get_user_block($event->subject_guid, $lang);
440
441
		if ($object instanceof ElggUser) {
442
			$event->object = get_user_block($event->object_guid, $lang);
443
			$event->object['type'] = 'user';
444 View Code Duplication
		} elseif ($object instanceof ElggWire) {
445
			$event->object['type'] = 'wire';
446
			$event->object['wire'] = wire_filter($object->description);
447
448
			$thread_id = $object->wire_thread;
449
			$reshare = $object->getEntitiesFromRelationship(array("relationship" => "reshare", "limit" => 1))[0];
450
451
			$url = "";
452
			if (!empty($reshare)) {
453
				$url = $reshare->getURL();
454
			}
455
456
			$text = "";
457
			if (!empty($reshare->title)) {
458
				$text = $reshare->title;
459
			} elseif (!empty($reshare->name)) {
460
				$text = $reshare->name;
461
			} elseif (!empty($reshare->description)) {
462
				$text = elgg_get_excerpt($reshare->description, 140);
463
			}
464
465
			$event->shareURL = $url;
466
			$event->shareText = gc_explode_translation($text, $lang);
467
		} elseif ($object instanceof ElggGroup) {
468
			$event->object['type'] = 'group';
469
			$event->object['name'] = gc_explode_translation($object->name, $lang);
470
		} elseif ($object instanceof ElggDiscussionReply) {
471
			if ($api_version == 0.9){
472
				$event->object['type'] = 'discussion-reply';
473
				$original_discussion = get_entity($object->container_guid);
474
				$event->object['name'] = gc_explode_translation($original_discussion->title, $lang);
475
			} else {
476
				$event->object['type'] = 'discussion-reply';
477
				$original_discussion = get_entity($object->container_guid);
478
				$event->object['name'] = $original_discussion->title;
479
				$event->object['description'] = $object->description;
480
			}
481
		} elseif ($object instanceof ElggFile) {
482
			$event->object['type'] = 'file';
483
			if ($api_version == 0.9){
484
				$event->object['name'] = gc_explode_translation($object->title, $lang);
485
			} else {
486
				$event->object['name'] = $object->title;
487
				$event->object['description'] = $object->description;
488
			}
489 View Code Duplication
		} elseif ($object instanceof ElggObject) {
490
			$event->object['type'] = 'discussion-add';
491
492
			if($object->title){
493
				if (strpos($object->title, '"en":') !== false) {
494
					$event->object['name'] = gc_explode_translation($object->title, $lang);
495
				} else {
496
					$event->object['name'] = $object->title;
497
				}
498
			} else if($object->name){
499
				if (strpos($object->name, '"en":') !== false) {
500
					$event->object['name'] = gc_explode_translation($object->name, $lang);
501
				} else {
502
					$event->object['name'] = $object->name;
503
				}
504
			}
505
506
			if (strpos($object->description, '"en":') !== false) {
507
				$event->object['description'] = gc_explode_translation($object->description, $lang);
508
			} else {
509
				$event->object['description'] = $object->description;
510
			}
511
512
			$other = get_entity($event->object_guid);
513
			$parent = get_entity($other->container_guid);
514
			if ($parent instanceof ElggGroup) {
515
				if (!isset($event->object['name'])) {
516
					$event->object['name'] = ($parent->title) ? $parent->title : $parent->name;
517
				}
518
			} else {
519
				if (!isset($event->object['name'])) {
520
					$event->object['name'] = ($parent->title) ? $parent->title : $parent->name;
521
				}
522
			}
523
		} else {
524
			//@TODO handle any unknown events
525
			if (strpos($object->title, '"en":') !== false) {
526
				$event->object['name'] = gc_explode_translation($object->title, $lang);
527
			} else {
528
				$event->object['name'] = $object->title;
529
			}
530
531
			if (strpos($object->description, '"en":') !== false) {
532
				$event->object['description'] = gc_explode_translation($object->description, $lang);
533
			} else {
534
				$event->object['description'] = $object->description;
535
			}
536
		}
537
	}
538
539
	return $activity;
540
}
541
542 View Code Duplication
function get_group_blogs($user, $guid, $limit, $offset, $lang)
543
{
544
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
545
	if (!$user_entity) {
546
		return "User was not found. Please try a different GUID, username, or email address";
547
	}
548
	if (!$user_entity instanceof ElggUser) {
549
		return "Invalid user. Please try a different GUID, username, or email address";
550
	}
551
	if (!elgg_is_logged_in()) {
552
		login($user_entity);
553
	}
554
555
	$group = get_entity($guid);
556
	if (!$group) {
557
		return "Group was not found. Please try a different GUID";
558
	}
559
	if (!$group instanceof ElggGroup) {
560
		return "Invalid group. Please try a different GUID";
561
	}
562
563
	$blogs = elgg_list_entities(array(
564
		'type' => 'object',
565
		'subtype' => 'blog',
566
		'container_guid' => $guid,
567
		'limit' => $limit,
568
		'offset' => $offset,
569
		'order_by' => 'e.last_action desc'
570
	));
571
572
	return json_decode($blogs);
573
}
574
575
function get_group_discussions($user, $guid, $limit, $offset, $lang)
576
{
577
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
578
	if (!$user_entity) {
579
		return "User was not found. Please try a different GUID, username, or email address";
580
	}
581
	if (!$user_entity instanceof ElggUser) {
582
		return "Invalid user. Please try a different GUID, username, or email address";
583
	}
584
	if (!elgg_is_logged_in()) {
585
		login($user_entity);
586
	}
587
588
	$group = get_entity($guid);
589
	if (!$group) {
590
		return "Group was not found. Please try a different GUID";
591
	}
592
	if (!$group instanceof ElggGroup) {
593
		return "Invalid group. Please try a different GUID";
594
	}
595
596
	$discussions = elgg_list_entities(array(
597
		'type' => 'object',
598
		'subtype' => 'groupforumtopic',
599
		'container_guid' => $guid,
600
		'limit' => $limit,
601
		'offset' => $offset,
602
		'order_by' => 'e.last_action desc'
603
	));
604
605
	$discussions = json_decode($discussions);
606 View Code Duplication
	foreach ($discussions as $discussion) {
607
		$likes = elgg_get_annotations(array(
608
			'guid' => $discussion->guid,
609
			'annotation_name' => 'likes'
610
		));
611
		$discussion->likes = count($likes);
612
613
		$liked = elgg_get_annotations(array(
614
			'guid' => $discussion->guid,
615
			'annotation_owner_guid' => $user_entity->guid,
616
			'annotation_name' => 'likes'
617
		));
618
		$discussion->liked = count($liked) > 0;
619
		$discussion->userDetails = get_user_block($discussion->owner_guid, $lang);
620
		$discussion->title = gc_explode_translation($discussion->title, $lang);
621
		$discussion->description = gc_explode_translation($discussion->description, $lang);
622
	}
623
624
	return $discussions;
625
}
626
627 View Code Duplication
function get_group_docs($user, $guid, $limit, $offset, $lang)
628
{
629
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
630
	if (!$user_entity) {
631
		return "User was not found. Please try a different GUID, username, or email address";
632
	}
633
	if (!$user_entity instanceof ElggUser) {
634
		return "Invalid user. Please try a different GUID, username, or email address";
635
	}
636
	if (!elgg_is_logged_in()) {
637
		login($user_entity);
638
	}
639
640
	$group = get_entity($guid);
641
	if (!$group) {
642
		return "Group was not found. Please try a different GUID";
643
	}
644
	if (!$group instanceof ElggGroup) {
645
		return "Invalid group. Please try a different GUID";
646
	}
647
648
	$docs = elgg_list_entities(array(
649
		'type' => 'object',
650
		'subtype' => 'etherpad',
651
		'container_guid' => $guid,
652
		'limit' => $limit,
653
		'offset' => $offset,
654
		'order_by' => 'e.last_action desc'
655
	));
656
657
	return json_decode($docs);
658
}
659
660 View Code Duplication
function get_group_events($user, $guid, $limit, $offset, $lang)
661
{
662
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
663
	if (!$user_entity) {
664
		return "User was not found. Please try a different GUID, username, or email address";
665
	}
666
	if (!$user_entity instanceof ElggUser) {
667
		return "Invalid user. Please try a different GUID, username, or email address";
668
	}
669
	if (!elgg_is_logged_in()) {
670
		login($user_entity);
671
	}
672
673
	$group = get_entity($guid);
674
	if (!$group) {
675
		return "Group was not found. Please try a different GUID";
676
	}
677
	if (!$group instanceof ElggGroup) {
678
		return "Invalid group. Please try a different GUID";
679
	}
680
681
	$events = elgg_list_entities(array(
682
		'type' => 'object',
683
		'subtype' => 'event_calendar',
684
		'container_guid' => $guid,
685
		'limit' => $limit,
686
		'offset' => $offset,
687
		'order_by' => 'e.last_action desc'
688
	));
689
690
	return json_decode($events);
691
}
692
693 View Code Duplication
function get_group_files($user, $guid, $limit, $offset, $lang)
694
{
695
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
696
	if (!$user_entity) {
697
		return "User was not found. Please try a different GUID, username, or email address";
698
	}
699
	if (!$user_entity instanceof ElggUser) {
700
		return "Invalid user. Please try a different GUID, username, or email address";
701
	}
702
	if (!elgg_is_logged_in()) {
703
		login($user_entity);
704
	}
705
706
	$group = get_entity($guid);
707
	if (!$group) {
708
		return "Group was not found. Please try a different GUID";
709
	}
710
	if (!$group instanceof ElggGroup) {
711
		return "Invalid group. Please try a different GUID";
712
	}
713
714
	$files = elgg_list_entities(array(
715
		'type' => 'object',
716
		'subtype' => 'file',
717
		'container_guid' => $guid,
718
		'limit' => $limit,
719
		'offset' => $offset,
720
		'order_by' => 'e.last_action desc'
721
	));
722
723
	return json_decode($files);
724
}
725
726
function get_groups_members($user, $guid, $limit, $offset, $lang)
727
{
728
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
729
	if (!$user_entity) {
730
		return "User was not found. Please try a different GUID, username, or email address";
731
	}
732
	if (!$user_entity instanceof ElggUser) {
733
		return "Invalid user. Please try a different GUID, username, or email address";
734
	}
735
	if (!elgg_is_logged_in()) {
736
		login($user_entity);
737
	}
738
739
	$group = get_entity($guid);
740
	if (!$group) {
741
		return "Group was not found. Please try a different GUID";
742
	}
743
	if (!$group instanceof ElggGroup) {
744
		return "Invalid group. Please try a different GUID";
745
	}
746
747
	$db_prefix = elgg_get_config('dbprefix');
748
	$members = elgg_list_entities_from_relationship(array(
749
		'type' => 'user',
750
		'limit' => $limit,
751
		'offset' => $offset,
752
		'relationship' => 'member',
753
		'relationship_guid' => $guid,
754
		'inverse_relationship' => true,
755
		'joins' => array("JOIN {$db_prefix}users_entity u ON e.guid=u.guid"),
756
		'order_by' => 'u.name ASC'
757
	));
758
	$members = json_decode($members);
759
760
	$data = array();
761 View Code Duplication
	foreach ($members as $member) {
762
		$member_obj = get_user($member->guid);
763
		$member_data = get_user_block($member->guid, $lang);
764
765
		$about = "";
766
		if ($member_obj->description) {
767
			$about = strip_tags($member_obj->description, '<p>');
768
			$about = str_replace("<p>&nbsp;</p>", '', $about);
769
		}
770
771
		$member_data['about'] = $about;
772
		$data[] = $member_data;
773
	}
774
775
	return $data;
776
}
777
778
function join_group_function($user, $guid, $lang)
779
{
780
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
781
	if (!$user_entity) {
782
		return "User was not found. Please try a different GUID, username, or email address";
783
	}
784
	if (!$user_entity instanceof ElggUser) {
785
		return "Invalid user. Please try a different GUID, username, or email address";
786
	}
787
	if (!elgg_is_logged_in()) {
788
		login($user_entity);
789
	}
790
791
	$group = get_entity($guid);
792
	if (!$group) {
793
		return "Group was not found. Please try a different GUID";
794
	}
795
	if (!$group instanceof ElggGroup) {
796
		return "Invalid group. Please try a different GUID";
797
	}
798
799
800
801
	// access bypass for getting invisible group
802
	$ia = elgg_set_ignore_access(true);
803
	$group = get_entity($guid);
804
	elgg_set_ignore_access($ia);
805
806
	if ($user_entity && ($group instanceof ElggGroup)) {
807
808
		// join or request
809
		$join = false;
810 View Code Duplication
		if ($group->isPublicMembership() || $group->canEdit($user_entity->guid)) {
811
			// anyone can join public groups and admins can join any group
812
			$join = true;
813
		} else {
814
			if (check_entity_relationship($group->guid, 'invited', $user_entity->guid)) {
815
				// user has invite to closed group
816
				$join = true;
817
			}
818
		}
819
820
		if ($join) {
821
			if (groups_join_group($group, $user_entity)) {
822
823
824
				// cyu - 05/12/2016: modified to comform to the business requirements documentation
825 View Code Duplication
				if (elgg_is_active_plugin('cp_notifications')) {
826
					$user_entity = elgg_get_logged_in_user_entity();
827
					add_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $group->getGUID());
828
					add_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $group->getGUID());
829
				}
830
831
				return elgg_echo("groups:joined");
832
			} else {
833
				return elgg_echo("groups:cantjoin");
834
			}
835
		} else {
836
			add_entity_relationship($user_entity->guid, 'membership_request', $group->guid);
837
838
			$owner = $group->getOwnerEntity();
839
840
			$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...
841
842
			$subject = elgg_echo('groups:request:subject', array(
843
				$user_entity->name,
844
				$group->name,
845
			), $owner->language);
846
847
			$body = elgg_echo('groups:request:body', array(
848
				$group->getOwnerEntity()->name,
849
				$user_entity->name,
850
				$group->name,
851
				$user_entity->getURL(),
852
				$url,
853
			), $owner->language);
854
855
			$params = [
856
				'action' => 'membership_request',
857
				'object' => $group,
858
			];
859
860
			// Notify group owner
861
			if (notify_user($owner->guid, $user_entity->getGUID(), $subject, $body, $params)) {
862
				return elgg_echo("groups:joinrequestmade");
863
			} else {
864
				return elgg_echo("groups:joinrequestnotmade");
865
			}
866
		}
867
	} else {
868
		return elgg_echo("groups:cantjoin");
869
	}
870
}
871
872
function leave_group_function($user, $guid, $lang)
873
{
874
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
875
	if (!$user_entity) {
876
		return "User was not found. Please try a different GUID, username, or email address";
877
	}
878
	if (!$user_entity instanceof ElggUser) {
879
		return "Invalid user. Please try a different GUID, username, or email address";
880
	}
881
	if (!elgg_is_logged_in()) {
882
		login($user_entity);
883
	}
884
885
	$group = get_entity($guid);
886
	if (!$group) {
887
		return "Group was not found. Please try a different GUID";
888
	}
889
	if (!$group instanceof ElggGroup) {
890
		return "Invalid group. Please try a different GUID";
891
	}
892
893
	elgg_set_page_owner_guid($group->guid);
894
895
	if ($user_entity && ($group instanceof ElggGroup)) {
896
		if ($group->getOwnerGUID() != elgg_get_logged_in_user_guid()) {
897
			if ($group->leave($user_entity)) {
898
899
				// cyu - remove all the relationships when a user leaves a group
900
				if (elgg_is_active_plugin('cp_notifications')) {
901
902
					$group_content_arr = array('blog','bookmark','groupforumtopic','event_calendar','file',/*'hjforumtopic','hjforum',*/'photo','album','task','page','page_top','task_top','idea');
903
					$dbprefix = elgg_get_config('dbprefix');
904
905
					$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'";
906
					foreach ($group_content_arr as $grp_content_subtype)
907
						$query .= " OR es.subtype = '{$grp_content_subtype}'";
908
					$query .= " )";
909
910
					$group_contents = get_data($query);
911
912
					// unsubscribe to the group
913
					remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $guid);
914
					remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $guid);
915
					// unsubscribe to group content if not already
916
					foreach ($group_contents as $group_content) {
917
						remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_email', $group_content->content_id);
918
						remove_entity_relationship($user_entity->getGUID(), 'cp_subscribed_to_site_mail', $group_content->content_id);
919
					}
920
921
				}
922
923
				//check if user is a group operator
924
				if(check_entity_relationship($user_entity->getGUID(), 'operator', $guid)){
925
					//remove operator rights
926
					remove_entity_relationship($user_entity->getGUID(), 'operator', $guid);
927
				}
928
929
				return elgg_echo("groups:left");
930
			} else {
931
				return elgg_echo("groups:cantleave");
932
			}
933
		} else {
934
			return elgg_echo("groups:cantleave");
935
		}
936
	} else {
937
		return elgg_echo("groups:cantleave");
938
	}
939
}
940
941
function invite_group_member($profileemail, $user, $guid, $lang)
942
{
943
	$invitee = is_numeric($profileemail) ? get_user($profileemail) : (strpos($profileemail, '@') !== false ? get_user_by_email($profileemail)[0] : get_user_by_username($profileemail));
944
	if (!$invitee) {
945
		return "User was not found. Please try a different GUID, username, or email address";
946
	}
947
	if (!$invitee instanceof ElggUser) {
948
		return "Invalid user. Please try a different GUID, username, or email address";
949
	}
950
951
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
952
	if (!$user_entity) {
953
		return "Viewer user was not found. Please try a different GUID, username, or email address";
954
	}
955
	if (!$user_entity instanceof ElggUser) {
956
		return "Invalid viewer user. Please try a different GUID, username, or email address";
957
	}
958
	if (!elgg_is_logged_in()) {
959
		login($user_entity);
960
	}
961
962
	$group = get_entity($guid);
963
	if (!$group) {
964
		return "Group was not found. Please try a different GUID";
965
	}
966
	if (!$group instanceof ElggGroup) {
967
		return "Invalid group. Please try a different GUID";
968
	}
969
970
	if (check_entity_relationship($group->guid, 'invited', $invitee->guid)) {
971
		return elgg_echo("groups:useralreadyinvited");
972
		continue;
973
	}
974
975
	if (check_entity_relationship($invitee->guid, 'member', $group->guid)) {
976
		// @todo add error message
977
		continue;
978
	}
979
980
	// Create relationship
981
	add_entity_relationship($group->guid, 'invited', $invitee->guid);
982
983
	$url = elgg_normalize_url("groups/invitations/$invitee->username");
984
985
	$subject = elgg_echo('groups:invite:subject', array(
986
		$invitee->name,
987
		$group->name
988
	), $invitee->language);
989
990
	$body = elgg_echo('groups:invite:body', array(
991
		$invitee->name,
992
		$user_entity->name,
993
		$group->name,
994
		$url,
995
	), $invitee->language);
996
997
	$params = [
998
		'action' => 'invite',
999
		'object' => $group,
1000
	];
1001
1002
	// Send notification
1003
	$result = notify_user($invitee->getGUID(), $group->owner_guid, $subject, $body, $params);
1004
1005
	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...
1006
		return elgg_echo("groups:userinvited");
1007
	} else {
1008
		return elgg_echo("groups:usernotinvited");
1009
	}
1010
}
1011
1012
function invite_group_members($profileemail, $user, $guid, $lang)
1013
{
1014
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
1015
	if (!$user_entity) {
1016
		return "Viewer user was not found. Please try a different GUID, username, or email address";
1017
	}
1018
	if (!$user_entity instanceof ElggUser) {
1019
		return "Invalid viewer user. Please try a different GUID, username, or email address";
1020
	}
1021
	if (!elgg_is_logged_in()) {
1022
		login($user_entity);
1023
	}
1024
1025
	$group = get_entity($guid);
1026
	if (!$group) {
1027
		return "Group was not found. Please try a different GUID";
1028
	}
1029
	if (!$group instanceof ElggGroup) {
1030
		return "Invalid group. Please try a different GUID";
1031
	}
1032
1033
	$user_guids = explode(',', $profileemail);
1034
	if (count($user_guids) > 0 && elgg_instanceof($group, 'group') && $group->canEdit()) {
1035
		foreach ($user_guids as $guid) {
1036
			$invitee = is_numeric($guid) ? get_user($guid) : (strpos($guid, '@') !== false ? get_user_by_email($guid)[0] : get_user_by_username($guid));
1037
			if (!$invitee) {
1038
				continue;
1039
			}
1040
			if (!$invitee instanceof ElggUser) {
1041
				continue;
1042
			}
1043
1044
			if (check_entity_relationship($group->guid, 'invited', $invitee->guid)) {
1045
				return elgg_echo("groups:useralreadyinvited");
1046
				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...
1047
			}
1048
1049
			if (check_entity_relationship($invitee->guid, 'member', $group->guid)) {
1050
				// @todo add error message
1051
				continue;
1052
			}
1053
1054
			// Create relationship
1055
			add_entity_relationship($group->guid, 'invited', $invitee->guid);
1056
1057
			$url = elgg_normalize_url("groups/invitations/$invitee->username");
1058
1059
			$subject = elgg_echo('groups:invite:subject', array(
1060
				$invitee->name,
1061
				$group->name
1062
			), $invitee->language);
1063
1064
			$body = elgg_echo('groups:invite:body', array(
1065
				$invitee->name,
1066
				$user_entity->name,
1067
				$group->name,
1068
				$url,
1069
			), $invitee->language);
1070
1071
			$params = [
1072
				'action' => 'invite',
1073
				'object' => $group,
1074
			];
1075
1076
			// Send notification
1077
			$result = notify_user($invitee->getGUID(), $group->owner_guid, $subject, $body, $params);
1078
1079
			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...
1080
				return elgg_echo("groups:userinvited");
1081
			} else {
1082
				return elgg_echo("groups:usernotinvited");
1083
			}
1084
		}
1085
	}
1086
}
1087
1088
function decline_group_invite($user, $guid, $lang)
1089
{
1090
	$user_entity = is_numeric($user) ? get_user($user) : (strpos($user, '@') !== false ? get_user_by_email($user)[0] : get_user_by_username($user));
1091
	if (!$user_entity) {
1092
		return "Viewer user was not found. Please try a different GUID, username, or email address";
1093
	}
1094
	if (!$user_entity instanceof ElggUser) {
1095
		return "Invalid viewer user. Please try a different GUID, username, or email address";
1096
	}
1097
	if (!elgg_is_logged_in()) {
1098
		login($user_entity);
1099
	}
1100
1101
	$group = get_entity($guid);
1102
	if (!$group) {
1103
		return "Group was not found. Please try a different GUID";
1104
	}
1105
	if (!$group instanceof ElggGroup) {
1106
		return "Invalid group. Please try a different GUID";
1107
	}
1108
1109
	// invisible groups require overriding access to delete invite
1110
	$old_access = elgg_set_ignore_access(true);
1111
	$group = get_entity($guid);
1112
	elgg_set_ignore_access($old_access);
1113
1114
	// If join request made
1115 View Code Duplication
	if (check_entity_relationship($group->guid, 'invited', $user_entity->guid)) {
1116
		remove_entity_relationship($group->guid, 'invited', $user_entity->guid);
1117
		return elgg_echo("groups:invitekilled");
1118
	}
1119
1120
	return false;
1121
}
1122