1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class representing a container for other elgg entities. |
5
|
|
|
* |
6
|
|
|
* @package Elgg.Core |
7
|
|
|
* @subpackage Groups |
8
|
|
|
* |
9
|
|
|
* @property string $name A short name that captures the purpose of the group |
10
|
|
|
* @property string $description A longer body of content that gives more details about the group |
11
|
|
|
*/ |
12
|
|
|
class ElggGroup extends \ElggEntity |
13
|
|
|
implements Friendable { |
14
|
|
|
|
15
|
|
|
const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted'; |
16
|
|
|
const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Sets the type to group. |
20
|
|
|
* |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
1 |
View Code Duplication |
protected function initializeAttributes() { |
|
|
|
|
24
|
1 |
|
parent::initializeAttributes(); |
25
|
|
|
|
26
|
1 |
|
$this->attributes['type'] = "group"; |
27
|
1 |
|
$this->attributes += self::getExternalAttributes(); |
28
|
1 |
|
$this->tables_split = 2; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get default values for attributes stored in a separate table |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
* @access private |
36
|
|
|
* |
37
|
|
|
* @see \Elgg\Database\EntityTable::getEntities |
38
|
|
|
*/ |
39
|
1 |
|
final public static function getExternalAttributes() { |
40
|
|
|
return [ |
41
|
1 |
|
'name' => null, |
42
|
1 |
|
'description' => null, |
43
|
1 |
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Construct a new group entity |
48
|
|
|
* |
49
|
|
|
* Plugin developers should only use the constructor to create a new entity. |
50
|
|
|
* To retrieve entities, use get_entity() and the elgg_get_entities* functions. |
51
|
|
|
* |
52
|
|
|
* @param \stdClass $row Database row result. Default is null to create a new group. |
53
|
|
|
* |
54
|
|
|
* @throws IOException|InvalidParameterException if there was a problem creating the group. |
55
|
|
|
*/ |
56
|
1 |
View Code Duplication |
public function __construct($row = null) { |
|
|
|
|
57
|
1 |
|
$this->initializeAttributes(); |
58
|
|
|
|
59
|
|
|
// compatibility for 1.7 api. |
60
|
1 |
|
$this->initialise_attributes(false); |
|
|
|
|
61
|
|
|
|
62
|
1 |
|
if (!empty($row)) { |
63
|
|
|
// Is $guid is a entity table DB row |
64
|
|
|
if ($row instanceof \stdClass) { |
65
|
|
|
// Load the rest |
66
|
|
|
if (!$this->load($row)) { |
67
|
|
|
$msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid; |
68
|
|
|
throw new \IOException($msg); |
69
|
|
|
} |
70
|
|
|
} else if ($row instanceof \ElggGroup) { |
71
|
|
|
// $row is an \ElggGroup so this is a copy constructor |
72
|
|
|
elgg_deprecated_notice('This type of usage of the \ElggGroup constructor was deprecated. Please use the clone method.', 1.7); |
73
|
|
|
foreach ($row->attributes as $key => $value) { |
74
|
|
|
$this->attributes[$key] = $value; |
75
|
|
|
} |
76
|
|
|
} else if (is_numeric($row)) { |
77
|
|
|
// $row is a GUID so load entity |
78
|
|
|
elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9); |
79
|
|
|
if (!$this->load($row)) { |
80
|
|
|
throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
|
throw new \InvalidParameterException("Unrecognized value passed to constuctor."); |
84
|
|
|
} |
85
|
|
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getDisplayName() { |
92
|
|
|
return $this->name; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function setDisplayName($displayName) { |
99
|
|
|
$this->name = $displayName; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add an \ElggObject to this group. |
104
|
|
|
* |
105
|
|
|
* @param \ElggObject $object The object. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
1 |
|
public function addObjectToGroup(\ElggObject $object) { |
110
|
1 |
|
$object->container_guid = $this->guid; |
111
|
|
|
return $object->save(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove an object from this containing group and sets the container to be |
116
|
|
|
* object's owner |
117
|
|
|
* |
118
|
|
|
* @param \ElggObject $object The object. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function removeObjectFromGroup($object) { |
123
|
|
|
if (is_numeric($object)) { |
124
|
|
|
elgg_deprecated_notice('\ElggGroup::removeObjectFromGroup() takes an \ElggObject not a guid.', 1.9); |
125
|
|
|
$object = get_entity($object); |
126
|
|
|
if (!elgg_instanceof($object, 'object')) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$object->container_guid = $object->owner_guid; |
132
|
|
|
return $object->save(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Wrapper around \ElggEntity::__get() |
137
|
|
|
* |
138
|
|
|
* @see \ElggEntity::__get() |
139
|
|
|
* |
140
|
|
|
* @param string $name Name |
141
|
|
|
* @return mixed |
142
|
|
|
* @todo deprecate appending group to username. Was a hack used for creating |
143
|
|
|
* URLs for group content. We stopped using the hack in 1.8. |
144
|
|
|
*/ |
145
|
|
|
public function __get($name) { |
146
|
|
|
if ($name == 'username') { |
147
|
|
|
return 'group:' . $this->getGUID(); |
148
|
|
|
} |
149
|
|
|
return parent::__get($name); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Wrapper around \ElggEntity::get() |
154
|
|
|
* |
155
|
|
|
* @param string $name Name |
156
|
|
|
* @return mixed |
157
|
|
|
* @deprecated 1.9 |
158
|
|
|
*/ |
159
|
|
|
public function get($name) { |
160
|
|
|
elgg_deprecated_notice("Use -> instead of get()", 1.9); |
161
|
|
|
return $this->__get($name); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Start friendable compatibility block: |
166
|
|
|
* |
167
|
|
|
* public function addFriend($friend_guid); |
168
|
|
|
public function removeFriend($friend_guid); |
169
|
|
|
public function isFriend(); |
170
|
|
|
public function isFriendsWith($user_guid); |
171
|
|
|
public function isFriendOf($user_guid); |
172
|
|
|
public function getFriends($subtype = "", $limit = 10, $offset = 0); |
173
|
|
|
public function getFriendsOf($subtype = "", $limit = 10, $offset = 0); |
174
|
|
|
public function getObjects($subtype="", $limit = 10, $offset = 0); |
175
|
|
|
public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0); |
176
|
|
|
public function countObjects($subtype = ""); |
177
|
|
|
*/ |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* For compatibility with Friendable. |
181
|
|
|
* |
182
|
|
|
* Join a group when you friend \ElggGroup. |
183
|
|
|
* |
184
|
|
|
* @param int $friend_guid The GUID of the user joining the group. |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
* @deprecated 1.9 Use \ElggGroup::join() |
188
|
|
|
*/ |
189
|
|
|
public function addFriend($friend_guid) { |
190
|
|
|
elgg_deprecated_notice("\ElggGroup::addFriend() is deprecated. Use \ElggGroup::join()", 1.9); |
191
|
|
|
$user = get_user($friend_guid); |
192
|
|
|
return $user ? $this->join($user) : false; |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* For compatibility with Friendable |
197
|
|
|
* |
198
|
|
|
* Leave group when you unfriend \ElggGroup. |
199
|
|
|
* |
200
|
|
|
* @param int $friend_guid The GUID of the user leaving. |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
* @deprecated 1.9 Use \ElggGroup::leave() |
204
|
|
|
*/ |
205
|
|
|
public function removeFriend($friend_guid) { |
206
|
|
|
elgg_deprecated_notice("\ElggGroup::removeFriend() is deprecated. Use \ElggGroup::leave()", 1.9); |
207
|
|
|
$user = get_user($friend_guid); |
208
|
|
|
return $user ? $this->leave($user) : false; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* For compatibility with Friendable |
213
|
|
|
* |
214
|
|
|
* Friending a group adds you as a member |
215
|
|
|
* |
216
|
|
|
* @return bool |
217
|
|
|
* @deprecated 1.9 Use \ElggGroup::isMember() |
218
|
|
|
*/ |
219
|
|
|
public function isFriend() { |
220
|
|
|
elgg_deprecated_notice("\ElggGroup::isFriend() is deprecated. Use \ElggGroup::isMember()", 1.9); |
221
|
|
|
return $this->isMember(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* For compatibility with Friendable |
226
|
|
|
* |
227
|
|
|
* @param int $user_guid The GUID of a user to check. |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
* @deprecated 1.9 Use \ElggGroup::isMember() |
231
|
|
|
*/ |
232
|
|
|
public function isFriendsWith($user_guid) { |
233
|
|
|
elgg_deprecated_notice("\ElggGroup::isFriendsWith() is deprecated. Use \ElggGroup::isMember()", 1.9); |
234
|
|
|
$user = get_user($user_guid); |
235
|
|
|
return $user ? $this->isMember($user) : false; |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* For compatibility with Friendable |
240
|
|
|
* |
241
|
|
|
* @param int $user_guid The GUID of a user to check. |
242
|
|
|
* |
243
|
|
|
* @return bool |
244
|
|
|
* @deprecated 1.9 Use \ElggGroup::isMember() |
245
|
|
|
*/ |
246
|
|
|
public function isFriendOf($user_guid) { |
247
|
|
|
elgg_deprecated_notice("\ElggGroup::isFriendOf() is deprecated. Use \ElggGroup::isMember()", 1.9); |
248
|
|
|
$user = get_user($user_guid); |
249
|
|
|
return $user ? $this->isMember($user) : false; |
|
|
|
|
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* For compatibility with Friendable |
254
|
|
|
* |
255
|
|
|
* @param string $subtype The GUID of a user to check. |
256
|
|
|
* @param int $limit Limit |
257
|
|
|
* @param int $offset Offset |
258
|
|
|
* |
259
|
|
|
* @return bool |
260
|
|
|
* @deprecated 1.9 Use \ElggGroup::getMembers() |
261
|
|
|
*/ |
262
|
|
|
public function getFriends($subtype = "", $limit = 10, $offset = 0) { |
263
|
|
|
elgg_deprecated_notice("\ElggGroup::getFriends() is deprecated. Use \ElggGroup::getMembers()", 1.9); |
264
|
|
|
return get_group_members($this->getGUID(), $limit, $offset); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* For compatibility with Friendable |
269
|
|
|
* |
270
|
|
|
* @param string $subtype The GUID of a user to check. |
271
|
|
|
* @param int $limit Limit |
272
|
|
|
* @param int $offset Offset |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
* @deprecated 1.9 Use \ElggGroup::getMembers() |
276
|
|
|
*/ |
277
|
|
|
public function getFriendsOf($subtype = "", $limit = 10, $offset = 0) { |
278
|
|
|
elgg_deprecated_notice("\ElggGroup::getFriendsOf() is deprecated. Use \ElggGroup::getMembers()", 1.9); |
279
|
|
|
return get_group_members($this->getGUID(), $limit, $offset); |
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get objects contained in this group. |
284
|
|
|
* |
285
|
|
|
* @param string $subtype Entity subtype |
286
|
|
|
* @param int $limit Limit |
287
|
|
|
* @param int $offset Offset |
288
|
|
|
* |
289
|
|
|
* @return array|false |
290
|
|
|
* @deprecated 1.9 Use elgg_get_entities() |
291
|
|
|
*/ |
292
|
|
|
public function getObjects($subtype = "", $limit = 10, $offset = 0) { |
293
|
|
|
elgg_deprecated_notice("\ElggGroup::getObjects() is deprecated. Use elgg_get_entities()", 1.9); |
294
|
|
|
return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* For compatibility with Friendable |
299
|
|
|
* |
300
|
|
|
* @param string $subtype Entity subtype |
301
|
|
|
* @param int $limit Limit |
302
|
|
|
* @param int $offset Offset |
303
|
|
|
* |
304
|
|
|
* @return array|false |
305
|
|
|
* @deprecated 1.9 Use elgg_get_entities() |
306
|
|
|
*/ |
307
|
|
|
public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0) { |
308
|
|
|
elgg_deprecated_notice("\ElggGroup::getFriendsObjects() is deprecated. Use elgg_get_entities()", 1.9); |
309
|
|
|
return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* For compatibility with Friendable |
314
|
|
|
* |
315
|
|
|
* @param string $subtype Subtype of entities |
316
|
|
|
* |
317
|
|
|
* @return array|false |
318
|
|
|
* @deprecated 1.9 Use elgg_get_entities() |
319
|
|
|
*/ |
320
|
|
|
public function countObjects($subtype = "") { |
321
|
|
|
elgg_deprecated_notice("\ElggGroup::countObjects() is deprecated. Use elgg_get_entities()", 1.9); |
322
|
|
|
return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", 10, 0, true); |
|
|
|
|
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* End friendable compatibility block |
327
|
|
|
*/ |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Get an array of group members. |
331
|
|
|
* |
332
|
|
|
* @param array $options Options array. See elgg_get_entities_from_relationships |
333
|
|
|
* for a complete list. Common ones are 'limit', 'offset', |
334
|
|
|
* and 'count'. Options set automatically are 'relationship', |
335
|
|
|
* 'relationship_guid', 'inverse_relationship', and 'type'. This argument |
336
|
|
|
* used to set the limit (deprecated usage) |
337
|
|
|
* @param int $offset Offset (deprecated) |
338
|
|
|
* @param bool $count Count (deprecated) |
339
|
|
|
* |
340
|
|
|
* @return array |
341
|
|
|
*/ |
342
|
|
|
public function getMembers($options = array(), $offset = 0, $count = false) { |
343
|
|
|
if (!is_array($options)) { |
344
|
|
|
elgg_deprecated_notice('\ElggGroup::getMembers() takes an options array.', 1.9); |
345
|
|
|
$options = array( |
346
|
|
|
'relationship' => 'member', |
347
|
|
|
'relationship_guid' => $this->getGUID(), |
348
|
|
|
'inverse_relationship' => true, |
349
|
|
|
'type' => 'user', |
350
|
|
|
'limit' => $options, |
351
|
|
|
'offset' => $offset, |
352
|
|
|
'count' => $count, |
353
|
|
|
); |
354
|
|
View Code Duplication |
} else { |
355
|
|
|
$options['relationship'] = 'member'; |
356
|
|
|
$options['relationship_guid'] = $this->getGUID(); |
357
|
|
|
$options['inverse_relationship'] = true; |
358
|
|
|
$options['type'] = 'user'; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return elgg_get_entities_from_relationship($options); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Returns whether the current group has open membership or not. |
366
|
|
|
* |
367
|
|
|
* @return bool |
368
|
|
|
*/ |
369
|
|
|
public function isPublicMembership() { |
370
|
|
|
return ($this->membership == ACCESS_PUBLIC); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Return the content access mode used by group_gatekeeper() |
375
|
|
|
* |
376
|
|
|
* @return string One of CONTENT_ACCESS_MODE_* constants |
377
|
|
|
* @access private |
378
|
|
|
* @since 1.9.0 |
379
|
|
|
*/ |
380
|
|
|
public function getContentAccessMode() { |
381
|
|
|
$mode = $this->content_access_mode; |
382
|
|
|
|
383
|
|
|
if (!is_string($mode)) { |
384
|
|
|
// fallback to 1.8 default behavior |
385
|
|
|
if ($this->isPublicMembership()) { |
386
|
|
|
$mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
387
|
|
|
} else { |
388
|
|
|
$mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY; |
389
|
|
|
} |
390
|
|
|
$this->content_access_mode = $mode; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// only support two modes for now |
394
|
|
|
if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) { |
395
|
|
|
return $mode; |
396
|
|
|
} |
397
|
|
|
return self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Set the content access mode used by group_gatekeeper() |
402
|
|
|
* |
403
|
|
|
* @param string $mode One of CONTENT_ACCESS_MODE_* constants |
404
|
|
|
* @return void |
405
|
|
|
* @access private |
406
|
|
|
* @since 1.9.0 |
407
|
|
|
*/ |
408
|
|
|
public function setContentAccessMode($mode) { |
409
|
|
|
// only support two modes for now |
410
|
|
|
if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) { |
411
|
|
|
$mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
$this->content_access_mode = $mode; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Is the given user a member of this group? |
419
|
|
|
* |
420
|
|
|
* @param \ElggUser $user The user. Default is logged in user. |
421
|
|
|
* |
422
|
|
|
* @return bool |
423
|
|
|
*/ |
424
|
|
|
public function isMember(\ElggUser $user = null) { |
425
|
|
|
if ($user == null) { |
426
|
|
|
$user = _elgg_services()->session->getLoggedInUser(); |
427
|
|
|
} |
428
|
|
|
if (!$user) { |
429
|
|
|
return false; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
$result = (bool)check_entity_relationship($user->guid, 'member', $this->guid); |
433
|
|
|
|
434
|
|
|
$params = array( |
435
|
|
|
'user' => $user, |
436
|
|
|
'group' => $this, |
437
|
|
|
); |
438
|
|
|
return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Join a user to this group. |
443
|
|
|
* |
444
|
|
|
* @param \ElggUser $user User joining the group. |
445
|
|
|
* |
446
|
|
|
* @return bool Whether joining was successful. |
447
|
|
|
*/ |
448
|
|
|
public function join(\ElggUser $user) { |
449
|
|
|
$result = add_entity_relationship($user->guid, 'member', $this->guid); |
450
|
|
|
|
451
|
|
|
if ($result) { |
452
|
|
|
$params = array('group' => $this, 'user' => $user); |
453
|
|
|
_elgg_services()->events->trigger('join', 'group', $params); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
return $result; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Remove a user from the group. |
461
|
|
|
* |
462
|
|
|
* @param \ElggUser $user User to remove from the group. |
463
|
|
|
* |
464
|
|
|
* @return bool Whether the user was removed from the group. |
465
|
|
|
*/ |
466
|
|
|
public function leave(\ElggUser $user) { |
467
|
|
|
// event needs to be triggered while user is still member of group to have access to group acl |
468
|
|
|
$params = array('group' => $this, 'user' => $user); |
469
|
|
|
_elgg_services()->events->trigger('leave', 'group', $params); |
470
|
|
|
|
471
|
|
|
return remove_entity_relationship($user->guid, 'member', $this->guid); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Load the \ElggGroup data from the database |
476
|
|
|
* |
477
|
|
|
* @param mixed $guid GUID of an \ElggGroup entity or database row from entity table |
478
|
|
|
* |
479
|
|
|
* @return bool |
480
|
|
|
*/ |
481
|
|
View Code Duplication |
protected function load($guid) { |
|
|
|
|
482
|
|
|
$attr_loader = new \Elgg\AttributeLoader(get_class(), 'group', $this->attributes); |
483
|
|
|
$attr_loader->requires_access_control = !($this instanceof \ElggPlugin); |
484
|
|
|
$attr_loader->secondary_loader = 'get_group_entity_as_row'; |
485
|
|
|
|
486
|
|
|
$attrs = $attr_loader->getRequiredAttributes($guid); |
487
|
|
|
if (!$attrs) { |
|
|
|
|
488
|
|
|
return false; |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
$this->attributes = $attrs; |
492
|
|
|
$this->tables_loaded = 2; |
493
|
|
|
$this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues()); |
494
|
|
|
_elgg_cache_entity($this); |
495
|
|
|
|
496
|
|
|
return true; |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* {@inheritdoc} |
501
|
|
|
*/ |
502
|
|
|
protected function update() { |
503
|
|
|
global $CONFIG; |
504
|
|
|
|
505
|
|
|
if (!parent::update()) { |
506
|
|
|
return false; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
$guid = (int)$this->guid; |
510
|
|
|
$name = sanitize_string($this->name); |
511
|
|
|
$description = sanitize_string($this->description); |
512
|
|
|
|
513
|
|
|
$query = "UPDATE {$CONFIG->dbprefix}groups_entity set" |
514
|
|
|
. " name='$name', description='$description' where guid=$guid"; |
515
|
|
|
|
516
|
|
|
return $this->getDatabase()->updateData($query) !== false; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* {@inheritdoc} |
521
|
|
|
*/ |
522
|
|
View Code Duplication |
protected function create() { |
|
|
|
|
523
|
|
|
global $CONFIG; |
524
|
|
|
|
525
|
|
|
$guid = parent::create(); |
526
|
|
|
if (!$guid) { |
527
|
|
|
// @todo this probably means permission to create entity was denied |
528
|
|
|
// Is returning false the correct thing to do |
529
|
|
|
return false; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
$name = sanitize_string($this->name); |
533
|
|
|
$description = sanitize_string($this->description); |
534
|
|
|
|
535
|
|
|
$query = "INSERT into {$CONFIG->dbprefix}groups_entity" |
536
|
|
|
. " (guid, name, description) values ($guid, '$name', '$description')"; |
537
|
|
|
|
538
|
|
|
$result = $this->getDatabase()->insertData($query); |
539
|
|
|
if ($result === false) { |
540
|
|
|
// TODO(evan): Throw an exception here? |
541
|
|
|
return false; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
return $guid; |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* {@inheritdoc} |
549
|
|
|
*/ |
550
|
|
View Code Duplication |
protected function prepareObject($object) { |
|
|
|
|
551
|
|
|
$object = parent::prepareObject($object); |
552
|
|
|
$object->name = $this->getDisplayName(); |
553
|
|
|
$object->description = $this->description; |
554
|
|
|
unset($object->read_access); |
555
|
|
|
return $object; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
|
559
|
|
|
// EXPORTABLE INTERFACE //////////////////////////////////////////////////////////// |
560
|
|
|
|
561
|
|
|
/** |
562
|
|
|
* Return an array of fields which can be exported. |
563
|
|
|
* |
564
|
|
|
* @return array |
565
|
|
|
* @deprecated 1.9 Use toObject() |
566
|
|
|
*/ |
567
|
|
|
public function getExportableValues() { |
568
|
|
|
return array_merge(parent::getExportableValues(), array( |
|
|
|
|
569
|
|
|
'name', |
570
|
|
|
'description', |
571
|
|
|
)); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Can a user comment on this group? |
576
|
|
|
* |
577
|
|
|
* @see \ElggEntity::canComment() |
578
|
|
|
* |
579
|
|
|
* @param int $user_guid User guid (default is logged in user) |
580
|
|
|
* @return bool |
581
|
|
|
* @since 1.8.0 |
582
|
|
|
*/ |
583
|
|
|
public function canComment($user_guid = 0) { |
584
|
|
|
$result = parent::canComment($user_guid); |
585
|
|
|
if ($result !== null) { |
586
|
|
|
return $result; |
587
|
|
|
} |
588
|
|
|
return false; |
589
|
|
|
} |
590
|
|
|
} |
591
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.