Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/ElggSite.php (1 issue)

1
<?php
2
/**
3
 * A Site entity.
4
 *
5
 * \ElggSite represents a single site entity.
6
 *
7
 * An \ElggSite object is an \ElggEntity child class with the subtype of "site."
8
 * It is created upon installation and holds information about a site:
9
 *  - name
10
 *  - description
11
 *  - url
12
 *
13
 * Every \ElggEntity belongs to a site.
14
 *
15
 * @note Internal: \ElggSite represents a single row from the entities table.
16
 *
17
 * @link       http://learn.elgg.org/en/stable/design/database.html
18
 *
19
 * @property      string $name        The name or title of the website
20
 * @property      string $description A motto, mission statement, or description of the website
21
 * @property-read string $url         The root web address for the site, including trailing slash
22
 */
23
class ElggSite extends \ElggEntity {
24
25
	/**
26
	 * {@inheritdoc}
27
	 */
28 4780
	protected function initializeAttributes() {
29 4780
		parent::initializeAttributes();
30 4780
		$this->attributes['subtype'] = 'site';
31 4780
	}
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36 4780
	public function getType() {
37 4780
		return 'site';
38
	}
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43 2
	public function save() {
44 2
		$db = $this->getDatabase();
45 2
		$row = $db->getDataRow("
46 2
			SELECT guid FROM {$db->prefix}entities WHERE type = '{$this->getType()}'
47
		");
48 2
		if ($row) {
49
			if ($row->guid == $this->attributes['guid']) {
50
				// can save active site
51
				return parent::save();
52
			}
53
54
			_elgg_services()->logger->error('More than 1 site entity cannot be created.');
55
			return false;
56
		}
57
58 2
		return parent::save(); // TODO: Change the autogenerated stub
59
	}
60
61
	/**
62
	 * Delete the site.
63
	 *
64
	 * @note You cannot delete the current site.
65
	 *
66
	 * @param bool $recursive If true (default) then all entities which are owned or contained by $this will also be deleted.
67
	 *
68
	 * @return bool
69
	 * @throws SecurityException
70
	 */
71
	public function delete($recursive = true) {
72
		if ($this->guid == 1) {
73
			throw new \SecurityException('You cannot delete the current site');
74
		}
75
76
		return parent::delete($recursive);
77
	}
78
79
	/**
80
	 * Disable the site
81
	 *
82
	 * @note You cannot disable the current site.
83
	 *
84
	 * @param string $reason    Optional reason for disabling
85
	 * @param bool   $recursive Recursively disable all contained entities?
86
	 *
87
	 * @return bool
88
	 * @throws SecurityException
89
	 */
90
	public function disable($reason = "", $recursive = true) {
91
		if ($this->guid == 1) {
92
			throw new \SecurityException('You cannot disable the current site');
93
		}
94
95
		return parent::disable($reason, $recursive);
96
	}
97
98
	/**
99
	 * {@inheritdoc}
100
	 */
101 2
	public function __set($name, $value) {
102 2
		if ($name === 'url') {
103
			_elgg_services()->logger->warn("ElggSite::url cannot be set");
104
			return;
105
		}
106 2
		parent::__set($name, $value);
107 2
	}
108
109
	/**
110
	 * {@inheritdoc}
111
	 */
112 4976
	public function __get($name) {
113 4976
		if ($name === 'url') {
114 1
			return $this->getURL();
115
		}
116 4976
		return parent::__get($name);
117
	}
118
119
	/**
120
	 * Returns the URL for this site
121
	 *
122
	 * @return string The URL
123
	 */
124 3
	public function getURL() {
125 3
		return _elgg_config()->wwwroot;
126
	}
127
128
	/**
129
	 * {@inheritdoc}
130
	 */
131
	protected function prepareObject($object) {
132
		$object = parent::prepareObject($object);
133
		$object->name = $this->getDisplayName();
134
		$object->description = $this->description;
135
		unset($object->read_access);
136
		return $object;
137
	}
138
139
	/**
140
	 * Get the domain for this site
141
	 *
142
	 * @return string
143
	 * @since 1.9
144
	 */
145 1
	public function getDomain() {
146 1
		$breakdown = parse_url($this->url);
147 1
		return $breakdown['host'];
148
	}
149
150
	/**
151
	 * Get the email address for the site
152
	 *
153
	 * This can be set in the basic site settings or fallback to [email protected]
154
	 *
155
	 * @return string
156
	 * @since 3.0.0
157
	 */
158 3
	public function getEmailAddress() {
159 3
		$email = $this->email;
160 3
		if (empty($email)) {
161 1
			$email = "[email protected]{$this->getDomain()}";
162
		}
163
164 3
		return $email;
0 ignored issues
show
Bug Best Practice introduced by Jerôme Bakker
The expression return $email also could return the type array which is incompatible with the documented return type string.
Loading history...
165
	}
166
}
167