1 | <?php |
||
2 | /** |
||
3 | * A group entity, used as a container for other entities. |
||
4 | * |
||
5 | * @property string $name A short name that captures the purpose of the group |
||
6 | * @property string $description A longer body of content that gives more details about the group |
||
7 | */ |
||
8 | class ElggGroup extends \ElggEntity { |
||
9 | |||
10 | const CONTENT_ACCESS_MODE_UNRESTRICTED = 'unrestricted'; |
||
11 | const CONTENT_ACCESS_MODE_MEMBERS_ONLY = 'members_only'; |
||
12 | |||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | */ |
||
16 | 112 | protected function initializeAttributes() { |
|
17 | 112 | parent::initializeAttributes(); |
|
18 | 112 | $this->attributes['subtype'] = 'group'; |
|
19 | 112 | } |
|
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | 112 | public function getType() { |
|
25 | 112 | return 'group'; |
|
26 | } |
||
27 | |||
28 | /** |
||
29 | * Add an \ElggObject to this group. |
||
30 | * |
||
31 | * @param \ElggObject $object The object. |
||
32 | * |
||
33 | * @return bool |
||
34 | */ |
||
35 | public function addObjectToGroup(\ElggObject $object) { |
||
36 | $object->container_guid = $this->guid; |
||
37 | return $object->save(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Remove an object from this containing group and sets the container to be |
||
42 | * object's owner |
||
43 | * |
||
44 | * @param \ElggObject $object The object. |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function removeObjectFromGroup(ElggObject $object) { |
||
49 | $object->container_guid = $object->owner_guid; |
||
50 | return $object->save(); |
||
0 ignored issues
–
show
|
|||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Get an array of group members. |
||
55 | * |
||
56 | * @param array $options Options array. See elgg_get_entities_from_relationships |
||
57 | * for a complete list. Common ones are 'limit', 'offset', |
||
58 | * and 'count'. Options set automatically are 'relationship', |
||
59 | * 'relationship_guid', 'inverse_relationship', and 'type'. |
||
60 | * |
||
61 | * @return array |
||
62 | */ |
||
63 | public function getMembers(array $options = []) { |
||
64 | $options['relationship'] = 'member'; |
||
65 | $options['relationship_guid'] = $this->getGUID(); |
||
66 | $options['inverse_relationship'] = true; |
||
67 | $options['type'] = 'user'; |
||
68 | |||
69 | return elgg_get_entities($options); |
||
0 ignored issues
–
show
|
|||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns whether the current group has open membership or not. |
||
74 | * |
||
75 | * @return bool |
||
76 | */ |
||
77 | 2 | public function isPublicMembership() { |
|
78 | 2 | return ($this->membership == ACCESS_PUBLIC); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Return the content access mode used by group_gatekeeper() |
||
83 | * |
||
84 | * @return string One of CONTENT_ACCESS_MODE_* constants |
||
85 | * @access private |
||
86 | * @since 1.9.0 |
||
87 | */ |
||
88 | 4 | public function getContentAccessMode() { |
|
89 | 4 | $mode = $this->content_access_mode; |
|
90 | |||
91 | 4 | if (!isset($mode)) { |
|
92 | 1 | if ($this->isPublicMembership()) { |
|
93 | 1 | $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
|
94 | } else { |
||
95 | 1 | $mode = self::CONTENT_ACCESS_MODE_MEMBERS_ONLY; |
|
96 | } |
||
97 | } |
||
98 | |||
99 | // only support two modes for now |
||
100 | 4 | if ($mode === self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) { |
|
101 | 3 | return $mode; |
|
102 | } |
||
103 | 4 | return self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Set the content access mode used by group_gatekeeper() |
||
108 | * |
||
109 | * @param string $mode One of CONTENT_ACCESS_MODE_* constants. If empty string, mode will not be changed. |
||
110 | * @return void |
||
111 | * @access private |
||
112 | * @since 1.9.0 |
||
113 | */ |
||
114 | 3 | public function setContentAccessMode($mode) { |
|
115 | 3 | if (!$mode && $this->content_access_mode) { |
|
116 | return; |
||
117 | } |
||
118 | |||
119 | // only support two modes for now |
||
120 | 3 | if ($mode !== self::CONTENT_ACCESS_MODE_MEMBERS_ONLY) { |
|
121 | 3 | $mode = self::CONTENT_ACCESS_MODE_UNRESTRICTED; |
|
122 | } |
||
123 | |||
124 | 3 | $this->content_access_mode = $mode; |
|
0 ignored issues
–
show
|
|||
125 | 3 | } |
|
126 | |||
127 | /** |
||
128 | * Is the given user a member of this group? |
||
129 | * |
||
130 | * @param \ElggUser $user The user. Default is logged in user. |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 4 | public function isMember(\ElggUser $user = null) { |
|
135 | 4 | if ($user == null) { |
|
136 | $user = _elgg_services()->session->getLoggedInUser(); |
||
137 | } |
||
138 | 4 | if (!$user) { |
|
139 | return false; |
||
140 | } |
||
141 | |||
142 | 4 | $result = (bool) check_entity_relationship($user->guid, 'member', $this->guid); |
|
143 | |||
144 | $params = [ |
||
145 | 4 | 'user' => $user, |
|
146 | 4 | 'group' => $this, |
|
147 | ]; |
||
148 | 4 | return _elgg_services()->hooks->trigger('is_member', 'group', $params, $result); |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Join a user to this group. |
||
153 | * |
||
154 | * @param \ElggUser $user User joining the group. |
||
155 | * @param array $params Additional params to pass to the 'join', 'group' event |
||
156 | * |
||
157 | * @return bool Whether joining was successful. |
||
158 | */ |
||
159 | 75 | public function join(\ElggUser $user, $params = []) { |
|
160 | 75 | $result = add_entity_relationship($user->guid, 'member', $this->guid); |
|
161 | |||
162 | 75 | if (!$result) { |
|
163 | return false; |
||
164 | } |
||
165 | |||
166 | $event_params = [ |
||
167 | 75 | 'group' => $this, |
|
168 | 75 | 'user' => $user, |
|
169 | ]; |
||
170 | |||
171 | 75 | if (is_array($params)) { |
|
172 | 75 | $event_params = array_merge($params, $event_params); |
|
173 | } |
||
174 | |||
175 | 75 | _elgg_services()->hooks->getEvents()->trigger('join', 'group', $event_params); |
|
176 | |||
177 | 75 | return true; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Remove a user from the group. |
||
182 | * |
||
183 | * @param \ElggUser $user User to remove from the group. |
||
184 | * |
||
185 | * @return bool Whether the user was removed from the group. |
||
186 | */ |
||
187 | 2 | public function leave(\ElggUser $user) { |
|
188 | // event needs to be triggered while user is still member of group to have access to group acl |
||
189 | 2 | $params = ['group' => $this, 'user' => $user]; |
|
190 | 2 | _elgg_services()->hooks->getEvents()->trigger('leave', 'group', $params); |
|
191 | |||
192 | 2 | return remove_entity_relationship($user->guid, 'member', $this->guid); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 1 | protected function prepareObject($object) { |
|
199 | 1 | $object = parent::prepareObject($object); |
|
200 | 1 | $object->name = $this->getDisplayName(); |
|
201 | 1 | $object->description = $this->description; |
|
202 | 1 | unset($object->read_access); |
|
203 | 1 | return $object; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Can a user comment on this group? |
||
208 | * |
||
209 | * @see \ElggEntity::canComment() |
||
210 | * |
||
211 | * @param int $user_guid User guid (default is logged in user) |
||
212 | * @param bool $default Default permission |
||
213 | * @return bool |
||
214 | * @since 1.8.0 |
||
215 | */ |
||
216 | 2 | public function canComment($user_guid = 0, $default = null) { |
|
217 | 2 | return false; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Checks if a tool option is enabled |
||
222 | * |
||
223 | * @param string $option The option to check |
||
224 | * @return bool |
||
225 | * @since 3.0.0 |
||
226 | */ |
||
227 | 1 | public function isToolEnabled($option) { |
|
228 | 1 | if (empty($option)) { |
|
229 | 1 | return false; |
|
230 | } |
||
231 | |||
232 | 1 | $setting = $this->{"{$option}_enable"}; |
|
233 | 1 | if ($setting === 'no') { |
|
234 | 1 | return false; |
|
235 | } |
||
236 | |||
237 | 1 | $tool_config = $this->getToolConfig($option); |
|
238 | 1 | if (!$tool_config) { |
|
239 | 1 | return false; |
|
240 | } |
||
241 | |||
242 | 1 | if ($setting !== null) { |
|
243 | 1 | return (bool) ($setting == 'yes'); |
|
244 | } |
||
245 | |||
246 | // check default setting |
||
247 | 1 | return (bool) $tool_config->default_on; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Enables a tool option |
||
252 | * |
||
253 | * @param string $option The option to enable |
||
254 | * @return bool |
||
255 | * @since 3.0.0 |
||
256 | */ |
||
257 | 2 | public function enableTool($option) { |
|
258 | 2 | if (!$this->getToolConfig($option)) { |
|
259 | 1 | return false; |
|
260 | } |
||
261 | |||
262 | 2 | $this->{"{$option}_enable"} = 'yes'; |
|
263 | 2 | return true; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * Disables a tool option |
||
268 | * |
||
269 | * @param string $option The option to disable |
||
270 | * @return bool |
||
271 | * @since 3.0.0 |
||
272 | */ |
||
273 | 2 | public function disableTool($option) { |
|
274 | 2 | if (!$this->getToolConfig($option)) { |
|
275 | 1 | return false; |
|
276 | } |
||
277 | |||
278 | 2 | $this->{"{$option}_enable"} = 'no'; |
|
279 | 2 | return true; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Returns the registered tool configuration |
||
284 | * |
||
285 | * @param string $option The tool option to get the config for |
||
286 | * @return stdClass|false Returns the config object or false if it does not exists |
||
287 | */ |
||
288 | 2 | protected function getToolConfig($option) { |
|
289 | 2 | if (empty($option)) { |
|
290 | return false; |
||
291 | } |
||
292 | |||
293 | 2 | $group_tool_options = (array) elgg_get_group_tool_options($this); |
|
294 | |||
295 | 2 | foreach ($group_tool_options as $config) { |
|
296 | 2 | if ($config->name !== $option) { |
|
297 | 2 | continue; |
|
298 | } |
||
299 | |||
300 | 2 | return $config; |
|
301 | } |
||
302 | |||
303 | 2 | return false; |
|
304 | } |
||
305 | } |
||
306 |