@@ -27,272 +27,272 @@ |
||
27 | 27 | */ |
28 | 28 | class ElggSite extends \ElggEntity { |
29 | 29 | |
30 | - /** |
|
31 | - * Initialize the attributes array. |
|
32 | - * This is vital to distinguish between metadata and base attributes. |
|
33 | - * |
|
34 | - * @return void |
|
35 | - */ |
|
36 | - protected function initializeAttributes() { |
|
37 | - parent::initializeAttributes(); |
|
38 | - |
|
39 | - $this->attributes['type'] = "site"; |
|
40 | - $this->attributes += self::getExternalAttributes(); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Get default values for attributes stored in a separate table |
|
45 | - * |
|
46 | - * @return array |
|
47 | - * @access private |
|
48 | - * |
|
49 | - * @see \Elgg\Database\EntityTable::getEntities |
|
50 | - */ |
|
51 | - final public static function getExternalAttributes() { |
|
52 | - return [ |
|
53 | - 'name' => null, |
|
54 | - 'description' => null, |
|
55 | - 'url' => null, |
|
56 | - ]; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Create a new \ElggSite. |
|
61 | - * |
|
62 | - * Plugin developers should only use the constructor to create a new entity. |
|
63 | - * To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
|
64 | - * |
|
65 | - * @param \stdClass $row Database row result. Default is null to create a new site. |
|
66 | - * |
|
67 | - * @throws IOException If cannot load remaining data from db |
|
68 | - * @throws InvalidParameterException If not passed a db result |
|
69 | - */ |
|
70 | - public function __construct(\stdClass $row = null) { |
|
71 | - $this->initializeAttributes(); |
|
72 | - |
|
73 | - if ($row) { |
|
74 | - // Load the rest |
|
75 | - if (!$this->load($row)) { |
|
76 | - $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid; |
|
77 | - throw new \IOException($msg); |
|
78 | - } |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * {@inheritdoc} |
|
84 | - */ |
|
85 | - public function save() { |
|
86 | - $db = $this->getDatabase(); |
|
87 | - $row = $db->getDataRow(" |
|
30 | + /** |
|
31 | + * Initialize the attributes array. |
|
32 | + * This is vital to distinguish between metadata and base attributes. |
|
33 | + * |
|
34 | + * @return void |
|
35 | + */ |
|
36 | + protected function initializeAttributes() { |
|
37 | + parent::initializeAttributes(); |
|
38 | + |
|
39 | + $this->attributes['type'] = "site"; |
|
40 | + $this->attributes += self::getExternalAttributes(); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Get default values for attributes stored in a separate table |
|
45 | + * |
|
46 | + * @return array |
|
47 | + * @access private |
|
48 | + * |
|
49 | + * @see \Elgg\Database\EntityTable::getEntities |
|
50 | + */ |
|
51 | + final public static function getExternalAttributes() { |
|
52 | + return [ |
|
53 | + 'name' => null, |
|
54 | + 'description' => null, |
|
55 | + 'url' => null, |
|
56 | + ]; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Create a new \ElggSite. |
|
61 | + * |
|
62 | + * Plugin developers should only use the constructor to create a new entity. |
|
63 | + * To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
|
64 | + * |
|
65 | + * @param \stdClass $row Database row result. Default is null to create a new site. |
|
66 | + * |
|
67 | + * @throws IOException If cannot load remaining data from db |
|
68 | + * @throws InvalidParameterException If not passed a db result |
|
69 | + */ |
|
70 | + public function __construct(\stdClass $row = null) { |
|
71 | + $this->initializeAttributes(); |
|
72 | + |
|
73 | + if ($row) { |
|
74 | + // Load the rest |
|
75 | + if (!$this->load($row)) { |
|
76 | + $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid; |
|
77 | + throw new \IOException($msg); |
|
78 | + } |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * {@inheritdoc} |
|
84 | + */ |
|
85 | + public function save() { |
|
86 | + $db = $this->getDatabase(); |
|
87 | + $row = $db->getDataRow(" |
|
88 | 88 | SELECT guid FROM {$db->prefix}sites_entity |
89 | 89 | "); |
90 | - if ($row) { |
|
91 | - if ($row->guid == $this->attributes['guid']) { |
|
92 | - // can save active site |
|
93 | - return parent::save(); |
|
94 | - } |
|
95 | - |
|
96 | - _elgg_services()->logger->error('More than 1 site entity cannot be created.'); |
|
97 | - return false; |
|
98 | - } |
|
99 | - |
|
100 | - return parent::save(); // TODO: Change the autogenerated stub |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Loads the full \ElggSite when given a guid. |
|
105 | - * |
|
106 | - * @param mixed $guid GUID of \ElggSite entity or database row object |
|
107 | - * |
|
108 | - * @return bool |
|
109 | - * @throws InvalidClassException |
|
110 | - */ |
|
111 | - protected function load($guid) { |
|
112 | - // TODO(steve) we only have 1 site, simplify this... |
|
113 | - $attr_loader = new \Elgg\AttributeLoader(get_class(), 'site', $this->attributes); |
|
114 | - $attr_loader->requires_access_control = !($this instanceof \ElggPlugin); |
|
115 | - $attr_loader->secondary_loader = 'get_site_entity_as_row'; |
|
116 | - |
|
117 | - $attrs = $attr_loader->getRequiredAttributes($guid); |
|
118 | - if (!$attrs) { |
|
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - $this->attributes = $attrs; |
|
123 | - $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues()); |
|
124 | - _elgg_services()->entityCache->set($this); |
|
125 | - |
|
126 | - return true; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * {@inheritdoc} |
|
131 | - */ |
|
132 | - protected function create() { |
|
133 | - $guid = parent::create(); |
|
134 | - $db = $this->getDatabase(); |
|
135 | - |
|
136 | - $query = " |
|
90 | + if ($row) { |
|
91 | + if ($row->guid == $this->attributes['guid']) { |
|
92 | + // can save active site |
|
93 | + return parent::save(); |
|
94 | + } |
|
95 | + |
|
96 | + _elgg_services()->logger->error('More than 1 site entity cannot be created.'); |
|
97 | + return false; |
|
98 | + } |
|
99 | + |
|
100 | + return parent::save(); // TODO: Change the autogenerated stub |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Loads the full \ElggSite when given a guid. |
|
105 | + * |
|
106 | + * @param mixed $guid GUID of \ElggSite entity or database row object |
|
107 | + * |
|
108 | + * @return bool |
|
109 | + * @throws InvalidClassException |
|
110 | + */ |
|
111 | + protected function load($guid) { |
|
112 | + // TODO(steve) we only have 1 site, simplify this... |
|
113 | + $attr_loader = new \Elgg\AttributeLoader(get_class(), 'site', $this->attributes); |
|
114 | + $attr_loader->requires_access_control = !($this instanceof \ElggPlugin); |
|
115 | + $attr_loader->secondary_loader = 'get_site_entity_as_row'; |
|
116 | + |
|
117 | + $attrs = $attr_loader->getRequiredAttributes($guid); |
|
118 | + if (!$attrs) { |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + $this->attributes = $attrs; |
|
123 | + $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues()); |
|
124 | + _elgg_services()->entityCache->set($this); |
|
125 | + |
|
126 | + return true; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * {@inheritdoc} |
|
131 | + */ |
|
132 | + protected function create() { |
|
133 | + $guid = parent::create(); |
|
134 | + $db = $this->getDatabase(); |
|
135 | + |
|
136 | + $query = " |
|
137 | 137 | INSERT INTO {$db->prefix}sites_entity |
138 | 138 | (guid, name, description, url) VALUES (:guid, :name, :desc, :url) |
139 | 139 | "; |
140 | - $params = [ |
|
141 | - ':guid' => $guid, |
|
142 | - ':name' => (string) $this->attributes['name'], |
|
143 | - ':desc' => (string) $this->attributes['description'], |
|
144 | - ':url' => '', |
|
145 | - ]; |
|
146 | - |
|
147 | - $result = $db->insertData($query, $params); |
|
148 | - if ($result === false) { |
|
149 | - return false; |
|
150 | - } |
|
151 | - |
|
152 | - return $guid; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * {@inheritdoc} |
|
157 | - */ |
|
158 | - protected function update() { |
|
159 | - if (!parent::update()) { |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - $guid = (int) $this->guid; |
|
164 | - $db = $this->getDatabase(); |
|
165 | - |
|
166 | - $query = " |
|
140 | + $params = [ |
|
141 | + ':guid' => $guid, |
|
142 | + ':name' => (string) $this->attributes['name'], |
|
143 | + ':desc' => (string) $this->attributes['description'], |
|
144 | + ':url' => '', |
|
145 | + ]; |
|
146 | + |
|
147 | + $result = $db->insertData($query, $params); |
|
148 | + if ($result === false) { |
|
149 | + return false; |
|
150 | + } |
|
151 | + |
|
152 | + return $guid; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * {@inheritdoc} |
|
157 | + */ |
|
158 | + protected function update() { |
|
159 | + if (!parent::update()) { |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + $guid = (int) $this->guid; |
|
164 | + $db = $this->getDatabase(); |
|
165 | + |
|
166 | + $query = " |
|
167 | 167 | UPDATE {$db->prefix}sites_entity |
168 | 168 | SET name = :name, description = :desc, url = :url WHERE guid = :guid |
169 | 169 | "; |
170 | - $params = [ |
|
171 | - ':guid' => $guid, |
|
172 | - ':name' => (string) $this->attributes['name'], |
|
173 | - ':desc' => (string) $this->attributes['description'], |
|
174 | - ':url' => '', |
|
175 | - ]; |
|
176 | - |
|
177 | - return $db->updateData($query, false, $params) !== false; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Delete the site. |
|
182 | - * |
|
183 | - * @note You cannot delete the current site. |
|
184 | - * |
|
185 | - * @return bool |
|
186 | - * @throws SecurityException |
|
187 | - */ |
|
188 | - public function delete() { |
|
189 | - global $CONFIG; |
|
190 | - if ($CONFIG->site->getGUID() == $this->guid) { |
|
191 | - throw new \SecurityException('You cannot delete the current site'); |
|
192 | - } |
|
193 | - |
|
194 | - return parent::delete(); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Disable the site |
|
199 | - * |
|
200 | - * @note You cannot disable the current site. |
|
201 | - * |
|
202 | - * @param string $reason Optional reason for disabling |
|
203 | - * @param bool $recursive Recursively disable all contained entities? |
|
204 | - * |
|
205 | - * @return bool |
|
206 | - * @throws SecurityException |
|
207 | - */ |
|
208 | - public function disable($reason = "", $recursive = true) { |
|
209 | - if ($this->guid == 1) { |
|
210 | - throw new \SecurityException('You cannot disable the current site'); |
|
211 | - } |
|
212 | - |
|
213 | - return parent::disable($reason, $recursive); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * {@inheritdoc} |
|
218 | - */ |
|
219 | - public function __set($name, $value) { |
|
220 | - if ($name === 'url') { |
|
221 | - _elgg_services()->logger->warn("ElggSite::url cannot be set"); |
|
222 | - return; |
|
223 | - } |
|
224 | - parent::__set($name, $value); |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * {@inheritdoc} |
|
229 | - */ |
|
230 | - public function __get($name) { |
|
231 | - if ($name === 'url') { |
|
232 | - return $this->getURL(); |
|
233 | - } |
|
234 | - return parent::__get($name); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Returns the URL for this site |
|
239 | - * |
|
240 | - * @return string The URL |
|
241 | - */ |
|
242 | - public function getURL() { |
|
243 | - return _elgg_services()->config->getSiteUrl(); |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * {@inheritdoc} |
|
248 | - */ |
|
249 | - public function getDisplayName() { |
|
250 | - return $this->name; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * {@inheritdoc} |
|
255 | - */ |
|
256 | - public function setDisplayName($displayName) { |
|
257 | - $this->name = $displayName; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * {@inheritdoc} |
|
262 | - */ |
|
263 | - protected function prepareObject($object) { |
|
264 | - $object = parent::prepareObject($object); |
|
265 | - $object->name = $this->getDisplayName(); |
|
266 | - $object->description = $this->description; |
|
267 | - unset($object->read_access); |
|
268 | - return $object; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * Get the domain for this site |
|
273 | - * |
|
274 | - * @return string |
|
275 | - * @since 1.9 |
|
276 | - */ |
|
277 | - public function getDomain() { |
|
278 | - $breakdown = parse_url($this->url); |
|
279 | - return $breakdown['host']; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Get the email address for the site |
|
284 | - * |
|
285 | - * This can be set in the basic site settings or fallback to noreply@domain |
|
286 | - * |
|
287 | - * @return string |
|
288 | - * @since 3.0.0 |
|
289 | - */ |
|
290 | - public function getEmailAddress() { |
|
291 | - $email = $this->email; |
|
292 | - if (empty($email)) { |
|
293 | - $email = "noreply@{$this->getDomain()}"; |
|
294 | - } |
|
295 | - |
|
296 | - return $email; |
|
297 | - } |
|
170 | + $params = [ |
|
171 | + ':guid' => $guid, |
|
172 | + ':name' => (string) $this->attributes['name'], |
|
173 | + ':desc' => (string) $this->attributes['description'], |
|
174 | + ':url' => '', |
|
175 | + ]; |
|
176 | + |
|
177 | + return $db->updateData($query, false, $params) !== false; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Delete the site. |
|
182 | + * |
|
183 | + * @note You cannot delete the current site. |
|
184 | + * |
|
185 | + * @return bool |
|
186 | + * @throws SecurityException |
|
187 | + */ |
|
188 | + public function delete() { |
|
189 | + global $CONFIG; |
|
190 | + if ($CONFIG->site->getGUID() == $this->guid) { |
|
191 | + throw new \SecurityException('You cannot delete the current site'); |
|
192 | + } |
|
193 | + |
|
194 | + return parent::delete(); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Disable the site |
|
199 | + * |
|
200 | + * @note You cannot disable the current site. |
|
201 | + * |
|
202 | + * @param string $reason Optional reason for disabling |
|
203 | + * @param bool $recursive Recursively disable all contained entities? |
|
204 | + * |
|
205 | + * @return bool |
|
206 | + * @throws SecurityException |
|
207 | + */ |
|
208 | + public function disable($reason = "", $recursive = true) { |
|
209 | + if ($this->guid == 1) { |
|
210 | + throw new \SecurityException('You cannot disable the current site'); |
|
211 | + } |
|
212 | + |
|
213 | + return parent::disable($reason, $recursive); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * {@inheritdoc} |
|
218 | + */ |
|
219 | + public function __set($name, $value) { |
|
220 | + if ($name === 'url') { |
|
221 | + _elgg_services()->logger->warn("ElggSite::url cannot be set"); |
|
222 | + return; |
|
223 | + } |
|
224 | + parent::__set($name, $value); |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * {@inheritdoc} |
|
229 | + */ |
|
230 | + public function __get($name) { |
|
231 | + if ($name === 'url') { |
|
232 | + return $this->getURL(); |
|
233 | + } |
|
234 | + return parent::__get($name); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Returns the URL for this site |
|
239 | + * |
|
240 | + * @return string The URL |
|
241 | + */ |
|
242 | + public function getURL() { |
|
243 | + return _elgg_services()->config->getSiteUrl(); |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * {@inheritdoc} |
|
248 | + */ |
|
249 | + public function getDisplayName() { |
|
250 | + return $this->name; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * {@inheritdoc} |
|
255 | + */ |
|
256 | + public function setDisplayName($displayName) { |
|
257 | + $this->name = $displayName; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * {@inheritdoc} |
|
262 | + */ |
|
263 | + protected function prepareObject($object) { |
|
264 | + $object = parent::prepareObject($object); |
|
265 | + $object->name = $this->getDisplayName(); |
|
266 | + $object->description = $this->description; |
|
267 | + unset($object->read_access); |
|
268 | + return $object; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * Get the domain for this site |
|
273 | + * |
|
274 | + * @return string |
|
275 | + * @since 1.9 |
|
276 | + */ |
|
277 | + public function getDomain() { |
|
278 | + $breakdown = parse_url($this->url); |
|
279 | + return $breakdown['host']; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Get the email address for the site |
|
284 | + * |
|
285 | + * This can be set in the basic site settings or fallback to noreply@domain |
|
286 | + * |
|
287 | + * @return string |
|
288 | + * @since 3.0.0 |
|
289 | + */ |
|
290 | + public function getEmailAddress() { |
|
291 | + $email = $this->email; |
|
292 | + if (empty($email)) { |
|
293 | + $email = "noreply@{$this->getDomain()}"; |
|
294 | + } |
|
295 | + |
|
296 | + return $email; |
|
297 | + } |
|
298 | 298 | } |