-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.php
201 lines (170 loc) · 5.46 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
<?php
/**
* Elgg-groups_admins_elections plugin
* @package elgg-groups_admins_elections
* @author Emmanuel Salomon @ManUtopiK
* @license GNU Affero General Public License, version 3 or late
* @link https://github.com/ManUtopiK/elgg-groups_admins_elections
**/
elgg_register_event_handler('init', 'system', 'groups_admins_elections_init');
/**
* Initialize elgg-groups_admins_elections plugin.
*/
function groups_admins_elections_init() {
// register a library of helper functions
$root = dirname(__FILE__);
elgg_register_library('groups_admins_elections:utilities', "$root/lib/utilities.php");
// register global menu
// Register a page handler, so we can have nice URLs
elgg_register_page_handler('elections', 'groups_admins_elections_handler');
// Register URL handler
// Extend view
elgg_extend_view('css/elgg', 'groups_admins_elections/css');
elgg_extend_view('js/elgg', 'groups_admins_elections/js');
elgg_extend_view('groups/profile/summary', 'groups_admins_elections/list_mandats');
// add to groups
// actions
$action_base = "$root/actions/groups_admins_elections";
elgg_register_action('elections/edit-mandat', "$action_base/edit-mandat.php");
elgg_register_action('elections/edit-candidat', "$action_base/edit-candidat.php");
elgg_register_action('elections/elect', "$action_base/elect.php");
elgg_register_action('elections/delete', "$action_base/delete.php");
// Register entity type
// Register a URL handler for mandat and candidat
elgg_register_entity_url_handler('object', 'mandat', 'mandat_url');
elgg_register_entity_url_handler('object', 'candidat', 'candidat_url');
elgg_register_entity_url_handler('object', 'elected', 'elected_url');
// Register entity menu
// write permission plugin hooks
elgg_register_plugin_hook_handler('permissions_check', 'group', 'groups_admins_elections_permission_check');
/*elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'ggouv_template_permission_check');
elgg_register_plugin_hook_handler('access:collections:read', 'user', 'ggouv_template_permission_check');*/
}
/**
* Dispatcher for elgg-groups_admins_elections plugin.
* URLs take the form of :
*
* @param array $page
*/
function groups_admins_elections_handler($page) {
elgg_load_library('groups_admins_elections:utilities');
if (!isset($page[0])) {
$page[0] = 'all';
}
//elgg_push_breadcrumb(elgg_echo('workflow'), 'workflow/all');
$base_dir = dirname(__FILE__) . '/pages/groups_admins_elections';
switch ($page[0]) {
default:
case 'all':
include "$base_dir/world.php";
break;
case 'mandat':
elgg_set_context($page[1]);
set_input('guid', $page[2]);
include "$base_dir/view_mandat.php";
break;
case 'mandats':
set_input('guid', $page[1]);
include "$base_dir/mandats.php";
break;
case 'candidat':
set_input('guid', $page[1]);
include "$base_dir/view_candidat.php";
break;
case 'elected':
set_input('guid', $page[1]);
include "$base_dir/view_elected.php";
break;
case 'add':
set_input('guid', $page[1]);
include "$base_dir/new.php";
break;
case 'edit':
set_input('guid', $page[1]);
include "$base_dir/edit.php";
break;
}
elgg_pop_context();
return true;
}
/**
* Populates the ->getUrl() method for mandat objects
*
* @param ElggEntity $entity The mandat object
* @return string mandat item URL
*/
function mandat_url($entity) {
$title = $entity->title;
$title = elgg_get_friendly_title($title);
return elgg_get_site_url() . "elections/mandat/view/" . $entity->getGUID() . "/" . $title;
}
/**
* Populates the ->getUrl() method for candidat objects
*
* @param ElggEntity $entity The candidat object
* @return string candidat item URL
*/
function candidat_url($entity) {
$title = $entity->title;
$title = elgg_get_friendly_title($title);
return elgg_get_site_url() . "elections/candidat/" . $entity->getGUID() . "/" . $title;
}
/**
* Populates the ->getUrl() method for elected objects
*
* @param ElggEntity $entity The elected object
* @return string elected item URL
*/
function elected_url($entity) {
$title = $entity->title;
$title = elgg_get_friendly_title($title);
return elgg_get_site_url() . "elections/elected/" . $entity->getGUID() . "/" . $title;
}
/**
* Let's user mandated writing to group.
*
* @param unknown_type $hook
* @param unknown_type $entity_type
* @param unknown_type $returnvalue
* @param unknown_type $params
*/
function groups_admins_elections_permission_check($hook, $entity_type, $returnvalue, $params) {
if (!$returnvalue && isset($params['entity']) && isset($params['user'])) {
if (check_entity_relationship($params['user']->guid, 'elected_in', $params['entity']->guid)) return true;
}
return $returnvalue;
}
/**
* Check if user is mandated.
*
* @param ElggUser $user
* @param ElggGroup $group
*
*@return ElggObject $mandat
*
* @todo check if we need to cache this
*/
function is_user_group_admin($user = 0, $group = 0) {
if (!$user) {
$user = elgg_get_logged_in_user_entity();
}
if (!$group) {
$user = elgg_get_page_owner_entity();
}
$list_mandats = elgg_get_entities(array(
'type' => 'object',
'subtype' => 'mandat',
'container_guid' => $group->guid,
));
foreach ($list_mandats as $mandat) {
$current_elected = elgg_get_entities_from_metadata(array(
'type' => 'object',
'subtypes' => 'elected',
'metadata_name' => 'mandat_guid',
'metadata_value' => $mandat->guid,
'limit' => 1,
));
if ($user->guid == $current_elected[0]->owner_guid) return $current_elected[0];
}
return false;
}