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: /**
16: * Sets the type to group.
17: *
18: * @return void
19: */
20: protected function initializeAttributes() {
21: parent::initializeAttributes();
22:
23: $this->attributes['type'] = "group";
24: $this->attributes['name'] = NULL;
25: $this->attributes['description'] = NULL;
26: $this->attributes['tables_split'] = 2;
27: }
28:
29: /**
30: * Construct a new group entity, optionally from a given guid value.
31: *
32: * @param mixed $guid If an int, load that GUID.
33: * If an entity table db row, then will load the rest of the data.
34: *
35: * @throws IOException|InvalidParameterException if there was a problem creating the group.
36: */
37: function __construct($guid = null) {
38: $this->initializeAttributes();
39:
40: // compatibility for 1.7 api.
41: $this->initialise_attributes(false);
42:
43: if (!empty($guid)) {
44: // Is $guid is a entity table DB row
45: if ($guid instanceof stdClass) {
46: // Load the rest
47: if (!$this->load($guid)) {
48: $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
49: throw new IOException($msg);
50: }
51: } else if ($guid instanceof ElggGroup) {
52: // $guid is an ElggGroup so this is a copy constructor
53: elgg_deprecated_notice('This type of usage of the ElggGroup constructor was deprecated. Please use the clone method.', 1.7);
54:
55: foreach ($guid->attributes as $key => $value) {
56: $this->attributes[$key] = $value;
57: }
58: } else if ($guid instanceof ElggEntity) {
59: // @todo why separate from else
60: throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggGroup'));
61: } else if (is_numeric($guid)) {
62: // $guid is a GUID so load entity
63: if (!$this->load($guid)) {
64: throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
65: }
66: } else {
67: throw new InvalidParameterException(elgg_echo('InvalidParameterException:UnrecognisedValue'));
68: }
69: }
70: }
71:
72: /**
73: * Add an ElggObject to this group.
74: *
75: * @param ElggObject $object The object.
76: *
77: * @return bool
78: */
79: public function addObjectToGroup(ElggObject $object) {
80: return add_object_to_group($this->getGUID(), $object->getGUID());
81: }
82:
83: /**
84: * Remove an object from the containing group.
85: *
86: * @param int $guid The guid of the object.
87: *
88: * @return bool
89: */
90: public function removeObjectFromGroup($guid) {
91: return remove_object_from_group($this->getGUID(), $guid);
92: }
93:
94: /**
95: * Returns an attribute or metadata.
96: *
97: * @see ElggEntity::get()
98: *
99: * @param string $name Name
100: *
101: * @return mixed
102: */
103: public function get($name) {
104: if ($name == 'username') {
105: return 'group:' . $this->getGUID();
106: }
107: return parent::get($name);
108: }
109:
110: /**
111: * Start friendable compatibility block:
112: *
113: * public function addFriend($friend_guid);
114: public function removeFriend($friend_guid);
115: public function isFriend();
116: public function isFriendsWith($user_guid);
117: public function isFriendOf($user_guid);
118: public function getFriends($subtype = "", $limit = 10, $offset = 0);
119: public function getFriendsOf($subtype = "", $limit = 10, $offset = 0);
120: public function getObjects($subtype="", $limit = 10, $offset = 0);
121: public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0);
122: public function countObjects($subtype = "");
123: */
124:
125: /**
126: * For compatibility with Friendable.
127: *
128: * Join a group when you friend ElggGroup.
129: *
130: * @param int $friend_guid The GUID of the user joining the group.
131: *
132: * @return bool
133: */
134: public function addFriend($friend_guid) {
135: return $this->join(get_entity($friend_guid));
136: }
137:
138: /**
139: * For compatibility with Friendable
140: *
141: * Leave group when you unfriend ElggGroup.
142: *
143: * @param int $friend_guid The GUID of the user leaving.
144: *
145: * @return bool
146: */
147: public function removeFriend($friend_guid) {
148: return $this->leave(get_entity($friend_guid));
149: }
150:
151: /**
152: * For compatibility with Friendable
153: *
154: * Friending a group adds you as a member
155: *
156: * @return bool
157: */
158: public function isFriend() {
159: return $this->isMember();
160: }
161:
162: /**
163: * For compatibility with Friendable
164: *
165: * @param int $user_guid The GUID of a user to check.
166: *
167: * @return bool
168: */
169: public function isFriendsWith($user_guid) {
170: return $this->isMember($user_guid);
171: }
172:
173: /**
174: * For compatibility with Friendable
175: *
176: * @param int $user_guid The GUID of a user to check.
177: *
178: * @return bool
179: */
180: public function isFriendOf($user_guid) {
181: return $this->isMember($user_guid);
182: }
183:
184: /**
185: * For compatibility with Friendable
186: *
187: * @param string $subtype The GUID of a user to check.
188: * @param int $limit Limit
189: * @param int $offset Offset
190: *
191: * @return bool
192: */
193: public function getFriends($subtype = "", $limit = 10, $offset = 0) {
194: return get_group_members($this->getGUID(), $limit, $offset);
195: }
196:
197: /**
198: * For compatibility with Friendable
199: *
200: * @param string $subtype The GUID of a user to check.
201: * @param int $limit Limit
202: * @param int $offset Offset
203: *
204: * @return bool
205: */
206: public function getFriendsOf($subtype = "", $limit = 10, $offset = 0) {
207: return get_group_members($this->getGUID(), $limit, $offset);
208: }
209:
210: /**
211: * Get objects contained in this group.
212: *
213: * @param string $subtype Entity subtype
214: * @param int $limit Limit
215: * @param int $offset Offset
216: *
217: * @return array|false
218: */
219: public function getObjects($subtype = "", $limit = 10, $offset = 0) {
220: // @todo are we deprecating this method, too?
221: return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false);
222: }
223:
224: /**
225: * For compatibility with Friendable
226: *
227: * @param string $subtype Entity subtype
228: * @param int $limit Limit
229: * @param int $offset Offset
230: *
231: * @return array|false
232: */
233: public function getFriendsObjects($subtype = "", $limit = 10, $offset = 0) {
234: // @todo are we deprecating this method, too?
235: return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", $limit, $offset, false);
236: }
237:
238: /**
239: * For compatibility with Friendable
240: *
241: * @param string $subtype Subtype of entities
242: *
243: * @return array|false
244: */
245: public function countObjects($subtype = "") {
246: // @todo are we deprecating this method, too?
247: return get_objects_in_group($this->getGUID(), $subtype, 0, 0, "", 10, 0, true);
248: }
249:
250: /**
251: * End friendable compatibility block
252: */
253:
254: /**
255: * Get a list of group members.
256: *
257: * @param int $limit Limit
258: * @param int $offset Offset
259: * @param bool $count Count
260: *
261: * @return mixed
262: */
263: public function getMembers($limit = 10, $offset = 0, $count = false) {
264: return get_group_members($this->getGUID(), $limit, $offset, 0, $count);
265: }
266:
267: /**
268: * Returns whether the current group is public membership or not.
269: *
270: * @return bool
271: */
272: public function isPublicMembership() {
273: if ($this->membership == ACCESS_PUBLIC) {
274: return true;
275: }
276:
277: return false;
278: }
279:
280: /**
281: * Return whether a given user is a member of this group or not.
282: *
283: * @param ElggUser $user The user
284: *
285: * @return bool
286: */
287: public function isMember($user = null) {
288: if (!($user instanceof ElggUser)) {
289: $user = elgg_get_logged_in_user_entity();
290: }
291: if (!($user instanceof ElggUser)) {
292: return false;
293: }
294: return is_group_member($this->getGUID(), $user->getGUID());
295: }
296:
297: /**
298: * Join an elgg user to this group.
299: *
300: * @param ElggUser $user User
301: *
302: * @return bool
303: */
304: public function join(ElggUser $user) {
305: return join_group($this->getGUID(), $user->getGUID());
306: }
307:
308: /**
309: * Remove a user from the group.
310: *
311: * @param ElggUser $user User
312: *
313: * @return bool
314: */
315: public function leave(ElggUser $user) {
316: return leave_group($this->getGUID(), $user->getGUID());
317: }
318:
319: /**
320: * Load the ElggGroup data from the database
321: *
322: * @param mixed $guid GUID of an ElggGroup entity or database row from entity table
323: *
324: * @return bool
325: */
326: protected function load($guid) {
327: $attr_loader = new ElggAttributeLoader(get_class(), 'group', $this->attributes);
328: $attr_loader->requires_access_control = !($this instanceof ElggPlugin);
329: $attr_loader->secondary_loader = 'get_group_entity_as_row';
330:
331: $attrs = $attr_loader->getRequiredAttributes($guid);
332: if (!$attrs) {
333: return false;
334: }
335:
336: $this->attributes = $attrs;
337: $this->attributes['tables_loaded'] = 2;
338: _elgg_cache_entity($this);
339:
340: return true;
341: }
342:
343: /**
344: * Override the save function.
345: *
346: * @return bool
347: */
348: public function save() {
349: // Save generic stuff
350: if (!parent::save()) {
351: return false;
352: }
353:
354: // Now save specific stuff
355:
356: _elgg_disable_caching_for_entity($this->guid);
357: $ret = create_group_entity($this->get('guid'), $this->get('name'), $this->get('description'));
358: _elgg_enable_caching_for_entity($this->guid);
359:
360: return $ret;
361: }
362:
363: // EXPORTABLE INTERFACE ////////////////////////////////////////////////////////////
364:
365: /**
366: * Return an array of fields which can be exported.
367: *
368: * @return array
369: */
370: public function getExportableValues() {
371: return array_merge(parent::getExportableValues(), array(
372: 'name',
373: 'description',
374: ));
375: }
376:
377: /**
378: * Can a user comment on this group?
379: *
380: * @see ElggEntity::canComment()
381: *
382: * @param int $user_guid User guid (default is logged in user)
383: * @return bool
384: * @since 1.8.0
385: */
386: public function canComment($user_guid = 0) {
387: $result = parent::canComment($user_guid);
388: if ($result !== null) {
389: return $result;
390: }
391: return false;
392: }
393: }
394: