From 282db51f06b6136c227fb7297f38c89c2880a6a6 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 18 Jun 2021 18:05:02 +0200 Subject: [PATCH] Display notification using services (draft) --- .../ChillActivityExtension.php | 1 + .../ActivityNotificationRenderer.php | 24 ++++++++++ .../Activity/showInNotification.html.twig | 4 ++ .../config/services/notification.yaml | 3 ++ .../Controller/NotificationController.php | 42 ++++++++++++++++++ .../Notification/NotificationRenderer.php | 44 +++++++++++++++++++ .../views/Notification/show.html.twig | 42 ++++++++++++++++++ src/Bundle/ChillMainBundle/config/routes.yaml | 4 ++ .../config/routes/notification.yaml | 3 ++ .../config/services/controller.yaml | 5 +++ .../config/services/notification.yaml | 6 ++- .../ChillPersonExtension.php | 1 + ...AccompanyingPeriodNotificationRenderer.php | 24 ++++++++++ .../showInNotification.html.twig | 3 ++ .../config/services/notification.yaml | 3 ++ 15 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillActivityBundle/Notification/ActivityNotificationRenderer.php create mode 100644 src/Bundle/ChillActivityBundle/Resources/views/Activity/showInNotification.html.twig create mode 100644 src/Bundle/ChillActivityBundle/config/services/notification.yaml create mode 100644 src/Bundle/ChillMainBundle/Controller/NotificationController.php create mode 100644 src/Bundle/ChillMainBundle/Notification/NotificationRenderer.php create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Notification/show.html.twig create mode 100644 src/Bundle/ChillMainBundle/config/routes/notification.yaml create mode 100644 src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationRenderer.php create mode 100644 src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/showInNotification.html.twig create mode 100644 src/Bundle/ChillPersonBundle/config/services/notification.yaml diff --git a/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php b/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php index 61354c7c8..e240bf17c 100644 --- a/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php +++ b/src/Bundle/ChillActivityBundle/DependencyInjection/ChillActivityExtension.php @@ -55,6 +55,7 @@ class ChillActivityExtension extends Extension implements PrependExtensionInterf $loader->load('services/controller.yaml'); $loader->load('services/form.yaml'); $loader->load('services/templating.yaml'); + $loader->load('services/notification.yaml'); } public function prepend(ContainerBuilder $container) diff --git a/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationRenderer.php b/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationRenderer.php new file mode 100644 index 000000000..f1134d421 --- /dev/null +++ b/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationRenderer.php @@ -0,0 +1,24 @@ +getRelatedEntityClass() == Activity::class; + } + + public function getTemplate() + { + return 'ChillActivityBundle:Activity:showInNotification.html.twig'; + } + + public function getTemplateData(Notification $notification) + { + return ['notification' => $notification]; + } +} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/showInNotification.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/showInNotification.html.twig new file mode 100644 index 000000000..5128e9a64 --- /dev/null +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/showInNotification.html.twig @@ -0,0 +1,4 @@ + +{{ dump(notification) }} + +Go to Activity diff --git a/src/Bundle/ChillActivityBundle/config/services/notification.yaml b/src/Bundle/ChillActivityBundle/config/services/notification.yaml new file mode 100644 index 000000000..e3667550c --- /dev/null +++ b/src/Bundle/ChillActivityBundle/config/services/notification.yaml @@ -0,0 +1,3 @@ +services: + Chill\ActivityBundle\Notification\ActivityNotificationRenderer: + autowire: true diff --git a/src/Bundle/ChillMainBundle/Controller/NotificationController.php b/src/Bundle/ChillMainBundle/Controller/NotificationController.php new file mode 100644 index 000000000..83b77d7b6 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Controller/NotificationController.php @@ -0,0 +1,42 @@ +security = $security; + } + + public function showAction(NotificationRepository $notificationRepository, NotificationRenderer $notificationRenderer) + { + $currentUser = $this->security->getUser(); + + $notifications = $notificationRepository->findAllForAttendee($currentUser); + + $templateData = array(); + foreach ($notifications as $notification) { + $data = [ + 'template' => $notificationRenderer->getTemplate($notification), + 'template_data' => $notificationRenderer->getTemplateData($notification), + 'notification' => $notification + ]; + $templateData[] = $data; + } + + return $this->render('ChillMainBundle:Notification:show.html.twig', [ + 'datas' => $templateData, + 'notifications' => $notifications, + ]); + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationRenderer.php b/src/Bundle/ChillMainBundle/Notification/NotificationRenderer.php new file mode 100644 index 000000000..d6f383b73 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/NotificationRenderer.php @@ -0,0 +1,44 @@ +renderers[] = $accompanyingPeriodNotificationRenderer; + $this->renderers[] = $activityNotificationRenderer; + } + + private function getRenderer(Notification $notification) + { + foreach ($this->renderers as $renderer) { + if($renderer->supports($notification)) { + return $renderer; + } + } + + throw new \Exception('No renderer for '. $notification); + } + + public function getTemplate(Notification $notification) + { + return $this->getRenderer($notification)->getTemplate(); + } + + public function getTemplateData(Notification $notification) + { + return $this->getRenderer($notification)->getTemplateData($notification); + } +} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Notification/show.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Notification/show.html.twig new file mode 100644 index 000000000..34aeee049 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Notification/show.html.twig @@ -0,0 +1,42 @@ +{% extends "@ChillMain/layout.html.twig" %} + +{% block content %} +
+
+

{{ "Notifications list" | trans }}

+ + + {%for data in datas %} + {% set notification = data.notification %} + +
+
{{ 'Message'|trans }}
+
{{ notification.message }}
+
+ +
+
{{ 'Date'|trans }}
+
{{ notification.date | date('long') }}
+
+ + +
+
{{ 'Sender'|trans }}
+
{{ notification.sender }}
+
+ +
+
{{ 'Addressees'|trans }}
+
{{ notification.addressees |join(', ') }}
+
+ +
+
{{ 'Entity'|trans }}
+
+ {% include data.template with data.template_data %} +
+
+ {% endfor %} +
+
+{% endblock content %} diff --git a/src/Bundle/ChillMainBundle/config/routes.yaml b/src/Bundle/ChillMainBundle/config/routes.yaml index 240a44590..697ec8ec0 100644 --- a/src/Bundle/ChillMainBundle/config/routes.yaml +++ b/src/Bundle/ChillMainBundle/config/routes.yaml @@ -34,6 +34,10 @@ chill_password_recover: resource: "@ChillMainBundle/config/routes/password_recover.yaml" prefix: "public/{_locale}/password" +chill_main_notification: + resource: "@ChillMainBundle/config/routes/notification.yaml" + prefix: "{_locale}/notification" + chill_crud: resource: "@ChillMainBundle" type: CRUD diff --git a/src/Bundle/ChillMainBundle/config/routes/notification.yaml b/src/Bundle/ChillMainBundle/config/routes/notification.yaml new file mode 100644 index 000000000..2112f882f --- /dev/null +++ b/src/Bundle/ChillMainBundle/config/routes/notification.yaml @@ -0,0 +1,3 @@ +chill_main_notification_show: + path: /show + controller: Chill\MainBundle\Controller\NotificationController::showAction diff --git a/src/Bundle/ChillMainBundle/config/services/controller.yaml b/src/Bundle/ChillMainBundle/config/services/controller.yaml index 6021e3d72..11a9bc274 100644 --- a/src/Bundle/ChillMainBundle/config/services/controller.yaml +++ b/src/Bundle/ChillMainBundle/config/services/controller.yaml @@ -33,3 +33,8 @@ services: $logger: '@Psr\Log\LoggerInterface' $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface' tags: ['controller.service_arguments'] + + Chill\MainBundle\Controller\NotificationController: + arguments: + $security: '@Symfony\Component\Security\Core\Security' + tags: ['controller.service_arguments'] diff --git a/src/Bundle/ChillMainBundle/config/services/notification.yaml b/src/Bundle/ChillMainBundle/config/services/notification.yaml index fa4fa15c6..c8d970c5d 100644 --- a/src/Bundle/ChillMainBundle/config/services/notification.yaml +++ b/src/Bundle/ChillMainBundle/config/services/notification.yaml @@ -4,7 +4,11 @@ services: $logger: '@Psr\Log\LoggerInterface' $twig: '@Twig\Environment' $mailer: '@swiftmailer.mailer.default' - # $mailerTransporter: '@swiftmailer.transport' + # $mailerTransporter: '@swiftmailer.transport' $router: '@Symfony\Component\Routing\RouterInterface' $translator: '@Symfony\Component\Translation\TranslatorInterface' $routeParameters: '%chill_main.notifications%' + + Chill\MainBundle\Notification\NotificationRenderer: + autoconfigure: true + autowire: true diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 03b05bbc8..1b22018a2 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -74,6 +74,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $loader->load('services/form.yaml'); $loader->load('services/alt_names.yaml'); $loader->load('services/household.yaml'); + $loader->load('services/notification.yaml'); // We can get rid of this file when the service 'chill.person.repository.person' is no more used. // We should use the PersonRepository service instead of a custom service name. $loader->load('services/repository.yaml'); diff --git a/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationRenderer.php b/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationRenderer.php new file mode 100644 index 000000000..ca76fbd5c --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationRenderer.php @@ -0,0 +1,24 @@ +getRelatedEntityClass() == AccompanyingPeriod::class; + } + + public function getTemplate() + { + return 'ChillPersonBundle:AccompanyingPeriod:showInNotification.html.twig'; + } + + public function getTemplateData(Notification $notification) + { + return ['notification' => $notification]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/showInNotification.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/showInNotification.html.twig new file mode 100644 index 000000000..d92de8cba --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingPeriod/showInNotification.html.twig @@ -0,0 +1,3 @@ + + Go to Acc. period. + diff --git a/src/Bundle/ChillPersonBundle/config/services/notification.yaml b/src/Bundle/ChillPersonBundle/config/services/notification.yaml new file mode 100644 index 000000000..58187defb --- /dev/null +++ b/src/Bundle/ChillPersonBundle/config/services/notification.yaml @@ -0,0 +1,3 @@ +services: + Chill\PersonBundle\Notification\AccompanyingPeriodNotificationRenderer: + autowire: true