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

use Engelsystem\Models\User\User;

/**
 * Generates a hint, if user joined angeltypes that require a driving license and the user has no driver license
 * information provided.
 *
 * @return string|null
 */
function user_driver_license_required_hint()
{
    $user = auth()->user();

    $angeltypes = User_angeltypes($user->id);
    $user_driver_license = UserDriverLicense($user->id);

    // User has already entered data, no hint needed.
    if (!empty($user_driver_license)) {
        return null;
    }

    foreach ($angeltypes as $angeltype) {
        if ($angeltype['requires_driver_license']) {
            return sprintf(
                __('You joined an angeltype which requires a driving license. Please edit your driving license information here: %s.'),
                '<a href="' . user_driver_license_edit_link() . '">' . __('driving license information') . '</a>'
            );
        }
    }

    return null;
}

/**
 * Route user driver licenses actions.
 *
 * @return array
 */
function user_driver_licenses_controller()
{
    $user = Auth()->user();

    if (!$user) {
        redirect(page_link_to(''));
    }

    $action = strip_request_item('action', 'edit');

    switch ($action) {
        default:
        case 'edit':
            return user_driver_license_edit_controller();
    }
}

/**
 * Link to user driver license edit page for given user.
 *
 * @param User $user
 * @return string
 */
function user_driver_license_edit_link($user = null)
{
    if (!$user) {
        return page_link_to('user_driver_licenses');
    }

    return page_link_to('user_driver_licenses', ['user_id' => $user->id]);
}

/**
 * Loads the user for the driver license.
 *
 * @return User
 */
function user_driver_license_load_user()
{
    $user = auth()->user();
    $request = request();
    $user_source = $user;

    if ($request->has('user_id')) {
        $user_source = User::find($request->input('user_id'));
        if (empty($user_source)) {
            redirect(user_driver_license_edit_link());
        }
    }

    return $user_source;
}

/**
 * Edit a users driver license information.
 *
 * @return array
 */
function user_driver_license_edit_controller()
{
    global $privileges;
    $user = Auth()->user();
    $request = request();
    $user_source = user_driver_license_load_user();

    // only privilege admin_user can edit other users driver license information
    if ($user->id != $user_source->id && !in_array('admin_user', $privileges)) {
        redirect(user_driver_license_edit_link());
    }

    $user_driver_license = UserDriverLicense($user_source->id);
    if (empty($user_driver_license)) {
        $wants_to_drive = false;
        $user_driver_license = UserDriverLicense_new();
    } else {
        $wants_to_drive = true;
    }

    if ($request->has('submit')) {
        $wants_to_drive = $request->has('wants_to_drive');
        if ($wants_to_drive) {
            $user_driver_license['has_car'] = $request->has('has_car');
            $user_driver_license['has_license_car'] = $request->has('has_license_car');
            $user_driver_license['has_license_3_5t_transporter'] = $request->has('has_license_3_5t_transporter');
            $user_driver_license['has_license_7_5t_truck'] = $request->has('has_license_7_5t_truck');
            $user_driver_license['has_license_12_5t_truck'] = $request->has('has_license_12_5t_truck');
            $user_driver_license['has_license_forklift'] = $request->has('has_license_forklift');

            if (UserDriverLicense_valid($user_driver_license)) {
                if (empty($user_driver_license['user_id'])) {
                    $user_driver_license = UserDriverLicenses_create($user_driver_license, $user_source->id);
                } else {
                    UserDriverLicenses_update($user_driver_license);
                }
                engelsystem_log('Driver license information updated.');
                success(__('Your driver license information has been saved.'));
                redirect(user_link($user_source->id));
            } else {
                error(__('Please select at least one driving license.'));
            }
        } elseif (!empty($user_driver_license['user_id'])) {
            UserDriverLicenses_delete($user_source->id);
            engelsystem_log('Driver license information removed.');
            success(__('Your driver license information has been removed.'));
            redirect(user_link($user_source->id));
        }
    }

    return [
        sprintf(__('Edit %s driving license information'), $user_source->name),
        UserDriverLicense_edit_view($user_source, $wants_to_drive, $user_driver_license)
    ];
}