Elgg  Version 5.1
ElggGroup.php
Go to the documentation of this file.
1 <?php
2 
5 
13 class ElggGroup extends \ElggEntity {
14 
15  const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted';
16  const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only';
17 
18  use PluginSettings;
19 
23  protected function initializeAttributes() {
24  parent::initializeAttributes();
25  $this->attributes['subtype'] = 'group';
26  }
27 
31  public function getType(): string {
32  return 'group';
33  }
34 
45  public function getMembers(array $options = []) {
46  $options['relationship'] = 'member';
47  $options['relationship_guid'] = $this->getGUID();
48  $options['inverse_relationship'] = true;
49  $options['type'] = 'user';
50 
52  }
53 
59  public function isPublicMembership(): bool {
60  return ($this->membership == ACCESS_PUBLIC);
61  }
62 
69  public function getContentAccessMode(): string {
70  $mode = $this->content_access_mode;
71 
72  if (!isset($mode)) {
73  if ($this->isPublicMembership()) {
74  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
75  } else {
76  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
77  }
78  }
79 
80  // only support two modes for now
81  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
82  return $mode;
83  }
84 
85  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
86  }
87 
96  public function setContentAccessMode(string $mode): void {
97  if (!$mode && $this->content_access_mode) {
98  return;
99  }
100 
101  // only support two modes for now
102  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
103  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
104  }
105 
106  $this->content_access_mode = $mode;
107  }
108 
116  public function isMember(\ElggUser $user = null): bool {
117  if ($user === null) {
118  $user = _elgg_services()->session_manager->getLoggedInUser();
119  }
120 
121  if (!$user instanceof \ElggUser) {
122  return false;
123  }
124 
125  return $user->hasRelationship($this->guid, 'member');
126  }
127 
136  public function join(\ElggUser $user, array $params = []): bool {
137  if (!$user->addRelationship($this->guid, 'member')) {
138  return false;
139  }
140 
141  $params['group'] = $this;
142  $params['user'] = $user;
143 
144  _elgg_services()->events->trigger('join', 'group', $params);
145 
146  return true;
147  }
148 
156  public function leave(\ElggUser $user): bool {
157  // event needs to be triggered while user is still member of group to have access to group acl
158  $params = [
159  'group' => $this,
160  'user' => $user,
161  ];
162  _elgg_services()->events->trigger('leave', 'group', $params);
163 
164  return $user->removeRelationship($this->guid, 'member');
165  }
166 
170  protected function prepareObject(\Elgg\Export\Entity $object) {
171  $object = parent::prepareObject($object);
172  $object->name = $this->getDisplayName();
173  $object->description = $this->description;
174  unset($object->read_access);
175  return $object;
176  }
177 
186  public function isToolEnabled(string $name): bool {
187  if (empty($name)) {
188  return false;
189  }
190 
191  $tool = $this->getTool($name);
192  if (!$tool instanceof Tool) {
193  return false;
194  }
195 
196  $md_name = $tool->mapMetadataName();
197  $setting = $this->$md_name;
198 
199  if (!isset($setting)) {
200  return $tool->isEnabledByDefault();
201  }
202 
203  return $setting == 'yes';
204  }
205 
214  public function enableTool(string $name): bool {
215  $tool = $this->getTool($name);
216  if (!$tool instanceof Tool) {
217  return false;
218  }
219 
220  $md_name = $tool->mapMetadataName();
221  $md_value = $tool->mapMetadataValue('yes');
222 
223  $this->$md_name = $md_value;
224 
225  return true;
226  }
227 
236  public function disableTool(string $name): bool {
237  $tool = $this->getTool($name);
238  if (!$tool instanceof Tool) {
239  return false;
240  }
241 
242  $md_name = $tool->mapMetadataName();
243  $md_value = $tool->mapMetadataValue('no');
244 
245  $this->$md_name = $md_value;
246 
247  return true;
248  }
249 
257  protected function getTool(string $name): ?Tool {
258  return _elgg_services()->group_tools->group($this)->get($name);
259  }
260 
268  public function canAccessContent(ElggUser $user = null): bool {
269  if (!isset($user)) {
270  $user = _elgg_services()->session_manager->getLoggedInUser();
271  }
272 
273  if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
274  if (!$user) {
275  return false;
276  }
277 
278  return $this->isMember($user) || $user->isAdmin();
279  }
280 
281  return true;
282  }
283 }
$params
Saves global plugin settings.
Definition: save.php:13
$mode
Configure site maintenance mode.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
getGUID()
Returns the guid.
addRelationship(int $guid_two, string $relationship)
Add a relationship between this an another entity.
Definition: ElggEntity.php:539
$options
Elgg admin footer.
Definition: footer.php:6
join(\ElggUser $user, array $params=[])
Join a user to this group.
Definition: ElggGroup.php:136
getContentAccessMode()
Return the content access mode.
Definition: ElggGroup.php:69
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:504
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
isToolEnabled(string $name)
Checks if a tool option is enabled.
Definition: ElggGroup.php:186
$user
Definition: ban.php:7
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggGroup.php:170
getMembers(array $options=[])
Get an array of group members.
Definition: ElggGroup.php:45
$description
Definition: record.php:15
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:59
enableTool(string $name)
Enables a tool option.
Definition: ElggGroup.php:214
disableTool(string $name)
Disables a tool option.
Definition: ElggGroup.php:236
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:156
setContentAccessMode(string $mode)
Set the content access mode.
Definition: ElggGroup.php:96
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:116
getTool(string $name)
Returns the registered tool configuration.
Definition: ElggGroup.php:257
getType()
{}
Definition: ElggGroup.php:31
removeRelationship(int $guid_two, string $relationship)
Remove a relationship.
Definition: ElggEntity.php:610
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
const ACCESS_PUBLIC
Definition: constants.php:12
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:268
initializeAttributes()
{}
Definition: ElggGroup.php:23
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:312