reduce complexity of user driver license controller

This commit is contained in:
msquare 2016-11-10 13:55:16 +01:00
parent f6da9148e7
commit 468a10774c
4 changed files with 42 additions and 34 deletions

View File

@ -192,12 +192,7 @@ function angeltype_controller() {
} }
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype); $user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
$user_driver_license = UserDriverLicense($user['UID']); $user_driver_license = UserDriverLicense($user['UID']);
if ($user_driver_license === false) {
engelsystem_error("Unable to load user driver license.");
}
$members = Users_by_angeltype($angeltype); $members = Users_by_angeltype($angeltype);
if ($members === false) { if ($members === false) {
engelsystem_error("Unable to load members."); engelsystem_error("Unable to load members.");

View File

@ -7,13 +7,7 @@ function user_driver_license_required_hint() {
global $user; global $user;
$angeltypes = User_angeltypes($user); $angeltypes = User_angeltypes($user);
if ($angeltypes === false) {
engelsystem_error("Unable to load user angeltypes.");
}
$user_driver_license = UserDriverLicense($user['UID']); $user_driver_license = UserDriverLicense($user['UID']);
if ($user_driver_license === false) {
engelsystem_error("Unable to load user driver license.");
}
$driving_license_information_required = false; $driving_license_information_required = false;
foreach ($angeltypes as $angeltype) { foreach ($angeltypes as $angeltype) {
@ -64,10 +58,10 @@ function user_driver_license_edit_link($user = null) {
} }
/** /**
* Edit a users driver license information. * Loads the user for the driver license.
*/ */
function user_driver_license_edit_controller() { function user_driver_license_load_user() {
global $privileges, $user; global $user;
$user_source = $user; $user_source = $user;
@ -76,11 +70,22 @@ function user_driver_license_edit_controller() {
if ($user_source == null) { if ($user_source == null) {
redirect(user_driver_license_edit_link()); redirect(user_driver_license_edit_link());
} }
}
// only privilege admin_user can edit other users driver license information
if ($user['UID'] != $user_source['UID'] && ! in_array('admin_user', $privileges)) { return $user_source;
redirect(user_driver_license_edit_link()); }
}
/**
* Edit a users driver license information.
*/
function user_driver_license_edit_controller() {
global $privileges, $user;
$user_source = user_driver_license_load_user();
// only privilege admin_user can edit other users driver license information
if ($user['UID'] != $user_source['UID'] && ! in_array('admin_user', $privileges)) {
redirect(user_driver_license_edit_link());
} }
$wants_to_drive = false; $wants_to_drive = false;
@ -92,9 +97,6 @@ function user_driver_license_edit_controller() {
$has_license_forklift = false; $has_license_forklift = false;
$user_driver_license = UserDriverLicense($user_source['UID']); $user_driver_license = UserDriverLicense($user_source['UID']);
if ($user_driver_license === false) {
engelsystem_error('Unable to load user driver license.');
}
if ($user_driver_license != null) { if ($user_driver_license != null) {
$wants_to_drive = true; $wants_to_drive = true;
$has_car = $user_driver_license['has_car']; $has_car = $user_driver_license['has_car'];
@ -122,10 +124,7 @@ function user_driver_license_edit_controller() {
if ($valid) { if ($valid) {
if (! $wants_to_drive && $user_driver_license != null) { if (! $wants_to_drive && $user_driver_license != null) {
$result = UserDriverLicenses_delete($user_source['UID']); UserDriverLicenses_delete($user_source['UID']);
if ($result === false) {
engelsystem_error("Unable to remove user driver license information");
}
engelsystem_log("Driver license information removed."); engelsystem_log("Driver license information removed.");
success(_("Your driver license information has been removed.")); success(_("Your driver license information has been removed."));
} else { } else {
@ -135,10 +134,6 @@ function user_driver_license_edit_controller() {
} else { } else {
$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); $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);
} }
if ($result === false) {
engelsystem_error("Unable to save user driver license information.");
}
engelsystem_log("Driver license information updated."); engelsystem_log("Driver license information updated.");
} }
success(_("Your driver license information has been saved.")); success(_("Your driver license information has been saved."));

View File

@ -28,12 +28,17 @@ function UserAngelType_exists($user, $angeltype) {
* @param User $user * @param User $user
*/ */
function User_angeltypes($user) { function User_angeltypes($user) {
return sql_select(" $result = sql_select("
SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`coordinator` SELECT `AngelTypes`.*, `UserAngelTypes`.`confirm_user_id`, `UserAngelTypes`.`coordinator`
FROM `UserAngelTypes` FROM `UserAngelTypes`
JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id` JOIN `AngelTypes` ON `UserAngelTypes`.`angeltype_id` = `AngelTypes`.`id`
WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "' WHERE `UserAngelTypes`.`user_id`='" . sql_escape($user['UID']) . "'
"); ");
if ($result === false) {
engelsystem_error("Unable to load user angeltypes.");
return false;
}
return $result;
} }
/** /**

View File

@ -7,6 +7,7 @@
function UserDriverLicense($user_id) { function UserDriverLicense($user_id) {
$user_driver_license = sql_select("SELECT * FROM `UserDriverLicenses` WHERE `user_id`='" . sql_escape($user_id) . "'"); $user_driver_license = sql_select("SELECT * FROM `UserDriverLicenses` WHERE `user_id`='" . sql_escape($user_id) . "'");
if ($user_driver_license === false) { if ($user_driver_license === false) {
engelsystem_error('Unable to load user driver license.');
return false; return false;
} }
if (count($user_driver_license) > 0) { if (count($user_driver_license) > 0) {
@ -27,7 +28,7 @@ function UserDriverLicense($user_id) {
* @param bool $has_license_forklift * @param bool $has_license_forklift
*/ */
function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) { function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) {
return sql_query(" $result = sql_query("
INSERT INTO `UserDriverLicenses` SET INSERT INTO `UserDriverLicenses` SET
`user_id`=" . sql_escape($user_id) . ", `user_id`=" . sql_escape($user_id) . ",
`has_car`=" . sql_bool($has_car) . ", `has_car`=" . sql_bool($has_car) . ",
@ -36,6 +37,10 @@ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_li
`has_license_7_5t_truck`=" . sql_bool($has_license_7_5t_truck) . ", `has_license_7_5t_truck`=" . sql_bool($has_license_7_5t_truck) . ",
`has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ",
`has_license_forklift`=" . sql_bool($has_license_forklift)); `has_license_forklift`=" . sql_bool($has_license_forklift));
if ($result === false) {
engelsystem_error('Unable to create user driver license');
}
return $result;
} }
/** /**
@ -50,7 +55,7 @@ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_li
* @param bool $has_license_forklift * @param bool $has_license_forklift
*/ */
function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) { function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_license_3_5t_transporter, $has_license_7_5t_truck, $has_license_12_5t_truck, $has_license_forklift) {
return sql_query("UPDATE `UserDriverLicenses` SET $result = sql_query("UPDATE `UserDriverLicenses` SET
`has_car`=" . sql_bool($has_car) . ", `has_car`=" . sql_bool($has_car) . ",
`has_license_car`=" . sql_bool($has_license_car) . ", `has_license_car`=" . sql_bool($has_license_car) . ",
`has_license_3_5t_transporter`=" . sql_bool($has_license_3_5t_transporter) . ", `has_license_3_5t_transporter`=" . sql_bool($has_license_3_5t_transporter) . ",
@ -58,6 +63,10 @@ function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_li
`has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ",
`has_license_forklift`=" . sql_bool($has_license_forklift) . " `has_license_forklift`=" . sql_bool($has_license_forklift) . "
WHERE `user_id`='" . sql_escape($user_id) . "'"); WHERE `user_id`='" . sql_escape($user_id) . "'");
if ($result === false) {
engelsystem_error("Unable to update user driver license information");
}
return $result;
} }
/** /**
@ -66,6 +75,10 @@ function UserDriverLicenses_update($user_id, $has_car, $has_license_car, $has_li
* @param int $user_id * @param int $user_id
*/ */
function UserDriverLicenses_delete($user_id) { function UserDriverLicenses_delete($user_id) {
return sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id)); $result = sql_query("DELETE FROM `UserDriverLicenses` WHERE `user_id`=" . sql_escape($user_id));
if ($result === false) {
engelsystem_error("Unable to remove user driver license information");
}
return $result;
} }
?> ?>