Elgg  Version 6.1
ElggSite.php
Go to the documentation of this file.
1 <?php
2 
6 
28 class ElggSite extends \ElggEntity {
29 
33  protected function initializeAttributes() {
34  parent::initializeAttributes();
35 
36  $this->attributes['type'] = 'site';
37  $this->attributes['subtype'] = 'site';
38 
39  $this->attributes['owner_guid'] = 0;
40  $this->attributes['container_guid'] = 0;
41 
42  $this->attributes['access_id'] = ACCESS_PUBLIC;
43  }
44 
48  public function save(): bool {
49  $qb = Select::fromTable(EntityTable::TABLE_NAME);
50  $qb->select('*')
51  ->where($qb->compare('type', '=', 'site', ELGG_VALUE_STRING));
52 
53  $row = $this->getDatabase()->getDataRow($qb);
54  if (!empty($row)) {
55  if ($row->guid == $this->attributes['guid']) {
56  // can save active site
57  return parent::save();
58  }
59 
60  _elgg_services()->logger->error('More than 1 site entity cannot be created.');
61  return false;
62  }
63 
64  return parent::save();
65  }
66 
70  public function delete(bool $recursive = true, bool $persistent = null): bool {
71  if ($this->guid === 1) {
72  throw new SecurityException('You cannot delete the current site');
73  }
74 
75  return parent::delete($recursive, $persistent);
76  }
77 
89  public function disable(string $reason = '', bool $recursive = true): bool {
90  if ($this->guid == 1) {
91  throw new SecurityException('You cannot disable the current site');
92  }
93 
94  return parent::disable($reason, $recursive);
95  }
96 
100  public function __set($name, $value) {
101  if ($name === 'url') {
102  _elgg_services()->logger->warning('ElggSite::url cannot be set');
103  return;
104  }
105 
106  parent::__set($name, $value);
107  }
108 
112  public function __get($name) {
113  if ($name === 'url') {
114  return $this->getURL();
115  }
116 
117  return parent::__get($name);
118  }
119 
125  public function getURL(): string {
126  return _elgg_services()->config->wwwroot;
127  }
128 
132  public function isCacheable(): bool {
133  return false;
134  }
135 
139  protected function prepareObject(\Elgg\Export\Entity $object) {
140  $object = parent::prepareObject($object);
141  $object->name = $this->getDisplayName();
142  $object->description = $this->description;
143  unset($object->read_access);
144  return $object;
145  }
146 
153  public function getDomain(): string {
154  $breakdown = parse_url($this->url);
155  return $breakdown['host'];
156  }
157 
166  public function getEmailAddress(): string {
168  if (empty($email)) {
169  // If all else fails, use the domain of the site.
170  $token = _elgg_services()->crypto->getRandomString(24);
171  $email = "noreply-{$token}@{$this->getDomain()}";
172  }
173 
174  return $email;
175  }
176 
180  public function updateLastAction(int $posted = null): int {
181  // setting last action on ElggSite makes no sense... just returning current value to be compliant
182  return $this->last_action;
183  }
184 }
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:43
__set($name, $value)
{}
Definition: ElggSite.php:100
$posted
Definition: sidebar.php:21
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
__get($name)
{}
Definition: ElggSite.php:112
save()
{}
Definition: ElggSite.php:48
$email
Definition: change_email.php:7
getEmailAddress()
Get the email address for the site.
Definition: ElggSite.php:166
$value
Definition: generic.php:51
updateLastAction(int $posted=null)
Definition: ElggSite.php:180
Throw when a Security Exception occurs.
disable(string $reason= '', bool $recursive=true)
Disable the site.
Definition: ElggSite.php:89
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggSite.php:139
$description
Definition: record.php:15
$token
getURL()
Returns the URL for this site.
Definition: ElggSite.php:125
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
isCacheable()
{}
Definition: ElggSite.php:132
const ELGG_VALUE_STRING
Definition: constants.php:112
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
$persistent
Definition: login_as.php:21
const ACCESS_PUBLIC
Definition: constants.php:12
getDomain()
Get the domain for this site.
Definition: ElggSite.php:153
$qb
Definition: queue.php:12
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:306
initializeAttributes()
{}
Definition: ElggSite.php:33