Completed
Push — master ( 0ffdca...fb72ff )
by Jeroen
15:02
created

mod/groups/actions/groups/edit.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Elgg groups plugin edit action.
4
 *
5
 * If editing an existing group, only the "group_guid" must be submitted. All other form
6
 * elements may be omitted and the corresponding data will be left as is.
7
 *
8
 * @package ElggGroups
9
 */
10
11
elgg_make_sticky_form('groups');
12
13
// Get group fields
14
$input = array();
15
foreach (elgg_get_config('group') as $shortname => $valuetype) {
16
	$value = get_input($shortname);
17
18
	if ($value === null) {
19
		// only submitted fields should be updated
20
		continue;
21
	}
22
23
	$input[$shortname] = $value;
24
25
	// @todo treat profile fields as unescaped: don't filter, encode on output
26
	if (is_array($input[$shortname])) {
27
		array_walk_recursive($input[$shortname], function (&$v) {
28
			$v = elgg_html_decode($v);
29
		});
30
	} else {
31
		$input[$shortname] = elgg_html_decode($input[$shortname]);
32
	}
33
34
	if ($valuetype == 'tags') {
35
		$input[$shortname] = string_to_tag_array($input[$shortname]);
36
	}
37
}
38
39
// only set if submitted
40
$name = get_input('name', null, false);
41
if ($name !== null) {
42
	$input['name'] = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
43
}
44
45
$user = elgg_get_logged_in_user_entity();
46
47
$group_guid = (int) get_input('group_guid');
48
49
if ($group_guid) {
50
	$is_new_group = false;
51
	$group = get_entity($group_guid);
52
	if (!$group instanceof ElggGroup || !$group->canEdit()) {
53
		$error = elgg_echo('groups:cantedit');
54
		return elgg_error_response($error);
55
	}
56
} else {
57
	if (elgg_get_plugin_setting('limited_groups', 'groups') == 'yes' && !$user->isAdmin()) {
58
		$error = elgg_echo('groups:cantcreate');
59
		return elgg_error_response($error);
60
	}
61
	
62
	$container_guid = get_input('container_guid', $user->guid);
63
	$container = get_entity($container_guid);
64
	
65
	if (!$container || !$container->canWriteToContainer($user->guid, 'group')) {
66
		$error = elgg_echo('groups:cantcreate');
67
		return elgg_error_response($error);
68
	}
69
	
70
	$is_new_group = true;
71
	$group = new ElggGroup();
72
	$group->container_guid = $container->guid;
73
}
74
75
// Assume we can edit or this is a new group
76
foreach ($input as $shortname => $value) {
77
	if ($value === '' && !in_array($shortname, ['name', 'description'])) {
78
		// The group profile displays all profile fields that have a value.
79
		// We don't want to display fields with empty string value, so we
80
		// remove the metadata completely.
81
		$group->deleteMetadata($shortname);
82
		continue;
83
	}
84
85
	$group->$shortname = $value;
86
}
87
88
// Validate create
89
if (!$group->name) {
90
	return elgg_error_response(elgg_echo('groups:notitle'));
91
}
92
93
// Set group tool options (only pass along saved entities)
94
$tool_entity = !$is_new_group ? $group : null;
95
$tool_options = groups_get_group_tool_options($tool_entity);
96
if ($tool_options) {
97
	foreach ($tool_options as $group_option) {
98
		$option_toggle_name = $group_option->name . "_enable";
99
		$option_default = $group_option->default_on ? 'yes' : 'no';
100
		$value = get_input($option_toggle_name);
101
102
		// if already has option set, don't change if no submission
103
		if ($group->$option_toggle_name && $value === null) {
104
			continue;
105
		}
106
107
		$group->$option_toggle_name = $value ? $value : $option_default;
108
	}
109
}
110
111
// Group membership - should these be treated with same constants as access permissions?
112
$value = get_input('membership');
113
if ($group->membership === null || $value !== null) {
114
	$is_public_membership = ($value == ACCESS_PUBLIC);
115
	$group->membership = $is_public_membership ? ACCESS_PUBLIC : ACCESS_PRIVATE;
116
}
117
118
$group->setContentAccessMode((string)get_input('content_access_mode'));
119
120
if ($is_new_group) {
121
	$group->access_id = ACCESS_PUBLIC;
122
}
123
124
$old_owner_guid = $is_new_group ? 0 : $group->owner_guid;
125
126
$value = get_input('owner_guid');
127
$new_owner_guid = ($value === null) ? $old_owner_guid : (int)$value;
128
129
if (!$is_new_group && $new_owner_guid && $new_owner_guid != $old_owner_guid) {
130
	// verify new owner is member and old owner/admin is logged in
131
	if ($group->isMember(get_user($new_owner_guid)) && ($old_owner_guid == $user->guid || $user->isAdmin())) {
132
		$group->owner_guid = $new_owner_guid;
133
		if ($group->container_guid == $old_owner_guid) {
134
			// Even though this action defaults container_guid to the logged in user guid,
135
			// the group may have initially been created with a custom script that assigned
136
			// a different container entity. We want to make sure we preserve the original
137
			// container if it the group is not contained by the original owner.
138
			$group->container_guid = $new_owner_guid;
139
		}
140
	}
141
}
142
143
if ($is_new_group) {
144
	// if new group, we need to save so group acl gets set in event handler
145
	if (!$group->save()) {
146
		return elgg_error_response(elgg_echo('groups:save_error'));
147
	}
148
}
149
150
// Invisible group support
151
// @todo this requires save to be called to create the acl for the group. This
152
// is an odd requirement and should be removed. Either the acl creation happens
153
// in the action or the visibility moves to a plugin hook
154
if (elgg_get_plugin_setting('hidden_groups', 'groups') == 'yes') {
155
	$value = get_input('vis');
156
	if ($is_new_group || $value !== null) {
157
		$visibility = (int)$value;
158
159
		if ($visibility == ACCESS_PRIVATE) {
160
			// Make this group visible only to group members. We need to use
161
			// ACCESS_PRIVATE on the form and convert it to group_acl here
162
			// because new groups do not have acl until they have been saved once.
163
			$visibility = $group->group_acl;
164
165
			// Force all new group content to be available only to members
166
			$group->setContentAccessMode(ElggGroup::CONTENT_ACCESS_MODE_MEMBERS_ONLY);
167
		}
168
169
		$group->access_id = $visibility;
170
	}
171
}
172
173
if (!$group->save()) {
174
	return elgg_error_response(elgg_echo('groups:save_error'));
175
}
176
177
// group saved so clear sticky form
178
elgg_clear_sticky_form('groups');
179
180
// group creator needs to be member of new group and river entry created
181
if ($is_new_group) {
182
183
	// @todo this should not be necessary...
184
	elgg_set_page_owner_guid($group->guid);
185
186
	$group->join($user);
0 ignored issues
show
It seems like $user defined by elgg_get_logged_in_user_entity() on line 45 can be null; however, ElggGroup::join() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
187
	elgg_create_river_item(array(
188
		'view' => 'river/group/create',
189
		'action_type' => 'create',
190
		'subject_guid' => $user->guid,
191
		'object_guid' => $group->guid,
192
	));
193
}
194
195
$group->saveIconFromUploadedFile('icon');
196
197
$data = [
198
	'entity' => $group,
199
];
200
return elgg_ok_response($data, elgg_echo('groups:saved'), $group->getURL());
201