Elgg  Version 1.11
ElggGroup.php
Go to the documentation of this file.
1 <?php
2 
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 
23  protected function initializeAttributes() {
24  parent::initializeAttributes();
25 
26  $this->attributes['type'] = "group";
27  $this->attributes += self::getExternalAttributes();
28  $this->tables_split = 2;
29  }
30 
39  final public static function getExternalAttributes() {
40  return [
41  'name' => null,
42  'description' => null,
43  ];
44  }
45 
56  public function __construct($row = null) {
57  $this->initializeAttributes();
58 
59  // compatibility for 1.7 api.
60  $this->initialise_attributes(false);
61 
62  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  }
87 
91  public function getDisplayName() {
92  return $this->name;
93  }
94 
98  public function setDisplayName($displayName) {
99  $this->name = $displayName;
100  }
101 
109  public function addObjectToGroup(\ElggObject $object) {
110  $object->container_guid = $this->guid;
111  return $object->save();
112  }
113 
122  public function removeObjectFromGroup($object) {
123  if (is_numeric($object)) {
124  elgg_deprecated_notice('\ElggGroup::removeObjectFromGroup() takes an \ElggObject not a guid.', 1.9);
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 
145  public function __get($name) {
146  if ($name == 'username') {
147  return 'group:' . $this->getGUID();
148  }
149  return parent::__get($name);
150  }
151 
159  public function get($name) {
160  elgg_deprecated_notice("Use -> instead of get()", 1.9);
161  return $this->__get($name);
162  }
163 
189  public function addFriend($friend_guid) {
190  elgg_deprecated_notice("\ElggGroup::addFriend() is deprecated. Use \ElggGroup::join()", 1.9);
192  return $user ? $this->join($user) : false;
193  }
194 
205  public function removeFriend($friend_guid) {
206  elgg_deprecated_notice("\ElggGroup::removeFriend() is deprecated. Use \ElggGroup::leave()", 1.9);
208  return $user ? $this->leave($user) : false;
209  }
210 
219  public function isFriend() {
220  elgg_deprecated_notice("\ElggGroup::isFriend() is deprecated. Use \ElggGroup::isMember()", 1.9);
221  return $this->isMember();
222  }
223 
232  public function isFriendsWith($user_guid) {
233  elgg_deprecated_notice("\ElggGroup::isFriendsWith() is deprecated. Use \ElggGroup::isMember()", 1.9);
235  return $user ? $this->isMember($user) : false;
236  }
237 
246  public function isFriendOf($user_guid) {
247  elgg_deprecated_notice("\ElggGroup::isFriendOf() is deprecated. Use \ElggGroup::isMember()", 1.9);
249  return $user ? $this->isMember($user) : false;
250  }
251 
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 
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 
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 
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 
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 
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  } else {
355  $options['relationship'] = 'member';
356  $options['relationship_guid'] = $this->getGUID();
357  $options['inverse_relationship'] = true;
358  $options['type'] = 'user';
359  }
360 
362  }
363 
369  public function isPublicMembership() {
370  return ($this->membership == ACCESS_PUBLIC);
371  }
372 
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 
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 
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 
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 
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 
481  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 
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);
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 
522  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);
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 
550  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 
567  public function getExportableValues() {
568  return array_merge(parent::getExportableValues(), array(
569  'name',
570  'description',
571  ));
572  }
573 
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 }
get_objects_in_group($group_guid, $subtype="", $owner_guid=0, $site_guid=0, $order_by="", $limit=10, $offset=0, $count=FALSE)
Return an array of objects in a given container.
countObjects($subtype="")
For compatibility with Friendable.
Definition: ElggGroup.php:320
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:67
getObjects($subtype="", $limit=10, $offset=0)
Get objects contained in this group.
Definition: ElggGroup.php:292
prepareObject($object)
{}
Definition: ElggGroup.php:550
addObjectToGroup(\ElggObject $object)
Add an to this group.
Definition: ElggGroup.php:109
get_group_members($group_guid, $limit=10, $offset=0, $site_guid=0, $count=false)
Return a list of this group&#39;s members.
$mode
Configure site maintenance mode.
loadAdditionalSelectValues(array $data)
Stores non-attributes from the loading of the entity as volatile data.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
isFriend()
For compatibility with Friendable.
Definition: ElggGroup.php:219
getFriendsObjects($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:307
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
getMembers($options=array(), $offset=0, $count=false)
End friendable compatibility block.
Definition: ElggGroup.php:342
create()
{}
Definition: ElggGroup.php:522
$object
Definition: upgrade.php:12
save()
Save an entity.
addFriend($friend_guid)
Start friendable compatibility block:
Definition: ElggGroup.php:189
_elgg_cache_entity(\ElggEntity $entity)
Cache an entity.
Definition: entities.php:92
$value
Definition: longtext.php:26
if($screenshots) $description
Definition: full.php:173
if(!$count) $offset
Definition: pagination.php:25
getDisplayName()
{}
Definition: ElggGroup.php:91
getGUID()
Returns the guid.
$guid
Removes an admin notice.
__get($name)
Wrapper around ::__get()
Definition: ElggGroup.php:145
__construct($row=null)
Construct a new group entity.
Definition: ElggGroup.php:56
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggGroup.php:39
sanitize_string($string)
Sanitize a string for database use.
Definition: database.php:140
getFriends($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:262
$params
Definition: login.php:72
$options
Definition: index.php:14
if(!$site) if(!($site instanceof ElggSite)) $site description
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:922
$limit
Definition: userpicker.php:31
getContentAccessMode()
Return the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:380
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
$key
Definition: summary.php:34
getFriendsOf($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:277
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
_elgg_services()
Definition: autoloader.php:14
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
$user
Definition: ban.php:13
check_entity_relationship($guid_one, $relationship, $guid_two)
Check if a relationship exists between two entities.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
join(\ElggUser $user)
Join a user to this group.
Definition: ElggGroup.php:448
elgg global
Pointer to the global context.
Definition: elgglib.js:12
isFriendsWith($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:232
$friend_guid
Definition: add.php:10
load($guid)
Load the data from the database.
Definition: ElggGroup.php:481
isFriendOf($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:246
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:369
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggGroup.php:567
$attrs
Definition: ajax_loader.php:30
const ACCESS_PUBLIC
Definition: elgglib.php:1956
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:466
removeObjectFromGroup($object)
Remove an object from this containing group and sets the container to be object&#39;s owner...
Definition: ElggGroup.php:122
$site name
setDisplayName($displayName)
{}
Definition: ElggGroup.php:98
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:424
update()
{}
Definition: ElggGroup.php:502
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
canComment($user_guid=0)
Can a user comment on this group?
Definition: ElggGroup.php:583
$row
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
$user_guid
Avatar remove action.
Definition: remove.php:6
$subtype
Definition: river.php:12
setContentAccessMode($mode)
Set the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:408
initializeAttributes()
Sets the type to group.
Definition: ElggGroup.php:23
removeFriend($friend_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:205
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382