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

use Engelsystem\Database\DB;

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

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

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

    $angelTypeSearch = '';
    $angelType = $request->input('angeltype', '');
    if (!empty($angelType)) {
        $angelTypeSearch = ' INNER JOIN `UserAngelTypes` ON (`UserAngelTypes`.`angeltype_id` = '
            . DB::getPdo()->quote($angelType)
            . ' AND `UserAngelTypes`.`user_id` = `User`.`UID`';
        if ($request->has('confirmed_only')) {
            $angelTypeSearch .= ' AND `UserAngelTypes`.`confirm_user_id`';
        }
        $angelTypeSearch .= ') ';
    }

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

    $users = DB::select('
          SELECT `User`.*
          FROM `User`
          ' . $angelTypeSearch . '
          LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
          LEFT JOIN `Shifts`
              ON (
                  `ShiftEntry`.`SID` = `Shifts`.`SID`
                  AND `Shifts`.`start` < ?
                  AND `Shifts`.`end` > ?
              )
          WHERE `User`.`Gekommen` = 1
          AND `Shifts`.`SID` IS NULL
          GROUP BY `User`.`UID`
          ORDER BY `Nick`
        ',
        [
            time(),
            time(),
        ]
    );

    $free_users_table = [];
    if ($search == '') {
        $tokens = [];
    } else {
        $tokens = explode(' ', $search);
    }
    foreach ($users as $usr) {
        if (count($tokens) > 0) {
            $match = false;
            $index = join('', $usr);
            foreach ($tokens as $t) {
                if (stristr($index, trim($t))) {
                    $match = true;
                    break;
                }
            }
            if (!$match) {
                continue;
            }
        }

        $free_users_table[] = [
            'name'        => User_Nick_render($usr),
            'shift_state' => User_shift_state_render($usr),
            'dect'        => $usr['DECT'],
            'jabber'      => $usr['jabber'],
            'email'       => $usr['email_by_human_allowed'] ? $usr['email'] : glyph('eye-close'),
            'actions'     =>
                in_array('admin_user', $privileges)
                    ? button(page_link_to('admin_user', ['id' => $usr['UID']]), _('edit'), 'btn-xs')
                    : ''
        ];
    }
    return page_with_title(admin_free_title(), [
        form([
            div('row', [
                div('col-md-4', [
                    form_text('search', _('Search'), $search)
                ]),
                div('col-md-4', [
                    form_select('angeltype', _('Angeltype'), $angel_types, $angelType)
                ]),
                div('col-md-2', [
                    form_checkbox('confirmed_only', _('Only confirmed'), $request->has('confirmed_only'))
                ]),
                div('col-md-2', [
                    form_submit('submit', _('Search'))
                ])
            ])
        ]),
        table([
            'name'        => _('Nick'),
            'shift_state' => '',
            'dect'        => _('DECT'),
            'jabber'      => _('Jabber'),
            'email'       => _('E-Mail'),
            'actions'     => ''
        ], $free_users_table)
    ]);
}