summaryrefslogtreecommitdiff
path: root/includes/controller/shift_entries_controller.php
blob: 5547c1833fff6950454c5496a814adb40f53a4bc (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
<?php

use Engelsystem\Models\User\User;
use Engelsystem\ShiftSignupState;
use Illuminate\Database\Eloquent\Collection;

/**
 * Route shift entry actions.
 *
 * @return array
 */
function shift_entries_controller()
{
    $user = auth()->user();
    if (!$user) {
        redirect(page_link_to('login'));
    }

    $action = strip_request_item('action');
    if (empty($action)) {
        redirect(user_link($user->id));
    }

    switch ($action) {
        case 'create':
            return shift_entry_create_controller();
        case 'delete':
            return shift_entry_delete_controller();
    }
}

/**
 * Sign up for a shift.
 *
 * @return array
 */
function shift_entry_create_controller()
{
    $user = auth()->user();
    $request = request();

    if (User_is_freeloader($user)) {
        redirect(page_link_to('user_myshifts'));
    }

    $shift = Shift($request->input('shift_id'));
    if (empty($shift)) {
        redirect(user_link($user->id));
    }

    $angeltype = AngelType($request->input('angeltype_id'));

    if (auth()->can('user_shifts_admin')) {
        return shift_entry_create_controller_admin($shift, $angeltype);
    }

    if (empty($angeltype)) {
        redirect(user_link($user->id));
    }

    if (User_is_AngelType_supporter($user, $angeltype)) {
        return shift_entry_create_controller_supporter($shift, $angeltype);
    }

    return shift_entry_create_controller_user($shift, $angeltype);
}

/**
 * Sign up for a shift.
 * Case: Admin
 *
 * @param array $shift
 * @param array $angeltype
 * @return array
 */
function shift_entry_create_controller_admin($shift, $angeltype)
{
    $signup_user = auth()->user();
    $request = request();

    if ($request->has('user_id')) {
        $signup_user = User::find($request->input('user_id'));
    }
    if (!$signup_user) {
        redirect(shift_link($shift));
    }

    $angeltypes = AngelTypes();
    if ($request->has('angeltype_id')) {
        $angeltype = AngelType($request->input('angeltype_id'));
    }
    if (empty($angeltype)) {
        if (count($angeltypes) == 0) {
            redirect(shift_link($shift));
        }
        $angeltype = $angeltypes[0];
    }

    if ($request->hasPostData('submit')) {
        ShiftEntry_create([
            'SID'              => $shift['SID'],
            'TID'              => $angeltype['id'],
            'UID'              => $signup_user->id,
            'Comment'          => '',
            'freeloaded'       => false,
            'freeload_comment' => ''
        ]);

        success(sprintf(__('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
        redirect(shift_link($shift));
    }

    /** @var User[]|Collection $users */
    $users = User::query()->orderBy('name')->get();
    $users_select = [];
    foreach ($users as $user) {
        $users_select[$user->id] = $user->name;
    }

    $angeltypes_select = [];
    foreach ($angeltypes as $a) {
        $angeltypes_select[$a['id']] = $a['name'];
    }

    $room = Room($shift['RID']);
    return [
        ShiftEntry_create_title(),
        ShiftEntry_create_view_admin($shift, $room, $angeltype, $angeltypes_select, $signup_user, $users_select)
    ];
}

/**
 * Sign up for a shift.
 * Case: Supporter
 *
 * @param array $shift
 * @param array $angeltype
 * @return array
 */
function shift_entry_create_controller_supporter($shift, $angeltype)
{
    $request = request();
    $signup_user = auth()->user();

    if ($request->has('user_id')) {
        $signup_user = User::find($request->input('user_id'));
    }
    if (!UserAngelType_exists($signup_user->id, $angeltype)) {
        error(__('User is not in angeltype.'));
        redirect(shift_link($shift));
    }

    $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
    $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
    $shift_signup_state = Shift_signup_allowed(
        $signup_user,
        $shift,
        $angeltype,
        null,
        null,
        $needed_angeltype,
        $shift_entries
    );
    if (!$shift_signup_state->isSignupAllowed()) {
        if ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
            error(__('This shift is already occupied.'));
        }
        redirect(shift_link($shift));
    }

    if ($request->hasPostData('submit')) {
        ShiftEntry_create([
            'SID'              => $shift['SID'],
            'TID'              => $angeltype['id'],
            'UID'              => $signup_user->id,
            'Comment'          => '',
            'freeloaded'       => false,
            'freeload_comment' => ''
        ]);

        success(sprintf(__('%s has been subscribed to the shift.'), User_Nick_render($signup_user)));
        redirect(shift_link($shift));
    }

    $users = Users_by_angeltype($angeltype);
    $users_select = [];
    foreach ($users as $u) {
        $users_select[$u->id] = $u->name;
    }

    $room = Room($shift['RID']);
    return [
        ShiftEntry_create_title(),
        ShiftEntry_create_view_supporter($shift, $room, $angeltype, $signup_user, $users_select)
    ];
}

/**
 * Generates an error message for the given shift signup state.
 *
 * @param ShiftSignupState $shift_signup_state
 */
function shift_entry_error_message(ShiftSignupState $shift_signup_state)
{
    if ($shift_signup_state->getState() == ShiftSignupState::ANGELTYPE) {
        error(__('You need be accepted member of the angeltype.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::COLLIDES) {
        error(__('This shift collides with one of your shifts.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::OCCUPIED) {
        error(__('This shift is already occupied.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::SHIFT_ENDED) {
        error(__('This shift ended already.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::NOT_ARRIVED) {
        error(__('You are not marked as arrived.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::NOT_YET) {
        error(__('You are not allowed to sign up yet.'));
    } elseif ($shift_signup_state->getState() == ShiftSignupState::SIGNED_UP) {
        error(__('You are signed up for this shift.'));
    }
}

/**
 * Sign up for a shift.
 * Case: User
 *
 * @param array $shift
 * @param array $angeltype
 * @return array
 */
function shift_entry_create_controller_user($shift, $angeltype)
{
    $request = request();

    $signup_user = auth()->user();
    $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
    $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $angeltype['id']);
    $shift_signup_state = Shift_signup_allowed(
        $signup_user,
        $shift,
        $angeltype,
        null,
        null,
        $needed_angeltype,
        $shift_entries
    );
    if (!$shift_signup_state->isSignupAllowed()) {
        shift_entry_error_message($shift_signup_state);
        redirect(shift_link($shift));
    }

    $comment = '';
    if ($request->hasPostData('submit')) {
        $comment = strip_request_item_nl('comment');
        ShiftEntry_create([
            'SID'              => $shift['SID'],
            'TID'              => $angeltype['id'],
            'UID'              => $signup_user->id,
            'Comment'          => $comment,
            'freeloaded'       => false,
            'freeload_comment' => ''
        ]);

        if ($angeltype['restricted'] == false && !UserAngelType_exists($signup_user->id, $angeltype)) {
            UserAngelType_create($signup_user->id, $angeltype);
        }

        success(__('You are subscribed. Thank you!'));
        redirect(shift_link($shift));
    }

    $room = Room($shift['RID']);
    return [
        ShiftEntry_create_title(),
        ShiftEntry_create_view_user($shift, $room, $angeltype, $comment)
    ];
}

/**
 * Link to create a shift entry.
 *
 * @param array $shift
 * @param array $angeltype
 * @param array $params
 * @return string URL
 */
function shift_entry_create_link($shift, $angeltype, $params = [])
{
    $params = array_merge([
        'action'       => 'create',
        'shift_id'     => $shift['SID'],
        'angeltype_id' => $angeltype['id']
    ], $params);
    return page_link_to('shift_entries', $params);
}

/**
 * Link to create a shift entry as admin.
 *
 * @param array $shift
 * @param array $params
 * @return string URL
 */
function shift_entry_create_link_admin($shift, $params = [])
{
    $params = array_merge([
        'action'   => 'create',
        'shift_id' => $shift['SID']
    ], $params);
    return page_link_to('shift_entries', $params);
}

/**
 * Load a shift entry from get parameter shift_entry_id.
 *
 * @return array
 */
function shift_entry_load()
{
    $request = request();

    if (!$request->has('shift_entry_id') || !test_request_int('shift_entry_id')) {
        redirect(page_link_to('user_shifts'));
    }
    $shiftEntry = ShiftEntry($request->input('shift_entry_id'));
    if (empty($shiftEntry)) {
        error(__('Shift entry not found.'));
        redirect(page_link_to('user_shifts'));
    }

    return $shiftEntry;
}

/**
 * Remove somebody from a shift.
 *
 * @return array
 */
function shift_entry_delete_controller()
{
    $user = auth()->user();
    $request = request();
    $shiftEntry = shift_entry_load();

    $shift = Shift($shiftEntry['SID']);
    $angeltype = AngelType($shiftEntry['TID']);
    $signout_user = User::find($shiftEntry['UID']);
    if (!Shift_signout_allowed($shift, $angeltype, $signout_user->id)) {
        error(__('You are not allowed to remove this shift entry. If necessary, ask your supporter or heaven to do so.'));
        redirect(user_link($signout_user->id));
    }

    if ($request->hasPostData('delete')) {
        ShiftEntry_delete($shiftEntry);
        success(__('Shift entry removed.'));
        redirect(shift_link($shift));
    }

    if ($user->id == $signout_user->id) {
        return [
            ShiftEntry_delete_title(),
            ShiftEntry_delete_view($shift, $angeltype, $signout_user->id)
        ];
    }

    return [
        ShiftEntry_delete_title(),
        ShiftEntry_delete_view_admin($shift, $angeltype, $signout_user)
    ];
}

/**
 * Link to delete a shift entry.
 *
 * @param array $shiftEntry
 * @param array $params
 * @return string URL
 */
function shift_entry_delete_link($shiftEntry, $params = [])
{
    $params = array_merge([
        'action'         => 'delete',
        'shift_entry_id' => $shiftEntry['id']
    ], $params);
    return page_link_to('shift_entries', $params);
}