summaryrefslogtreecommitdiff
path: root/includes/controller/user_angeltypes_controller.php
blob: 038d919e037bad33cbd8267c384d2d2f5e34e8dd (plain)
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
<?php

use Engelsystem\Models\User\User;

/**
 * Display a hint for team/angeltype supporters if there are unconfirmed users for his angeltype.
 *
 * @return string|null
 */
function user_angeltypes_unconfirmed_hint()
{
    $unconfirmed_user_angeltypes = User_unconfirmed_AngelTypes(auth()->user()->id);
    if (count($unconfirmed_user_angeltypes) == 0) {
        return null;
    }

    $unconfirmed_links = [];
    foreach ($unconfirmed_user_angeltypes as $user_angeltype) {
        $unconfirmed_links[] = '<a href="'
            . page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $user_angeltype['angeltype_id']])
            . '">' . $user_angeltype['name']
            . ' (+' . $user_angeltype['count'] . ')'
            . '</a>';
    }

    $count = count($unconfirmed_user_angeltypes);
    return _e(
            'There is %d unconfirmed angeltype.',
            'There are %d unconfirmed angeltypes.',
            $count,
            [$count]
        )
        . ' ' . __('Angel types which need approvals:')
        . ' ' . join(', ', $unconfirmed_links);
}

/**
 * Remove all unconfirmed users from a specific angeltype.
 *
 * @return array
 */
function user_angeltypes_delete_all_controller()
{
    $request = request();

    if (!$request->has('angeltype_id')) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $angeltype = AngelType($request->input('angeltype_id'));
    if (empty($angeltype)) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) {
        error(__('You are not allowed to delete all users for this angeltype.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->hasPostData('deny_all')) {
        UserAngelTypes_delete_all($angeltype['id']);

        engelsystem_log(sprintf('Denied all users for angeltype %s', AngelType_name_render($angeltype, true)));
        success(sprintf(__('Denied all users for angeltype %s.'), AngelType_name_render($angeltype)));
        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        __('Deny all users'),
        UserAngelTypes_delete_all_view($angeltype)
    ];
}

/**
 * Confirm all unconfirmed users for an angeltype.
 *
 * @return array
 */
function user_angeltypes_confirm_all_controller()
{
    $user = auth()->user();
    $request = request();

    if (!$request->has('angeltype_id')) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $angeltype = AngelType($request->input('angeltype_id'));
    if (empty($angeltype)) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if (!auth()->can('admin_user_angeltypes') && !User_is_AngelType_supporter($user, $angeltype)) {
        error(__('You are not allowed to confirm all users for this angeltype.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->hasPostData('confirm_all')) {
        UserAngelTypes_confirm_all($angeltype['id'], $user->id);

        engelsystem_log(sprintf('Confirmed all users for angeltype %s', AngelType_name_render($angeltype, true)));
        success(sprintf(__('Confirmed all users for angeltype %s.'), AngelType_name_render($angeltype)));
        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        __('Confirm all users'),
        UserAngelTypes_confirm_all_view($angeltype)
    ];
}

/**
 * Confirm an user for an angeltype.
 *
 * @return array
 */
function user_angeltype_confirm_controller()
{
    $user = auth()->user();
    $request = request();

    if (!$request->has('user_angeltype_id')) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_angeltype = UserAngelType($request->input('user_angeltype_id'));
    if (empty($user_angeltype)) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $angeltype = AngelType($user_angeltype['angeltype_id']);
    if (empty($angeltype)) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if (!User_is_AngelType_supporter($user, $angeltype)) {
        error(__('You are not allowed to confirm this users angeltype.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_source = User::find($user_angeltype['user_id']);
    if (!$user_source) {
        error(__('User doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->hasPostData('confirm_user')) {
        UserAngelType_confirm($user_angeltype['id'], $user->id);

        engelsystem_log(sprintf(
            '%s confirmed for angeltype %s',
            User_Nick_render($user_source, true),
            AngelType_name_render($angeltype, true)
        ));
        success(sprintf(
            __('%s confirmed for angeltype %s.'),
            User_Nick_render($user_source),
            AngelType_name_render($angeltype)
        ));
        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        __('Confirm angeltype for user'),
        UserAngelType_confirm_view($user_angeltype, $user_source, $angeltype)
    ];
}

/**
 * Remove a user from an Angeltype.
 *
 * @return array
 */
function user_angeltype_delete_controller()
{
    $request = request();
    $user = auth()->user();

    if (!$request->has('user_angeltype_id')) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_angeltype = UserAngelType($request->input('user_angeltype_id'));
    if (empty($user_angeltype)) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $angeltype = AngelType($user_angeltype['angeltype_id']);
    if (empty($angeltype)) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_source = User::find($user_angeltype['user_id']);
    if (!$user_source) {
        error(__('User doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($user->id != $user_angeltype['user_id'] && !User_is_AngelType_supporter($user, $angeltype)) {
        error(__('You are not allowed to delete this users angeltype.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->hasPostData('delete')) {
        UserAngelType_delete($user_angeltype);

        engelsystem_log(sprintf('User %s removed from %s.', User_Nick_render($user_source, true), $angeltype['name']));
        success(sprintf(__('User %s removed from %s.'), User_Nick_render($user_source), $angeltype['name']));

        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        __('Remove angeltype'),
        UserAngelType_delete_view($user_angeltype, $user_source, $angeltype)
    ];
}

/**
 * Update an UserAngelType.
 *
 * @return array
 */
function user_angeltype_update_controller()
{
    $supporter = false;
    $request = request();

    if (!auth()->can('admin_angel_types')) {
        error(__('You are not allowed to set supporter rights.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if (!$request->has('user_angeltype_id')) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->has('supporter') && preg_match('/^[01]$/', $request->input('supporter'))) {
        $supporter = $request->input('supporter') == '1';
    } else {
        error(__('No supporter update given.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_angeltype = UserAngelType($request->input('user_angeltype_id'));
    if (empty($user_angeltype)) {
        error(__('User angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $angeltype = AngelType($user_angeltype['angeltype_id']);
    if (empty($angeltype)) {
        error(__('Angeltype doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    $user_source = User::find($user_angeltype['user_id']);
    if (!$user_source) {
        error(__('User doesn\'t exist.'));
        throw_redirect(page_link_to('angeltypes'));
    }

    if ($request->hasPostData('submit')) {
        UserAngelType_update($user_angeltype['id'], $supporter);

        $msg = $supporter
            ? __('Added supporter rights for %s to %s.')
            : __('Removed supporter rights for %s from %s.');
        engelsystem_log(sprintf(
            $msg,
            AngelType_name_render($angeltype, true),
            User_Nick_render($user_source, true)
        ));
        success(sprintf(
            $msg,
            AngelType_name_render($angeltype),
            User_Nick_render($user_source)
        ));

        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        $supporter ? __('Add supporter rights') : __('Remove supporter rights'),
        UserAngelType_update_view($user_angeltype, $user_source, $angeltype, $supporter)
    ];
}

/**
 * User joining an Angeltype (Or supporter doing this for him).
 *
 * @return array
 */
function user_angeltype_add_controller()
{
    $angeltype = load_angeltype();

    // User is joining by itself
    if (!User_is_AngelType_supporter(auth()->user(), $angeltype)) {
        return user_angeltype_join_controller($angeltype);
    }

    // Allow to add any user

    // Default selection
    $user_source = auth()->user();

    // Load possible users, that are not in the angeltype already
    $users_source = Users_by_angeltype_inverted($angeltype);

    if (request()->hasPostData('submit')) {
        $user_source = load_user();

        if (!UserAngelType_exists($user_source->id, $angeltype)) {
            $user_angeltype_id = UserAngelType_create($user_source->id, $angeltype);

            engelsystem_log(sprintf(
                'User %s added to %s.',
                User_Nick_render($user_source, true),
                AngelType_name_render($angeltype, true)
            ));
            success(sprintf(
                __('User %s added to %s.'),
                User_Nick_render($user_source),
                AngelType_name_render($angeltype)
            ));

            UserAngelType_confirm($user_angeltype_id, $user_source->id);
            engelsystem_log(sprintf(
                'User %s confirmed as %s.',
                User_Nick_render($user_source, true),
                AngelType_name_render($angeltype, true)
            ));

            throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
        }
    }

    return [
        __('Add user to angeltype'),
        UserAngelType_add_view($angeltype, $users_source, $user_source->id)
    ];
}

/**
 * A user joins an angeltype.
 *
 * @param array $angeltype
 * @return array
 */
function user_angeltype_join_controller($angeltype)
{
    $user = auth()->user();

    $user_angeltype = UserAngelType_by_User_and_AngelType($user->id, $angeltype);
    if (!empty($user_angeltype)) {
        error(sprintf(__('You are already a %s.'), $angeltype['name']));
        throw_redirect(page_link_to('angeltypes'));
    }

    if (request()->hasPostData('submit')) {
        $user_angeltype_id = UserAngelType_create($user->id, $angeltype);

        $success_message = sprintf(__('You joined %s.'), $angeltype['name']);
        engelsystem_log(sprintf(
            'User %s joined %s.',
            User_Nick_render($user, true),
            AngelType_name_render($angeltype, true)
        ));
        success($success_message);

        if (auth()->can('admin_user_angeltypes')) {
            UserAngelType_confirm($user_angeltype_id, $user->id);
            engelsystem_log(sprintf(
                'User %s confirmed as %s.',
                User_Nick_render($user, true),
                AngelType_name_render($angeltype, true)
            ));
        }

        throw_redirect(page_link_to('angeltypes', ['action' => 'view', 'angeltype_id' => $angeltype['id']]));
    }

    return [
        sprintf(__('Become a %s'), $angeltype['name']),
        UserAngelType_join_view($user, $angeltype)
    ];
}

/**
 * Route UserAngelType actions.
 *
 * @return array
 */
function user_angeltypes_controller()
{
    $request = request();
    if (!$request->has('action')) {
        throw_redirect(page_link_to('angeltypes'));
    }

    switch ($request->input('action')) {
        case 'delete_all':
            return user_angeltypes_delete_all_controller();
        case 'confirm_all':
            return user_angeltypes_confirm_all_controller();
        case 'confirm':
            return user_angeltype_confirm_controller();
        case 'delete':
            return user_angeltype_delete_controller();
        case 'update':
            return user_angeltype_update_controller();
        case 'add':
            return user_angeltype_add_controller();
        default:
            throw_redirect(page_link_to('angeltypes'));
    }
}