Elgg  Version master
ElggAccessCollection.php
Go to the documentation of this file.
1 <?php
2 
12 
18  public function __construct(stdClass $row = null) {
19  $this->initializeAttributes();
20 
21  foreach ((array) $row as $key => $value) {
22  $this->attributes[$key] = $value;
23  }
24  }
25 
32  protected function initializeAttributes(): void {
33  parent::initializeAttributes();
34 
35  $this->attributes['id'] = null;
36  $this->attributes['owner_guid'] = _elgg_services()->session_manager->getLoggedInUserGuid();
37  $this->attributes['name'] = null;
38  $this->attributes['subtype'] = null;
39  }
40 
50  public function __set($name, $value): void {
51  switch ($name) {
52  case 'id':
53  $value = (int) $value;
54  break;
55  case 'subtype':
56  if (isset($value)) {
57  $value = trim((string) $value);
58  if (strlen($value) > 255) {
59  throw new \Elgg\Exceptions\LengthException('The "subtype" of an ElggAccessCollection cannot be greated than 255 characters');
60  }
61  }
62  break;
63  case 'name':
64  $value = trim((string) $value);
65  if (empty($value)) {
66  throw new \Elgg\Exceptions\LengthException('The "name" of an ElggAccessCollection cannot be empty');
67  }
68  break;
69  case 'owner_guid':
70  $value = (int) $value;
71  if (!_elgg_services()->entityTable->exists($value)) {
72  throw new \Elgg\Exceptions\InvalidArgumentException("The 'owner_guid' ({$value}) for the ElggAccessColelction doesn't seem to exists");
73  }
74  break;
75  }
76 
77  $this->attributes[$name] = $value;
78  }
79 
87  public function __get($name) {
88  return $this->attributes[$name] ?? null;
89  }
90 
96  public function getOwnerEntity(): ?\ElggEntity {
97  return _elgg_services()->entityTable->get($this->owner_guid);
98  }
99 
105  public function getDisplayName(): string {
106 
107  $filter = function($name = null) {
108  if (!isset($name)) {
109  $name = _elgg_services()->translator->translate('access:limited:label');
110  }
111 
112  $params = [
113  'access_collection' => $this,
114  ];
115  return (string) _elgg_services()->events->triggerResults('access_collection:name', $this->getType(), $params, $name);
116  };
117 
118  $user = _elgg_services()->session_manager->getLoggedInUser();
119  $owner = $this->getOwnerEntity();
120  if (!$user || !$owner) {
121  // User is not logged in or does not access to the owner entity:
122  // return default 'Limited' label
123  return $filter();
124  }
125 
126  if ($user->isAdmin() || $owner->guid == $user->guid) {
127  return $filter($this->name);
128  }
129 
130  return $filter();
131  }
132 
136  public function save(): bool {
137  if ($this->id > 0) {
138  return _elgg_services()->accessCollections->update($this);
139  }
140 
141  return _elgg_services()->accessCollections->create($this);
142  }
143 
147  public function delete(): bool {
148  return _elgg_services()->accessCollections->delete($this);
149  }
150 
158  public function canEdit(int $user_guid = null): bool {
159  return _elgg_services()->accessCollections->canEdit($this->id, $user_guid);
160  }
161 
169  public function getMembers(array $options = []) {
170  return _elgg_services()->accessCollections->getMembers($this->id, $options);
171  }
172 
180  public function hasMember(int $member_guid = 0): bool {
181  return _elgg_services()->accessCollections->hasUser($member_guid, $this->id);
182  }
183 
191  public function addMember(int $member_guid = 0): bool {
192  return _elgg_services()->accessCollections->addUser($member_guid, $this->id);
193  }
194 
202  public function removeMember(int $member_guid = 0): bool {
203  return _elgg_services()->accessCollections->removeUser($member_guid, $this->id);
204  }
205 
209  public function getURL(): string {
210  $type = $this->getType();
211  $params = [
212  'access_collection' => $this,
213  ];
214  $url = _elgg_services()->events->triggerResults('access_collection:url', $type, $params);
215  return elgg_normalize_url($url);
216  }
217 
221  public function toObject(array $params = []) {
222  $object = new \Elgg\Export\AccessCollection();
223  $object->type = $this->getType();
224  $object->subtype = $this->getSubtype();
225  $object->id = $this->id;
226  $object->owner_guid = $this->owner_guid;
227  $object->name = $this->name;
228 
229  $params['access_collection'] = $this;
230 
231  return _elgg_services()->events->triggerResults('to:object', 'access_collection', $params, $object);
232  }
233 
237  public function getSystemLogID(): int {
238  return (int) $this->id;
239  }
240 
244  public function getObjectFromID(int $id) {
245  return _elgg_services()->accessCollections->get($id);
246  }
247 
251  public function getType(): string {
252  return 'access_collection';
253  }
254 
258  public function getSubtype(): string {
259  if (isset($this->subtype)) {
260  return $this->subtype;
261  }
262 
263  return $this->name;
264  }
265 }
canEdit(int $user_guid=null)
Check if user can edit this collection.
addMember(int $member_guid=0)
Adds a user to access collection.
$user_guid
Definition: login_as.php:10
$params
Saves global plugin settings.
Definition: save.php:13
$owner
Definition: upload.php:7
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
toObject(array $params=[])
{}
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
$type
Definition: delete.php:21
hasMember(int $member_guid=0)
Checks if user is already in access collection.
getDisplayName()
Get readable access level name for this collection.
$value
Definition: generic.php:51
__set($name, $value)
Set an attribute.
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
$owner_guid
initializeAttributes()
Initialize the attributes array.
__construct(stdClass $row=null)
Create an access collection object.
$filter
Layout content filter.
Definition: filter.php:18
$user
Definition: ban.php:7
A generic class that contains shared code among , , and .
Definition: ElggData.php:10
getMembers(array $options=[])
Returns members of the access collection.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
__get($name)
Get an attribute.
getOwnerEntity()
Returns owner entity of the collection.
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
$subtype
Definition: delete.php:22
foreach($plugin_guids as $guid) if(empty($deactivated_plugins)) $url
Definition: deactivate.php:39
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
removeMember(int $member_guid=0)
Removes a user from access collection.
elgg_normalize_url(string $url)
Definition: output.php:163
$site name
Definition: settings.php:15
$id
Generic annotation delete action.
Definition: delete.php:6