Elgg  Version 1.9
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['name'] = null;
28  $this->attributes['description'] = null;
29  $this->tables_split = 2;
30  }
31 
42  public function __construct($row = null) {
43  $this->initializeAttributes();
44 
45  // compatibility for 1.7 api.
46  $this->initialise_attributes(false);
47 
48  if (!empty($row)) {
49  // Is $guid is a entity table DB row
50  if ($row instanceof stdClass) {
51  // Load the rest
52  if (!$this->load($row)) {
53  $msg = "Failed to load new " . get_class() . " for GUID:" . $row->guid;
54  throw new IOException($msg);
55  }
56  } else if ($row instanceof ElggGroup) {
57  // $row is an ElggGroup so this is a copy constructor
58  elgg_deprecated_notice('This type of usage of the ElggGroup constructor was deprecated. Please use the clone method.', 1.7);
59  foreach ($row->attributes as $key => $value) {
60  $this->attributes[$key] = $value;
61  }
62  } else if (is_numeric($row)) {
63  // $row is a GUID so load entity
64  elgg_deprecated_notice('Passing a GUID to constructor is deprecated. Use get_entity()', 1.9);
65  if (!$this->load($row)) {
66  throw new IOException("Failed to load new " . get_class() . " from GUID:" . $row);
67  }
68  } else {
69  throw new InvalidParameterException("Unrecognized value passed to constuctor.");
70  }
71  }
72  }
73 
77  public function getDisplayName() {
78  return $this->name;
79  }
80 
84  public function setDisplayName($displayName) {
85  $this->name = $displayName;
86  }
87 
95  public function addObjectToGroup(ElggObject $object) {
96  $object->container_guid = $this->guid;
97  return $object->save();
98  }
99 
108  public function removeObjectFromGroup($object) {
109  if (is_numeric($object)) {
110  elgg_deprecated_notice('ElggGroup::removeObjectFromGroup() takes an ElggObject not a guid.', 1.9);
112  if (!elgg_instanceof($object, 'object')) {
113  return false;
114  }
115  }
116 
117  $object->container_guid = $object->owner_guid;
118  return $object->save();
119  }
120 
131  public function __get($name) {
132  if ($name == 'username') {
133  return 'group:' . $this->getGUID();
134  }
135  return parent::__get($name);
136  }
137 
145  public function get($name) {
146  elgg_deprecated_notice("Use -> instead of get()", 1.9);
147  return $this->__get($name);
148  }
149 
175  public function addFriend($friend_guid) {
176  elgg_deprecated_notice("ElggGroup::addFriend() is deprecated. Use ElggGroup::join()", 1.9);
178  return $user ? $this->join($user) : false;
179  }
180 
191  public function removeFriend($friend_guid) {
192  elgg_deprecated_notice("ElggGroup::removeFriend() is deprecated. Use ElggGroup::leave()", 1.9);
194  return $user ? $this->leave($user) : false;
195  }
196 
205  public function isFriend() {
206  elgg_deprecated_notice("ElggGroup::isFriend() is deprecated. Use ElggGroup::isMember()", 1.9);
207  return $this->isMember();
208  }
209 
218  public function isFriendsWith($user_guid) {
219  elgg_deprecated_notice("ElggGroup::isFriendsWith() is deprecated. Use ElggGroup::isMember()", 1.9);
221  return $user ? $this->isMember($user) : false;
222  }
223 
232  public function isFriendOf($user_guid) {
233  elgg_deprecated_notice("ElggGroup::isFriendOf() is deprecated. Use ElggGroup::isMember()", 1.9);
235  return $user ? $this->isMember($user) : false;
236  }
237 
248  public function getFriends($subtype = "", $limit = 10, $offset = 0) {
249  elgg_deprecated_notice("ElggGroup::getFriends() is deprecated. Use ElggGroup::getMembers()", 1.9);
250  return get_group_members($this->getGUID(), $limit, $offset);
251  }
252 
263  public function getFriendsOf($subtype = "", $limit = 10, $offset = 0) {
264  elgg_deprecated_notice("ElggGroup::getFriendsOf() is deprecated. Use ElggGroup::getMembers()", 1.9);
265  return get_group_members($this->getGUID(), $limit, $offset);
266  }
267 
278  public function getObjects($subtype = "", $limit = 10, $offset = 0) {
279  elgg_deprecated_notice("ElggGroup::getObjects() is deprecated. Use elgg_get_entities()", 1.9);
280  return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false);
281  }
282 
293  public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0) {
294  elgg_deprecated_notice("ElggGroup::getFriendsObjects() is deprecated. Use elgg_get_entities()", 1.9);
295  return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false);
296  }
297 
306  public function countObjects($subtype = "") {
307  elgg_deprecated_notice("ElggGroup::countObjects() is deprecated. Use elgg_get_entities()", 1.9);
308  return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", 10, 0, true);
309  }
310 
328  public function getMembers($options = array(), $offset = 0, $count = false) {
329  if (!is_array($options)) {
330  elgg_deprecated_notice('ElggGroup::getMembers() takes an options array.', 1.9);
331  $options = array(
332  'relationship' => 'member',
333  'relationship_guid' => $this->getGUID(),
334  'inverse_relationship' => true,
335  'type' => 'user',
336  'limit' => $options,
337  'offset' => $offset,
338  'count' => $count,
339  );
340  } else {
341  $options['relationship'] = 'member';
342  $options['relationship_guid'] = $this->getGUID();
343  $options['inverse_relationship'] = true;
344  $options['type'] = 'user';
345  }
346 
348  }
349 
355  public function isPublicMembership() {
356  return ($this->membership == ACCESS_PUBLIC);
357  }
358 
366  public function getContentAccessMode() {
367  $mode = $this->content_access_mode;
368 
369  if (!is_string($mode)) {
370  // fallback to 1.8 default behavior
371  if ($this->isPublicMembership()) {
372  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
373  } else {
374  $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY;
375  }
376  $this->content_access_mode = $mode;
377  }
378 
379  // only support two modes for now
380  if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
381  return $mode;
382  }
383  return self::CONTENT_ACCESS_MODE_UNRESTRICTED;
384  }
385 
394  public function setContentAccessMode($mode) {
395  // only support two modes for now
396  if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) {
397  $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED;
398  }
399 
400  $this->content_access_mode = $mode;
401  }
402 
410  public function isMember(ElggUser $user = null) {
411  if ($user == null) {
413  }
414  if (!$user) {
415  return false;
416  }
417 
418  $result = (bool)check_entity_relationship($user->guid, 'member', $this->guid);
419 
420  $params = array(
421  'user' => $user,
422  'group' => $this,
423  );
424  return elgg_trigger_plugin_hook('is_member', 'group', $params, $result);
425  }
426 
434  public function join(ElggUser $user) {
435  $result = add_entity_relationship($user->guid, 'member', $this->guid);
436 
437  if ($result) {
438  $params = array('group' => $this, 'user' => $user);
439  elgg_trigger_event('join', 'group', $params);
440  }
441 
442  return $result;
443  }
444 
452  public function leave(ElggUser $user) {
453  // event needs to be triggered while user is still member of group to have access to group acl
454  $params = array('group' => $this, 'user' => $user);
455  elgg_trigger_event('leave', 'group', $params);
456 
457  return remove_entity_relationship($user->guid, 'member', $this->guid);
458  }
459 
467  protected function load($guid) {
468  $attr_loader = new Elgg_AttributeLoader(get_class(), 'group', $this->attributes);
469  $attr_loader->requires_access_control = !($this instanceof ElggPlugin);
470  $attr_loader->secondary_loader = 'get_group_entity_as_row';
471 
472  $attrs = $attr_loader->getRequiredAttributes($guid);
473  if (!$attrs) {
474  return false;
475  }
476 
477  $this->attributes = $attrs;
478  $this->tables_loaded = 2;
479  $this->loadAdditionalSelectValues($attr_loader->getAdditionalSelectValues());
480  _elgg_cache_entity($this);
481 
482  return true;
483  }
484 
488  protected function update() {
489  global $CONFIG;
490 
491  if (!parent::update()) {
492  return false;
493  }
494 
495  $guid = (int)$this->guid;
496  $name = sanitize_string($this->name);
498 
499  $query = "UPDATE {$CONFIG->dbprefix}groups_entity set"
500  . " name='$name', description='$description' where guid=$guid";
501 
502  return $this->getDatabase()->updateData($query) !== false;
503  }
504 
508  protected function create() {
509  global $CONFIG;
510 
511  $guid = parent::create();
512  $name = sanitize_string($this->name);
514 
515  $query = "INSERT into {$CONFIG->dbprefix}groups_entity"
516  . " (guid, name, description) values ($guid, '$name', '$description')";
517 
518  $result = $this->getDatabase()->insertData($query);
519  if ($result === false) {
520  // TODO(evan): Throw an exception here?
521  return false;
522  }
523 
524  return $guid;
525  }
526 
530  protected function prepareObject($object) {
531  $object = parent::prepareObject($object);
532  $object->name = $this->getDisplayName();
533  $object->description = $this->description;
534  unset($object->read_access);
535  return $object;
536  }
537 
538 
539  // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
540 
547  public function getExportableValues() {
548  return array_merge(parent::getExportableValues(), array(
549  'name',
550  'description',
551  ));
552  }
553 
563  public function canComment($user_guid = 0) {
564  $result = parent::canComment($user_guid);
565  if ($result !== null) {
566  return $result;
567  }
568  return false;
569  }
570 }
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:306
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:278
prepareObject($object)
{}
Definition: ElggGroup.php:530
leave(ElggUser $user)
Remove a user from the group.
Definition: ElggGroup.php:452
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:205
getFriendsObjects($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:293
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:328
create()
{}
Definition: ElggGroup.php:508
$object
Definition: upgrade.php:12
if(elgg_in_context('widget')) $offset
Definition: pagination.php:20
save()
Save an entity.
addFriend($friend_guid)
Start friendable compatibility block:
Definition: ElggGroup.php:175
$value
Definition: longtext.php:29
$comment description
Definition: save.php:49
if($screenshots) $description
Definition: full.php:173
getDisplayName()
{}
Definition: ElggGroup.php:77
getGUID()
Returns the guid.
$guid
Removes an admin notice.
__get($name)
Wrapper around ElggEntity::__get()
Definition: ElggGroup.php:131
__construct($row=null)
Construct a new group entity.
Definition: ElggGroup.php:42
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:248
$params
Definition: login.php:72
$options
Definition: index.php:14
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an ElggEntity and optionally for type and subtype.
Definition: entities.php:1886
$limit
Definition: userpicker.php:33
getContentAccessMode()
Return the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:366
get_user($guid)
Get a user object from a GUID.
Definition: users.php:222
$key
Definition: summary.php:34
getFriendsOf($subtype="", $limit=10, $offset=0)
For compatibility with Friendable.
Definition: ElggGroup.php:263
const CONTENT_ACCESS_MODE_UNRESTRICTED
Definition: ElggGroup.php:15
global $CONFIG
initialise_attributes($pre18_api=true)
Initialise the attributes array.
Definition: ElggData.php:39
$user
Definition: ban.php:13
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
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)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
isFriendsWith($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:218
$friend_guid
Definition: add.php:10
load($guid)
Load the ElggGroup data from the database.
Definition: ElggGroup.php:467
isMember(ElggUser $user=null)
Is the given user a member of this group?
Definition: ElggGroup.php:410
isFriendOf($user_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:232
const CONTENT_ACCESS_MODE_MEMBERS_ONLY
Definition: ElggGroup.php:16
isPublicMembership()
Returns whether the current group has open membership or not.
Definition: ElggGroup.php:355
getExportableValues()
Return an array of fields which can be exported.
Definition: ElggGroup.php:547
$attrs
Definition: ajax_loader.php:30
const ACCESS_PUBLIC
Definition: elgglib.php:2123
$count
Definition: tools.php:19
removeObjectFromGroup($object)
Remove an object from this containing group and sets the container to be object&#39;s owner...
Definition: ElggGroup.php:108
setDisplayName($displayName)
{}
Definition: ElggGroup.php:84
update()
{}
Definition: ElggGroup.php:488
canComment($user_guid=0)
Can a user comment on this group?
Definition: ElggGroup.php:563
$row
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
addObjectToGroup(ElggObject $object)
Add an ElggObject to this group.
Definition: ElggGroup.php:95
elgg_get_entities_from_relationship($options)
Return entities matching a given query joining against a relationship.
join(ElggUser $user)
Join a user to this group.
Definition: ElggGroup.php:434
$user_guid
Avatar remove action.
Definition: remove.php:6
elgg_trigger_event($event, $object_type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: elgglib.php:720
$subtype
Definition: river.php:10
setContentAccessMode($mode)
Set the content access mode used by group_gatekeeper()
Definition: ElggGroup.php:394
_elgg_cache_entity(ElggEntity $entity)
Cache an entity.
Definition: entities.php:101
Elgg Object.
Definition: ElggObject.php:22
initializeAttributes()
Sets the type to group.
Definition: ElggGroup.php:23
removeFriend($friend_guid)
For compatibility with Friendable.
Definition: ElggGroup.php:191
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604