Elgg  Version 4.x
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() {
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() {
60  return ($this->membership == ACCESS_PUBLIC);
61  }
62 
69  public function getContentAccessMode() {
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  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
85  }
86 
95  public function setContentAccessMode($mode) {
96  if (!$mode && $this->content_access_mode) {
97  return;
98  }
99 
100  // only support two modes for now
101  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
102  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
103  }
104 
105  $this->content_access_mode = $mode;
106  }
107 
115  public function isMember(\ElggUser $user = null) {
116  if ($user == null) {
117  $user = _elgg_services()->session->getLoggedInUser();
118  }
119  if (!$user) {
120  return false;
121  }
122 
123  $result = (bool) check_entity_relationship($user->guid, 'member', $this->guid);
124 
125  $params = [
126  'user' => $user,
127  'group' => $this,
128  ];
129  return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result);
130  }
131 
140  public function join(\ElggUser $user, $params = []) {
141  $result = add_entity_relationship($user->guid, 'member', $this->guid);
142 
143  if (!$result) {
144  return false;
145  }
146 
147  $event_params = [
148  'group' => $this,
149  'user' => $user,
150  ];
151 
152  if (is_array($params)) {
153  $event_params = array_merge($params, $event_params);
154  }
155 
156  _elgg_services()->events->trigger('join', 'group', $event_params);
157 
158  return true;
159  }
160 
168  public function leave(\ElggUser $user) {
169  // event needs to be triggered while user is still member of group to have access to group acl
170  $params = ['group' => $this, 'user' => $user];
171  _elgg_services()->events->trigger('leave', 'group', $params);
172 
173  return remove_entity_relationship($user->guid, 'member', $this->guid);
174  }
175 
179  protected function prepareObject(\Elgg\Export\Entity $object) {
180  $object = parent::prepareObject($object);
181  $object->name = $this->getDisplayName();
182  $object->description = $this->description;
183  unset($object->read_access);
184  return $object;
185  }
186 
195  public function isToolEnabled($name) {
196  if (empty($name)) {
197  return false;
198  }
199 
200  $tool = $this->getTool($name);
201  if (!$tool instanceof Tool) {
202  return false;
203  }
204 
205  $md_name = $tool->mapMetadataName();
206  $setting = $this->$md_name;
207 
208  if (!isset($setting)) {
209  return $tool->isEnabledByDefault();
210  }
211 
212  return $setting == 'yes';
213  }
214 
223  public function enableTool($name) {
224  $tool = $this->getTool($name);
225  if (!$tool instanceof Tool) {
226  return false;
227  }
228 
229  $md_name = $tool->mapMetadataName();
230  $md_value = $tool->mapMetadataValue('yes');
231 
232  $this->$md_name = $md_value;
233 
234  return true;
235  }
236 
245  public function disableTool($name) {
246  $tool = $this->getTool($name);
247  if (!$tool instanceof Tool) {
248  return false;
249  }
250 
251  $md_name = $tool->mapMetadataName();
252  $md_value = $tool->mapMetadataValue('no');
253 
254  $this->$md_name = $md_value;
255 
256  return true;
257  }
258 
266  protected function getTool($name) {
267  return _elgg_services()->group_tools->group($this)->get($name);
268  }
269 
277  public function canAccessContent(ElggUser $user = null) {
278  if (!isset($user)) {
279  $user = _elgg_services()->session->getLoggedInUser();
280  }
281 
282  if ($this->getContentAccessMode() == self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
283  if (!$user) {
284  return false;
285  }
286 
287  return $this->isMember($user) || $user->isAdmin();
288  }
289 
290  return true;
291  }
292 }
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$params
Saves global plugin settings.
Definition: save.php:13
$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.
getGUID()
Returns the guid.
getTool($name)
Returns the registered tool configuration.
Definition: ElggGroup.php:266
$options
Elgg admin footer.
Definition: footer.php:6
join(\ElggUser $user, $params=[])
Join a user to this group.
Definition: ElggGroup.php:140
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:545
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
$user
Definition: ban.php:7
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggGroup.php:179
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
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:168
disableTool($name)
Disables a tool option.
Definition: ElggGroup.php:245
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:115
isToolEnabled($name)
Checks if a tool option is enabled.
Definition: ElggGroup.php:195
getType()
{}
Definition: ElggGroup.php:31
enableTool($name)
Enables a tool option.
Definition: ElggGroup.php:223
_elgg_services()
Get the global service provider.
Definition: elgglib.php:777
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:277
setContentAccessMode($mode)
Set the content access mode.
Definition: ElggGroup.php:95
initializeAttributes()
{}
Definition: ElggGroup.php:23
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:318