engelsystem/includes/controller/shifts_controller.php

360 lines
11 KiB
PHP
Raw Permalink Normal View History

<?php
2017-08-28 16:21:10 +02:00
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Redirector;
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-10-15 19:25:55 +02:00
use Engelsystem\Models\Location;
2023-01-22 18:43:09 +01:00
use Engelsystem\Models\Shifts\NeededAngelType;
use Engelsystem\Models\Shifts\ScheduleShift;
2023-01-03 22:19:03 +01:00
use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\Shifts\ShiftSignupStatus;
2016-11-23 22:31:11 +01:00
use Engelsystem\ShiftSignupState;
use Illuminate\Support\Str;
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param array|Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2017-01-02 03:57:23 +01:00
function shift_link($shift)
{
2017-08-30 14:59:27 +02:00
$parameters = ['action' => 'view'];
2023-01-03 22:19:03 +01:00
if (isset($shift['shift_id']) || isset($shift['id'])) {
$parameters['shift_id'] = $shift['shift_id'] ?? $shift['id'];
}
2017-08-30 14:59:27 +02:00
2023-11-13 16:56:52 +01:00
return url('/shifts', $parameters);
2014-12-19 22:41:55 +01:00
}
2017-01-03 03:22:48 +01:00
/**
2023-01-03 22:19:03 +01:00
* @param Shift $shift
2017-01-03 03:22:48 +01:00
* @return string
*/
2023-01-03 22:19:03 +01:00
function shift_edit_link(Shift $shift)
2017-01-02 03:57:23 +01:00
{
2023-11-13 16:56:52 +01:00
return url('/user-shifts', ['edit_shift' => $shift->id]);
2014-12-19 22:41:55 +01:00
}
/**
* Edit a single shift.
2017-01-03 03:22:48 +01:00
*
* @return string
*/
2017-01-02 03:57:23 +01:00
function shift_edit_controller()
{
$valid = true;
$request = request();
2017-01-02 15:43:36 +01:00
if (!auth()->can('admin_shifts')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if (!$request->has('edit_shift') || !test_request_int('edit_shift')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 03:57:23 +01:00
}
$shift_id = $request->input('edit_shift');
2017-01-02 15:43:36 +01:00
2023-01-22 18:43:09 +01:00
$shift = Shift::findOrFail($shift_id);
2023-01-03 22:19:03 +01:00
if (ScheduleShift::whereShiftId($shift->id)->first()) {
warning(__(
'This shift was imported from a schedule so some changes will be overwritten with the next import.'
));
}
2017-01-02 15:43:36 +01:00
2023-10-15 19:25:55 +02:00
$locations = [];
foreach (Location::orderBy('name')->get() as $location) {
$locations[$location->id] = $location->name;
2020-09-06 23:50:36 +02:00
}
2022-11-09 00:02:30 +01:00
$angeltypes = AngelType::all()->pluck('name', 'id')->toArray();
$shifttypes = ShiftType::all()->pluck('name', 'id')->toArray();
2017-01-02 15:43:36 +01:00
$needed_angel_types = collect(NeededAngelTypes_by_shift($shift))->pluck('count', 'angel_type_id')->toArray();
2017-01-02 03:57:23 +01:00
foreach (array_keys($angeltypes) as $angeltype_id) {
2017-01-02 15:43:36 +01:00
if (!isset($needed_angel_types[$angeltype_id])) {
2017-01-02 03:57:23 +01:00
$needed_angel_types[$angeltype_id] = 0;
}
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
$shifttype_id = $shift->shift_type_id;
$title = $shift->title;
$description = $shift->description;
2023-10-15 19:25:55 +02:00
$rid = $shift->location_id;
2023-01-03 22:19:03 +01:00
$start = $shift->start;
$end = $shift->end;
2017-01-02 15:43:36 +01:00
if ($request->hasPostData('submit')) {
2017-01-02 03:57:23 +01:00
// Name/Bezeichnung der Schicht, darf leer sein
2017-01-02 15:43:36 +01:00
$title = strip_request_item('title');
$description = strip_request_item_nl('description');
2017-01-02 15:43:36 +01:00
// Auswahl der sichtbaren Locations für die Schichten
if (
$request->has('rid')
&& preg_match('/^\d+$/', $request->input('rid'))
2023-10-15 19:25:55 +02:00
&& isset($locations[$request->input('rid')])
) {
$rid = $request->input('rid');
2017-01-02 15:43:36 +01:00
} else {
$valid = false;
2023-10-13 11:53:13 +02:00
error(__('Please select a location.'));
2017-01-02 15:43:36 +01:00
}
if ($request->has('shifttype_id') && isset($shifttypes[$request->input('shifttype_id')])) {
$shifttype_id = $request->input('shifttype_id');
2017-01-02 03:57:23 +01:00
} else {
$valid = false;
2023-01-03 22:19:03 +01:00
error(__('Please select a shifttype.'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
if ($request->has('start') && $tmp = DateTime::createFromFormat('Y-m-d H:i', $request->input('start'))) {
2017-01-02 03:57:23 +01:00
$start = $tmp;
} else {
$valid = false;
2023-01-03 22:19:03 +01:00
error(__('Please enter a valid starting time for the shifts.'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
if ($request->has('end') && $tmp = DateTime::createFromFormat('Y-m-d H:i', $request->input('end'))) {
2017-01-02 03:57:23 +01:00
$end = $tmp;
} else {
$valid = false;
2023-01-03 22:19:03 +01:00
error(__('Please enter a valid ending time for the shifts.'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($start >= $end) {
$valid = false;
2023-01-03 22:19:03 +01:00
error(__('The ending time has to be after the starting time.'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
foreach ($needed_angel_types as $needed_angeltype_id => $count) {
$needed_angel_types[$needed_angeltype_id] = 0;
2022-12-28 12:31:31 +01:00
$queryKey = 'angeltype_count_' . $needed_angeltype_id;
if ($request->has($queryKey)) {
if (test_request_int($queryKey)) {
$needed_angel_types[$needed_angeltype_id] = trim($request->input($queryKey));
} else {
$valid = false;
error(sprintf(
__('Please check your input for needed angels of type %s.'),
$angeltypes[$needed_angeltype_id]
2023-01-03 22:19:03 +01:00
));
}
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($valid) {
2023-01-22 18:43:09 +01:00
$oldShift = Shift::find($shift->id);
2023-01-03 22:19:03 +01:00
$shift->shift_type_id = $shifttype_id;
$shift->title = $title;
$shift->description = $description;
2023-10-15 19:25:55 +02:00
$shift->location_id = $rid;
2023-01-03 22:19:03 +01:00
$shift->start = $start;
$shift->end = $end;
$shift->updatedBy()->associate(auth()->user());
$shift->save();
event('shift.updating', [
'shift' => $shift,
'oldShift' => $oldShift,
]);
2023-01-03 22:19:03 +01:00
2023-01-22 18:43:09 +01:00
NeededAngelType::whereShiftId($shift_id)->delete();
2017-01-02 03:57:23 +01:00
$needed_angel_types_info = [];
foreach ($needed_angel_types as $type_id => $count) {
2023-01-22 18:43:09 +01:00
$angeltype = AngelType::find($type_id);
if (!empty($angeltype) && $count > 0) {
$neededAngelType = new NeededAngelType();
$neededAngelType->shift()->associate($shift);
$neededAngelType->angel_type_id = $type_id;
$neededAngelType->count = $count;
$neededAngelType->save();
$needed_angel_types_info[] = $angeltypes[$type_id] . ': ' . $count;
}
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
engelsystem_log(
2017-01-03 14:12:17 +01:00
'Updated shift \'' . $shifttypes[$shifttype_id] . ', ' . $title
2023-01-03 22:19:03 +01:00
. '\' from ' . $start->format('Y-m-d H:i')
. ' to ' . $end->format('Y-m-d H:i')
2017-01-03 14:12:17 +01:00
. ' with angel types ' . join(', ', $needed_angel_types_info)
2021-11-27 11:34:20 +01:00
. ' and description ' . $description
2017-01-02 15:43:36 +01:00
);
success(__('Shift updated.'));
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
throw_redirect(shift_link($shift));
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
$angel_types_spinner = '';
2017-01-02 03:57:23 +01:00
foreach ($angeltypes as $angeltype_id => $angeltype_name) {
2022-10-18 19:15:22 +02:00
$angel_types_spinner .= form_spinner(
2022-12-05 02:03:36 +01:00
'angeltype_count_' . $angeltype_id,
2023-12-04 23:33:07 +01:00
htmlspecialchars($angeltype_name),
$needed_angel_types[$angeltype_id],
[],
2024-03-25 00:03:39 +01:00
(bool) ScheduleShift::whereShiftId($shift->id)->first(),
2022-10-18 19:15:22 +02:00
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$link = button(url('/shifts', ['action' => 'view', 'shift_id' => $shift_id]), icon('chevron-left'), 'btn-sm', '', __('general.back'));
2017-01-02 15:43:36 +01:00
return page_with_title(
$link . ' ' . shifts_title(),
2017-01-02 15:43:36 +01:00
[
msg(),
2017-12-25 23:12:52 +01:00
'<noscript>'
. info(__('This page is much more comfortable with javascript.'), true)
2017-12-25 23:12:52 +01:00
. '</noscript>',
2017-01-02 15:43:36 +01:00
form([
form_select('shifttype_id', __('Shifttype'), $shifttypes, $shifttype_id),
2023-10-01 22:33:58 +02:00
form_text('title', __('title.title'), $title),
2023-10-15 19:25:55 +02:00
form_select('rid', __('Location:'), $locations, $rid),
2023-01-03 22:19:03 +01:00
form_text('start', __('Start:'), $start->format('Y-m-d H:i')),
form_text('end', __('End:'), $end->format('Y-m-d H:i')),
2021-11-27 11:34:20 +01:00
form_textarea('description', __('Additional description'), $description),
2023-12-04 23:33:07 +01:00
form_info(
'',
__('This description is for single shifts, otherwise please use the description in shift type.')
),
'<h2>' . __('Needed angels') . '</h2>',
2017-01-02 15:43:36 +01:00
$angel_types_spinner,
form_submit('submit', icon('save') . __('form.save')),
]),
2017-01-02 15:43:36 +01:00
]
);
}
function shift_delete_controller(): void
2017-01-02 03:57:23 +01:00
{
$request = request();
2017-01-02 15:43:36 +01:00
// Only accessible for admins / ShiCos with user_shifts_admin privileg
if (!auth()->can('user_shifts_admin')) {
throw new HttpForbidden();
2017-01-02 15:43:36 +01:00
}
// Must contain shift id and confirmation
if (!$request->has('delete_shift') || !$request->hasPostData('delete')) {
throw new HttpNotFound();
2017-01-02 03:57:23 +01:00
}
$shift_id = $request->input('delete_shift');
$shift = Shift::findOrFail($shift_id);
2017-01-02 15:43:36 +01:00
event('shift.deleting', ['shift' => $shift]);
2017-01-02 15:43:36 +01:00
$shift->delete();
engelsystem_log(
'Deleted shift ' . $shift->title . ': ' . $shift->shiftType->name
. ' from ' . $shift->start->format('Y-m-d H:i')
. ' to ' . $shift->end->format('Y-m-d H:i')
);
success(__('Shift deleted.'));
2017-01-02 15:43:36 +01:00
/** @var Redirector $redirect */
$redirect = app('redirect');
$old = $redirect->back()->getHeaderLine('location');
if (Str::contains($old, '/shifts') && Str::contains($old, 'action=view')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 15:43:36 +01:00
}
throw_redirect($old);
}
2017-01-03 03:22:48 +01:00
/**
* @return array
*/
2017-01-02 03:57:23 +01:00
function shift_controller()
{
2018-10-10 03:10:28 +02:00
$user = auth()->user();
$request = request();
2017-01-02 15:43:36 +01:00
if (!auth()->can('user_shifts')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if (!$request->has('shift_id')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$shift = Shift($request->input('shift_id'));
2018-01-14 17:47:26 +01:00
if (empty($shift)) {
error(__('Shift could not be found.'));
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
$shifttype = $shift->shiftType;
2023-10-15 19:25:55 +02:00
$location = $shift->location;
2023-01-03 22:19:03 +01:00
/** @var AngelType[] $angeltypes */
2022-11-09 00:02:30 +01:00
$angeltypes = AngelType::all();
2018-10-10 03:10:28 +02:00
$user_shifts = Shifts_by_user($user->id);
2017-01-02 15:43:36 +01:00
$shift_signup_state = new ShiftSignupState(ShiftSignupStatus::OCCUPIED, 0);
2022-11-09 00:02:30 +01:00
foreach ($angeltypes as $angeltype) {
2017-01-02 03:57:23 +01:00
$needed_angeltype = NeededAngeltype_by_Shift_and_Angeltype($shift, $angeltype);
2018-10-09 21:47:31 +02:00
if (empty($needed_angeltype)) {
continue;
}
2018-10-09 21:47:31 +02:00
2023-01-18 13:02:11 +01:00
$shift_entries = $shift->shiftEntries()
->where('angel_type_id', $angeltype->id)
->get();
2022-11-09 00:02:30 +01:00
$needed_angeltype = (new AngelType())->forceFill($needed_angeltype);
2017-01-02 15:43:36 +01:00
$angeltype_signup_state = Shift_signup_allowed(
$user,
$shift,
$angeltype,
null,
$user_shifts,
$needed_angeltype,
$shift_entries
);
$shift_signup_state->combineWith($angeltype_signup_state);
2022-11-09 00:02:30 +01:00
$angeltype->shift_signup_state = $angeltype_signup_state;
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
return [
2023-12-04 23:33:07 +01:00
htmlspecialchars($shift->shiftType->name),
2023-10-15 19:25:55 +02:00
Shift_view($shift, $shifttype, $location, $angeltypes, $shift_signup_state),
2017-01-02 15:43:36 +01:00
];
2014-12-19 22:41:55 +01:00
}
2017-01-03 03:22:48 +01:00
/**
2022-12-22 00:08:54 +01:00
* @return array
2017-01-03 03:22:48 +01:00
*/
2017-01-02 03:57:23 +01:00
function shifts_controller()
{
$request = request();
if (!$request->has('action')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2022-12-22 00:08:54 +01:00
return match ($request->input('action')) {
2023-01-18 13:02:11 +01:00
'view' => shift_controller(),
'next' => shift_next_controller(), // throws redirect
2023-11-13 16:56:52 +01:00
default => throw_redirect(url('/')),
2022-12-22 00:08:54 +01:00
};
2014-12-19 22:41:55 +01:00
}
2014-12-19 22:59:18 +01:00
/**
* Redirects the user to his next shift.
*/
2017-01-02 03:57:23 +01:00
function shift_next_controller()
{
if (!auth()->can('user_shifts')) {
2023-11-13 16:56:52 +01:00
throw_redirect(url('/'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-01-03 22:19:03 +01:00
$upcoming_shifts = ShiftEntries_upcoming_for_user(auth()->user());
2017-01-02 15:43:36 +01:00
2023-01-18 13:02:11 +01:00
if (!$upcoming_shifts->isEmpty()) {
throw_redirect(shift_link($upcoming_shifts[0]->shift));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2023-11-13 16:56:52 +01:00
throw_redirect(url('/user-shifts'));
2014-12-19 22:59:18 +01:00
}