Elgg  Version 6.0
ElggSite.php
Go to the documentation of this file.
1 <?php
2 
6 
28 class ElggSite extends \ElggEntity {
29 
33  protected function initializeAttributes() {
34  // Using ElggData for testing purposes
36 
37  $this->attributes['guid'] = null;
38  $this->attributes['type'] = 'site';
39  $this->attributes['subtype'] = 'site';
40 
41  $this->attributes['owner_guid'] = 0;
42  $this->attributes['container_guid'] = 0;
43 
44  $this->attributes['access_id'] = ACCESS_PUBLIC;
45  $this->attributes['time_updated'] = null;
46  $this->attributes['last_action'] = null;
47  $this->attributes['enabled'] = 'yes';
48  $this->attributes['deleted'] = 'no';
49  $this->attributes['time_deleted'] = null;
50  }
51 
55  public function getType(): string {
56  return 'site';
57  }
58 
62  public function save(): bool {
63  $qb = Select::fromTable(EntityTable::TABLE_NAME);
64  $qb->select('*')
65  ->where($qb->compare('type', '=', 'site', ELGG_VALUE_STRING));
66 
67  $row = $this->getDatabase()->getDataRow($qb);
68  if (!empty($row)) {
69  if ($row->guid == $this->attributes['guid']) {
70  // can save active site
71  return parent::save();
72  }
73 
74  _elgg_services()->logger->error('More than 1 site entity cannot be created.');
75  return false;
76  }
77 
78  return parent::save();
79  }
80 
84  public function delete(bool $recursive = true, bool $persistent = null): bool {
85  if ($this->guid === 1) {
86  throw new SecurityException('You cannot delete the current site');
87  }
88 
89  return parent::delete($recursive, $persistent);
90  }
91 
103  public function disable(string $reason = '', bool $recursive = true): bool {
104  if ($this->guid == 1) {
105  throw new SecurityException('You cannot disable the current site');
106  }
107 
108  return parent::disable($reason, $recursive);
109  }
110 
114  public function __set($name, $value) {
115  if ($name === 'url') {
116  _elgg_services()->logger->warning('ElggSite::url cannot be set');
117  return;
118  }
119 
120  parent::__set($name, $value);
121  }
122 
126  public function __get($name) {
127  if ($name === 'url') {
128  return $this->getURL();
129  }
130 
131  return parent::__get($name);
132  }
133 
139  public function getURL(): string {
140  return _elgg_services()->config->wwwroot;
141  }
142 
146  public function isCacheable(): bool {
147  return false;
148  }
149 
153  protected function prepareObject(\Elgg\Export\Entity $object) {
154  $object = parent::prepareObject($object);
155  $object->name = $this->getDisplayName();
156  $object->description = $this->description;
157  unset($object->read_access);
158  return $object;
159  }
160 
167  public function getDomain(): string {
168  $breakdown = parse_url($this->url);
169  return $breakdown['host'];
170  }
171 
180  public function getEmailAddress(): string {
182  if (empty($email)) {
183  // If all else fails, use the domain of the site.
184  $token = _elgg_services()->crypto->getRandomString(24);
185  $email = "noreply-{$token}@{$this->getDomain()}";
186  }
187 
188  return $email;
189  }
190 
194  public function updateLastAction(int $posted = null): int {
195  // setting last action on ElggSite makes no sense... just returning current value to be compliant
196  return $this->last_action;
197  }
198 }
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:43
__set($name, $value)
{}
Definition: ElggSite.php:114
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
__get($name)
{}
Definition: ElggSite.php:126
getType()
{}
Definition: ElggSite.php:55
save()
{}
Definition: ElggSite.php:62
$email
Definition: change_email.php:7
getEmailAddress()
Get the email address for the site.
Definition: ElggSite.php:180
$value
Definition: generic.php:51
updateLastAction(int $posted=null)
Definition: ElggSite.php:194
Throw when a Security Exception occurs.
disable(string $reason= '', bool $recursive=true)
Disable the site.
Definition: ElggSite.php:103
initializeAttributes()
Initialize the attributes array.
Definition: ElggData.php:34
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggSite.php:153
$description
Definition: record.php:15
$token
getURL()
Returns the URL for this site.
Definition: ElggSite.php:139
$posted
Definition: comment.php:77
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
isCacheable()
{}
Definition: ElggSite.php:146
const ELGG_VALUE_STRING
Definition: constants.php:112
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
$persistent
Definition: login_as.php:21
const ACCESS_PUBLIC
Definition: constants.php:12
getDomain()
Get the domain for this site.
Definition: ElggSite.php:167
$qb
Definition: queue.php:12
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:324
initializeAttributes()
{}
Definition: ElggSite.php:33