You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/controller/user_driver_licenses_contro...

145 lines
4.1 KiB
PHTML

<?php
use Engelsystem\Models\User\User;
/**
8 years ago
* Generates a hint, if user joined angeltypes that require a driving license and the user has no driver license
* information provided.
*
* @return string|null
*/
8 years ago
function user_driver_license_required_hint()
{
$user = auth()->user();
8 years ago
// User has already entered data, no hint needed.
if ($user->license->wantsToDrive()) {
8 years ago
return null;
}
$angeltypes = User_angeltypes($user->id);
8 years ago
foreach ($angeltypes as $angeltype) {
if ($angeltype['requires_driver_license']) {
8 years ago
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() . '" class="text-info">' . __('driving license information') . '</a>'
8 years ago
);
8 years ago
}
}
8 years ago
8 years ago
return null;
}
/**
* Route user driver licenses actions.
*
* @return array
*/
8 years ago
function user_driver_licenses_controller()
{
$user = auth()->user();
8 years ago
if (!$user) {
throw_redirect(page_link_to(''));
8 years ago
}
8 years ago
8 years ago
$action = strip_request_item('action', 'edit');
8 years ago
8 years ago
switch ($action) {
8 years ago
default:
case 'edit':
return user_driver_license_edit_controller();
}
}
/**
* Link to user driver license edit page for given user.
*
* @param User $user
* @return string
*/
8 years ago
function user_driver_license_edit_link($user = null)
{
if (!$user) {
8 years ago
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
*/
8 years ago
function user_driver_license_load_user()
{
$request = request();
$user_source = auth()->user();
8 years ago
if ($request->has('user_id')) {
$user_source = User::find($request->input('user_id'));
if (empty($user_source)) {
throw_redirect(user_driver_license_edit_link());
8 years ago
}
}
8 years ago
8 years ago
return $user_source;
}
/**
* Edit a users driver license information.
*
* @return array
*/
8 years ago
function user_driver_license_edit_controller()
{
$user = auth()->user();
$request = request();
8 years ago
$user_source = user_driver_license_load_user();
8 years ago
// only privilege admin_user can edit other users driver license information
if ($user->id != $user_source->id && !auth()->can('admin_user')) {
throw_redirect(user_driver_license_edit_link());
8 years ago
}
$driverLicense = $user_source->license;
if ($request->hasPostData('submit')) {
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');
$driverLicense->drive_12t= $request->has('has_license_12t_truck');
$driverLicense->drive_forklift = $request->has('has_license_forklift');
if ($driverLicense->wantsToDrive()) {
$driverLicense->save();
engelsystem_log('Driver license information updated.');
success(__('Your driver license information has been saved.'));
throw_redirect(user_link($user_source->id));
8 years ago
} else {
error(__('Please select at least one driving license.'));
8 years ago
}
} 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();
engelsystem_log('Driver license information removed.');
success(__('Your driver license information has been removed.'));
throw_redirect(user_link($user_source->id));
}
}
8 years ago
8 years ago
return [
sprintf(__('Edit %s driving license information'), $user_source->name),
UserDriverLicense_edit_view($user_source, $driverLicense)
8 years ago
];
}