This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
/
start.php
540 lines (455 loc) · 14.6 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
/**
* Group Subtypes
*
* @author Ismayil Khayredinov <info@hypejunction.com>
* @copyright Copyright (c) 2015, Ismayil Khayredinov
*/
require_once __DIR__ . '/autoloader.php';
elgg_register_event_handler('init', 'system', 'group_subtypes_init', 900);
/**
* Initialize the plugin
* @return void
*/
function group_subtypes_init() {
// Search
elgg_unregister_entity_type('group', '');
elgg_unregister_plugin_hook_handler('search', 'group', 'search_groups_hook');
elgg_register_plugin_hook_handler('search', 'group', 'group_subtypes_search_hook');
elgg_register_plugin_hook_handler('route', 'groups', 'group_subtypes_route_edit_pages');
$hooks = _elgg_services()->hooks->getAllHandlers();
$conf = group_subtypes_get_config();
$identifiers = array();
foreach ($conf as $subtype => $options) {
elgg_register_entity_type('group', $subtype);
// If a custom form/action has been setup for the subtype,
// apply custom tools logic
elgg_register_plugin_hook_handler('action', "groups/edit/$subtype", 'group_subtypes_update_fields_config');
$identifier = elgg_extract('identifier', $options, 'groups');
if (in_array($identifier, $identifiers)) {
continue;
}
elgg_register_plugin_hook_handler('page_identifier', "group:$subtype", 'group_subtypes_page_identifier');
elgg_register_plugin_hook_handler('list_subtypes', $identifier, 'group_subtypes_list_subtypes');
if ($identifier !== 'groups') {
elgg_register_menu_item('site', array(
'name' => $identifier,
'href' => "$identifier/all",
'text' => elgg_echo($identifier),
));
elgg_register_plugin_hook_handler('route', $identifier, 'group_subtypes_router', 1);
// core doesn't run 'route' hooks on an updated page identifier, so we will register the callback manually
if (!isset($hooks['route']['groups'])) {
continue;
}
foreach ($hooks['route']['groups'] as $priority => $callbacks) {
$callbacks = (array) $callbacks;
foreach ($callbacks as $callback) {
elgg_register_plugin_hook_handler('route', $identifier, $callback, $priority);
}
}
}
}
elgg_register_plugin_hook_handler('permissions_check:parent', 'group', 'group_subtypes_can_parent');
// Form hacks
elgg_register_plugin_hook_handler('action', 'groups/edit', 'group_subtypes_update_fields_config');
elgg_extend_view('forms/groups/edit', 'forms/groups/edit/subtype');
elgg_extend_view('forms/groups/edit', 'forms/groups/edit/parent_guid');
// Group URL
elgg_register_plugin_hook_handler('entity:url', 'group', 'group_subtypes_rewrite_group_urls');
// Admin
elgg_register_admin_menu_item('administer', 'groups', null, 20);
elgg_register_admin_menu_item('administer', 'subtypes', 'groups', 10);
elgg_register_action('admin/groups/subtypes/add', __DIR__ . '/actions/admin/groups/subtypes/add.php', 'admin');
elgg_register_action('admin/groups/subtypes/config', __DIR__ . '/actions/admin/groups/subtypes/config.php', 'admin');
elgg_register_action('admin/groups/subtypes/change_subtype', __DIR__ . '/actions/admin/groups/subtypes/change_subtype.php', 'admin');
elgg_extend_view('admin.css', 'forms/admin/groups/subtypes/config.css');
// Group tools
if (elgg_is_active_plugin('group_tools')) {
elgg_unextend_view("groups/edit", "group_tools/group_edit_tabbed", 1);
elgg_unextend_view("groups/edit", "group_tools/group_edit_tabbed_js", 999999999);
elgg_extend_view('css/elgg', 'forms/groups/edit/group_tools.css');
}
// Menus
elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'group_subtypes_replace_owner_block_labels', 999);
// Modules
elgg_register_plugin_hook_handler('view_vars', 'groups/profile/module', 'groups_subtypes_replace_group_module_title', 999);
}
/**
* Returns configuration
* @return array
*/
function group_subtypes_get_config() {
static $config;
if (!isset($config)) {
$setting = elgg_get_plugin_setting('config', 'group_subtypes');
if ($setting) {
$config = unserialize($setting);
} else {
$config = array();
}
}
return $config;
}
/**
* Route groups add page
*
* @param string $hook "route"
* @param string $type "groups"
* @param mixed $return Identifier and segments
* @param array $params Hook params
* @return mixed
*/
function group_subtypes_route_edit_pages($hook, $type, $return, $params) {
if (!is_array($return)) {
return;
}
$initial_identifier = elgg_extract('identifier', $params, 'groups');
$identifier = elgg_extract('identifier', $return);
$segments = elgg_extract('segments', $return);
$page = array_shift($segments);
if ($page == 'add') {
$parent_guid = array_shift($segments);
$subtype = array_shift($segments);
echo elgg_view_resource('groups/add', array(
'parent_guid' => $parent_guid,
'subtype' => $subtype,
'segments' => $segments,
'identifier' => $initial_identifier,
));
return false;
}
if ($page == 'edit') {
$guid = array_shift($segments);
echo elgg_view_resource('groups/edit', array(
'guid' => $guid,
'segments' => $segments,
'identifier' => $initial_identifier,
));
return false;
}
}
/**
* Returns 'subtypes' ege* value for given page identifier
*
* @param string $identifier Page identifier
* @return mixed
*/
function group_subtypes_get_subtypes($identifier = 'groups') {
$subtypes = elgg_trigger_plugin_hook('list_subtypes', $identifier, null, ELGG_ENTITIES_ANY_VALUE);
if (is_array($subtypes)) {
return array_unique($subtypes);
}
return $subtypes;
}
/**
* Returns page identifier for a group entity
*
* @param ElggGroup $entity Group entity
* @return string
*/
function group_subtypes_get_identifier(ElggGroup $entity) {
$type = $entity->getType();
$subtype = $entity->getSubtype();
return elgg_trigger_plugin_hook('page_identifier', "$type:$subtype", ['entity' => $entity], 'groups');
}
/**
* Route group subtypes identifiers to groups page handler
*
* @param string $hook "route"
* @param string $type Subtype name
* @param array $return Identifier and segments
* @param array $params Hook params
* @return array
*/
function group_subtypes_router($hook, $type, $return, $params) {
if (!is_array($return)) {
return;
}
// Get original identifier
$identifier = elgg_extract('identifier', $params);
if ($identifier !== 'groups') {
$tools = elgg_get_config('group_tool_options');
// Namespace tool options
foreach ($tools as $key => $tool) {
$tool->label = elgg_echo("$identifier:tools:$tool->name:label");
// Let group options prevail. Do not enable anything by default
$tool->default_on = false;
$tools[$key] = $tool;
}
elgg_set_config('group_tool_options', $tools);
}
$segments = elgg_extract('segments', $return);
return array(
'identifier' => 'groups',
'handler' => 'groups',
'segments' => $segments,
);
}
/**
* Specify which group subtypes are to be shown on the page
*
* @param string $hook "list_subtypes"
* @param string $type Page identifier
* @param mixed $return Subtypes
* @param array $params Hook params
* @return mixed
*/
function group_subtypes_list_subtypes($hook, $type, $return, $params) {
$subtypes = array();
$conf = group_subtypes_get_config();
foreach ($conf as $subtype => $options) {
$identifier = elgg_extract('identifier', $options, 'groups');
if ($identifier == $type) {
$subtypes[] = $subtype;
}
}
if (empty($subtypes)) {
return;
}
if (is_array($return)) {
return array_merge($return, $subtypes);
}
return $subtypes;
}
/**
* Returns page identifier for a given group subtype
*
* @param string $hook "page_identifier"
* @param string $type "$type:$subtype"
* @param string $return Page identifier
* @param array $params Hook params
* @return string
*/
function group_subtypes_page_identifier($hook, $type, $return, $params) {
list($type, $subtype) = explode(':', $type);
if ($type != 'group') {
return;
}
$conf = group_subtypes_get_config();
$options = elgg_extract($subtype, $conf, array());
return elgg_extract('identifier', $options, 'groups');
}
/**
* Check if an entity can parent(contain) another entity
*
* @param string $hook "permissions_check:parent"
* @param string $type "group"
* @param bool $return Permission
* @param array $params Hook params
* @return bool
*/
function group_subtypes_can_parent($hook, $type, $return, $params) {
$parent = elgg_extract('parent', $params);
$subtype = elgg_extract('subtype', $params);
$conf = group_subtypes_get_config();
if (empty($conf)) {
return false;
}
if (!$parent instanceof ElggGroup) {
return $conf[$subtype]['root'];
}
return in_array($parent->getSubtype(), $conf[$subtype]['parents']);
}
/**
* Update group fields config before the group is saved
*
* @param string $hook "action"
* @param string $type "groups/edit"
* @param mixed $return Proceed with action?
* @param array $params Hook params
* @return void
*/
function group_subtypes_update_fields_config($hook, $type, $return, $params) {
$fields = elgg_get_config('group');
$guid = get_input('group_guid');
$entity = get_entity($guid);
if ($entity) {
$subtype = $entity->getSubtype();
} else {
$subtype = get_input('subtype');
if ($subtype) {
// only add subtype to group fields if it's a new entity
$fields['subtype'] = 'hidden';
}
}
if ($subtype) {
// apply subtype tool presets
$conf = group_subtypes_get_config();
$tools = elgg_get_config('group_tool_options');
if ($tools) {
foreach ($tools as $group_option) {
$option_name = $group_option->name . "_enable";
$option_value = in_array($group_option->name, $conf[$subtype]['tools']) ? 'yes' : 'no';
if (empty(get_input($option_name))) {
set_input($option_name, $option_value);
}
}
}
}
if (get_input('container_guid')) {
$fields['container_guid'] = 'hidden';
}
if (get_input('parent_guid')) {
$fields['parent_guid'] = 'hidden';
}
elgg_set_config('group', $fields);
}
/**
* Rewrite group urls to match configured identifier
*
* @param string $hook "entity:url"
* @param string $type "group"
* @param string $return URL
* @param array $params Hook params
* @return string
*/
function group_subtypes_rewrite_group_urls($hook, $type, $return, $params) {
$entity = elgg_extract('entity', $params);
if (!$entity instanceof ElggGroup) {
return;
}
$subtype = $entity->getSubtype();
if (!$subtype) {
return;
}
$identifier = group_subtypes_get_identifier($entity);
if ($identifier != 'groups') {
$title = elgg_get_friendly_title($entity->name);
return elgg_normalize_url("$identifier/profile/$entity->guid/$title");
}
}
/**
* Configure tools available to subtype
* Called before the group edit form is rendered
*
* @param string $subtype Group subtype
* @return void
*/
function group_subtypes_configure_tools($subtype) {
$conf = group_subtypes_get_config();
if ($conf[$subtype]['preset_tools']) {
elgg_set_config('group_tool_options', null);
} else {
$tools = elgg_get_config('group_tool_options');
if ($tools) {
foreach ($tools as $key => $group_option) {
if (!in_array($group_option->name, $conf[$subtype]['tools'])) {
unset($tools[$key]);
}
}
}
elgg_set_config('group_tool_options', $tools);
}
}
/**
* Get groups that match the search parameters.
*
* @param string $hook Hook name
* @param string $type Hook type
* @param array $value Empty array
* @param array $params Search parameters
* @return array
*/
function group_subtypes_search_hook($hook, $type, $value, $params) {
$params['joins'] = (array) elgg_extract('joins', $params, array());
$params['wheres'] = (array) elgg_extract('wheres', $params, array());
$db_prefix = elgg_get_config('dbprefix');
$query = sanitise_string($params['query']);
$join = "JOIN {$db_prefix}groups_entity ge ON e.guid = ge.guid";
array_unshift($params['joins'], $join);
$fields = array('name', 'description');
$where = search_get_where_sql('ge', $fields, $params);
$params['wheres'][] = $where;
$params['count'] = TRUE;
$count = elgg_get_entities($params);
// no need to continue if nothing here.
if (!$count) {
return array('entities' => array(), 'count' => $count);
}
$params['count'] = FALSE;
if (isset($params['sort']) || !isset($params['order_by'])) {
$params['order_by'] = search_get_order_by_sql('e', 'ge', $params['sort'], $params['order']);
}
$entities = elgg_get_entities($params);
// add the volatile data for why these entities have been returned.
foreach ($entities as $entity) {
$name = search_get_highlighted_relevant_substrings($entity->name, $query);
$entity->setVolatileData('search_matched_title', $name);
$description = search_get_highlighted_relevant_substrings($entity->description, $query);
$entity->setVolatileData('search_matched_description', $description);
}
return array(
'entities' => $entities,
'count' => $count,
);
}
/**
* Returns an array of group subtypes that can be added to the parent
*
* @param ElggEntity $parent Parent entity
* @return array
*/
function group_subtypes_get_allowed_subtypes_for_parent($parent = null) {
$allowed_subtypes = array();
$subtypes = get_registered_entity_types('group');
foreach ($subtypes as $subtype) {
$params = array(
'parent' => $parent,
'type' => 'group',
'subtype' => $subtype,
);
$can_parent = elgg_trigger_plugin_hook('permissions_check:parent', 'group', $params, true);
if ($can_parent) {
$allowed_subtypes[] = $subtype;
}
}
return $allowed_subtypes;
}
/**
* Replaces owner block labels with type specific strings
*
* @param string $hook "register"
* @param string $type "menu:owner_block"
* @param array $return Menu
* @param array $params Hook params
* @return array
*/
function group_subtypes_replace_owner_block_labels($hook, $type, $return, $params) {
$entity = elgg_extract('entity', $params);
if (!$entity instanceof ElggGroup) {
return;
}
$identifier = group_subtypes_get_identifier($entity) ? : 'groups';
if ($identifier == 'groups') {
return;
}
foreach ($return as $key => $item) {
$item->setText(elgg_echo("$identifier:tools:{$item->getName()}"));
$return[$key] = $item;
}
return $return;
}
/**
* Replace group profile module title
*
* @param string $hook "view_vars"
* @param string $type "groups/profile/module"
* @param array $vars View vars
* @param array $params Hook params
* @return array
*/
function groups_subtypes_replace_group_module_title($hook, $type, $vars, $params) {
$route = _elgg_services()->request->getUrlSegments();
$context = array_shift($route);
if ($context == 'groups') {
return;
}
$group_str = elgg_echo('groups:group');
$groups_str = elgg_echo('item:group');
$title = elgg_extract('title', $vars, '');
$title = preg_replace("/{$group_str}|{$groups_str}/i", '', $title);
$title = ucfirst(trim($title, ' '));
$vars['title'] = $title;
return $vars;
}