1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
<?php
/**
* Elgg groups plugin
*
* @package ElggGroups
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @author Marcus Povey
* @copyright Curverider Ltd 2008
* @link http://elgg.com/
*/
/**
* Initialise the groups plugin.
* Register actions, set up menus
*/
function groups_init()
{
global $CONFIG;
// Set up the menu for logged in users
if (isloggedin())
{
add_menu(elgg_echo('groups'), $CONFIG->wwwroot . "pg/groups/owned/" . $_SESSION['user']->username,'groups');
}
else
{
add_menu(elgg_echo('groups'), $CONFIG->wwwroot . "mod/groups/",array(
menu_item(elgg_echo('groups:all'),$CONFIG->wwwroot."mod/groups/all.php"),
));
}
// Register a page handler, so we can have nice URLs
register_page_handler('groups','groups_page_handler');
// Register a URL handler for groups
register_entity_url_handler('groups_url','group','all');
// Register an icon handler for groups
register_page_handler('icon','groups_icon_handler');
// Register some actions
register_action("groups/edit",false, $CONFIG->pluginspath . "groups/actions/edit.php");
register_action("groups/join",false, $CONFIG->pluginspath . "groups/actions/join.php");
register_action("groups/leave",false, $CONFIG->pluginspath . "groups/actions/leave.php");
register_action("groups/joinrequest",false, $CONFIG->pluginspath . "groups/actions/joinrequest.php");
register_action("groups/addtogroup",false, $CONFIG->pluginspath . "groups/actions/addtogroup.php");
// Use group widgets
use_widgets('groups');
// Add a page owner handler
add_page_owner_handler('groups_page_owner_handler');
// Add some widgets
add_widget_type('group_members_widget',elgg_echo('groups:widgets:members:title'), elgg_echo('groups:widgets:members:description'), 'groups');
add_widget_type('group_entities_widget',elgg_echo('groups:widgets:entities:title'), elgg_echo('groups:widgets:entities:description'), 'groups');
//extend some views
extend_view('profile/icon','groups/icon');
extend_view('css','groups/css');
// Write access permissions
register_plugin_hook('access:collections:write', 'all', 'groups_write_acl_plugin_hook');
// For now, we'll hard code the groups profile items as follows:
// TODO make this user configurable
// Language short codes must be of the form "groups:key"
// where key is the array key below
$CONFIG->group = array(
'name' => 'text',
'description' => 'longtext',
'briefdescription' => 'text',
'interests' => 'tags',
'website' => 'url',
);
}
/**
* Sets up submenus for the groups system. Triggered on pagesetup.
*
*/
function groups_submenus() {
global $CONFIG;
// Get the page owner entity
$page_owner = page_owner_entity();
// Submenu items for all group pages
if ($page_owner instanceof ElggGroup) {
if ($page_owner->canEdit()) {
add_submenu_item(elgg_echo('groups:edit'),$CONFIG->wwwroot . "mod/groups/edit.php?group_guid=" . $page_owner->getGUID());
add_submenu_item(elgg_echo('groups:invite'),$CONFIG->wwwroot . "mod/groups/invite.php?group_guid={$page_owner->getGUID()}");
}
if ($page_owner->isMember($_SESSION['user'])) {
add_submenu_item(elgg_echo('groups:leave'), $CONFIG->wwwroot . "action/groups/leave?group_guid=" . $page_owner->getGUID());
} else {
if ($page_owner->isPublicMembership())
{
add_submenu_item(elgg_echo('groups:join'),$CONFIG->wwwroot . "action/groups/join?group_guid={$page_owner->getGUID()}");
}
else
{
add_submenu_item(elgg_echo('groups:joinrequest'),$CONFIG->wwwroot . "action/groups/joinrequest?group_guid={$page_owner->getGUID()}");
}
}
add_submenu_item(elgg_echo('groups:forum'),$CONFIG->wwwroot . "pg/groups/forum/{$page_owner->getGUID()}/");
}
// Add submenu options
if (get_context() == "groups") {
if ((page_owner() == $_SESSION['guid'] || !page_owner()) && isloggedin()) {
add_submenu_item(elgg_echo('groups:new'), $CONFIG->wwwroot."pg/groups/new/");
add_submenu_item(elgg_echo('groups:yours'), $CONFIG->wwwroot . "pg/groups/owned/" . $_SESSION['user']->username);
add_submenu_item(elgg_echo('groups:all'), $CONFIG->wwwroot . "pg/groups/world/");
} else if (page_owner()) {
$page_owner = page_owner_entity();
add_submenu_item(sprintf(elgg_echo('groups:yours'), $CONFIG->wwwroot . "pg/groups/owned/" . $page_owner->username));
add_submenu_item(elgg_echo('groups:all'), $CONFIG->wwwroot . "pg/groups/world/");
} else {
add_submenu_item(elgg_echo('groups:all'), $CONFIG->wwwroot . "pg/groups/world/");
}
}
}
/**
* Set a page owner handler.
*
*/
function groups_page_owner_handler()
{
$group_guid = get_input('group_guid');
if ($group_guid)
{
$group = get_entity($group_guid);
if ($group instanceof ElggGroup)
return $group->owner_guid;
}
return false;
}
/**
* Group page handler
*
* @param array $page Array of page elements, forwarded by the page handling mechanism
*/
function groups_page_handler($page)
{
global $CONFIG;
if (isset($page[0]))
{
// See what context we're using
switch($page[0])
{
case "new" :
include($CONFIG->pluginspath . "groups/new.php");
break;
case "world":
include($CONFIG->pluginspath . "groups/all.php");
break;
case "forum":
set_input('group_guid', $page[1]);
include($CONFIG->pluginspath . "groups/forum.php");
break;
case "owned" :
// Owned by a user
if (isset($page[1]))
set_input('username',$page[1]);
include($CONFIG->pluginspath . "groups/index.php");
break;
default:
set_input('group_guid', $page[0]);
include($CONFIG->pluginspath . "groups/groupprofile.php");
break;
}
}
}
/**
* Populates the ->getUrl() method for group objects
*
* @param ElggEntity $entity File entity
* @return string File URL
*/
function groups_url($entity) {
global $CONFIG;
$title = friendly_title($entity->name);
return $CONFIG->url . "pg/groups/{$entity->guid}/$title/";
}
/**
* Groups created, so add users to access lists.
*/
function groups_create_event_listener($event, $object_type, $object)
{
if (($event == 'create') && ($object_type == 'group') && ($object instanceof ElggGroup))
{
$group_id = create_access_collection(elgg_echo('groups:group') . ": " . $object->name);
if ($group_id)
{
$object->group_acl = $group_id;
}
else
return false;
}
return true;
}
/**
* Hook to listen to read access control requests and return all the groups you are a member of.
*/
function groups_read_acl_plugin_hook($hook, $entity_type, $returnvalue, $params)
{
//error_log("READ: " . var_export($returnvalue));
$user = $_SESSION['user'];
if ($user)
{
$membership = get_users_membership($user->guid);
if ($membership)
{
foreach ($membership as $group)
$returnvalue[$user->guid][$group->group_acl] = elgg_echo('groups:group') . ": " . $group->name;
return $returnvalue;
}
}
}
/**
* Return the write access for the current group if the user has write access to it.
*/
function groups_write_acl_plugin_hook($hook, $entity_type, $returnvalue, $params)
{
$page_owner = page_owner_entity();
if ($page_owner instanceof ElggGroup)
{
if (can_write_to_container())
{
$returnvalue[$page_owner->group_acl] = elgg_echo('groups:group') . ": " . $page_owner->name;
return $returnvalue;
}
}
}
/**
* Groups deleted, so remove access lists.
*/
function groups_delete_event_listener($event, $object_type, $object)
{
delete_access_collection($object->access_id);
return true;
}
/**
* Listens to a group join event and adds a user to the group's access control
*
*/
function groups_user_join_event_listener($event, $object_type, $object) {
$group = $object['group'];
$user = $object['user'];
$acl = $group->group_acl;
add_user_to_access_collection($user->guid, $acl);
return true;
}
/**
* Listens to a group leave event and removes a user from the group's access control
*
*/
function groups_user_leave_event_listener($event, $object_type, $object) {
$group = $object['group'];
$user = $object['user'];
$acl = $group->group_acl;
remove_user_from_access_collection($user->guid, $acl);
return true;
}
// Register a handler for create groups
register_elgg_event_handler('create', 'group', 'groups_create_event_listener');
// Register a handler for delete groups
register_elgg_event_handler('delete', 'group', 'groups_delete_event_listener');
// Make sure the groups initialisation function is called on initialisation
register_elgg_event_handler('init','system','groups_init');
register_elgg_event_handler('join','group','groups_user_join_event_listener');
register_elgg_event_handler('leave','group','groups_user_leave_event_listener');
register_elgg_event_handler('pagesetup','system','groups_submenus');
// Register actions
global $CONFIG;
register_action("groups/addtopic",false,$CONFIG->pluginspath . "groups/actions/forums/addtopic.php",true);
register_action("groups/deletetopic",false,$CONFIG->pluginspath . "groups/actions/forums/deletetopic.php",true);
register_action("groups/addpost",false,$CONFIG->pluginspath . "groups/actions/forums/addpost.php",true);
register_action("groups/edittopic",false,$CONFIG->pluginspath . "groups/actions/forums/edittopic.php",true);
?>
|