summaryrefslogtreecommitdiff
path: root/includes/pages/admin_free.php
blob: 709954a01687a99598cba437007fc3f382b9b015 (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
<?php

use Engelsystem\Database\DB;
use Engelsystem\Models\User\User;
use Illuminate\Database\Query\JoinClause;

/**
 * @return string
 */
function admin_free_title()
{
    return __('Free angels');
}

/**
 * @return string
 */
function admin_free()
{
    $request = request();

    $search = '';
    if ($request->has('search')) {
        $search = strip_request_item('search');
    }

    $angel_types_source = DB::select('SELECT `id`, `name` FROM `AngelTypes` ORDER BY `name`');
    $angel_types = [
        '' => __('Alle')
    ];
    foreach ($angel_types_source as $angel_type) {
        $angel_types[$angel_type['id']] = $angel_type['name'];
    }

    $angelType = $request->input('angeltype', '');
    $query = User::with('personalData')
        ->select('users.*')
        ->leftJoin('ShiftEntry', 'users.id', 'ShiftEntry.UID')
        ->leftJoin('users_state', 'users.id', 'users_state.user_id')
        ->leftJoin('Shifts', function ($join) {
            /** @var JoinClause $join */
            $join->on('ShiftEntry.SID', '=', 'Shifts.SID')
                ->where('Shifts.start', '<', time())
                ->where('Shifts.end', '>', time());
        })
        ->where('users_state.arrived', '=', 1)
        ->whereNull('Shifts.SID')
        ->orderBy('users.name')
        ->groupBy('users.id');

    if (!empty($angelType)) {
        $query->join('UserAngelTypes', function ($join) use ($angelType) {
            /** @var JoinClause $join */
            $join->on('UserAngelTypes.user_id', '=', 'users.id')
                ->where('UserAngelTypes.angeltype_id', '=', $angelType);
        });
        $query->join('AngelTypes', 'UserAngelTypes.angeltype_id', 'AngelTypes.id')
            ->whereNotNull('UserAngelTypes.confirm_user_id')
            ->orWhere('AngelTypes.restricted', '=', '0');
    }

    if ($request->has('submit')) {
        $users = $query->get();
    } else {
        $users = [];
    }
    $free_users_table = [];
    if ($search == '') {
        $tokens = [];
    } else {
        $tokens = explode(' ', $search);
    }
    foreach ($users as $usr) {
        if (count($tokens) > 0) {
            $match = false;
            $index = join('', $usr->attributesToArray());
            foreach ($tokens as $token) {
                $token = trim($token);
                if (!empty($token) && stristr($index, $token)) {
                    $match = true;
                    break;
                }
            }
            if (!$match) {
                continue;
            }
        }

        $free_users_table[] = [
            'name'        => User_Nick_render($usr) . User_Pronoun_render($usr),
            'shift_state' => User_shift_state_render($usr),
            'last_shift'  => User_last_shift_render($usr),
            'dect'        => $usr->contact->dect,
            'email'       => $usr->settings->email_human ? ($usr->contact->email ? $usr->contact->email : $usr->email) : glyph('eye-close'),
            'actions'     =>
                auth()->can('admin_user')
                    ? button(page_link_to('admin_user', ['id' => $usr->id]), __('edit'), 'btn-xs')
                    : ''
        ];
    }
    return page_with_title(admin_free_title(), [
        form([
            div('row', [
                div('col-md-12 form-inline', [
                    form_text('search', __('Search'), $search),
                    form_select('angeltype', __('Angeltype'), $angel_types, $angelType),
                    form_submit('submit', __('Search'))
                ]),
            ]),
        ]),
        table([
            'name'        => __('Nick'),
            'shift_state' => __('Next shift'),
            'last_shift'  => __('Last shift'),
            'dect'        => __('DECT'),
            'email'       => __('E-Mail'),
            'actions'     => ''
        ], $free_users_table),
    ]);
}