diff --git a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php index debf0f1be..8eb897f11 100644 --- a/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php +++ b/src/Bundle/ChillTaskBundle/Controller/SingleTaskController.php @@ -23,6 +23,7 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Privacy\PrivacyEvent; use Chill\TaskBundle\Entity\SingleTask; +use Chill\TaskBundle\Event\AssignTaskEvent; use Chill\TaskBundle\Event\TaskEvent; use Chill\TaskBundle\Event\UI\UIEvent; use Chill\TaskBundle\Form\SingleTaskType; @@ -525,6 +526,13 @@ final class SingleTaskController extends AbstractController $this->eventDispatcher->dispatch(new TaskEvent($task), TaskEvent::PERSIST); + if (null !== $task->getAssignee()) { + $this->eventDispatcher->dispatch( + new AssignTaskEvent($task, $user), + AssignTaskEvent::PERSIST + ); + } + $em->flush(); $this->addFlash('success', $this->translator->trans('The task is created')); diff --git a/src/Bundle/ChillTaskBundle/DependencyInjection/ChillTaskExtension.php b/src/Bundle/ChillTaskBundle/DependencyInjection/ChillTaskExtension.php index 0122861d6..8a9f95d14 100644 --- a/src/Bundle/ChillTaskBundle/DependencyInjection/ChillTaskExtension.php +++ b/src/Bundle/ChillTaskBundle/DependencyInjection/ChillTaskExtension.php @@ -42,6 +42,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface $loader->load('services/timeline.yaml'); $loader->load('services/fixtures.yaml'); $loader->load('services/form.yaml'); + $loader->load('services/notification.yaml'); } public function prepend(ContainerBuilder $container) diff --git a/src/Bundle/ChillTaskBundle/Event/AssignTaskEvent.php b/src/Bundle/ChillTaskBundle/Event/AssignTaskEvent.php new file mode 100644 index 000000000..ba582a9ed --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Event/AssignTaskEvent.php @@ -0,0 +1,35 @@ +task; + } + + public function getAssignedUser(): User + { + return $this->assignedUser; + } +} diff --git a/src/Bundle/ChillTaskBundle/Event/TaskAssignEventSubscriber.php b/src/Bundle/ChillTaskBundle/Event/TaskAssignEventSubscriber.php new file mode 100644 index 000000000..701d3277b --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Event/TaskAssignEventSubscriber.php @@ -0,0 +1,63 @@ + ['onTaskAssigned', 0], + ]; + } + + /** + * Send a notification when a user is assigned to a task. + * + * The notification will be sent to the newly assigned user + */ + public function onTaskAssigned(AssignTaskEvent $event): void + { + $task = $event->getTask(); + $assignedUser = $event->getAssignedUser(); + + $title = $task->getTitle(); + + $context = [ + 'task' => $task, + 'assignedUser' => $assignedUser, + 'title' => $title, + ]; + + $notification = new Notification(); + $notification + ->setRelatedEntityId($task->getId()) + ->setRelatedEntityClass(SingleTask::class) + ->setTitle($this->engine->render('@ChillTask/Notification/task_assignment_notification_title.txt.twig', $context)) + ->setMessage($this->engine->render('@ChillTask/Notification/task_assignment_notification_content.txt.twig', $context)) + ->addAddressee($assignedUser) + ->setType(AssignTaskNotificationFlagProvider::FLAG); + + $this->entityManager->persist($notification); + } +} diff --git a/src/Bundle/ChillTaskBundle/Notification/AssignTaskNotificationFlagProvider.php b/src/Bundle/ChillTaskBundle/Notification/AssignTaskNotificationFlagProvider.php new file mode 100644 index 000000000..2be454840 --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Notification/AssignTaskNotificationFlagProvider.php @@ -0,0 +1,21 @@ + $task->getId()]); + return new TranslatableMessage('notification.task.title %title%', ['title' => $task->getTitle()]); } public function getAssociatedPersons(Notification $notification, array $options = []): array @@ -62,7 +62,7 @@ final readonly class TaskNotificationHandler implements NotificationHandlerInter return [$task->getPerson()]; } - public function getRelatedEntity(Notification $notification): ?SingleTask + public function getRelatedEntity(Notification $notification): object { return $this->taskRepository->find($notification->getRelatedEntityId()); } diff --git a/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_content.txt.twig b/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_content.txt.twig new file mode 100644 index 000000000..c338700d6 --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_content.txt.twig @@ -0,0 +1,16 @@ +{{ assignedUser.label }}, + +Une tâche vous a été assignée. + +Titre de la tâche: "{{ task.title }}". +{% if task.endDate %} + +Vous êtes invités à accomplir cette tâche avant le {{ task.endDate|format_date('long') }} +{% endif %} + + +Vous pouvez visualiser la tâche sur cette page: + +{{ absolute_url(path('chill_task_single_task_show', {'id': task.id, '_locale': 'fr'})) }} + +Cordialement, diff --git a/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_title.txt.twig b/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_title.txt.twig new file mode 100644 index 000000000..b837536a1 --- /dev/null +++ b/src/Bundle/ChillTaskBundle/Resources/views/Notification/task_assignment_notification_title.txt.twig @@ -0,0 +1 @@ +Une tâche demande votre attention diff --git a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/List/index_item.html.twig b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/List/index_item.html.twig index 87b9c4086..f9d52be1a 100644 --- a/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/List/index_item.html.twig +++ b/src/Bundle/ChillTaskBundle/Resources/views/SingleTask/List/index_item.html.twig @@ -18,14 +18,14 @@
Some task
-