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 <?php
use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Http\Exceptions\HttpNotFound;
use Engelsystem\Http\Redirector;
use Engelsystem\Models\AngelType; use Engelsystem\Models\AngelType;
use Engelsystem\Models\Location; use Engelsystem\Models\Location;
use Engelsystem\Models\Shifts\NeededAngelType; use Engelsystem\Models\Shifts\NeededAngelType;
@ -8,6 +11,7 @@ use Engelsystem\Models\Shifts\Shift;
use Engelsystem\Models\Shifts\ShiftType; use Engelsystem\Models\Shifts\ShiftType;
use Engelsystem\Models\Shifts\ShiftSignupStatus; use Engelsystem\Models\Shifts\ShiftSignupStatus;
use Engelsystem\ShiftSignupState; use Engelsystem\ShiftSignupState;
use Illuminate\Support\Str;
/** /**
* @param array|Shift $shift * @param array|Shift $shift
@ -23,15 +27,6 @@ function shift_link($shift)
return url('/shifts', $parameters); 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 * @param Shift $shift
* @return string * @return string
@ -231,70 +226,53 @@ function shift_edit_controller()
); );
} }
/** function shift_delete_controller(): void
* @return string
*/
function shift_delete_controller()
{ {
$request = request(); $request = request();
// Only accessible for admins / ShiCos with user_shifts_admin privileg
if (!auth()->can('user_shifts_admin')) { 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) // Must contain shift id and confirmation
if (!$request->has('delete_shift') || !preg_match('/^\d+$/', $request->input('delete_shift'))) { if (!$request->has('delete_shift') || !$request->hasPostData('delete')) {
throw_redirect(url('/user-shifts')); throw new HttpNotFound();
} }
$shift_id = $request->input('delete_shift'); $shift_id = $request->input('delete_shift');
$shift = Shift::findOrFail($shift_id);
$shift = Shift($shift_id); foreach ($shift->shiftEntries as $entry) {
if (empty($shift)) { event('shift.entry.deleting', [
throw_redirect(url('/user-shifts')); 'user' => $entry->user,
'start' => $shift->start,
'end' => $shift->end,
'name' => $shift->shiftType->name,
'title' => $shift->title,
'type' => $entry->angelType->name,
'location' => $shift->location,
'freeloaded' => $entry->freeloaded,
]);
} }
// Schicht löschen bestätigt $shift->delete();
if ($request->hasPostData('delete')) {
foreach ($shift->shiftEntries as $entry) {
event('shift.entry.deleting', [
'user' => $entry->user,
'start' => $shift->start,
'end' => $shift->end,
'name' => $shift->shiftType->name,
'title' => $shift->title,
'type' => $entry->angelType->name,
'location' => $shift->location,
'freeloaded' => $entry->freeloaded,
]);
}
$shift->delete(); engelsystem_log(
'Deleted shift ' . $shift->title . ': ' . $shift->shiftType->name
engelsystem_log( . ' from ' . $shift->start->format('Y-m-d H:i')
'Deleted shift ' . $shift->title . ': ' . $shift->shiftType->name . ' to ' . $shift->end->format('Y-m-d H:i')
. ' from ' . $shift->start->format('Y-m-d H:i')
. ' to ' . $shift->end->format('Y-m-d H:i')
);
success(__('Shift deleted.'));
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'),
]),
]
); );
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'));
}
throw_redirect($old);
} }
/** /**

View File

@ -141,11 +141,23 @@ function form_info($label, $text = '')
* @param string $class * @param string $class
* @param bool $wrapForm * @param bool $wrapForm
* @param string $buttonType * @param string $buttonType
* @param array $dataAttributes
* @return string * @return string
*/ */
function form_submit($name, $label, $class = '', $wrapForm = true, $buttonType = 'primary', $title = '') function form_submit(
{ $name,
$button = '<button class="btn btn-' . $buttonType . ($class ? ' ' . $class : '') . '" type="submit" name="' . $name . '" title="' . $title . '">' $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 . $label
. '</button>'; . '</button>';

View File

@ -245,7 +245,7 @@ class ShiftCalendarShiftRenderer
{ {
$header_buttons = ''; $header_buttons = '';
if (auth()->can('admin_shifts')) { 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( button(
url('/user-shifts', ['edit_shift' => $shift->id]), url('/user-shifts', ['edit_shift' => $shift->id]),
icon('pencil'), icon('pencil'),
@ -253,14 +253,26 @@ class ShiftCalendarShiftRenderer
'', '',
__('form.edit') __('form.edit')
), ),
button( form([
url('/user-shifts', ['delete_shift' => $shift->id]), form_hidden('delete_shift', $shift->id),
icon('trash'), form_submit(
'btn-' . $class . ' btn-sm border-light text-white', 'delete',
'', icon('trash'),
__('form.delete') 'btn-' . $class . ' btn-sm border-light text-white ms-1',
), false,
]) . '</div>'; '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])),
]);
} }
$shift_heading = $shift->start->format('H:i') . ' &dash; ' $shift_heading = $shift->start->format('H:i') . ' &dash; '
. $shift->end->format('H:i') . ' &mdash; ' . $shift->end->format('H:i') . ' &mdash; '

View File

@ -207,7 +207,25 @@ function Shift_view(
if ($shift_admin || $admin_shifttypes || $admin_locations) { if ($shift_admin || $admin_shifttypes || $admin_locations) {
$buttons = [ $buttons = [
$shift_admin ? button(shift_edit_link($shift), icon('pencil'), '', '', __('form.edit')) : '', $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 $admin_shifttypes
? button(url('/admin/shifttypes/' . $shifttype->id), htmlspecialchars($shifttype->name)) ? button(url('/admin/shifttypes/' . $shifttype->id), htmlspecialchars($shifttype->name))
: '', : '',

View File

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