Completed
Push — 3.0 ( a46cca...861f1c )
by Jeroen
204:32 queued 99:44
created

engine/classes/ElggGroup.php (2 issues)

1
<?php
2
3
use Elgg\Groups\Tool;
4
5
/**
6
 * A group entity, used as a container for other entities.
7
 *
8
 * @property      string $name                A short name that captures the purpose of the group
9
 * @property      string $description         A longer body of content that gives more details about the group
10
 * @property-read string $content_access_mode Content access mode for this group
11
 */
12
class ElggGroup extends \ElggEntity {
13
14
	const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted';
15
	const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only';
16
17
	/**
18
	 * {@inheritdoc}
19
	 */
20 137
	protected function initializeAttributes() {
21 137
		parent::initializeAttributes();
22 137
		$this->attributes['subtype'] = 'group';
23 137
	}
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28 137
	public function getType() {
29 137
		return 'group';
30
	}
31
	
32
	/**
33
	 * {@inheritDoc}
34
	 * @see ElggEntity::getMetadata()
35
	 */
36 112
	public function getMetadata($name) {
37 112
		if ($name === 'group_acl') {
38
			elgg_deprecated_notice("Getting 'group_acl' metadata has been deprecated, use ElggGroup::getOwnedAccessCollection('group_acl')", '3.0');
39
		}
40
		
41 112
		return parent::getMetadata($name);
42
	}
43
	
44
	/**
45
	 * {@inheritDoc}
46
	 * @see ElggEntity::setMetadata()
47
	 */
48 97
	public function setMetadata($name, $value, $value_type = '', $multiple = false) {
49 97
		if ($name === 'group_acl') {
50
			elgg_deprecated_notice("Setting 'group_acl' metadata has been deprecated, use ElggGroup::getOwnedAccessCollection('group_acl')", '3.0');
51
			return false;
52
		}
53
		
54 97
		return parent::setMetadata($name, $value, $value_type, $multiple);
55
	}
56
57
	/**
58
	 * Add an \ElggObject to this group.
59
	 *
60
	 * @param \ElggObject $object The object.
61
	 *
62
	 * @return bool
63
	 */
64
	public function addObjectToGroup(\ElggObject $object) {
65
		$object->container_guid = $this->guid;
66
		return $object->save();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object->save() also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
67
	}
68
69
	/**
70
	 * Remove an object from this containing group and sets the container to be
71
	 * object's owner
72
	 *
73
	 * @param \ElggObject $object The object.
74
	 *
75
	 * @return bool
76
	 */
77
	public function removeObjectFromGroup(ElggObject $object) {
78
		$object->container_guid = $object->owner_guid;
79
		return $object->save();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object->save() also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
80
	}
81
82
	/**
83
	 * Get an array of group members.
84
	 *
85
	 * @param array $options Options array. See elgg_get_entities
86
	 *                       for a complete list. Common ones are 'limit', 'offset',
87
	 *                       and 'count'. Options set automatically are 'relationship',
88
	 *                       'relationship_guid', 'inverse_relationship', and 'type'.
89
	 *
90
	 * @return \ElggEntity[]|int|mixed
91
	 */
92
	public function getMembers(array $options = []) {
93
		$options['relationship'] = 'member';
94
		$options['relationship_guid'] = $this->getGUID();
95
		$options['inverse_relationship'] = true;
96
		$options['type'] = 'user';
97
98
		return elgg_get_entities($options);
99
	}
100
101
	/**
102
	 * Returns whether the current group has open membership or not.
103
	 *
104
	 * @return bool
105
	 */
106 2
	public function isPublicMembership() {
107 2
		return ($this->membership == ACCESS_PUBLIC);
108
	}
109
110
	/**
111
	 * Return the content access mode used by group_gatekeeper()
112
	 *
113
	 * @return string One of CONTENT_ACCESS_MODE_* constants
114
	 * @since 1.9.0
115
	 */
116 21
	public function getContentAccessMode() {
117 21
		$mode = $this->content_access_mode;
118
119 21
		if (!isset($mode)) {
120 1
			if ($this->isPublicMembership()) {
121 1
				$mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
122
			} else {
123 1
				$mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
124
			}
125
		}
126
127
		// only support two modes for now
128 21
		if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
129 14
			return $mode;
130
		}
131 10
		return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
132
	}
133
134
	/**
135
	 * Set the content access mode used by group_gatekeeper()
136
	 *
137
	 * @param string $mode One of CONTENT_ACCESS_MODE_* constants. If empty string, mode will not be changed
138
	 *
139
	 * @return void
140
	 * @since 1.9.0
141
	 */
142 3
	public function setContentAccessMode($mode) {
143 3
		if (!$mode && $this->content_access_mode) {
144
			return;
145
		}
146
147
		// only support two modes for now
148 3
		if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
149 3
			$mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
150
		}
151
152 3
		$this->content_access_mode = $mode;
153 3
	}
154
155
	/**
156
	 * Is the given user a member of this group?
157
	 *
158
	 * @param \ElggUser $user The user. Default is logged in user.
159
	 *
160
	 * @return bool
161
	 */
162 16
	public function isMember(\ElggUser $user = null) {
163 16
		if ($user == null) {
164
			$user = _elgg_services()->session->getLoggedInUser();
165
		}
166 16
		if (!$user) {
167
			return false;
168
		}
169
170 16
		$result = (bool) check_entity_relationship($user->guid, 'member', $this->guid);
171
172
		$params = [
173 16
			'user' => $user,
174 16
			'group' => $this,
175
		];
176 16
		return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result);
177
	}
178
179
	/**
180
	 * Join a user to this group.
181
	 *
182
	 * @param \ElggUser $user   User joining the group.
183
	 * @param array     $params Additional params to pass to the 'join', 'group' event
184
	 *
185
	 * @return bool Whether joining was successful.
186
	 */
187 78
	public function join(\ElggUser $user, $params = []) {
188 78
		$result = add_entity_relationship($user->guid, 'member', $this->guid);
189
	
190 78
		if (!$result) {
191
			return false;
192
		}
193
		
194
		$event_params = [
195 78
			'group' => $this,
196 78
			'user' => $user,
197
		];
198
		
199 78
		if (is_array($params)) {
200 78
			$event_params = array_merge($params, $event_params);
201
		}
202
		
203 78
		_elgg_services()->events->trigger('join', 'group', $event_params);
204
	
205 78
		return true;
206
	}
207
208
	/**
209
	 * Remove a user from the group.
210
	 *
211
	 * @param \ElggUser $user User to remove from the group.
212
	 *
213
	 * @return bool Whether the user was removed from the group.
214
	 */
215 2
	public function leave(\ElggUser $user) {
216
		// event needs to be triggered while user is still member of group to have access to group acl
217 2
		$params = ['group' => $this, 'user' => $user];
218 2
		_elgg_services()->events->trigger('leave', 'group', $params);
219
220 2
		return remove_entity_relationship($user->guid, 'member', $this->guid);
221
	}
222
223
	/**
224
	 * {@inheritdoc}
225
	 */
226 8
	protected function prepareObject(\Elgg\Export\Entity $object) {
227 8
		$object = parent::prepareObject($object);
228 8
		$object->name = $this->getDisplayName();
229 8
		$object->description = $this->description;
230 8
		unset($object->read_access);
231 8
		return $object;
232
	}
233
234
	/**
235
	 * Can a user comment on this group?
236
	 *
237
	 * @see \ElggEntity::canComment()
238
	 *
239
	 * @param int  $user_guid User guid (default is logged in user)
240
	 * @param bool $default   Default permission
241
	 * @return bool
242
	 * @since 1.8.0
243
	 */
244 2
	public function canComment($user_guid = 0, $default = null) {
245 2
		return false;
246
	}
247
	
248
	/**
249
	 * Checks if a tool option is enabled
250
	 *
251
	 * @param string $name Tool name
252
	 *
253
	 * @return bool
254
	 * @since 3.0.0
255
	 */
256 1
	public function isToolEnabled($name) {
257 1
		if (empty($name)) {
258 1
			return false;
259
		}
260
261 1
		$tool = $this->getTool($name);
262 1
		if (!$tool instanceof Tool) {
263 1
			return false;
264
		}
265
266 1
		$md_name = $tool->mapMetadataName();
267 1
		$setting = $this->$md_name;
268
269 1
		if (!isset($setting)) {
270 1
			return $tool->isEnabledByDefault();
271
		}
272
273 1
		return $setting == 'yes';
274
	}
275
276
	/**
277
	 * Enables a tool option
278
	 *
279
	 * @param string $name The option to enable
280
	 *
281
	 * @return bool
282
	 * @since 3.0.0
283
	 */
284 2
	public function enableTool($name) {
285 2
		$tool = $this->getTool($name);
286 2
		if (!$tool instanceof Tool) {
287 1
			return false;
288
		}
289
290 2
		$md_name = $tool->mapMetadataName();
291 2
		$md_value = $tool->mapMetadataValue('yes');
292
293 2
		$this->$md_name = $md_value;
294
295 2
		return true;
296
	}
297
	
298
	/**
299
	 * Disables a tool option
300
	 *
301
	 * @param string $name The option to disable
302
	 *
303
	 * @return bool
304
	 * @since 3.0.0
305
	 */
306 2
	public function disableTool($name) {
307 2
		$tool = $this->getTool($name);
308 2
		if (!$tool instanceof Tool) {
309 1
			return false;
310
		}
311
312 2
		$md_name = $tool->mapMetadataName();
313 2
		$md_value = $tool->mapMetadataValue('no');
314
315 2
		$this->$md_name = $md_value;
316
317 2
		return true;
318
	}
319
320
	/**
321
	 * Returns the registered tool configuration
322
	 *
323
	 * @param string $name Tool name
324
	 *
325
	 * @return Tool|null
326
	 * @deprecated 3.0 Use ElggGroup::getTool
327
	 */
328
	protected function getToolConfig($name) {
329
		return $this->getTool($name);
330
	}
331
332
	/**
333
	 * Returns the registered tool configuration
334
	 *
335
	 * @param string $name Tool name
336
	 *
337
	 * @return Tool|null
338
	 */
339 2
	protected function getTool($name) {
340 2
		return elgg()->group_tools->group($this)->get($name);
341
	}
342
343
	/**
344
	 * Check if current user can access group content based on his/her membership status
345
	 * and group's content access policy
346
	 *
347
	 * @param ElggUser|null $user User
348
	 * @return bool
349
	 */
350 17
	public function canAccessContent(ElggUser $user = null) {
351 17
		if (!isset($user)) {
352 17
			$user = _elgg_services()->session->getLoggedInUser();
353
		}
354
355 17
		if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
356 11
			if (!$user) {
357
				return false;
358
			}
359
360 11
			return $this->isMember($user) || $user->isAdmin();
361
		}
362
363 6
		return true;
364
	}
365
}
366