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

use Engelsystem\Database\DB;

/**
 * Sign up for a shift.
 *
 * @return string
 */
function shift_entry_add_controller()
{
    global $privileges, $user;

    $request = request();
    $shift_id = 0;
    if ($request->has('shift_id') && preg_match('/^\d+$/', $request->input('shift_id'))) {
        $shift_id = $request->input('shift_id');
    } else {
        redirect(page_link_to('user_shifts'));
    }

    // Locations laden
    $rooms = Rooms();
    $room_array = [];
    foreach ($rooms as $room) {
        $room_array[$room['RID']] = $room['Name'];
    }

    $shift = Shift($shift_id);
    if ($shift == null) {
        redirect(page_link_to('user_shifts'));
    }
    $shift['Name'] = $room_array[$shift['RID']];

    $type_id = null;
    if ($request->has('type_id') && preg_match('/^\d+$/', $request->input('type_id'))) {
        $type_id = $request->input('type_id');
    }

    if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter', $privileges)) {
        if($type_id == null) {
            // If no angeltype id is given, then select first existing angeltype.
            $needed_angeltypes = NeededAngelTypes_by_shift($shift_id);
            if(count($needed_angeltypes) > 0) {
                $type_id = $needed_angeltypes[0]['id'];
            }
        }
        $type = AngelType($type_id);
    } else {
        // TODO: Move queries to model
        $type = DB::selectOne('
            SELECT *
            FROM `UserAngelTypes`
            JOIN `AngelTypes` ON (`UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`)
            WHERE `AngelTypes`.`id` = ?
            AND (
                `AngelTypes`.`restricted` = 0
                OR (
                    `UserAngelTypes`.`user_id` = ?
                    AND NOT `UserAngelTypes`.`confirm_user_id` IS NULL
                )
            )
        ', [$type_id, $user['UID']]);
    }

    if (empty($type)) {
        redirect(page_link_to('user_shifts'));
    }

    if (
        $request->has('user_id')
        && preg_match('/^\d+$/', $request->input('user_id'))
        && (
            in_array('user_shifts_admin', $privileges)
            || in_array('shiftentry_edit_angeltype_supporter', $privileges)
        )
    ) {
        $user_id = $request->input('user_id');
    } else {
        $user_id = $user['UID'];
    }

    $needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $type);
    $shift_entries = ShiftEntries_by_shift_and_angeltype($shift['SID'], $type['id']);

    $shift_signup_allowed = Shift_signup_allowed(
        User($user_id),
        $shift,
        $type,
        null,
        null,
        $needed_angeltype,
        $shift_entries
    );
    if (!$shift_signup_allowed->isSignupAllowed()) {
        error(_('You are not allowed to sign up for this shift. Maybe shift is full or already running.'));
        redirect(shift_link($shift));
    }

    if ($request->has('submit')) {
        $selected_type_id = $type_id;
        if (in_array('user_shifts_admin', $privileges) || in_array('shiftentry_edit_angeltype_supporter',
                $privileges)
        ) {

            if (count(DB::select('SELECT `UID` FROM `User` WHERE `UID`=? LIMIT 1', [$user_id])) == 0) {
                redirect(page_link_to('user_shifts'));
            }

            if (
                $request->has('angeltype_id')
                && test_request_int('angeltype_id')
                && count(DB::select(
                    'SELECT `id` FROM `AngelTypes` WHERE `id`=? LIMIT 1',
                    [$request->input('angeltype_id')]
                )) > 0
            ) {
                $selected_type_id = $request->input('angeltype_id');
            }
        }

        if (count(DB::select(
            'SELECT `id` FROM `ShiftEntry` WHERE `SID`= ? AND `UID` = ?',
            [$shift['SID'], $user_id]))
        ) {
            return error(_('This angel does already have an entry for this shift.'), true);
        }

        $freeloaded = isset($shift['freeloaded']) ? $shift['freeloaded'] : false;
        $freeload_comment = isset($shift['freeload_comment']) ? $shift['freeload_comment'] : '';
        if (in_array('user_shifts_admin', $privileges)) {
            $freeloaded = $request->has('freeloaded');
            $freeload_comment = strip_request_item_nl('freeload_comment');
        }

        $comment = strip_request_item_nl('comment');
        ShiftEntry_create([
            'SID'              => $shift_id,
            'TID'              => $selected_type_id,
            'UID'              => $user_id,
            'Comment'          => $comment,
            'freeloaded'       => $freeloaded,
            'freeload_comment' => $freeload_comment
        ]);

        if (
            $type['restricted'] == 0
            && count(DB::select('
              SELECT `UserAngelTypes`.`id` FROM `UserAngelTypes`
              INNER JOIN `AngelTypes` ON `AngelTypes`.`id` = `UserAngelTypes`.`angeltype_id`
              WHERE `angeltype_id` = ?
              AND `user_id` = ?
            ', [$selected_type_id, $user_id])) == 0
        ) {
            DB::insert(
                'INSERT INTO `UserAngelTypes` (`user_id`, `angeltype_id`) VALUES (?, ?)',
                [$user_id, $selected_type_id]
            );
        }

        $user_source = User($user_id);
        engelsystem_log(
            'User ' . User_Nick_render($user_source)
            . ' signed up for shift ' . $shift['name']
            . ' from ' . date('Y-m-d H:i', $shift['start'])
            . ' to ' . date('Y-m-d H:i', $shift['end'])
        );
        success(_('You are subscribed. Thank you!') . ' <a href="' . page_link_to('user_myshifts') . '">' . _('My shifts') . ' &raquo;</a>');
        redirect(shift_link($shift));
    }

    $angeltype_select = '';
    if (in_array('user_shifts_admin', $privileges)) {
        $users = DB::select('
            SELECT *,
            (
                SELECT count(*)
                FROM `ShiftEntry`
                WHERE `freeloaded`=1
                AND `ShiftEntry`.`UID`=`User`.`UID`
            ) AS `freeloaded`
            FROM `User`
            ORDER BY `Nick`
        ');
        $users_select = [];
        foreach ($users as $usr) {
            $users_select[$usr['UID']] = $usr['Nick'] . ($usr['freeloaded'] == 0 ? '' : ' (' . _('Freeloader') . ')');
        }
        $user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);

        $angeltypes_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
        $angeltypes = [];
        foreach ($angeltypes_source as $angeltype) {
            $angeltypes[$angeltype['id']] = $angeltype['name'];
        }
        $angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']);
    } elseif (in_array('shiftentry_edit_angeltype_supporter', $privileges) && User_is_AngelType_supporter($user, $type)) {
        $users = Users_by_angeltype($type);
        $users_select = [];
        foreach ($users as $usr) {
            if (!$type['restricted'] || $usr['confirm_user_id'] != null) {
                $users_select[$usr['UID']] = $usr['Nick'];
            }
        }
        $user_text = html_select_key('user_id', 'user_id', $users_select, $user['UID']);

        $angeltypes_source = User_angeltypes($user);
        $angeltypes = [];
        foreach ($angeltypes_source as $angeltype) {
            if ($angeltype['supporter']) {
                $angeltypes[$angeltype['id']] = $angeltype['name'];
            }
            $angeltype_select = html_select_key('angeltype_id', 'angeltype_id', $angeltypes, $type['id']);
        }
    } else {
        $user_text = User_Nick_render($user);
        $angeltype_select = $type['name'];
    }

    return ShiftEntry_edit_view(
        $user_text,
        date('Y-m-d H:i', $shift['start'])
        . ' &ndash; '
        . date('Y-m-d H:i', $shift['end'])
        . ' (' . shift_length($shift) . ')',
        $shift['Name'],
        $shift['name'],
        $angeltype_select, '',
        false,
        null,
        in_array('user_shifts_admin', $privileges)
    );
}

/**
 * Load a shift entry from get parameter entry_id.
 */
function shift_entry_load() {
    $request = request();

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

/**
 * Remove somebody from a shift.
 */
function shift_entry_delete_controller()
{
    global $privileges, $user;
    $request = request();
    $shiftEntry = shift_entry_load();

    $shift = Shift($shiftEntry['SID']);
    $angeltype = AngelType($shiftEntry['TID']);
    $signout_user = User($shiftEntry['UID']);
    if(!Shift_signout_allowed($shift, $angeltype, $signout_user)) {
        error(_('You are not allowed to remove this shift entry. If neccessary, ask your supporter or heaven to do so.'));
        redirect(user_link($signout_user));
    }
    
    if($request->has('continue')) {
        ShiftEntry_delete($shiftEntry);
        success(_('Shift entry removed.'));
        redirect(shift_link($shift));
    }

    if($user['UID'] == $signout_user['UID']) {
        return ShiftEntry_delete_view($shiftEntry, $shift, $angeltype, $signout_user);
    }
    
    return ShiftEntry_delete_view_admin($shiftEntry, $shift, $angeltype, $signout_user);
}