Elgg  Version 5.1
ElggSite.php
Go to the documentation of this file.
1 <?php
2 
4 
26 class ElggSite extends \ElggEntity {
27 
31  protected function initializeAttributes() {
32  // Using ElggData for testing purposes
34 
35  $this->attributes['guid'] = null;
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  $this->attributes['time_updated'] = null;
44  $this->attributes['last_action'] = null;
45  $this->attributes['enabled'] = 'yes';
46  }
47 
51  public function getType(): string {
52  return 'site';
53  }
54 
58  public function save(): bool {
59  $db = $this->getDatabase();
60  $qb = \Elgg\Database\Select::fromTable('entities', 'e');
61  $qb->select('e.*')
62  ->where($qb->compare('e.type', '=', 'site', ELGG_VALUE_STRING));
63 
64  $row = $db->getDataRow($qb);
65 
66  if (!empty($row)) {
67  if ($row->guid == $this->attributes['guid']) {
68  // can save active site
69  return parent::save();
70  }
71 
72  _elgg_services()->logger->error('More than 1 site entity cannot be created.');
73  return false;
74  }
75 
76  return parent::save();
77  }
78 
89  public function delete(bool $recursive = true): bool {
90  if ($this->guid == 1) {
91  throw new SecurityException('You cannot delete the current site');
92  }
93 
94  return parent::delete($recursive);
95  }
96 
108  public function disable(string $reason = '', bool $recursive = true): bool {
109  if ($this->guid == 1) {
110  throw new SecurityException('You cannot disable the current site');
111  }
112 
113  return parent::disable($reason, $recursive);
114  }
115 
119  public function __set($name, $value) {
120  if ($name === 'url') {
121  _elgg_services()->logger->warning('ElggSite::url cannot be set');
122  return;
123  }
124 
125  parent::__set($name, $value);
126  }
127 
131  public function __get($name) {
132  if ($name === 'url') {
133  return $this->getURL();
134  }
135 
136  return parent::__get($name);
137  }
138 
144  public function getURL(): string {
145  return _elgg_services()->config->wwwroot;
146  }
147 
151  public function isCacheable(): bool {
152  return false;
153  }
154 
158  protected function prepareObject(\Elgg\Export\Entity $object) {
159  $object = parent::prepareObject($object);
160  $object->name = $this->getDisplayName();
161  $object->description = $this->description;
162  unset($object->read_access);
163  return $object;
164  }
165 
172  public function getDomain(): string {
173  $breakdown = parse_url($this->url);
174  return $breakdown['host'];
175  }
176 
185  public function getEmailAddress(): string {
187  if (empty($email)) {
188  // If all else fails, use the domain of the site.
189  $token = _elgg_services()->crypto->getRandomString(24);
190  $email = "noreply-{$token}@{$this->getDomain()}";
191  }
192 
193  return $email;
194  }
195 
199  public function updateLastAction(int $posted = null): int {
200  // setting last action on ElggSite makes no sense... just returning current value to be compliant
201  return $this->last_action;
202  }
203 }
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:43
__set($name, $value)
{}
Definition: ElggSite.php:119
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
__get($name)
{}
Definition: ElggSite.php:131
getType()
{}
Definition: ElggSite.php:51
save()
{}
Definition: ElggSite.php:58
$email
Definition: change_email.php:7
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:139
getEmailAddress()
Get the email address for the site.
Definition: ElggSite.php:185
$value
Definition: generic.php:51
updateLastAction(int $posted=null)
Definition: ElggSite.php:199
Throw when a Security Exception occurs.
disable(string $reason= '', bool $recursive=true)
Disable the site.
Definition: ElggSite.php:108
foreach($recommendedExtensions as $extension) if(empty(ini_get('session.gc_probability'))||empty(ini_get('session.gc_divisor'))) $db
initializeAttributes()
Initialize the attributes array.
Definition: ElggData.php:34
prepareObject(\Elgg\Export\Entity $object)
{}
Definition: ElggSite.php:158
$description
Definition: record.php:15
$token
getURL()
Returns the URL for this site.
Definition: ElggSite.php:144
$posted
Definition: comment.php:89
if($email instanceof\Elgg\Email) $object
Definition: body.php:24
isCacheable()
{}
Definition: ElggSite.php:151
const ELGG_VALUE_STRING
Definition: constants.php:112
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
const ACCESS_PUBLIC
Definition: constants.php:12
getDomain()
Get the domain for this site.
Definition: ElggSite.php:172
$qb
Definition: queue.php:11
getDisplayName()
Get the entity&#39;s display name.
Definition: ElggEntity.php:312
initializeAttributes()
{}
Definition: ElggSite.php:31