2015-12-19 23:31:08 +01:00
< ? php
2015-12-20 11:36:12 +01:00
/**
* Generates a hint , if user joined angeltypes that require a driving license and the user has no driver license information provided .
*/
function user_driver_license_required_hint () {
global $user ;
$angeltypes = User_angeltypes ( $user );
2016-09-29 09:43:08 +02:00
if ( $angeltypes === false ) {
2015-12-20 11:36:12 +01:00
engelsystem_error ( " Unable to load user angeltypes. " );
2016-09-29 09:43:08 +02:00
}
2015-12-20 11:36:12 +01:00
$user_driver_license = UserDriverLicense ( $user [ 'UID' ]);
2016-09-29 09:43:08 +02:00
if ( $user_driver_license === false ) {
2015-12-20 11:36:12 +01:00
engelsystem_error ( " Unable to load user driver license. " );
2016-09-29 09:43:08 +02:00
}
2015-12-20 11:36:12 +01:00
$driving_license_information_required = false ;
2016-09-29 09:43:08 +02:00
foreach ( $angeltypes as $angeltype ) {
2015-12-20 11:36:12 +01:00
if ( $angeltype [ 'requires_driver_license' ]) {
$driving_license_information_required = true ;
break ;
}
2016-09-29 09:43:08 +02:00
}
2015-12-20 11:36:12 +01:00
2016-09-29 09:43:08 +02:00
if ( $driving_license_information_required && $user_driver_license == null ) {
2015-12-20 11:36:12 +01:00
return info ( 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>' ), true );
2016-09-29 09:43:08 +02:00
}
2015-12-20 11:36:12 +01:00
return '' ;
}
2015-12-19 23:31:08 +01:00
/**
* Route user driver licenses actions .
*/
function user_driver_licenses_controller () {
2016-08-21 18:00:24 +02:00
global $user ;
2015-12-19 23:31:08 +01:00
2016-09-29 09:43:08 +02:00
if ( ! isset ( $user )) {
2015-12-19 23:31:08 +01:00
redirect ( page_link_to ( '' ));
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
2016-09-29 09:43:08 +02:00
if ( ! isset ( $_REQUEST [ 'action' ])) {
2015-12-19 23:31:08 +01:00
$_REQUEST [ 'action' ] = 'edit' ;
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
switch ( $_REQUEST [ 'action' ]) {
default :
case 'edit' :
return user_driver_license_edit_controller ();
}
}
/**
* Link to user driver license edit page for given user .
*
* @ param User $user
*/
function user_driver_license_edit_link ( $user = null ) {
2016-09-29 09:43:08 +02:00
if ( $user == null ) {
2015-12-19 23:31:08 +01:00
return page_link_to ( 'user_driver_licenses' );
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
return page_link_to ( 'user_driver_licenses' ) . '&user_id=' . $user [ 'UID' ];
}
/**
* Edit a users driver license information .
*/
function user_driver_license_edit_controller () {
global $privileges , $user ;
2016-10-01 10:48:19 +02:00
$user_source = $user ;
2015-12-19 23:31:08 +01:00
if ( isset ( $_REQUEST [ 'user_id' ])) {
$user_source = User ( $_REQUEST [ 'user_id' ]);
2016-09-29 09:43:08 +02:00
if ( $user_source === false ) {
2015-12-19 23:31:08 +01:00
engelsystem_error ( 'Unable to load angeltype.' );
2016-09-29 09:43:08 +02:00
}
if ( $user_source == null ) {
2015-12-19 23:31:08 +01:00
redirect ( user_driver_license_edit_link ());
2016-09-29 09:43:08 +02:00
}
// only privilege admin_user can edit other users driver license information
if ( $user [ 'UID' ] != $user_source [ 'UID' ] && ! in_array ( 'admin_user' , $privileges )) {
2015-12-19 23:31:08 +01:00
redirect ( user_driver_license_edit_link ());
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
}
$wants_to_drive = false ;
$has_car = false ;
$has_license_car = false ;
$has_license_3_5t_transporter = false ;
$has_license_7_5t_truck = false ;
$has_license_12_5t_truck = false ;
$has_license_forklift = false ;
$user_driver_license = UserDriverLicense ( $user_source [ 'UID' ]);
2016-09-29 09:43:08 +02:00
if ( $user_driver_license === false ) {
2015-12-19 23:31:08 +01:00
engelsystem_error ( 'Unable to load user driver license.' );
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
if ( $user_driver_license != null ) {
$wants_to_drive = true ;
$has_car = $user_driver_license [ 'has_car' ];
$has_license_car = $user_driver_license [ 'has_license_car' ];
$has_license_3_5t_transporter = $user_driver_license [ 'has_license_3_5t_transporter' ];
$has_license_7_5t_truck = $user_driver_license [ 'has_license_7_5t_truck' ];
$has_license_12_5t_truck = $user_driver_license [ 'has_license_12_5t_truck' ];
$has_license_forklift = $user_driver_license [ 'has_license_forklift' ];
}
if ( isset ( $_REQUEST [ 'submit' ])) {
2016-09-29 12:08:12 +02:00
$valid = true ;
2015-12-19 23:31:08 +01:00
$wants_to_drive = isset ( $_REQUEST [ 'wants_to_drive' ]);
$has_car = isset ( $_REQUEST [ 'has_car' ]);
$has_license_car = isset ( $_REQUEST [ 'has_license_car' ]);
$has_license_3_5t_transporter = isset ( $_REQUEST [ 'has_license_3_5t_transporter' ]);
$has_license_7_5t_truck = isset ( $_REQUEST [ 'has_license_7_5t_truck' ]);
$has_license_12_5t_truck = isset ( $_REQUEST [ 'has_license_12_5t_truck' ]);
$has_license_forklift = isset ( $_REQUEST [ 'has_license_forklift' ]);
if ( $wants_to_drive && ! $has_license_car && ! $has_license_3_5t_transporter && ! $has_license_7_5t_truck && ! $has_license_12_5t_truck && ! $has_license_forklift ) {
2016-09-29 12:08:12 +02:00
$valid = false ;
2015-12-19 23:31:08 +01:00
error ( _ ( " Please select at least one driving license. " ));
}
2016-09-29 12:08:12 +02:00
if ( $valid ) {
2015-12-19 23:31:08 +01:00
if ( ! $wants_to_drive && $user_driver_license != null ) {
$result = UserDriverLicenses_delete ( $user_source [ 'UID' ]);
2016-09-29 09:43:08 +02:00
if ( $result === false ) {
2015-12-20 21:07:08 +01:00
engelsystem_error ( " Unable to remove user driver license information " );
2016-09-29 09:43:08 +02:00
}
2015-12-20 21:07:08 +01:00
engelsystem_log ( " Driver license information removed. " );
2015-12-19 23:31:08 +01:00
success ( _ ( " Your driver license information has been removed. " ));
} else {
if ( $wants_to_drive ) {
2016-09-29 09:43:08 +02:00
if ( $user_driver_license == null ) {
2015-12-19 23:31:08 +01:00
$result = UserDriverLicenses_create ( $user_source [ 'UID' ], $has_car , $has_license_car , $has_license_3_5t_transporter , $has_license_7_5t_truck , $has_license_12_5t_truck , $has_license_forklift );
2016-09-29 09:43:08 +02:00
} else {
2015-12-19 23:31:08 +01:00
$result = UserDriverLicenses_update ( $user_source [ 'UID' ], $has_car , $has_license_car , $has_license_3_5t_transporter , $has_license_7_5t_truck , $has_license_12_5t_truck , $has_license_forklift );
2016-09-29 09:43:08 +02:00
}
2015-12-19 23:31:08 +01:00
2016-09-29 09:43:08 +02:00
if ( $result === false ) {
2015-12-20 21:07:08 +01:00
engelsystem_error ( " Unable to save user driver license information. " );
2016-09-29 09:43:08 +02:00
}
2015-12-20 21:07:08 +01:00
engelsystem_log ( " Driver license information updated. " );
2015-12-19 23:31:08 +01:00
}
success ( _ ( " Your driver license information has been saved. " ));
}
redirect ( user_link ( $user_source ));
}
}
return [
sprintf ( _ ( " Edit %s driving license information " ), $user_source [ 'Nick' ]),
UserDriverLicense_edit_view ( $user_source , $wants_to_drive , $has_car , $has_license_car , $has_license_3_5t_transporter , $has_license_7_5t_truck , $has_license_12_5t_truck , $has_license_forklift )
];
}
?>