Elgg  Version master
ElggGroup.php
Go to the documentation of this file.
1 <?php
2 
5 
14 class ElggGroup extends \ElggEntity {
15 
16  const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted';
17  const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only';
18 
19  use PluginSettings;
20 
24  protected function initializeAttributes() {
25  parent::initializeAttributes();
26 
27  $this->attributes['type'] = 'group';
28  $this->attributes['subtype'] = 'group';
29  }
30 
41  public function getMembers(array $options = []) {
42  $options['relationship'] = 'member';
43  $options['relationship_guid'] = $this->getGUID();
44  $options['inverse_relationship'] = true;
45  $options['type'] = 'user';
46 
48  }
49 
55  public function isPublicMembership(): bool {
56  return ($this->membership === ACCESS_PUBLIC);
57  }
58 
65  public function getContentAccessMode(): string {
66  $mode = $this->content_access_mode;
67 
68  if (!isset($mode)) {
69  if ($this->isPublicMembership()) {
70  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
71  } else {
72  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
73  }
74  }
75 
76  // only support two modes for now
77  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
78  return $mode;
79  }
80 
81  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
82  }
83 
92  public function setContentAccessMode(string $mode): void {
93  if (!$mode && $this->content_access_mode) {
94  return;
95  }
96 
97  // only support two modes for now
98  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
99  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
100  }
101 
102  $this->content_access_mode = $mode;
103  }
104 
112  public function isMember(\ElggUser $user = null): bool {
113  if ($user === null) {
114  $user = _elgg_services()->session_manager->getLoggedInUser();
115  }
116 
117  if (!$user instanceof \ElggUser) {
118  return false;
119  }
120 
121  return $user->hasRelationship($this->guid, 'member');
122  }
123 
132  public function join(\ElggUser $user, array $params = []): bool {
133  if (!$user->addRelationship($this->guid, 'member')) {
134  return false;
135  }
136 
137  $params['group'] = $this;
138  $params['user'] = $user;
139 
140  _elgg_services()->events->trigger('join', 'group', $params);
141 
142  return true;
143  }
144 
152  public function leave(\ElggUser $user): bool {
153  // event needs to be triggered while user is still member of group to have access to group acl
154  $params = [
155  'group' => $this,
156  'user' => $user,
157  ];
158  _elgg_services()->events->trigger('leave', 'group', $params);
159 
160  return $user->removeRelationship($this->guid, 'member');
161  }
162 
166  protected function prepareObject(\Elgg\Export\Entity $object) {
167  $object = parent::prepareObject($object);
168  $object->name = $this->getDisplayName();
169  $object->description = $this->description;
170  unset($object->read_access);
171  return $object;
172  }
173 
182  public function isToolEnabled(string $name): bool {
183  if (empty($name)) {
184  return false;
185  }
186 
187  $tool = $this->getTool($name);
188  if (!$tool instanceof Tool) {
189  return false;
190  }
191 
192  $md_name = $tool->mapMetadataName();
193  $setting = $this->$md_name;
194 
195  if (!isset($setting)) {
196  return $tool->isEnabledByDefault();
197  }
198 
199  return $setting == 'yes';
200  }
201 
210  public function enableTool(string $name): bool {
211  $tool = $this->getTool($name);
212  if (!$tool instanceof Tool) {
213  return false;
214  }
215 
216  $md_name = $tool->mapMetadataName();
217  $md_value = $tool->mapMetadataValue('yes');
218 
219  $this->$md_name = $md_value;
220 
221  return true;
222  }
223 
232  public function disableTool(string $name): bool {
233  $tool = $this->getTool($name);
234  if (!$tool instanceof Tool) {
235  return false;
236  }
237 
238  $md_name = $tool->mapMetadataName();
239  $md_value = $tool->mapMetadataValue('no');
240 
241  $this->$md_name = $md_value;
242 
243  return true;
244  }
245 
253  protected function getTool(string $name): ?Tool {
254  return _elgg_services()->group_tools->group($this)->get($name);
255  }
256 
264  public function canAccessContent(ElggUser $user = null): bool {
265  if (!isset($user)) {
266  $user = _elgg_services()->session_manager->getLoggedInUser();
267  }
268 
269  if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
270  if (!$user) {
271  return false;
272  }
273 
274  return $this->isMember($user) || $user->isAdmin();
275  }
276 
277  return true;
278  }
279 }
$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.
Definition: ElggEntity.php:469
join(\ElggUser $user, array $params=[])
Join a user to this group.
Definition: ElggGroup.php:132
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
getContentAccessMode()
Return the content access mode.
Definition: ElggGroup.php:65
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:16
isToolEnabled(string $name)
Checks if a tool option is enabled.
Definition: ElggGroup.php:182
$user
Definition: ban.php:7
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggGroup.php:166
getMembers(array $options=[])
Get an array of group members.
Definition: ElggGroup.php:41
$description
Definition: record.php:15
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:17
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:55
enableTool(string $name)
Enables a tool option.
Definition: ElggGroup.php:210
disableTool(string $name)
Disables a tool option.
Definition: ElggGroup.php:232
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:152
setContentAccessMode(string $mode)
Set the content access mode.
Definition: ElggGroup.php:92
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:112
getTool(string $name)
Returns the registered tool configuration.
Definition: ElggGroup.php:253
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
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:264
initializeAttributes()
{}
Definition: ElggGroup.php:24
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:306