mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Notification: send an email when a notification is created
This commit is contained in:
parent
4ef024516c
commit
6f561f57a6
@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Notification\Email;
|
||||||
|
|
||||||
|
use Chill\MainBundle\Entity\Notification;
|
||||||
|
use Chill\MainBundle\Entity\NotificationComment;
|
||||||
|
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||||
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||||
|
use Symfony\Component\Mailer\MailerInterface;
|
||||||
|
use Symfony\Component\Mime\Email;
|
||||||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
class NotificationMailer
|
||||||
|
{
|
||||||
|
private LoggerInterface $logger;
|
||||||
|
|
||||||
|
private MailerInterface $mailer;
|
||||||
|
|
||||||
|
private TranslatorInterface $translator;
|
||||||
|
|
||||||
|
public function __construct(MailerInterface $mailer, LoggerInterface $logger, TranslatorInterface $translator)
|
||||||
|
{
|
||||||
|
$this->mailer = $mailer;
|
||||||
|
$this->logger = $logger;
|
||||||
|
$this->translator = $translator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function postPersistComment(NotificationComment $comment, LifecycleEventArgs $eventArgs): void
|
||||||
|
{
|
||||||
|
foreach (
|
||||||
|
array_merge(
|
||||||
|
$comment->getNotification()->getAddressees()->toArray(),
|
||||||
|
[$comment->getNotification()->getSender()]
|
||||||
|
) as $dest
|
||||||
|
) {
|
||||||
|
if (null === $dest->getEmail() || $comment->getCreatedBy() !== $dest) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$email = new TemplatedEmail();
|
||||||
|
$email
|
||||||
|
->to($dest->getEmail())
|
||||||
|
->subject('Re: [Chill] ' . $comment->getNotification()->getTitle())
|
||||||
|
->textTemplate('@ChillMain/Notification/email_notification_comment_persist.fr.md.twig')
|
||||||
|
->context([
|
||||||
|
'comment' => $comment,
|
||||||
|
'dest' => $dest,
|
||||||
|
]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->mailer->send($email);
|
||||||
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->warning('[NotificationMailer] could not send an email notification about comment', [
|
||||||
|
'to' => $dest->getEmail(),
|
||||||
|
'error_message' => $e->getMessage(),
|
||||||
|
'error_trace' => $e->getTraceAsString(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a email after a notification is persisted.
|
||||||
|
*/
|
||||||
|
public function postPersistNotification(Notification $notification, LifecycleEventArgs $eventArgs): void
|
||||||
|
{
|
||||||
|
foreach ($notification->getAddressees() as $addressee) {
|
||||||
|
if (null === $addressee->getEmail()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($notification->isSystem()) {
|
||||||
|
$email = new Email();
|
||||||
|
$email
|
||||||
|
->text($notification->getMessage())
|
||||||
|
->subject('[Chill] ' . $notification->getTitle());
|
||||||
|
} else {
|
||||||
|
$email = new TemplatedEmail();
|
||||||
|
$email
|
||||||
|
->textTemplate('@ChillMain/Notification/email_non_system_notification_content.fr.md.twig')
|
||||||
|
->context([
|
||||||
|
'notification' => $notification,
|
||||||
|
'dest' => $addressee,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$email->to($addressee->getEmail());
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->mailer->send($email);
|
||||||
|
} catch (TransportExceptionInterface $e) {
|
||||||
|
$this->logger->warning('[NotificationMailer] could not send an email notification', [
|
||||||
|
'to' => $addressee->getEmail(),
|
||||||
|
'error_message' => $e->getMessage(),
|
||||||
|
'error_trace' => $e->getTraceAsString(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
{{ dest.label }},
|
||||||
|
|
||||||
|
{{ notification.sender.label }} a créé une notification pour vous:
|
||||||
|
|
||||||
|
> {{ notification.title }}
|
||||||
|
>
|
||||||
|
>
|
||||||
|
{%- for line in notification.message|split("\n") %}
|
||||||
|
> {{ line }}
|
||||||
|
{%- if not loop.last %}
|
||||||
|
>
|
||||||
|
{%- endif %}
|
||||||
|
{%- endfor %}
|
||||||
|
|
||||||
|
Vous pouvez visualiser la notification et y répondre ici:
|
||||||
|
|
||||||
|
{{ absolute_url(path('chill_main_notification_show', {'_locale': 'fr', 'id': notification.id }, false)) }}
|
||||||
|
|
||||||
|
--
|
||||||
|
Le logiciel Chill
|
@ -0,0 +1,19 @@
|
|||||||
|
{{ dest.label }},
|
||||||
|
|
||||||
|
{{ comment.createdBy.label }} a créé un commentaire sur la notification "{{ comment.notification.title }}".
|
||||||
|
|
||||||
|
Commentaire:
|
||||||
|
|
||||||
|
{% for line in comment.content|split("\n") %}
|
||||||
|
> {{ line }}
|
||||||
|
{%- if not loop.last %}
|
||||||
|
>
|
||||||
|
{%- endif %}
|
||||||
|
{%- endfor %}
|
||||||
|
|
||||||
|
Vous pouvez visualiser la notification et y répondre ici:
|
||||||
|
|
||||||
|
{{ absolute_url(path('chill_main_notification_show', {'_locale': 'fr', 'id': comment.notification.id }, false)) }}
|
||||||
|
|
||||||
|
--
|
||||||
|
Le logiciel Chill
|
@ -49,3 +49,22 @@ services:
|
|||||||
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
|
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
|
||||||
lazy: true
|
lazy: true
|
||||||
method: 'onEditNotificationComment'
|
method: 'onEditNotificationComment'
|
||||||
|
|
||||||
|
Chill\MainBundle\Notification\Email\NotificationMailer:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
tags:
|
||||||
|
-
|
||||||
|
name: 'doctrine.orm.entity_listener'
|
||||||
|
event: 'postPersist'
|
||||||
|
entity: 'Chill\MainBundle\Entity\Notification'
|
||||||
|
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
|
||||||
|
lazy: true
|
||||||
|
method: 'postPersistNotification'
|
||||||
|
-
|
||||||
|
name: 'doctrine.orm.entity_listener'
|
||||||
|
event: 'postPersist'
|
||||||
|
entity: 'Chill\MainBundle\Entity\NotificationComment'
|
||||||
|
# set the 'lazy' option to TRUE to only instantiate listeners when they are used
|
||||||
|
lazy: true
|
||||||
|
method: 'postPersistComment'
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
{{ 'period_notification.You are designated to a new period.'|trans }}
|
{{ 'period_notification.You are designated to a new period.'|trans }}
|
||||||
|
|
||||||
{{ 'period_notification.See it online'|trans }}: {{ path('chill_person_accompanying_course_index', {'accompanying_period_id': accompanyingCourse.id}, true) }}
|
{{ 'period_notification.See it online'|trans }}:
|
||||||
|
|
||||||
|
{{ absolute_url(path('chill_person_accompanying_course_index', {'accompanying_period_id': accompanyingCourse.id}, false)) }}
|
||||||
|
|
||||||
{{ 'period_notification.Persons are'|trans }}:
|
{{ 'period_notification.Persons are'|trans }}:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user