Use confirmation dialog and redirect back when deleting shifts

This commit is contained in:
Igor Scheller 2024-01-28 14:35:36 +01:00 committed by xuwhite
parent b10264d6ef
commit 4fa5db8a42
5 changed files with 95 additions and 75 deletions

View File

@ -1,5 +1,8 @@
<?php
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Redirector;
use Engelsystem\Models\AngelType;
use Engelsystem\Models\Location;
use Engelsystem\Models\Shifts\NeededAngelType;
@ -8,6 +11,7 @@ use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\Shifts\ShiftSignupStatus;
use Engelsystem\ShiftSignupState;
use Illuminate\Support\Str;
/**
* @param array|Shift $shift
@ -23,15 +27,6 @@ function shift_link($shift)
return url('/shifts', $parameters);
}
/**
* @param Shift $shift
* @return string
*/
function shift_delete_link(Shift $shift)
{
return url('/user-shifts', ['delete_shift' => $shift->id]);
}
/**
* @param Shift $shift
* @return string
@ -231,30 +226,23 @@ function shift_edit_controller()
);
}
/**
* @return string
*/
function shift_delete_controller()
function shift_delete_controller(): void
{
$request = request();
// Only accessible for admins / ShiCos with user_shifts_admin privileg
if (!auth()->can('user_shifts_admin')) {
throw_redirect(url('/user-shifts'));
throw new HttpForbidden();
}
// Schicht komplett löschen (nur für admins/user mit user_shifts_admin privileg)
if (!$request->has('delete_shift') || !preg_match('/^\d+$/', $request->input('delete_shift'))) {
throw_redirect(url('/user-shifts'));
// Must contain shift id and confirmation
if (!$request->has('delete_shift') || !$request->hasPostData('delete')) {
throw new HttpNotFound();
}
$shift_id = $request->input('delete_shift');
$shift = Shift::findOrFail($shift_id);
$shift = Shift($shift_id);
if (empty($shift)) {
throw_redirect(url('/user-shifts'));
}
// Schicht löschen bestätigt
if ($request->hasPostData('delete')) {
foreach ($shift->shiftEntries as $entry) {
event('shift.entry.deleting', [
'user' => $entry->user,
@ -276,25 +264,15 @@ function shift_delete_controller()
. ' to ' . $shift->end->format('Y-m-d H:i')
);
success(__('Shift deleted.'));
/** @var Redirector $redirect */
$redirect = app('redirect');
$old = $redirect->back()->getHeaderLine('location');
if (Str::contains($old, '/shifts') && Str::contains($old, 'action=view')) {
throw_redirect(url('/user-shifts'));
}
$link = button(url('/shifts', ['action' => 'view', 'shift_id' => $shift_id]), icon('chevron-left'), 'btn-sm', '', __('general.back'));
return page_with_title(
$link . ' ' . shifts_title(),
[
error(sprintf(
__('Do you want to delete the shift %s from %s to %s?'),
$shift->shiftType->name,
$shift->start->format(__('general.datetime')),
$shift->end->format(__('H:i'))
), true),
form([
form_hidden('delete_shift', $shift->id),
form_submit('delete', icon('trash') . __('form.delete'), '', true, 'danger'),
]),
]
);
throw_redirect($old);
}
/**

View File

@ -141,11 +141,23 @@ function form_info($label, $text = '')
* @param string $class
* @param bool $wrapForm
* @param string $buttonType
* @param array $dataAttributes
* @return string
*/
function form_submit($name, $label, $class = '', $wrapForm = true, $buttonType = 'primary', $title = '')
{
$button = '<button class="btn btn-' . $buttonType . ($class ? ' ' . $class : '') . '" type="submit" name="' . $name . '" title="' . $title . '">'
function form_submit(
$name,
$label,
$class = '',
$wrapForm = true,
$buttonType = 'primary',
$title = '',
array $dataAttributes = []
) {
$add = '';
foreach ($dataAttributes as $dataType => $dataValue) {
$add .= ' data-' . $dataType . '="'. htmlspecialchars($dataValue) .'"';
}
$button = '<button class="btn btn-' . $buttonType . ($class ? ' ' . $class : '') . '" type="submit" name="' . $name . '" title="' . $title . '"' . $add . '>'
. $label
. '</button>';

View File

@ -245,7 +245,7 @@ class ShiftCalendarShiftRenderer
{
$header_buttons = '';
if (auth()->can('admin_shifts')) {
$header_buttons = '<div class="ms-auto d-print-none">' . table_buttons([
$header_buttons = div('ms-auto d-print-none d-flex', [
button(
url('/user-shifts', ['edit_shift' => $shift->id]),
icon('pencil'),
@ -253,14 +253,26 @@ class ShiftCalendarShiftRenderer
'',
__('form.edit')
),
button(
url('/user-shifts', ['delete_shift' => $shift->id]),
form([
form_hidden('delete_shift', $shift->id),
form_submit(
'delete',
icon('trash'),
'btn-' . $class . ' btn-sm border-light text-white',
'',
__('form.delete')
'btn-' . $class . ' btn-sm border-light text-white ms-1',
false,
'danger',
__('form.delete'),
[
'confirm_submit_title' => __('Do you want to delete the shift "%s" from %s to %s?', [
$shift->shiftType->name,
$shift->start->format(__('general.datetime')),
$shift->end->format(__('H:i'))
]),
'confirm_button_text' => icon('trash') . __('form.delete'),
]
),
]) . '</div>';
], url('/user-shifts', ['delete_shift' => $shift->id])),
]);
}
$shift_heading = $shift->start->format('H:i') . ' &dash; '
. $shift->end->format('H:i') . ' &mdash; '

View File

@ -207,7 +207,25 @@ function Shift_view(
if ($shift_admin || $admin_shifttypes || $admin_locations) {
$buttons = [
$shift_admin ? button(shift_edit_link($shift), icon('pencil'), '', '', __('form.edit')) : '',
$shift_admin ? button(shift_delete_link($shift), icon('trash'), 'btn-danger', '', __('form.delete')) : '',
$shift_admin ? form([
form_hidden('delete_shift', $shift->id),
form_submit(
'delete',
icon('trash'),
'',
false,
'danger',
__('form.delete'),
[
'confirm_submit_title' => __('Do you want to delete the shift "%s" from %s to %s?', [
$shift->shiftType->name,
$shift->start->format(__('general.datetime')),
$shift->end->format(__('H:i'))
]),
'confirm_button_text' => icon('trash') . __('form.delete'),
]
),
], url('/user-shifts', ['delete_shift' => $shift->id])): '',
$admin_shifttypes
? button(url('/admin/shifttypes/' . $shifttype->id), htmlspecialchars($shifttype->name))
: '',

View File

@ -321,8 +321,8 @@ msgstr "Benötigte Engel"
msgid "Shift deleted."
msgstr "Schicht gelöscht."
msgid "Do you want to delete the shift %s from %s to %s?"
msgstr "Möchtest Du die Schicht %s von %s bis %s löschen?"
msgid "Do you want to delete the shift \"%s\" from %s to %s?"
msgstr "Möchtest Du die Schicht \"%s\" von %s bis %s löschen?"
msgid "Shift could not be found."
msgstr "Schicht konnte nicht gefunden werden."