-
Notifications
You must be signed in to change notification settings - Fork 3
/
start.php
370 lines (320 loc) · 10.1 KB
/
start.php
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* Overrides for the groups plugin on Elgg community site
*
*/
elgg_register_event_handler('init', 'system', 'community_groups_init');
/**
* Initialize the community groups extension plugin
*/
function community_groups_init() {
$action_path = elgg_get_plugins_path() . 'community_groups/actions';
elgg_extend_view('css/elgg', 'community_groups/css');
// admin controls use a lightbox
if (elgg_in_context('discussion') && elgg_is_admin_logged_in()) {
elgg_load_js('lightbox');
elgg_load_css('lightbox');
}
// group tabs
if (elgg_in_context('groups')) {
elgg_register_plugin_hook_handler('register', 'menu:filter', 'community_groups_filter_menu');
}
elgg_register_plugin_hook_handler('route', 'groups', 'community_groups_router');
elgg_register_plugin_hook_handler('route', 'discussion', 'community_discussions_router');
// do not need normal sidebar menu in community site
elgg_unregister_event_handler('pagesetup', 'system', 'groups_setup_sidebar_menus');
// only admins can create groups
elgg_register_plugin_hook_handler('register', 'menu:title', 'community_groups_restrict_group_add_button');
elgg_register_plugin_hook_handler('action', 'groups/edit', 'community_groups_restrict_group_edit_action');
// attempt to join a group on first post
elgg_register_plugin_hook_handler('action', 'discussion/save', 'community_groups_discussion_save_handler');
// groups administration
elgg_register_menu_item('page', array(
'name' => 'groups',
'href' => 'admin/groups/main',
'text' => elgg_echo('admin:groups'),
'context' => 'admin',
'priority' => 200,
'section' => 'administer'
));
elgg_register_action('groups/combine', "$action_path/groups/combine.php", 'admin');
elgg_register_action('groups/categorize', "$action_path/groups/categorize.php", 'admin');
elgg_register_action("groups/delete", elgg_get_plugins_path() . "groups/actions/groups/delete.php", 'admin');
elgg_register_action("groups/saveblogsettings", "$action_path/groups/saveblogsettings.php", 'admin');
elgg_register_action("groups/change_owner", "$action_path/groups/change_owner.php", 'admin');
// set up site menu for discussion
$item = new ElggMenuItem('discussion', elgg_echo('discussion'), 'discussion/all');
elgg_register_menu_item('site', $item);
// modify the menus on discussion posts
if (elgg_is_admin_logged_in()) {
elgg_register_plugin_hook_handler('register', 'menu:cg:moderator', 'community_groups_moderator_menu');
elgg_extend_view('object/discussion', 'community_groups/discussion/controls');
elgg_extend_view('object/discussion_reply', 'community_groups/discussion/controls');
elgg_register_ajax_view('community_groups/discussion/offtopic');
}
elgg_register_plugin_hook_handler('register', 'menu:entity', 'community_groups_limit_editing');
elgg_register_action('discussion/remove_ad', "$action_path/discussion/remove_ad.php", 'admin');
elgg_register_action('discussion/offtopic', "$action_path/discussion/offtopic.php", 'admin');
}
/**
* Attempt to join a group before posting a new discussion topic.
*
* @param string $hook 'action'
* @param string $type 'discussion/save'
* @param mixed $result ignored
* @param mixed $params ignored
*
* @return void
*/
function community_groups_discussion_save_handler($hook, $type, $result, $params) {
// just editing existing topic, so we don't need to join for permission to do that
if (get_input('topic_guid')) {
return NULL;
}
$group = get_entity(get_input('container_guid'));
// Not a valid group. The action will give the appropriate error message
if (!$group) {
return NULL;
}
// Already a member, so the permissions check is useless
if ($group->isMember()) {
return NULL;
}
// not a member; attempt join
if ($group->isPublicMembership() && $group->join(elgg_get_logged_in_user_entity())) {
system_message(elgg_echo("cg:groups:join:success", array($group->getDisplayName())));
} else {
register_error(elgg_echo('cg:groups:join:failure', array($group->getDisplayName())));
}
}
/**
* Replace the filter menu for group selection
*
* @param string $hook Hook name
* @param string $type Hook type
* @param array $menu Menu items
* @param array $params Parameters for the menu
* @return array
*/
function community_groups_filter_menu($hook, $type, $menu, $params) {
// main page does not have the filter request param
$main_page = !(bool)get_input('filter');
$menu = array();
$groups = array('featured', 'popular', 'support', 'language', 'developers', 'plugins');
$priority = 100;
foreach ($groups as $name) {
$options = array(
'name' => $name,
'text' => elgg_echo("groups:$name"),
'href' => "groups/all?filter=$name",
'priority' => $priority++,
);
if ($main_page && $name == 'featured') {
$options['selected'] = true;
}
$menu[] = ElggMenuItem::factory($options);
}
return $menu;
}
/**
* Route overridden group pages
*
* @param type $hook The name of the hook
* @param type $context The context/handler
* @param type $params The parameters for routing
*/
function community_groups_router($hook, $context, $params, $result) {
if ($result === false) {
return $result;
}
$community_base = __DIR__ . "/pages";
if ($context == 'groups' && $params['segments'][0] == 'all') {
require "$community_base/groups.php";
return false;
}
}
/**
* Route overridden discussion pages
*
* @param type $hook The name of the hook
* @param type $context The context/handler
* @param type $params The parameters for routing
*/
function community_discussions_router($hook, $context, $params, $result) {
if ($result === false) {
return $result;
}
$community_base = __DIR__ . "/pages";
if (!isset($params['segments'][0])) {
$params['segments'][0] = 'all';
}
switch ($params['segments'][0]) {
case 'new':
require "$community_base/discussion/new.php";
return false;
case 'all':
elgg_register_menu_item('title', array(
'name' => 'discussion:add',
'href' => '/discussion/new',
'text' => elgg_echo('discussion:add'),
'class' => 'elgg-button elgg-button-submit',
));
break;
}
}
/**
* Remove the group add button for non-admins
*
* @param string $hook Hook name
* @param string $type Hook type
* @param array $menu Array of ElggMenuItem objects
* @return array
*/
function community_groups_restrict_group_add_button($hook, $type, $menu) {
if (elgg_get_context() == 'groups') {
if (!elgg_is_admin_logged_in()) {
foreach ($menu as $index => $item) {
if ($item->getName() == 'add') {
unset($menu[$index]);
}
}
}
}
return $menu;
}
/**
* Only allow admins to create groups.
* Catches the edit action and if this is a new group, checks that
* an admin is logged in. If an existing group, lets the action proceed.
*/
function community_groups_restrict_group_edit_action() {
$guid = get_input('group_guid', 0);
if ($guid) {
// group edit action
return true;
}
// group create
return elgg_is_admin_logged_in();
}
/**
* Add moderator buttons for discussions and discussion replies
*
* @param string $hook
* @param string $type
* @param array $menu
* @param array $params
*/
function community_groups_moderator_menu($hook, $type, $menu, $params) {
$entity = $params['entity'];
if ($entity->getSubtype() === 'discussion') {
$options = array(
'name' => 'remove_ad',
'text' => elgg_echo('cg:menu:remove_ad'),
'href' => "action/discussion/remove_ad?guid=" . $entity->getGUID(),
'is_action' => true,
);
$menu[] = ElggMenuItem::factory($options);
return $menu;
}
if ($entity instanceof ElggDiscussionReply) {
$options = array(
'name' => 'offtopic',
'text' => elgg_echo('cg:menu:offtopic'),
'href' => "ajax/view/community_groups/discussion/offtopic?guid=" . $entity->guid,
'link_class' => 'elgg-lightbox',
);
$menu[] = ElggMenuItem::factory($options);
$options = array(
'name' => 'remove_ad',
'text' => elgg_echo('cg:menu:remove_ad'),
'href' => "action/discussion/remove_ad?guid=" . $entity->guid,
'is_action' => true,
);
$menu[] = ElggMenuItem::factory($options);
return $menu;
}
}
/**
* Limit editing of discussion topics and replies based on time since posted
*
* This is to prevent people from posting inflammatory comments and then
* editing them before an admin sees them.
*
* @param string $hook
* @param string $type
* @param ElggMenuItem[] $menu
* @param array $params
*/
function community_groups_limit_editing($hook, $type, $menu, $params) {
if (!elgg_in_context('discussion')) {
return;
}
$entity = $params['entity'];
// Check that we're dealing either with a discussion or a discussion reply
if ($entity->getSubtype() != 'discussion' && !$entity instanceof ElggDiscussionReply) {
return;
}
$owner_guid = $entity->getOwnerGUID();
$time = $entity->getTimeCreated();
if (!community_groups_can_edit($owner_guid, $time)) {
foreach ($menu as $index => $item) {
if ($item->getName() == 'edit') {
unset($menu[$index]);
}
}
}
if (!community_groups_can_delete($owner_guid, $time)) {
foreach ($menu as $index => $item) {
if ($item->getName() == 'delete') {
unset($menu[$index]);
}
}
}
return $menu;
}
/**
* Can this viewer edit this forum topic/comment
*
* @param int $owner_guid
* @param int $time_created
* @return bool
*/
function community_groups_can_edit($owner_guid, $time_created) {
if (elgg_is_admin_logged_in()) {
return true;
}
// they have 30 minutes to edit
if (elgg_get_logged_in_user_guid() == $owner_guid) {
if ((time() - $time_created) < 30 * 60) {
return true;
}
}
return false;
}
/**
* Can this viewer delete this forum topic/comment
*
* @param int $owner_guid
* @param int $time_created
* @return bool
*/
function community_groups_can_delete($owner_guid, $time_created) {
if (elgg_is_admin_logged_in()) {
return true;
}
// they have 5 minutes to delete
if (elgg_get_logged_in_user_guid() == $owner_guid) {
if ((time() - $time_created) < 5 * 60) {
return true;
}
}
return false;
}
/**
* Get a list of group categories
*
* @return array
*/
function community_groups_get_categories() {
return array('support', 'plugins', 'language', 'developers');
}