2015-12-19 23:31:08 +01:00
|
|
|
<?php
|
|
|
|
|
2018-10-10 03:10:28 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
|
2015-12-20 11:36:12 +01:00
|
|
|
/**
|
2017-01-02 15:43:36 +01:00
|
|
|
* Generates a hint, if user joined angeltypes that require a driving license and the user has no driver license
|
|
|
|
* information provided.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return string|null
|
2015-12-20 11:36:12 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_driver_license_required_hint()
|
|
|
|
{
|
2018-10-09 21:47:31 +02:00
|
|
|
$user = auth()->user();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
// User has already entered data, no hint needed.
|
2021-12-19 18:38:42 +01:00
|
|
|
if ($user->license->wantsToDrive()) {
|
2017-01-02 15:43:36 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:57:04 +01:00
|
|
|
$angeltypes = $user->userAngelTypes;
|
2017-01-02 03:57:23 +01:00
|
|
|
foreach ($angeltypes as $angeltype) {
|
2022-12-03 00:57:04 +01:00
|
|
|
if ($angeltype->requires_driver_license) {
|
2017-01-02 15:43:36 +01:00
|
|
|
return sprintf(
|
2018-08-29 21:55:32 +02:00
|
|
|
__('You joined an angeltype which requires a driving license. Please edit your driving license information here: %s.'),
|
2021-07-24 12:38:23 +02:00
|
|
|
'<a href="' . user_driver_license_edit_link() . '" class="text-info">' . __('driving license information') . '</a>'
|
2017-01-02 15:43:36 +01:00
|
|
|
);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2015-12-20 11:36:12 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return null;
|
2015-12-20 11:36:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-19 23:31:08 +01:00
|
|
|
/**
|
|
|
|
* Route user driver licenses actions.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2015-12-19 23:31:08 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_driver_licenses_controller()
|
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
$user = auth()->user();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-10-08 21:15:56 +02:00
|
|
|
if (!$user) {
|
2022-06-16 23:00:56 +02:00
|
|
|
throw_redirect(page_link_to());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$action = strip_request_item('action', 'edit');
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2022-12-22 00:08:54 +01:00
|
|
|
return match ($action) {
|
|
|
|
'edit' => user_driver_license_edit_controller(),
|
|
|
|
default => user_driver_license_edit_controller(),
|
|
|
|
};
|
2015-12-19 23:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link to user driver license edit page for given user.
|
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @param User $user
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return string
|
2015-12-19 23:31:08 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_driver_license_edit_link($user = null)
|
|
|
|
{
|
2018-10-10 03:10:28 +02:00
|
|
|
if (!$user) {
|
2017-01-02 03:57:23 +01:00
|
|
|
return page_link_to('user_driver_licenses');
|
|
|
|
}
|
2018-10-10 03:10:28 +02:00
|
|
|
|
|
|
|
return page_link_to('user_driver_licenses', ['user_id' => $user->id]);
|
2015-12-19 23:31:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-10 13:55:16 +01:00
|
|
|
* Loads the user for the driver license.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
2018-10-10 03:10:28 +02:00
|
|
|
* @return User
|
2015-12-19 23:31:08 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_driver_license_load_user()
|
|
|
|
{
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2018-10-17 01:30:10 +02:00
|
|
|
$user_source = auth()->user();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-07-18 21:38:53 +02:00
|
|
|
if ($request->has('user_id')) {
|
2018-10-10 03:10:28 +02:00
|
|
|
$user_source = User::find($request->input('user_id'));
|
2018-01-14 17:47:26 +01:00
|
|
|
if (empty($user_source)) {
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(user_driver_license_edit_link());
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2016-09-29 09:43:08 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $user_source;
|
2016-11-10 13:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit a users driver license information.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2016-11-10 13:55:16 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function user_driver_license_edit_controller()
|
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
$user = auth()->user();
|
2017-07-18 21:38:53 +02:00
|
|
|
$request = request();
|
2017-01-02 03:57:23 +01:00
|
|
|
$user_source = user_driver_license_load_user();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
// only privilege admin_user can edit other users driver license information
|
2018-11-12 14:41:23 +01:00
|
|
|
if ($user->id != $user_source->id && !auth()->can('admin_user')) {
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(user_driver_license_edit_link());
|
2017-01-02 15:43:36 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 18:38:42 +01:00
|
|
|
$driverLicense = $user_source->license;
|
2018-11-20 16:02:03 +01:00
|
|
|
if ($request->hasPostData('submit')) {
|
2021-12-19 18:38:42 +01:00
|
|
|
if ($request->has('wants_to_drive')) {
|
|
|
|
$driverLicense->has_car = $request->has('has_car');
|
|
|
|
$driverLicense->drive_car = $request->has('has_license_car');
|
|
|
|
$driverLicense->drive_3_5t = $request->has('has_license_3_5t_transporter');
|
|
|
|
$driverLicense->drive_7_5t = $request->has('has_license_7_5t_truck');
|
2022-10-18 19:15:22 +02:00
|
|
|
$driverLicense->drive_12t = $request->has('has_license_12t_truck');
|
2021-12-19 18:38:42 +01:00
|
|
|
$driverLicense->drive_forklift = $request->has('has_license_forklift');
|
|
|
|
|
|
|
|
if ($driverLicense->wantsToDrive()) {
|
|
|
|
$driverLicense->save();
|
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log('Driver license information updated.');
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Your driver license information has been saved.'));
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(user_link($user_source->id));
|
2017-01-02 03:57:23 +01:00
|
|
|
} else {
|
2018-08-29 21:55:32 +02:00
|
|
|
error(__('Please select at least one driving license.'));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2021-12-19 18:38:42 +01:00
|
|
|
} else {
|
|
|
|
$driverLicense->has_car = false;
|
|
|
|
$driverLicense->drive_car = false;
|
|
|
|
$driverLicense->drive_3_5t = false;
|
|
|
|
$driverLicense->drive_7_5t = false;
|
|
|
|
$driverLicense->drive_12t = false;
|
|
|
|
$driverLicense->drive_forklift = false;
|
|
|
|
$driverLicense->save();
|
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
engelsystem_log('Driver license information removed.');
|
2018-08-29 21:55:32 +02:00
|
|
|
success(__('Your driver license information has been removed.'));
|
2019-09-08 02:25:49 +02:00
|
|
|
throw_redirect(user_link($user_source->id));
|
2015-12-19 23:31:08 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return [
|
2022-07-10 21:12:10 +02:00
|
|
|
sprintf(__('Edit %s driving license information'), $user_source->displayName),
|
2023-02-05 18:03:00 +01:00
|
|
|
UserDriverLicense_edit_view($user_source, $driverLicense),
|
2017-01-02 15:43:36 +01:00
|
|
|
];
|
2015-12-19 23:31:08 +01:00
|
|
|
}
|