Elgg  Version 2.3
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  }
29 
38  final public static function getExternalAttributes() {
39  return [
40  'name' => null,
41  'description' => null,
42  ];
43  }
44 
55  public function __construct($row = null) {
56  $this->initializeAttributes();
57 
58  if (!empty($row)) {
59  // Is $guid is a entity table DB row
60  if ($row instanceof \stdClass) {
61  // Load the rest
62  if (!$this->load($row)) {
63  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
64  throw new \IOException($msg);
65  }
66  } else if (is_numeric($row)) {
67  // $row is a GUID so load entity
68  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
69  if (!$this->load($row)) {
70  throw new \IOException("Failed to load new " . get_class() . " from GUID:" . $row);
71  }
72  } else {
73  throw new \InvalidParameterException("Unrecognized value passed to constuctor.");
74  }
75  }
76  }
77 
81  public function getDisplayName() {
82  return $this->name;
83  }
84 
88  public function setDisplayName($displayName) {
89  $this->name = $displayName;
90  }
91 
99  public function addObjectToGroup(\ElggObject $object) {
100  $object->container_guid = $this->guid;
101  return $object->save();
102  }
103 
112  public function removeObjectFromGroup($object) {
113  if (is_numeric($object)) {
114  elgg_deprecated_notice('\ElggGroup::removeObjectFromGroup() takes an \ElggObject not a guid.', 1.9);
116  if (!elgg_instanceof($object, 'object')) {
117  return false;
118  }
119  }
120 
121  $object->container_guid = $object->owner_guid;
122  return $object->save();
123  }
124 
135  public function __get($name) {
136  if ($name == 'username') {
137  return 'group:' . $this->getGUID();
138  }
139  return parent::__get($name);
140  }
141 
149  public function get($name) {
150  elgg_deprecated_notice("Use -> instead of get()", 1.9);
151  return $this->__get($name);
152  }
153 
179  public function addFriend($friend_guid) {
180  elgg_deprecated_notice("\ElggGroup::addFriend() is deprecated. Use \ElggGroup::join()", 1.9);
182  return $user ? $this->join($user) : false;
183  }
184 
195  public function removeFriend($friend_guid) {
196  elgg_deprecated_notice("\ElggGroup::removeFriend() is deprecated. Use \ElggGroup::leave()", 1.9);
198  return $user ? $this->leave($user) : false;
199  }
200 
209  public function isFriend() {
210  elgg_deprecated_notice("\ElggGroup::isFriend() is deprecated. Use \ElggGroup::isMember()", 1.9);
211  return $this->isMember();
212  }
213 
222  public function isFriendsWith($user_guid) {
223  elgg_deprecated_notice("\ElggGroup::isFriendsWith() is deprecated. Use \ElggGroup::isMember()", 1.9);
225  return $user ? $this->isMember($user) : false;
226  }
227 
236  public function isFriendOf($user_guid) {
237  elgg_deprecated_notice("\ElggGroup::isFriendOf() is deprecated. Use \ElggGroup::isMember()", 1.9);
239  return $user ? $this->isMember($user) : false;
240  }
241 
252  public function getFriends($subtype = "", $limit = 10, $offset = 0) {
253  elgg_deprecated_notice("\ElggGroup::getFriends() is deprecated. Use \ElggGroup::getMembers()", 1.9);
254  $options = [
255  'limit' => $limit,
256  'offset' => $offset,
257  ];
258  if ($subtype) {
259  $options['subtype'] = $subtype;
260  }
261  return $this->getMembers($options);
262  }
263 
274  public function getFriendsOf($subtype = "", $limit = 10, $offset = 0) {
275  elgg_deprecated_notice("\ElggGroup::getFriendsOf() is deprecated. Use \ElggGroup::getMembers()", 1.9);
276  return get_group_members($this->getGUID(), $limit, $offset);
277  }
278 
289  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
290  elgg_deprecated_notice("\ElggGroup::getObjects() is deprecated. Use elgg_get_entities()", 1.9);
291  $options = [
292  'type' => 'object',
293  'container_guid' => $this->guid,
294  'limit' => $limit,
295  'offset' => $offset,
296  ];
297  if ($subtype) {
298  $options['subtype'] = $subtype;
299  }
300  return elgg_get_entities($options);
301  }
302 
313  public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0) {
314  elgg_deprecated_notice("\ElggGroup::getFriendsObjects() is deprecated. Use elgg_get_entities()", 1.9);
315  $options = [
316  'type' => 'object',
317  'limit' => $limit,
318  'offset' => $offset,
319  'relationship' => 'member',
320  'inverse_relationship' => true,
321  'relationship_guid' => $this->guid,
322  'relationship_join_on' => 'owner_guid',
323  ];
324  if ($subtype) {
325  $options['subtype'] = $subtype;
326  }
328  }
329 
338  public function countObjects($subtype = "") {
339  elgg_deprecated_notice("\ElggGroup::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
340  $options = [
341  'count' => true,
342  'type' => 'object',
343  'container_guid' => $this->guid,
344  ];
345  if ($subtype) {
346  $options['subtype'] = $subtype;
347  }
348  return elgg_get_entities($options);
349  }
350 
368  public function getMembers($options = array(), $offset = 0, $count = false) {
369  if (!is_array($options)) {
370  elgg_deprecated_notice('\ElggGroup::getMembers() takes an options array.', 1.9);
371  $options = array(
372  'relationship' => 'member',
373  'relationship_guid' => $this->getGUID(),
374  'inverse_relationship' => true,
375  'type' => 'user',
376  'limit' => $options,
377  'offset' => $offset,
378  'count' => $count,
379  );
380  } else {
381  $options['relationship'] = 'member';
382  $options['relationship_guid'] = $this->getGUID();
383  $options['inverse_relationship'] = true;
384  $options['type'] = 'user';
385  }
386 
388  }
389 
395  public function isPublicMembership() {
396  return ($this->membership == ACCESS_PUBLIC);
397  }
398 
406  public function getContentAccessMode() {
407  $mode = $this->content_access_mode;
408 
409  if (!is_string($mode)) {
410  // fallback to 1.8 default behavior
411  if ($this->isPublicMembership()) {
412  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
413  } else {
414  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
415  }
416  $this->content_access_mode = $mode;
417  }
418 
419  // only support two modes for now
420  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
421  return $mode;
422  }
423  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
424  }
425 
434  public function setContentAccessMode($mode) {
435  if (!$mode && $this->content_access_mode) {
436  return;
437  }
438 
439  // only support two modes for now
440  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
441  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
442  }
443 
444  $this->content_access_mode = $mode;
445  }
446 
454  public function isMember(\ElggUser $user = null) {
455  if ($user == null) {
456  $user = _elgg_services()->session->getLoggedInUser();
457  }
458  if (!$user) {
459  return false;
460  }
461 
462  $result = (bool)check_entity_relationship($user->guid, 'member', $this->guid);
463 
464  $params = array(
465  'user' => $user,
466  'group' => $this,
467  );
468  return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result);
469  }
470 
478  public function join(\ElggUser $user) {
479  $result = add_entity_relationship($user->guid, 'member', $this->guid);
480 
481  if ($result) {
482  $params = array('group' => $this, 'user' => $user);
483  _elgg_services()->events->trigger('join', 'group', $params);
484  }
485 
486  return $result;
487  }
488 
496  public function leave(\ElggUser $user) {
497  // event needs to be triggered while user is still member of group to have access to group acl
498  $params = array('group' => $this, 'user' => $user);
499  _elgg_services()->events->trigger('leave', 'group', $params);
500 
501  return remove_entity_relationship($user->guid, 'member', $this->guid);
502  }
503 
511  protected function load($guid) {
512  $attr_loader = new \Elgg\AttributeLoader(get_class(), 'group', $this->attributes);
513  $attr_loader->requires_access_control = !($this instanceof \ElggPlugin);
514  $attr_loader->secondary_loader = 'get_group_entity_as_row';
515 
516  $attrs = $attr_loader->getRequiredAttributes($guid);
517  if (!$attrs) {
518  return false;
519  }
520 
521  $this->attributes = $attrs;
522  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
523  _elgg_services()->entityCache->set($this);
524 
525  return true;
526  }
527 
531  protected function update() {
532  global $CONFIG;
533 
534  if (!parent::update()) {
535  return false;
536  }
537 
538  $guid = (int)$this->guid;
539  $name = sanitize_string($this->name);
541 
542  $query = "UPDATE {$CONFIG->dbprefix}groups_entity set"
543  . " name='$name', description='$description' where guid=$guid";
544 
545  return $this->getDatabase()->updateData($query) !== false;
546  }
547 
551  protected function create() {
552  global $CONFIG;
553 
554  $guid = parent::create();
555  if (!$guid) {
556  // @todo this probably means permission to create entity was denied
557  // Is returning false the correct thing to do
558  return false;
559  }
560 
561  $name = sanitize_string($this->name);
563 
564  $query = "INSERT into {$CONFIG->dbprefix}groups_entity"
565  . " (guid, name, description) values ($guid, '$name', '$description')";
566 
567  $result = $this->getDatabase()->insertData($query);
568  if ($result === false) {
569  // TODO(evan): Throw an exception here?
570  return false;
571  }
572 
573  return $guid;
574  }
575 
579  protected function prepareObject($object) {
580  $object = parent::prepareObject($object);
581  $object->name = $this->getDisplayName();
582  $object->description = $this->description;
583  unset($object->read_access);
584  return $object;
585  }
586 
587 
588  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
589 
596  public function getExportableValues() {
597  return array_merge(parent::getExportableValues(), array(
598  'name',
599  'description',
600  ));
601  }
602 
613  public function canComment($user_guid = 0, $default = null) {
614  $result = parent::canComment($user_guid, $default);
615  if ($result !== null) {
616  return $result;
617  }
618  return false;
619  }
620 }
countObjects($subtype="")
For compatibility with Friendable.
Definition: ElggGroup.php:338
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
getDatabase()
Provides a pointer to the database object.
Definition: ElggData.php:48
getObjects($subtype="", $limit=10, $offset=0)
Get objects contained in this group.
Definition: ElggGroup.php:289
prepareObject($object)
{}
Definition: ElggGroup.php:579
addObjectToGroup(\ElggObject $object)
Add an to this group.
Definition: ElggGroup.php:99
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:209
getFriendsObjects($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:313
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:368
create()
{}
Definition: ElggGroup.php:551
save()
Save an entity.
addFriend($friend_guid)
Start friendable compatibility block:
Definition: ElggGroup.php:179
$subtype
Definition: delete.php:28
if(!$count) $offset
Definition: pagination.php:26
getDisplayName()
{}
Definition: ElggGroup.php:81
getGUID()
Returns the guid.
$default
Definition: checkbox.php:34
$guid
Removes an admin notice.
__get($name)
Wrapper around ::__get()
Definition: ElggGroup.php:135
__construct($row=null)
Construct a new group entity.
Definition: ElggGroup.php:55
static getExternalAttributes()
Get default values for attributes stored in a separate table.
Definition: ElggGroup.php:38
sanitize_string($string)
Sanitizes a string for use in a query.
Definition: database.php:153
$options
Elgg admin footer.
Definition: footer.php:6
getFriends($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:252
$params
Definition: login.php:72
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:736
$limit
Definition: userpicker.php:38
getContentAccessMode()
Return the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:406
if($categories) $description
Definition: full.php:176
get_user($guid)
Get a user object from a GUID.
Definition: users.php:87
getFriendsOf($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:274
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
global $CONFIG
$user
Definition: ban.php:13
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:326
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:1098
join(\ElggUser $user)
Join a user to this group.
Definition: ElggGroup.php:478
elgg global
Pointer to the global context.
Definition: elgglib.js:12
isFriendsWith($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:222
$friend_guid
Definition: add.php:10
load($guid)
Load the data from the database.
Definition: ElggGroup.php:511
isFriendOf($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:236
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:395
canComment($user_guid=0, $default=null)
Can a user comment on this group?
Definition: ElggGroup.php:613
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggGroup.php:596
$attrs
Definition: ajax_loader.php:30
const ACCESS_PUBLIC
Definition: elgglib.php:2084
leave(\ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:496
removeObjectFromGroup($object)
Remove an object from this containing group and sets the container to be object&#39;s owner...
Definition: ElggGroup.php:112
$site name
setDisplayName($displayName)
{}
Definition: ElggGroup.php:88
isMember(\ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:454
update()
{}
Definition: ElggGroup.php:531
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
$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
setContentAccessMode($mode)
Set the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:434
initializeAttributes()
Sets the type to group.
Definition: ElggGroup.php:23
removeFriend($friend_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:195
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204