engelsystem/includes/controller/shifts_controller.php

383 lines
12 KiB
PHP
Raw Normal View History

<?php
2017-08-28 16:21:10 +02:00
2022-11-09 00:02:30 +01:00
use Engelsystem\Models\AngelType;
2023-01-22 18:43:09 +01:00
use Engelsystem\Models\Shifts\NeededAngelType;
2023-03-25 20:54:06 +01:00
use Engelsystem\Models\Room;
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;
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
return page_link_to('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_delete_link(Shift $shift)
2017-01-02 03:57:23 +01:00
{
2023-01-03 22:19:03 +01:00
return page_link_to('user_shifts', ['delete_shift' => $shift->id]);
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-01-03 22:19:03 +01:00
return page_link_to('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')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('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')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('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
2020-09-06 23:50:36 +02:00
$rooms = [];
2023-02-26 11:27:41 +01:00
foreach (Room::orderBy('name')->get() as $room) {
2020-09-06 23:50:36 +02:00
$rooms[$room->id] = $room->name;
}
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
2022-11-09 00:02:30 +01:00
$needed_angel_types = collect(NeededAngelTypes_by_shift($shift_id))->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;
$rid = $shift->room_id;
$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'))
2020-09-06 23:50:36 +02:00
&& isset($rooms[$request->input('rid')])
) {
$rid = $request->input('rid');
2017-01-02 15:43:36 +01:00
} else {
$valid = false;
2023-01-03 22:19:03 +01:00
error(__('Please select a room.'));
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;
$shift->room_id = $rid;
$shift->start = $start;
$shift->end = $end;
$shift->updatedBy()->associate(auth()->user());
$shift->save();
2023-01-22 18:43:09 +01:00
mail_shift_change($oldShift, $shift);
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),
2022-10-18 19:15:22 +02:00
$needed_angel_types[$angeltype_id]
);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
return page_with_title(
shifts_title(),
[
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),
form_text('title', __('Title'), $title),
2020-09-06 23:50:36 +02:00
form_select('rid', __('Room:'), $rooms, $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', __('Save')),
]),
2017-01-02 15:43:36 +01:00
]
);
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function shift_delete_controller()
{
$request = request();
2017-01-02 15:43:36 +01:00
if (!auth()->can('user_shifts_admin')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_shifts'));
2017-01-02 15:43:36 +01:00
}
// Schicht komplett löschen (nur für admins/user mit user_shifts_admin privileg)
2017-08-30 14:59:27 +02:00
if (!$request->has('delete_shift') || !preg_match('/^\d+$/', $request->input('delete_shift'))) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_shifts'));
2017-01-02 03:57:23 +01:00
}
$shift_id = $request->input('delete_shift');
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$shift = Shift($shift_id);
2018-01-14 17:47:26 +01:00
if (empty($shift)) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_shifts'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
// Schicht löschen bestätigt
if ($request->hasPostData('delete')) {
2023-01-18 13:02:11 +01:00
foreach ($shift->shiftEntries as $entry) {
event('shift.entry.deleting', [
2023-01-18 13:02:11 +01:00
'user' => $entry->user,
2023-01-03 22:19:03 +01:00
'start' => $shift->start,
'end' => $shift->end,
'name' => $shift->shiftType->name,
'title' => $shift->title,
2023-01-18 13:02:11 +01:00
'type' => $entry->angelType->name,
2023-01-03 22:19:03 +01:00
'room' => $shift->room,
2023-01-18 13:02:11 +01:00
'freeloaded' => $entry->freeloaded,
]);
}
2023-01-03 22:19:03 +01:00
$shift->delete();
2017-01-02 15:43:36 +01:00
engelsystem_log(
2023-01-18 13:02:11 +01:00
'Deleted shift ' . $shift->title . ': ' . $shift->shiftType->name
2023-01-03 22:19:03 +01:00
. ' from ' . $shift->start->format('Y-m-d H:i')
. ' to ' . $shift->end->format('Y-m-d H:i')
2017-01-02 15:43:36 +01:00
);
success(__('Shift deleted.'));
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_shifts'));
2017-01-02 15:43:36 +01:00
}
2017-01-02 03:57:23 +01:00
return page_with_title(shifts_title(), [
2017-01-02 15:43:36 +01:00
error(sprintf(
__('Do you want to delete the shift %s from %s to %s?'),
2023-01-03 22:19:03 +01:00
$shift->shiftType->name,
2023-02-04 02:43:47 +01:00
$shift->start->format(__('Y-m-d H:i')),
$shift->end->format(__('H:i'))
2017-01-02 15:43:36 +01:00
), true),
form([
2023-01-03 22:19:03 +01:00
form_hidden('delete_shift', $shift->id),
form_submit('delete', __('delete')),
]),
2017-01-02 15:43:36 +01:00
]);
}
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')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('/'));
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
if (!$request->has('shift_id')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('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.'));
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('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;
$room = $shift->room;
/** @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),
Shift_view($shift, $shifttype, $room, $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')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('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
2022-12-22 00:08:54 +01:00
default => throw_redirect(page_link_to('/')),
};
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')) {
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('/'));
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
2019-09-08 02:25:49 +02:00
throw_redirect(page_link_to('user_shifts'));
2014-12-19 22:59:18 +01:00
}