From 7bc4ad97799e1ed5d8be0a50a5a8581feefea885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 30 Dec 2021 01:02:16 +0100 Subject: [PATCH] twig extension for listing notification on a given entity --- .../Notification/NotificationPresence.php | 40 +++++++++++++++++++ .../Templating/NotificationTwigExtension.php | 19 +++++++++ .../NotificationTwigExtensionRuntime.php | 31 ++++++++++++++ .../Repository/NotificationRepository.php | 23 +++++++++++ ...extension_list_notifications_for.html.twig | 17 ++++++++ .../config/services/notification.yaml | 6 +++ .../views/AccompanyingCourse/index.html.twig | 15 +++++-- 7 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Notification/NotificationPresence.php create mode 100644 src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtension.php create mode 100644 src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtensionRuntime.php create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Notification/extension_list_notifications_for.html.twig diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationPresence.php b/src/Bundle/ChillMainBundle/Notification/NotificationPresence.php new file mode 100644 index 000000000..e003a5afc --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/NotificationPresence.php @@ -0,0 +1,40 @@ +security = $security; + $this->notificationRepository = $notificationRepository; + } + + /** + * @return array|Notification[] + */ + public function getNotificationsForClassAndEntity(string $relatedEntityClass, int $relatedEntityId): array + { + if ($this->security->getUser() instanceof User) { + return $this->notificationRepository->findNotificationAsAddresseeByRelatedEntityAndUser( + $relatedEntityClass, + $relatedEntityId, + $this->security->getUser() + ); + } + + return []; + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtension.php b/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtension.php new file mode 100644 index 000000000..529da2bc1 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtension.php @@ -0,0 +1,19 @@ + true, + 'is_safe' => ['html'], + ]), + ]; + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtensionRuntime.php b/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtensionRuntime.php new file mode 100644 index 000000000..b3ef0f84e --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/Templating/NotificationTwigExtensionRuntime.php @@ -0,0 +1,31 @@ +notificationPresence = $notificationPresence; + } + + public function listNotificationsFor(Environment $environment, string $relatedEntityClass, int $relatedEntityId, array $options = []): string + { + $notifications = $this->notificationPresence->getNotificationsForClassAndEntity($relatedEntityClass, $relatedEntityId); + + if ([] === $notifications) { + return ''; + } + + return $environment->render('@ChillMain/Notification/extension_list_notifications_for.html.twig', [ + 'notifications' => $notifications, + ]); + } + +} diff --git a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php index eea2d41e1..755b5f2dd 100644 --- a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php @@ -133,6 +133,29 @@ final class NotificationRepository implements ObjectRepository return $query->getQuery()->getResult(); } + /** + * @param string $relatedEntityClass + * @param int $relatedEntityId + * @param User $user + * @return array|Notification[] + */ + public function findNotificationAsAddresseeByRelatedEntityAndUser(string $relatedEntityClass, int $relatedEntityId, User $user): array + { + $qb = $this->repository->createQueryBuilder('n'); + + $qb + ->select('n') + ->where($qb->expr()->eq('n.relatedEntityClass', ':relatedEntityClass')) + ->andWhere($qb->expr()->eq('n.relatedEntityId', ':relatedEntityId')) + ->andWhere($qb->expr()->isMemberOf(':user', 'n.addressees')) + ->setParameter('relatedEntityClass', $relatedEntityClass) + ->setParameter('relatedEntityId', $relatedEntityId) + ->setParameter('user', $user) + ; + + return $qb->getQuery()->getResult(); + } + /** * @param mixed|null $limit * @param mixed|null $offset diff --git a/src/Bundle/ChillMainBundle/Resources/views/Notification/extension_list_notifications_for.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Notification/extension_list_notifications_for.html.twig new file mode 100644 index 000000000..db6aa5d13 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Notification/extension_list_notifications_for.html.twig @@ -0,0 +1,17 @@ +
+ {% for notification in notifications %} +
+
{{ 'notification.you were notified by %sender%'|trans({'%sender%': notification.sender|chill_entity_render_string }) }}
+
+
    +
  • + +
  • +
  • + +
  • +
+
+
+ {% endfor %} +
diff --git a/src/Bundle/ChillMainBundle/config/services/notification.yaml b/src/Bundle/ChillMainBundle/config/services/notification.yaml index 5d4390877..4ec90de41 100644 --- a/src/Bundle/ChillMainBundle/config/services/notification.yaml +++ b/src/Bundle/ChillMainBundle/config/services/notification.yaml @@ -16,3 +16,9 @@ services: Chill\MainBundle\Notification\NotificationHandlerManager: arguments: $handlers: !tagged_iterator chill_main.notification_handler + + Chill\MainBundle\Notification\NotificationPresence: ~ + + Chill\MainBundle\Notification\Templating\NotificationTwigExtension: ~ + + Chill\MainBundle\Notification\Templating\NotificationTwigExtensionRuntime: ~ diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig index 6b143469b..821b80b12 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig @@ -13,14 +13,23 @@ {% endmacro %} {% block js %} -{{ parent() }} -{{ encore_entry_script_tags('page_accompanying_course_index_person_locate') }} -{{ encore_entry_script_tags('page_accompanying_course_index_masonry') }} + {{ parent() }} + {{ encore_entry_script_tags('page_accompanying_course_index_person_locate') }} + {{ encore_entry_script_tags('page_accompanying_course_index_masonry') }} + {{ encore_entry_script_tags('mod_notification_toggle_read_status') }} +{% endblock %} + +{% block css %} + {{ parent() }} + {{ encore_entry_link_tags('mod_notification_toggle_read_status') }} + {% endblock %} {% block content %}
+ {{ chill_list_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', accompanyingCourse.id) }} +
{% if 'DRAFT' == accompanyingCourse.step %}