From 4429516a22522243631bec7eea6413d1a8266a56 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 20 Dec 2023 23:31:59 +0100 Subject: [PATCH] Added shift update event to send templated emails --- config/app.php | 2 + includes/controller/shifts_controller.php | 5 +- includes/helper/shift_helper.php | 47 +++++++++++++ includes/mailer/shifts_mailer.php | 80 ---------------------- includes/pages/schedule/ImportSchedule.php | 15 +++- resources/lang/de_DE/additional.po | 3 + resources/lang/de_DE/default.po | 48 ++++++------- resources/lang/en_US/additional.po | 3 + resources/lang/en_US/default.po | 24 +++++++ resources/views/emails/updated-shift.twig | 42 ++++++++++++ 10 files changed, 162 insertions(+), 107 deletions(-) create mode 100644 resources/views/emails/updated-shift.twig diff --git a/config/app.php b/config/app.php index 6449154a..565fddb1 100644 --- a/config/app.php +++ b/config/app.php @@ -83,5 +83,7 @@ return [ \Engelsystem\Events\Listener\Shift::class . '@deletedEntryCreateWorklog', \Engelsystem\Events\Listener\Shift::class . '@deletedEntrySendEmail', ], + + 'shift.updating' => \Engelsystem\Events\Listener\Shift::class . '@updatedShiftSendEmail', ], ]; diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index a32ad3b4..c552f91b 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -160,7 +160,10 @@ function shift_edit_controller() $shift->updatedBy()->associate(auth()->user()); $shift->save(); - mail_shift_change($oldShift, $shift); + event('shift.updating', [ + 'shift' => $shift, + 'oldShift' => $oldShift, + ]); NeededAngelType::whereShiftId($shift_id)->delete(); $needed_angel_types_info = []; diff --git a/includes/helper/shift_helper.php b/includes/helper/shift_helper.php index 0b9f0ef8..fd27f70b 100644 --- a/includes/helper/shift_helper.php +++ b/includes/helper/shift_helper.php @@ -6,8 +6,11 @@ use Carbon\Carbon; use Engelsystem\Helpers\Shifts; use Engelsystem\Mail\EngelsystemMailer; use Engelsystem\Models\Location; +use Engelsystem\Models\Shifts\Shift as ShiftModel; +use Engelsystem\Models\Shifts\ShiftEntry; use Engelsystem\Models\User\User; use Engelsystem\Models\Worklog; +use Illuminate\Database\Eloquent\Collection; use Psr\Log\LoggerInterface; class Shift @@ -85,4 +88,48 @@ class Shift ] ); } + + public function updatedShiftSendEmail( + ShiftModel $shift, + ShiftModel $oldShift + ): void { + // Only send e-mail on relevant changes + if ( + $oldShift->shift_type_id == $shift->shift_type_id + && $oldShift->title == $shift->title + && $oldShift->start == $shift->start + && $oldShift->end == $shift->end + && $oldShift->location_id == $shift->location_id + ) { + return; + } + + $shift->load(['shiftType', 'location']); + $oldShift->load(['shiftType', 'location']); + /** @var ShiftEntry[]|Collection $shiftEntries */ + $shiftEntries = $shift->shiftEntries() + ->with(['angelType', 'user.settings']) + ->get(); + + foreach ($shiftEntries as $shiftEntry) { + $user = $shiftEntry->user; + $angelType = $shiftEntry->angelType; + + if (!$user->settings->email_shiftinfo || $shift->end < Carbon::now()) { + continue; + } + + $this->mailer->sendViewTranslated( + $user, + 'notification.shift.updated', + 'emails/updated-shift', + [ + 'shift' => $shift, + 'oldShift' => $oldShift, + 'angelType' => $angelType, + 'username' => $user->displayName, + ] + ); + } + } } diff --git a/includes/mailer/shifts_mailer.php b/includes/mailer/shifts_mailer.php index 2531c9ac..5de920a3 100644 --- a/includes/mailer/shifts_mailer.php +++ b/includes/mailer/shifts_mailer.php @@ -1,87 +1,7 @@ shiftEntries() - ->with(['user', 'user.settings']) - ->get(); - $old_location = $old_shift->location; - $new_location = $new_shift->location; - - $noticeable_changes = false; - - $message = __('A Shift you are registered on has changed:'); - $message .= "\n"; - - if ($old_shift->shift_type_id != $new_shift->shift_type_id) { - $message .= sprintf( - __('* Shift type changed from %s to %s'), - $old_shift->shiftType->name, - $new_shift->shiftType->name - ) . "\n"; - $noticeable_changes = true; - } - - if ($old_shift->title != $new_shift->title) { - $message .= sprintf(__('* Shift title changed from %s to %s'), $old_shift->title, $new_shift->title) . "\n"; - $noticeable_changes = true; - } - - if ($old_shift->start->timestamp != $new_shift->start->timestamp) { - $message .= sprintf( - __('* Shift Start changed from %s to %s'), - $old_shift->start->format(__('general.datetime')), - $new_shift->start->format(__('general.datetime')) - ) . "\n"; - $noticeable_changes = true; - } - - if ($old_shift->end->timestamp != $new_shift->end->timestamp) { - $message .= sprintf( - __('* Shift End changed from %s to %s'), - $old_shift->end->format(__('general.datetime')), - $new_shift->end->format(__('general.datetime')) - ) . "\n"; - $noticeable_changes = true; - } - - if ($old_shift->location_id != $new_shift->location_id) { - $message .= sprintf(__('* Shift Location changed from %s to %s'), $old_location->name, $new_location->name) . "\n"; - $noticeable_changes = true; - } - - if (!$noticeable_changes) { - // There are no changes worth sending an E-Mail - return; - } - - $message .= "\n"; - $message .= __('The updated Shift:') . "\n"; - - $message .= $new_shift->shiftType->name . "\n"; - $message .= $new_shift->title . "\n"; - $message .= $new_shift->start->format(__('general.datetime')) . ' - ' . $new_shift->end->format(__('H:i')) . "\n"; - $message .= $new_location->name . "\n\n"; - $message .= url('/shifts', ['action' => 'view', 'shift_id' => $new_shift->id]) . "\n"; - - foreach ($shiftEntries as $shiftEntry) { - $user = $shiftEntry->user; - if ($user->settings->email_shiftinfo) { - engelsystem_email_to_user( - $user, - __('Your Shift has changed'), - $message, - true - ); - } - } -} function mail_shift_assign(User $user, Shift $shift) { diff --git a/includes/pages/schedule/ImportSchedule.php b/includes/pages/schedule/ImportSchedule.php index 30beb33a..1eeaa23c 100644 --- a/includes/pages/schedule/ImportSchedule.php +++ b/includes/pages/schedule/ImportSchedule.php @@ -172,7 +172,6 @@ class ImportSchedule extends BaseController '' ); - $this->fireDeleteShiftEntryEvents($event, $schedule); $this->deleteEvent($event, $schedule); } $schedule->delete(); @@ -280,7 +279,6 @@ class ImportSchedule extends BaseController } foreach ($deleteEvents as $event) { - $this->fireDeleteShiftEntryEvents($event, $scheduleUrl); $this->deleteEvent($event, $scheduleUrl); } @@ -372,6 +370,7 @@ class ImportSchedule extends BaseController /** @var ScheduleShift $scheduleShift */ $scheduleShift = ScheduleShift::whereGuid($event->getGuid())->where('schedule_id', $schedule->id)->first(); $shift = $scheduleShift->shift; + $oldShift = Shift::find($shift->id); $shift->title = $event->getTitle(); $shift->shift_type_id = $shiftTypeId; $shift->start = $event->getDate()->copy()->timezone($eventTimeZone); @@ -381,6 +380,8 @@ class ImportSchedule extends BaseController $shift->updatedBy()->associate($user); $shift->save(); + $this->fireUpdateShiftUpdateEvent($oldShift, $shift); + $this->log( 'Updated schedule shift "{shift}" in "{location}" ({from} {to}, {guid})', [ @@ -400,6 +401,8 @@ class ImportSchedule extends BaseController $shift = $scheduleShift->shift; $shift->delete(); + $this->fireDeleteShiftEntryEvents($event, $schedule); + $this->log( 'Deleted schedule shift "{shift}" in {location} ({from} {to}, {guid})', [ @@ -412,6 +415,14 @@ class ImportSchedule extends BaseController ); } + protected function fireUpdateShiftUpdateEvent(Shift $oldShift, Shift $newShift): void + { + event('shift.updating', [ + 'shift' => $newShift, + 'oldShift' => $oldShift, + ]); + } + /** * @param Request $request * @return Event[]|Room[]|Location[] diff --git a/resources/lang/de_DE/additional.po b/resources/lang/de_DE/additional.po index 5e68b2b1..d29a780e 100644 --- a/resources/lang/de_DE/additional.po +++ b/resources/lang/de_DE/additional.po @@ -254,6 +254,9 @@ msgstr "" "Da die gelöschte Schicht bereits vergangen ist, " "haben wir einen entsprechenden Arbeitseinsatz hinzugefügt." +msgid "notification.shift.updated" +msgstr "Deine Schicht wurde aktualisiert" + msgid "notification.shift.no_next_found" msgstr "Es wurde keine verfügbare Schicht gefunden." diff --git a/resources/lang/de_DE/default.po b/resources/lang/de_DE/default.po index 85661719..4fb9b241 100644 --- a/resources/lang/de_DE/default.po +++ b/resources/lang/de_DE/default.po @@ -432,30 +432,6 @@ msgstr "" msgid "%s (%s as %s) in %s, %s - %s" msgstr "%s (%s als %s) in %s, %s - %s" -msgid "A Shift you are registered on has changed:" -msgstr "Eine deiner Schichten hat sich geändert:" - -msgid "* Shift type changed from %s to %s" -msgstr "* Schichttyp von %s in %s geändert" - -msgid "* Shift title changed from %s to %s" -msgstr "* Schicht Titel von %s nach %s geändert" - -msgid "* Shift Start changed from %s to %s" -msgstr "* Schicht Beginn von %s nach %s geändert" - -msgid "* Shift End changed from %s to %s" -msgstr "* Schicht Ende von %s nach %s geändert" - -msgid "* Shift Location changed from %s to %s" -msgstr "* Schicht Ort von %s to %s geändert" - -msgid "The updated Shift:" -msgstr "Die aktualisierte Schicht:" - -msgid "Your Shift has changed" -msgstr "Deine Schicht hat sich geändert" - msgid "You have been assigned to a Shift:" msgstr "Du wurdest in eine Schicht eingetragen:" @@ -2034,3 +2010,27 @@ msgstr "Wenn du dich für Schichten eintragen willst, komm gerne im Himmel vorbe msgid "design.title" msgstr "Design" + +msgid "notification.shift.updated.introduction" +msgstr "Eine deiner Schichten wurde aktualisiert:" + +msgid "notification.shift.updated.type" +msgstr "Schichttyp wurde von %s in %s geändert" + +msgid "notification.shift.updated.title" +msgstr "Schicht Titel wurde von %s in %s geändert" + +msgid "notification.shift.updated.description" +msgstr "Schicht Beschreibung wurde geändert" + +msgid "notification.shift.updated.start" +msgstr "Schicht Start wurde von %s zu %s geändert" + +msgid "notification.shift.updated.end" +msgstr "Schicht Ende wurde von %s zu %s geändert" + +msgid "notification.shift.updated.location" +msgstr "Schicht Ort wurde von %s nach %s verschoben" + +msgid "notification.shift.updated.shift" +msgstr "Die aktualisierte Schicht:" diff --git a/resources/lang/en_US/additional.po b/resources/lang/en_US/additional.po index 7be350ba..c828af52 100644 --- a/resources/lang/en_US/additional.po +++ b/resources/lang/en_US/additional.po @@ -253,6 +253,9 @@ msgstr "" "Since the deleted shift was already done, " "we added a worklog entry instead, to keep your work hours correct." +msgid "notification.shift.updated" +msgstr "Your shift was updated" + msgid "notification.shift.no_next_found" msgstr "There is no available shift." diff --git a/resources/lang/en_US/default.po b/resources/lang/en_US/default.po index 11372266..ed632a5a 100644 --- a/resources/lang/en_US/default.po +++ b/resources/lang/en_US/default.po @@ -896,6 +896,30 @@ msgstr "" msgid "form.recover" msgstr "Recover" +msgid "notification.shift.updated.introduction" +msgstr "Your shift has changed:" + +msgid "notification.shift.updated.type" +msgstr "Shift type changed from %s to %s" + +msgid "notification.shift.updated.title" +msgstr "Shift title changed from %s to %s" + +msgid "notification.shift.updated.description" +msgstr "Shift description changed" + +msgid "notification.shift.updated.start" +msgstr "Shift start changed from %s to %s" + +msgid "notification.shift.updated.end" +msgstr "Shift end changed from %s to %s" + +msgid "notification.shift.updated.location" +msgstr "Shift location moved from %s to %s" + +msgid "notification.shift.updated.shift" +msgstr "The updated Shift:" + msgid "general.actions" msgstr "Actions" diff --git a/resources/views/emails/updated-shift.twig b/resources/views/emails/updated-shift.twig new file mode 100644 index 00000000..4528273e --- /dev/null +++ b/resources/views/emails/updated-shift.twig @@ -0,0 +1,42 @@ +{% extends "emails/mail.twig" %} + +{% block introduction %} +{{ __('notification.shift.updated.introduction') }} +{% endblock %} + +{% block message %} +{%- if oldShift.shift_type_id != shift.shift_type_id -%} +* {{ __('notification.shift.updated.type', [oldShift.shiftType.name, shift.shiftType.name]) }} +{% endif %} +{%- if oldShift.title != shift.title -%} +* {{ __('notification.shift.updated.title', [oldShift.title, shift.title]) }} +{% endif %} +{%- if oldShift.description != shift.description -%} +* {{ __('notification.shift.updated.description', [oldShift.description, shift.description]) }} +{% endif %} +{%- if oldShift.start != shift.start -%} +* {{ __( + 'notification.shift.updated.start', + [oldShift.start.format(__('general.datetime')), shift.start.format(__('general.datetime'))] +) }} +{% endif %} +{%- if oldShift.end != shift.end -%} +* {{ __( + 'notification.shift.updated.end', + [oldShift.end.format(__('general.datetime')), shift.end.format(__('general.datetime'))] +) }} +{% endif %} +{%- if oldShift.location_id != shift.location_id -%} +* {{ __('notification.shift.updated.location', [oldShift.location.name, shift.location.name]) }} +{% endif %} + +{{ __('notification.shift.updated.shift') }} + +{{ shift.shiftType.name }} +{{ shift.title }} +{{ shift.description }} +{{ shift.start.format(__('general.datetime')) }} - {{ shift.end.format(__('H:i')) }} +{{ shift.location.name }} + +{{ url('/shifts', {'action': 'view', 'shift_id': shift.id})|raw }} +{% endblock %}