Elgg  Version 3.0
ElggGroup.php
Go to the documentation of this file.
1 <?php
2 
4 
12 class ElggGroup extends \ElggEntity {
13 
14  const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted';
15  const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only';
16 
20  protected function initializeAttributes() {
21  parent::initializeAttributes();
22  $this->attributes['subtype'] = 'group';
23  }
24 
28  public function getType() {
29  return 'group';
30  }
31 
36  public function getMetadata($name) {
37  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  return parent::getMetadata($name);
42  }
43 
48  public function setMetadata($name, $value, $value_type = '', $multiple = false) {
49  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  return parent::setMetadata($name, $value, $value_type, $multiple);
55  }
56 
64  public function addObjectToGroup(\ElggObject $object) {
65  $object->container_guid = $this->guid;
66  return $object->save();
67  }
68 
78  $object->container_guid = $object->owner_guid;
79  return $object->save();
80  }
81 
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 
99  }
100 
106  public function isPublicMembership() {
107  return ($this->membership == ACCESS_PUBLIC);
108  }
109 
116  public function getContentAccessMode() {
117  $mode = $this->content_access_mode;
118 
119  if (!isset($mode)) {
120  if ($this->isPublicMembership()) {
121  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
122  } else {
123  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
124  }
125  }
126 
127  // only support two modes for now
128  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
129  return $mode;
130  }
131  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
132  }
133 
142  public function setContentAccessMode($mode) {
143  if (!$mode && $this->content_access_mode) {
144  return;
145  }
146 
147  // only support two modes for now
148  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
149  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
150  }
151 
152  $this->content_access_mode = $mode;
153  }
154 
162  public function isMember(\ElggUser $user = null) {
163  if ($user == null) {
164  $user = _elgg_services()->session->getLoggedInUser();
165  }
166  if (!$user) {
167  return false;
168  }
169 
170  $result = (bool) check_entity_relationship($user->guid, 'member', $this->guid);
171 
172  $params = [
173  'user' => $user,
174  'group' => $this,
175  ];
176  return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result);
177  }
178 
187  public function join(\ElggUser $user, $params = []) {
188  $result = add_entity_relationship($user->guid, 'member', $this->guid);
189 
190  if (!$result) {
191  return false;
192  }
193 
194  $event_params = [
195  'group' => $this,
196  'user' => $user,
197  ];
198 
199  if (is_array($params)) {
200  $event_params = array_merge($params, $event_params);
201  }
202 
203  _elgg_services()->events->trigger('join', 'group', $event_params);
204 
205  return true;
206  }
207 
215  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  $params = ['group' => $this, 'user' => $user];
218  _elgg_services()->events->trigger('leave', 'group', $params);
219 
220  return remove_entity_relationship($user->guid, 'member', $this->guid);
221  }
222 
226  protected function prepareObject(\Elgg\Export\Entity $object) {
227  $object = parent::prepareObject($object);
228  $object->name = $this->getDisplayName();
229  $object->description = $this->description;
230  unset($object->read_access);
231  return $object;
232  }
233 
244  public function canComment($user_guid = 0, $default = null) {
245  return false;
246  }
247 
256  public function isToolEnabled($name) {
257  if (empty($name)) {
258  return false;
259  }
260 
261  $tool = $this->getTool($name);
262  if (!$tool instanceof Tool) {
263  return false;
264  }
265 
266  $md_name = $tool->mapMetadataName();
267  $setting = $this->$md_name;
268 
269  if (!isset($setting)) {
270  return $tool->isEnabledByDefault();
271  }
272 
273  return $setting == 'yes';
274  }
275 
284  public function enableTool($name) {
285  $tool = $this->getTool($name);
286  if (!$tool instanceof Tool) {
287  return false;
288  }
289 
290  $md_name = $tool->mapMetadataName();
291  $md_value = $tool->mapMetadataValue('yes');
292 
293  $this->$md_name = $md_value;
294 
295  return true;
296  }
297 
306  public function disableTool($name) {
307  $tool = $this->getTool($name);
308  if (!$tool instanceof Tool) {
309  return false;
310  }
311 
312  $md_name = $tool->mapMetadataName();
313  $md_value = $tool->mapMetadataValue('no');
314 
315  $this->$md_name = $md_value;
316 
317  return true;
318  }
319 
328  protected function getToolConfig($name) {
329  return $this->getTool($name);
330  }
331 
339  protected function getTool($name) {
340  return elgg()->group_tools->group($this)->get($name);
341  }
342 
350  public function canAccessContent(ElggUser $user = null) {
351  if (!isset($user)) {
352  $user = _elgg_services()->session->getLoggedInUser();
353  }
354 
355  if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
356  if (!$user) {
357  return false;
358  }
359 
360  return $this->isMember($user) || $user->isAdmin();
361  }
362 
363  return true;
364  }
365 }
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
addObjectToGroup(\ElggObject $object)
Add an to this group.
Definition: ElggGroup.php:64
$mode
Configure site maintenance mode.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
save()
Save an entity.
getGUID()
Returns the guid.
$guid
Removes an admin notice.
getTool($name)
Returns the registered tool configuration.
Definition: ElggGroup.php:339
$options
Elgg admin footer.
Definition: footer.php:6
$user_guid
Validate a user.
Definition: validate.php:6
join(\ElggUser $user, $params=[])
Join a user to this group.
Definition: ElggGroup.php:187
Configuration exception.
getContentAccessMode()
Return the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:116
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:14
$user
Definition: ban.php:7
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:841
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggGroup.php:226
getMembers(array $options=[])
Get an array of group members.
Definition: ElggGroup.php:92
$default
Definition: checkbox.php:35
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:15
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:106
canComment($user_guid=0, $default=null)
Can a user comment on this group?
Definition: ElggGroup.php:244
getMetadata($name)
Definition: ElggGroup.php:36
$value
Definition: debugging.php:7
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:215
disableTool($name)
Disables a tool option.
Definition: ElggGroup.php:306
removeObjectFromGroup(ElggObject $object)
Remove an object from this containing group and sets the container to be object&#39;s owner...
Definition: ElggGroup.php:77
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:162
isToolEnabled($name)
Checks if a tool option is enabled.
Definition: ElggGroup.php:256
getType()
{}
Definition: ElggGroup.php:28
enableTool($name)
Enables a tool option.
Definition: ElggGroup.php:284
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
const ACCESS_PUBLIC
Definition: constants.php:14
canAccessContent(ElggUser $user=null)
Check if current user can access group content based on his/her membership status and group&#39;s content...
Definition: ElggGroup.php:350
if(elgg_view_exists("plugins/{$plugin_id}/settings")) $description
Definition: full.php:179
setContentAccessMode($mode)
Set the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:142
getToolConfig($name)
Returns the registered tool configuration.
Definition: ElggGroup.php:328
var elgg
Definition: elgglib.js:4
initializeAttributes()
{}
Definition: ElggGroup.php:20
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:307
setMetadata($name, $value, $value_type= '', $multiple=false)
Definition: ElggGroup.php:48