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
|
<?php
/**
* Text for Angeltype related links.
*/
function angeltypes_title() {
return _("Angeltypes");
}
/**
* Route angeltype actions.
*/
function angeltypes_controller() {
$action = strip_request_item('action', 'list');
switch ($action) {
default:
case 'list':
return angeltypes_list_controller();
case 'view':
return angeltype_controller();
case 'edit':
return angeltype_edit_controller();
case 'delete':
return angeltype_delete_controller();
case 'about':
return angeltypes_about_controller();
}
}
/**
* Path to angeltype view.
*
* @param AngelType $angeltype_id
*/
function angeltype_link($angeltype_id) {
return page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype_id;
}
/**
* Job description for all angeltypes (public to everyone)
*/
function angeltypes_about_controller() {
global $user;
if (isset($user)) {
$angeltypes = AngelTypes_with_user($user);
} else {
$angeltypes = AngelTypes();
}
return [
_("Teams/Job description"),
AngelTypes_about_view($angeltypes, isset($user))
];
}
/**
* Delete an Angeltype.
*/
function angeltype_delete_controller() {
global $privileges;
if (! in_array('admin_angel_types', $privileges)) {
redirect(page_link_to('angeltypes'));
}
$angeltype = load_angeltype();
if (isset($_REQUEST['confirmed'])) {
AngelType_delete($angeltype);
success(sprintf(_("Angeltype %s deleted."), AngelType_name_render($angeltype)));
redirect(page_link_to('angeltypes'));
}
return [
sprintf(_("Delete angeltype %s"), $angeltype['name']),
AngelType_delete_view($angeltype)
];
}
/**
* Change an Angeltype.
*/
function angeltype_edit_controller() {
global $privileges, $user;
// In supporter mode only allow to modify description
$supporter_mode = ! in_array('admin_angel_types', $privileges);
if (isset($_REQUEST['angeltype_id'])) {
// Edit existing angeltype
$angeltype = load_angeltype();
if (! User_is_AngelType_supporter($user, $angeltype)) {
redirect(page_link_to('angeltypes'));
}
} else {
// New angeltype
if ($supporter_mode) {
// Supporters aren't allowed to create new angeltypes.
redirect(page_link_to('angeltypes'));
}
$angeltype = AngelType_new();
}
if (isset($_REQUEST['submit'])) {
$valid = true;
if (! $supporter_mode) {
if (isset($_REQUEST['name'])) {
$result = AngelType_validate_name($_REQUEST['name'], $angeltype);
$angeltype['name'] = $result->getValue();
if (! $result->isValid()) {
$valid = false;
error(_("Please check the name. Maybe it already exists."));
}
}
$angeltype['restricted'] = isset($_REQUEST['restricted']);
$angeltype['requires_driver_license'] = isset($_REQUEST['requires_driver_license']);
}
$angeltype['description'] = strip_request_item_nl('description', $angeltype['description']);
if ($valid) {
if ($angeltype['id'] != null) {
AngelType_update($angeltype);
} else {
$angeltype = AngelType_create($angeltype);
}
success("Angel type saved.");
redirect(angeltype_link($angeltype['id']));
}
}
return [
sprintf(_("Edit %s"), $angeltype['name']),
AngelType_edit_view($angeltype, $supporter_mode)
];
}
/**
* View details of a given angeltype.
*/
function angeltype_controller() {
global $privileges, $user;
if (! in_array('angeltypes', $privileges)) {
redirect('?');
}
$angeltype = load_angeltype();
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
$user_driver_license = UserDriverLicense($user['UID']);
$members = Users_by_angeltype($angeltype);
return [
sprintf(_("Team %s"), $angeltype['name']),
AngelType_view($angeltype, $members, $user_angeltype, in_array('admin_user_angeltypes', $privileges) || $user_angeltype['supporter'], in_array('admin_angel_types', $privileges), $user_angeltype['supporter'], $user_driver_license, $user)
];
}
/**
* View a list of all angeltypes.
*/
function angeltypes_list_controller() {
global $privileges, $user;
if (! in_array('angeltypes', $privileges)) {
redirect('?');
}
$angeltypes = AngelTypes_with_user($user);
foreach ($angeltypes as &$angeltype) {
$actions = [
button(page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'], _("view"), "btn-xs")
];
if (in_array('admin_angel_types', $privileges)) {
$actions[] = button(page_link_to('angeltypes') . '&action=edit&angeltype_id=' . $angeltype['id'], _("edit"), "btn-xs");
$actions[] = button(page_link_to('angeltypes') . '&action=delete&angeltype_id=' . $angeltype['id'], _("delete"), "btn-xs");
}
$angeltype['membership'] = AngelType_render_membership($angeltype);
if ($angeltype['user_angeltype_id'] != null) {
$actions[] = button(page_link_to('user_angeltypes') . '&action=delete&user_angeltype_id=' . $angeltype['user_angeltype_id'], _("leave"), "btn-xs");
} else {
$actions[] = button(page_link_to('user_angeltypes') . '&action=add&angeltype_id=' . $angeltype['id'], _("join"), "btn-xs");
}
$angeltype['restricted'] = $angeltype['restricted'] ? glyph('lock') : '';
$angeltype['name'] = '<a href="' . page_link_to('angeltypes') . '&action=view&angeltype_id=' . $angeltype['id'] . '">' . $angeltype['name'] . '</a>';
$angeltype['actions'] = table_buttons($actions);
}
return [
angeltypes_title(),
AngelTypes_list_view($angeltypes, in_array('admin_angel_types', $privileges))
];
}
/**
* Loads an angeltype from given angeltype_id request param.
*/
function load_angeltype() {
if (! isset($_REQUEST['angeltype_id'])) {
redirect(page_link_to('angeltypes'));
}
$angeltype = AngelType($_REQUEST['angeltype_id']);
if ($angeltype == null) {
error(_("Angeltype doesn't exist."));
redirect(page_link_to('angeltypes'));
}
return $angeltype;
}
?>
|