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  $this->attributes['subtype'] = 'group';
27  }
28 
32  public function getType(): string {
33  return 'group';
34  }
35 
46  public function getMembers(array $options = []) {
47  $options['relationship'] = 'member';
48  $options['relationship_guid'] = $this->getGUID();
49  $options['inverse_relationship'] = true;
50  $options['type'] = 'user';
51 
53  }
54 
60  public function isPublicMembership(): bool {
61  return ($this->membership === ACCESS_PUBLIC);
62  }
63 
70  public function getContentAccessMode(): string {
71  $mode = $this->content_access_mode;
72 
73  if (!isset($mode)) {
74  if ($this->isPublicMembership()) {
75  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
76  } else {
77  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
78  }
79  }
80 
81  // only support two modes for now
82  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
83  return $mode;
84  }
85 
86  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
87  }
88 
97  public function setContentAccessMode(string $mode): void {
98  if (!$mode && $this->content_access_mode) {
99  return;
100  }
101 
102  // only support two modes for now
103  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
104  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
105  }
106 
107  $this->content_access_mode = $mode;
108  }
109 
117  public function isMember(\ElggUser $user = null): bool {
118  if ($user === null) {
119  $user = _elgg_services()->session_manager->getLoggedInUser();
120  }
121 
122  if (!$user instanceof \ElggUser) {
123  return false;
124  }
125 
126  return $user->hasRelationship($this->guid, 'member');
127  }
128 
137  public function join(\ElggUser $user, array $params = []): bool {
138  if (!$user->addRelationship($this->guid, 'member')) {
139  return false;
140  }
141 
142  $params['group'] = $this;
143  $params['user'] = $user;
144 
145  _elgg_services()->events->trigger('join', 'group', $params);
146 
147  return true;
148  }
149 
157  public function leave(\ElggUser $user): bool {
158  // event needs to be triggered while user is still member of group to have access to group acl
159  $params = [
160  'group' => $this,
161  'user' => $user,
162  ];
163  _elgg_services()->events->trigger('leave', 'group', $params);
164 
165  return $user->removeRelationship($this->guid, 'member');
166  }
167 
171  protected function prepareObject(\Elgg\Export\Entity $object) {
172  $object = parent::prepareObject($object);
173  $object->name = $this->getDisplayName();
174  $object->description = $this->description;
175  unset($object->read_access);
176  return $object;
177  }
178 
187  public function isToolEnabled(string $name): bool {
188  if (empty($name)) {
189  return false;
190  }
191 
192  $tool = $this->getTool($name);
193  if (!$tool instanceof Tool) {
194  return false;
195  }
196 
197  $md_name = $tool->mapMetadataName();
198  $setting = $this->$md_name;
199 
200  if (!isset($setting)) {
201  return $tool->isEnabledByDefault();
202  }
203 
204  return $setting == 'yes';
205  }
206 
215  public function enableTool(string $name): bool {
216  $tool = $this->getTool($name);
217  if (!$tool instanceof Tool) {
218  return false;
219  }
220 
221  $md_name = $tool->mapMetadataName();
222  $md_value = $tool->mapMetadataValue('yes');
223 
224  $this->$md_name = $md_value;
225 
226  return true;
227  }
228 
237  public function disableTool(string $name): bool {
238  $tool = $this->getTool($name);
239  if (!$tool instanceof Tool) {
240  return false;
241  }
242 
243  $md_name = $tool->mapMetadataName();
244  $md_value = $tool->mapMetadataValue('no');
245 
246  $this->$md_name = $md_value;
247 
248  return true;
249  }
250 
258  protected function getTool(string $name): ?Tool {
259  return _elgg_services()->group_tools->group($this)->get($name);
260  }
261 
269  public function canAccessContent(ElggUser $user = null): bool {
270  if (!isset($user)) {
271  $user = _elgg_services()->session_manager->getLoggedInUser();
272  }
273 
274  if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
275  if (!$user) {
276  return false;
277  }
278 
279  return $this->isMember($user) || $user->isAdmin();
280  }
281 
282  return true;
283  }
284 }
$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:552
join(\ElggUser $user, array $params=[])
Join a user to this group.
Definition: ElggGroup.php:137
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:70
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:187
$user
Definition: ban.php:7
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggGroup.php:171
getMembers(array $options=[])
Get an array of group members.
Definition: ElggGroup.php:46
$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:60
enableTool(string $name)
Enables a tool option.
Definition: ElggGroup.php:215
disableTool(string $name)
Disables a tool option.
Definition: ElggGroup.php:237
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:157
setContentAccessMode(string $mode)
Set the content access mode.
Definition: ElggGroup.php:97
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:117
getTool(string $name)
Returns the registered tool configuration.
Definition: ElggGroup.php:258
getType()
{}
Definition: ElggGroup.php:32
removeRelationship(int $guid_two, string $relationship)
Remove a relationship.
Definition: ElggEntity.php:623
_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:269
initializeAttributes()
{}
Definition: ElggGroup.php:24
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:324