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
|
<?php
/**
* Elgg Group Alias
*
* @package Lorea
* @subpackage GroupAlias
* @homepage http://lorea.org/plugin/group_alias
* @copyright 2011-2012 Lorea Faeries <federation@lorea.org>
* @license COPYING, http://www.gnu.org/licenses/agpl
*
* Copyright 2011-2013 Lorea Faeries <federation@lorea.org>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
elgg_register_event_handler('init', 'system', 'group_alias_init');
/**
* Initialize the group alias plugin.
*
*/
function group_alias_init() {
// Register group alias library
$library_path = elgg_get_plugins_path() . 'group_alias/lib/group_alias.php';
elgg_register_library('elgg:group_alias', $library_path);
// Register a page handler, so we can have nice URLs
elgg_register_page_handler('g', 'group_alias_page_handler');
// Override URL handlers for groups
elgg_register_entity_url_handler('group', 'all', 'group_alias_url');
// Add alias field
elgg_register_plugin_hook_handler('profile:fields', 'group', 'group_alias_fields_setup');
// Override groups/edit action
$action_base = elgg_get_plugins_path() . 'group_alias/actions/groups';
elgg_register_action("groups/edit", "$action_base/edit.php");
// Extend the main css view
elgg_extend_view('css/elgg', 'group_alias/css');
elgg_extend_view('js/elgg', 'group_alias/js');
// Register tests
elgg_register_plugin_hook_handler('unit_test', 'system', 'group_alias_test');
elgg_register_event_handler('upgrade', 'system', 'group_alias_run_upgrades');
}
function group_alias_run_upgrades() {
$upgrade_tools = elgg_get_plugins_path() . 'upgrade-tools/lib/upgrade_tools.php';
if (file_exists($upgrade_tools) && include_once($upgrade_tools)) {
upgrade_module_run('group_alias');
}
}
function group_alias_test($hook, $type, $value, $params) {
$value[] = elgg_get_config('pluginspath') . "group_alias/tests/group_alias_test.php";
return $value;
}
function get_group_from_group_alias($alias){
return current(elgg_get_entities_from_metadata(array(
'type' => 'group',
'metadata_name' => 'alias',
'metadata_value' => $alias,
'limit' => 1,
)));
}
/**
* Dispatcher for group alias.
* URLs take the form of
* All groups: g/
* Group profile: g/<alias>
* Group Tools: g/<alias>/<handler> => <handler>/group/<guid>
*
* @param array $page
* @return bool
*/
function group_alias_page_handler($page) {
elgg_set_context('groups');
if (!isset($page[0])) {
groups_page_handler(array('all'), 'groups');
return true;
}
$group = get_group_from_group_alias(str_replace(' ', '+', urldecode($page[0])));
if($group && !isset($page[1])){
groups_page_handler(array('profile', $group->guid));
} elseif($group && isset($page[1])) {
forward("$page[1]/group/$group->guid");
} else {
return groups_page_handler($page);
}
return true;
}
function group_alias_fields_setup($hook, $type, $return, $params) {
return array_merge($return, array('alias' => 'group_alias'));
}
function group_alias_from_name($group) {
$alias = elgg_get_friendly_title($group->name);
$alias = preg_replace("/-/", "_", $alias);
$alias = urldecode($alias);
return $alias;
}
/**
* This is an event hook for create/update group to handle the alias
* without overriding the groups/edit action.
*/
function group_alias_save_hook($event, $type, $entity) {
$alias_from_name = group_alias_from_name($entity);
$aliased_group = get_group_from_group_alias($alias_from_name);
// If the alias is already taken by another group, append GUID.
if (elgg_instanceof($aliased_group, 'group')
&& $aliased_group->guid != $entity->guid) {
$entity->set('alias', "$alias_from_name$entity->guid");
return TRUE;
}
// Force keeping default or existing alias if empty or not changeable
if (empty($entity->alias) || !elgg_get_config('changeable_group_alias')) {
$entity->set('alias', $alias_from_name);
}
return TRUE;
}
/**
* Override the group url
*
* @param ElggObject $group Group object
* @return string
*/
function group_alias_url($group) {
if(!$group->alias){
return groups_url($group);
}
return "g/$group->alias";
}
/**
* Convert a group name to an alias if it does not exist already.
* Return the newly, or existing group alias.
*
* @param ElggGroup $group Group object
* @return string
*/
function group_alias_update_from_name($group) {
if (!empty($group->alias)) {
return $group->alias;
}
$alias = group_alias_from_name($group);
// If alias is taken, append GUID
$g = get_group_from_group_alias($alias);
if (elgg_instanceof($g, 'group') && $g->guid != $group->guid) {
$alias .= $group->guid;
}
$group->set('alias', $alias);
return $alias;
}
|