reduce complexity of user driver license

This commit is contained in:
msquare 2016-11-17 14:35:04 +01:00
parent b5784e5961
commit 9c4636f5c4
5 changed files with 115 additions and 94 deletions

View File

@ -33,11 +33,9 @@ function user_driver_licenses_controller() {
redirect(page_link_to('')); redirect(page_link_to(''));
} }
if (! isset($_REQUEST['action'])) { $action = strip_request_item('action', 'edit');
$_REQUEST['action'] = 'edit';
}
switch ($_REQUEST['action']) { switch ($action) {
default: default:
case 'edit': case 'edit':
return user_driver_license_edit_controller(); return user_driver_license_edit_controller();
@ -87,64 +85,48 @@ function user_driver_license_edit_controller() {
redirect(user_driver_license_edit_link()); redirect(user_driver_license_edit_link());
} }
$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']); $user_driver_license = UserDriverLicense($user_source['UID']);
if ($user_driver_license != null) { if ($user_driver_license == null) {
$wants_to_drive = false;
$user_driver_license = UserDriverLicense_new($user_source);
} else {
$wants_to_drive = true; $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'])) { if (isset($_REQUEST['submit'])) {
$valid = true; $valid = true;
$wants_to_drive = isset($_REQUEST['wants_to_drive']); $wants_to_drive = isset($_REQUEST['wants_to_drive']);
$has_car = isset($_REQUEST['has_car']); if ($wants_to_drive) {
$has_license_car = isset($_REQUEST['has_license_car']); $user_driver_license['has_car'] = isset($_REQUEST['has_car']);
$has_license_3_5t_transporter = isset($_REQUEST['has_license_3_5t_transporter']); $user_driver_license['has_license_car'] = isset($_REQUEST['has_license_car']);
$has_license_7_5t_truck = isset($_REQUEST['has_license_7_5t_truck']); $user_driver_license['has_license_3_5t_transporter'] = isset($_REQUEST['has_license_3_5t_transporter']);
$has_license_12_5t_truck = isset($_REQUEST['has_license_12_5t_truck']); $user_driver_license['has_license_7_5t_truck'] = isset($_REQUEST['has_license_7_5t_truck']);
$has_license_forklift = isset($_REQUEST['has_license_forklift']); $user_driver_license['has_license_12_5t_truck'] = isset($_REQUEST['has_license_12_5t_truck']);
$user_driver_license['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) { if (UserDriverLicense_valid($user_driver_license)) {
$valid = false; if ($user_driver_license == null) {
UserDriverLicenses_create($user_driver_license);
} 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));
} else {
error(_("Please select at least one driving license.")); error(_("Please select at least one driving license."));
} }
} elseif ($user_driver_license['id'] != null) {
if ($valid) {
if (! $wants_to_drive && $user_driver_license != null) {
UserDriverLicenses_delete($user_source['UID']); UserDriverLicenses_delete($user_source['UID']);
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 {
if ($wants_to_drive) {
if ($user_driver_license == null) {
$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);
} 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);
}
engelsystem_log("Driver license information updated.");
}
success(_("Your driver license information has been saved."));
}
redirect(user_link($user_source)); redirect(user_link($user_source));
} }
} }
return [ return [
sprintf(_("Edit %s driving license information"), $user_source['Nick']), 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) UserDriverLicense_edit_view($user_source, $wants_to_drive, $user_driver_license)
]; ];
} }

View File

@ -1,5 +1,21 @@
<?php <?php
/**
* Returns an array with the attributes of shift entries.
* FIXME! Needs entity object.
*/
function ShiftEntry_new() {
return [
'id' => null,
'SID' => null,
'TID' => null,
'UID' => null,
'Comment' => null,
'freeloaded_comment' => null,
'freeloaded' => false
];
}
/** /**
* Counts all freeloaded shifts. * Counts all freeloaded shifts.
*/ */

View File

@ -1,8 +1,37 @@
<?php <?php
/**
* Returns a new empty UserDriverLicense
* FIXME entity object needed
*/
function UserDriverLicense_new($user) {
return [
'user_id' => $user['UID'],
'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
];
}
/**
* Is it valid?
*
* @param UserDriverLicense $user_driver_license
* The UserDriverLicense to check
* @return boolean
*/
function UserDriverLicense_valid($user_driver_license) {
return $user_driver_license['has_car'] || $user_driver_license['has_license_car'] || $user_driver_license['has_license_3_5t_transporter'] || $user_driver_license['has_license_7_5t_truck'] || $user_driver_license['has_license_12_5t_truck'] || $user_driver_license['has_license_forklift'];
}
/** /**
* Get a users driver license information * Get a users driver license information
* @param int $user_id The users id *
* @param int $user_id
* The users id
*/ */
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) . "'");
@ -19,24 +48,19 @@ function UserDriverLicense($user_id) {
/** /**
* Create a user's driver license entry * Create a user's driver license entry
* *
* @param bool $user_id * @param UserDriverLicense $user_driver_license
* @param bool $has_car * The UserDriverLicense to create
* @param bool $has_license_car
* @param bool $has_license_3_5t_transporter
* @param bool $has_license_7_5t_truck
* @param bool $has_license_12_5t_truck
* @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_driver_license) {
$result = sql_query(" $result = sql_query("
INSERT INTO `UserDriverLicenses` SET INSERT INTO `UserDriverLicenses` SET
`user_id`=" . sql_escape($user_id) . ", `user_id`=" . sql_escape($user_driver_license['user_id']) . ",
`has_car`=" . sql_bool($has_car) . ", `has_car`=" . sql_bool($user_driver_license['has_car']) . ",
`has_license_car`=" . sql_bool($has_license_car) . ", `has_license_car`=" . sql_bool($user_driver_license['has_license_car']) . ",
`has_license_3_5t_transporter`=" . sql_bool($has_license_3_5t_transporter) . ", `has_license_3_5t_transporter`=" . sql_bool($user_driver_license['has_license_3_5t_transporter']) . ",
`has_license_7_5t_truck`=" . sql_bool($has_license_7_5t_truck) . ", `has_license_7_5t_truck`=" . sql_bool($user_driver_license['has_license_7_5t_truck']) . ",
`has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_12_5t_truck`=" . sql_bool($user_driver_license['has_license_12_5t_truck']) . ",
`has_license_forklift`=" . sql_bool($has_license_forklift)); `has_license_forklift`=" . sql_bool($user_driver_license['has_license_forklift']));
if ($result === false) { if ($result === false) {
engelsystem_error('Unable to create user driver license'); engelsystem_error('Unable to create user driver license');
} }
@ -46,23 +70,18 @@ function UserDriverLicenses_create($user_id, $has_car, $has_license_car, $has_li
/** /**
* Update a user's driver license entry * Update a user's driver license entry
* *
* @param bool $user_id * @param UserDriverLicense $user_driver_license
* @param bool $has_car * The UserDriverLicense to update
* @param bool $has_license_car
* @param bool $has_license_3_5t_transporter
* @param bool $has_license_7_5t_truck
* @param bool $has_license_12_5t_truck
* @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_driver_license) {
$result = sql_query("UPDATE `UserDriverLicenses` SET $result = sql_query("UPDATE `UserDriverLicenses` SET
`has_car`=" . sql_bool($has_car) . ", `has_car`=" . sql_bool($user_driver_license['has_car']) . ",
`has_license_car`=" . sql_bool($has_license_car) . ", `has_license_car`=" . sql_bool($user_driver_license['has_license_car']) . ",
`has_license_3_5t_transporter`=" . sql_bool($has_license_3_5t_transporter) . ", `has_license_3_5t_transporter`=" . sql_bool($user_driver_license['has_license_3_5t_transporter']) . ",
`has_license_7_5t_truck`=" . sql_bool($has_license_7_5t_truck) . ", `has_license_7_5t_truck`=" . sql_bool($user_driver_license['has_license_7_5t_truck']) . ",
`has_license_12_5t_truck`=" . sql_bool($has_license_12_5t_truck) . ", `has_license_12_5t_truck`=" . sql_bool($user_driver_license['has_license_12_5t_truck']) . ",
`has_license_forklift`=" . sql_bool($has_license_forklift) . " `has_license_forklift`=" . sql_bool($user_driver_license['has_license_forklift']) . "
WHERE `user_id`='" . sql_escape($user_id) . "'"); WHERE `user_id`='" . sql_escape($user_driver_license['user_id']) . "'");
if ($result === false) { if ($result === false) {
engelsystem_error("Unable to update user driver license information"); engelsystem_error("Unable to update user driver license information");
} }

View File

@ -8,6 +8,10 @@ function shifts_title() {
/** /**
* Start different controllers for deleting shifts and shift_entries, edit shifts and add shift entries. * Start different controllers for deleting shifts and shift_entries, edit shifts and add shift entries.
* FIXME:
* Transform into shift controller and shift entry controller.
* Split actions into shift edit, shift delete, shift entry edit, shift entry delete
* Introduce simpler and beautiful actions for shift entry join/leave for users
*/ */
function user_shifts() { function user_shifts() {
global $user; global $user;

View File

@ -2,16 +2,16 @@
/** /**
* Edit a user's driving license information. * Edit a user's driving license information.
*
* @param User $user_source * @param User $user_source
* The user
* @param bool $wants_to_drive * @param bool $wants_to_drive
* @param bool $has_car * true, if the user wants to drive
* @param bool $has_license_car * @param UserDriverLicense $user_driver_license
* @param bool $has_license_3_5t_transporter * The user driver license
* @param bool $has_license_7_5t_truck * @return string
* @param bool $has_license_12_5t_truck
* @param bool $has_license_forklift
*/ */
function 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) { function UserDriverLicense_edit_view($user_source, $wants_to_drive, $user_driver_license) {
return page_with_title(sprintf(_("Edit %s driving license information"), User_Nick_render($user_source)), [ return page_with_title(sprintf(_("Edit %s driving license information"), User_Nick_render($user_source)), [
buttons([ buttons([
button(user_link($user_source), _("Back to profile"), 'back') button(user_link($user_source), _("Back to profile"), 'back')
@ -22,17 +22,17 @@ function UserDriverLicense_edit_view($user_source, $wants_to_drive, $has_car, $h
form_checkbox('wants_to_drive', _("I am willing to operate cars for the PL"), $wants_to_drive), form_checkbox('wants_to_drive', _("I am willing to operate cars for the PL"), $wants_to_drive),
div('panel panel-default', [ div('panel panel-default', [
div('panel-body', [ div('panel-body', [
form_checkbox('has_car', _("I have my own car with me and am willing to use it for the PL (You'll get reimbursed for fuel)"), $has_car), form_checkbox('has_car', _("I have my own car with me and am willing to use it for the PL (You'll get reimbursed for fuel)"), $user_driver_license['has_car']),
heading(_("Driver license"), 3), heading(_("Driver license"), 3),
form_checkbox('has_license_car', _("Car"), $has_license_car), form_checkbox('has_license_car', _("Car"), $user_driver_license['has_license_car']),
form_checkbox('has_license_3_5t_transporter', _("Transporter 3,5t"), $has_license_3_5t_transporter), form_checkbox('has_license_3_5t_transporter', _("Transporter 3,5t"), $user_driver_license['has_license_3_5t_transporter']),
form_checkbox('has_license_7_5t_truck', _("Truck 7,5t"), $has_license_7_5t_truck), form_checkbox('has_license_7_5t_truck', _("Truck 7,5t"), $user_driver_license['has_license_7_5t_truck']),
form_checkbox('has_license_12_5t_truck', _("Truck 12,5t"), $has_license_12_5t_truck), form_checkbox('has_license_12_5t_truck', _("Truck 12,5t"), $user_driver_license['has_license_12_5t_truck']),
form_checkbox('has_license_forklift', _("Forklift"), $has_license_forklift) form_checkbox('has_license_forklift', _("Forklift"), $user_driver_license['has_license_forklift'])
]) ])
], 'driving_license'), ], 'driving_license'),
form_submit('submit', _("Save")) form_submit('submit', _("Save"))
]) , ]),
'<script type="text/javascript"> '<script type="text/javascript">
$(function() { $(function() {
if($("#wants_to_drive").is(":checked")) if($("#wants_to_drive").is(":checked"))