These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Add users to a group |
||
4 | * |
||
5 | * @package ElggGroups |
||
6 | */ |
||
7 | $logged_in_user = elgg_get_logged_in_user_entity(); |
||
8 | |||
9 | $user_guid = get_input('user_guid'); |
||
10 | if (!is_array($user_guid)) { |
||
11 | $user_guid = [$user_guid]; |
||
12 | } |
||
13 | $group_guid = get_input('group_guid'); |
||
14 | $group = get_entity($group_guid); |
||
15 | View Code Duplication | if (!($group instanceof ElggGroup) || !$group->canEdit()) { |
|
1 ignored issue
–
show
|
|||
16 | register_error(elgg_echo('actionunauthorized')); |
||
17 | forward(REFERER); |
||
18 | } |
||
19 | |||
20 | $errors = []; |
||
21 | if (sizeof($user_guid)) { |
||
22 | foreach ($user_guid as $u_guid) { |
||
23 | $user = get_user($u_guid); |
||
24 | if (empty($user)) { |
||
25 | continue; |
||
26 | } |
||
27 | |||
28 | if (!$group->isMember($user)) { |
||
29 | if (groups_join_group($group, $user)) { |
||
30 | $subject = elgg_echo('groups:welcome:subject', [$group->name], $user->language); |
||
31 | |||
32 | $body = elgg_echo('groups:welcome:body', [ |
||
33 | $user->name, |
||
34 | $group->name, |
||
35 | $group->getURL(), |
||
36 | ], $user->language); |
||
37 | |||
38 | $params = [ |
||
39 | 'action' => 'add_membership', |
||
40 | 'object' => $group, |
||
41 | 'url' => $group->getURL(), |
||
42 | ]; |
||
43 | |||
44 | // Send welcome notification to user |
||
45 | notify_user($user->getGUID(), $group->owner_guid, $subject, $body, $params); |
||
46 | |||
47 | system_message(elgg_echo('groups:addedtogroup')); |
||
48 | } else { |
||
49 | $errors[] = elgg_echo('groups:error:addedtogroup', [$user->name]); |
||
50 | } |
||
51 | } else { |
||
52 | $errors[] = elgg_echo('groups:add:alreadymember', [$user->name]); |
||
53 | |||
54 | // if an invitation is still pending clear it up, we don't need it |
||
55 | remove_entity_relationship($group->guid, 'invited', $user->guid); |
||
56 | |||
57 | // if a membership request is still pending clear it up, we don't need it |
||
58 | remove_entity_relationship($user->guid, 'membership_request', $group->guid); |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | |||
63 | if ($errors) { |
||
64 | foreach ($errors as $error) { |
||
65 | register_error($error); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | forward(REFERER); |
||
70 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.