Completed
Push — api_subgroup ( a86bf1 )
by
unknown
18:35
created

group.php ➔ get_subgroup()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 70
Code Lines 40

Duplication

Lines 29
Ratio 41.43 %

Importance

Changes 0
Metric Value
cc 7
eloc 40
nc 24
nop 3
dl 29
loc 70
rs 6.8519
c 0
b 0
f 0

How to fix   Long Method   

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