From cf780b6e3629b1596ecebc002c500e08750fee82 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 10 Jun 2025 15:55:01 +0200 Subject: [PATCH] WIP add notification preferences table to user profile form --- .../Controller/UserProfileController.php | 26 +++----- src/Bundle/ChillMainBundle/Entity/User.php | 11 ++++ .../Form/Type/NotificationFlagsType.php | 63 +++++++++++++++++++ .../ChillMainBundle/Form/UserProfileType.php | 41 ++++++++++++ .../Notification/NotificationFlagManager.php | 4 +- .../Resources/views/User/profile.html.twig | 26 ++++++++ .../ChillMainBundle/config/services/form.yaml | 5 ++ .../translations/messages.fr.yml | 18 +++++- 8 files changed, 172 insertions(+), 22 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php create mode 100644 src/Bundle/ChillMainBundle/Form/UserProfileType.php diff --git a/src/Bundle/ChillMainBundle/Controller/UserProfileController.php b/src/Bundle/ChillMainBundle/Controller/UserProfileController.php index a48d1a1e2..c027da98e 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserProfileController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserProfileController.php @@ -11,14 +11,12 @@ declare(strict_types=1); namespace Chill\MainBundle\Controller; -use Chill\MainBundle\Form\UserPhonenumberType; +use Chill\MainBundle\Form\UserProfileType; use Chill\MainBundle\Security\ChillSecurity; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; -use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; -use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Component\Routing\Annotation\Route; @@ -41,16 +39,15 @@ final class UserProfileController extends AbstractController } $user = $this->security->getUser(); - $editForm = $this->createPhonenumberEditForm($user); + $editForm = $this->createForm(UserProfileType::class, $user); + $editForm->add('submit', SubmitType::class); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { - $phonenumber = $editForm->get('phonenumber')->getData(); - - $user->setPhonenumber($phonenumber); - - $this->managerRegistry->getManager()->flush(); - $this->addFlash('success', $this->translator->trans('user.profile.Phonenumber successfully updated!')); + $em = $this->managerRegistry->getManager(); + $em->persist($user); + $em->flush(); + $this->addFlash('success', $this->translator->trans('user.profile.Profile successfully updated!')); return $this->redirectToRoute('chill_main_user_profile'); } @@ -60,13 +57,4 @@ final class UserProfileController extends AbstractController 'form' => $editForm->createView(), ]); } - - private function createPhonenumberEditForm(UserInterface $user): FormInterface - { - return $this->createForm( - UserPhonenumberType::class, - $user, - ) - ->add('submit', SubmitType::class, ['label' => $this->translator->trans('Save')]); - } } diff --git a/src/Bundle/ChillMainBundle/Entity/User.php b/src/Bundle/ChillMainBundle/Entity/User.php index a45cb0209..2874ec46a 100644 --- a/src/Bundle/ChillMainBundle/Entity/User.php +++ b/src/Bundle/ChillMainBundle/Entity/User.php @@ -616,6 +616,17 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter return $this; } + + public function getNotificationFlags(): array + { + return $this->notificationFlags; + } + + public function setNotificationFlags(array $notificationFlags) + { + $this->notificationFlags = $notificationFlags; + } + public function getNotificationFlagData(string $flag): array { return $this->notificationFlags[$flag] ?? []; diff --git a/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php b/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php new file mode 100644 index 000000000..07cc51454 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php @@ -0,0 +1,63 @@ +notificationFlagProviders = $notificationFlagManager->getAllNotificationFlagProviders(); + } + + public function buildForm(FormBuilderInterface $builder, array $options): void + { +// $builder->setDataMapper(new NotificationFlagDataMapper($this->notificationFlagProviders)); + + foreach ($this->notificationFlagProviders as $flagProvider) { + $flag = $flagProvider->getFlag(); + $builder->add($flag, FormType::class, [ + 'label' => $flagProvider->getLabel(), + 'mapped' => false, + 'required' => false, + ]); + + $builder->get($flag) + ->add('immediate_email', CheckboxType::class, [ + 'label' => false, + 'required' => false, + ]) + ->add('daily_email', CheckboxType::class, [ + 'label' => false, + 'required' => false, + ]); + } + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'empty_data' => [], + ]); + } +} diff --git a/src/Bundle/ChillMainBundle/Form/UserProfileType.php b/src/Bundle/ChillMainBundle/Form/UserProfileType.php new file mode 100644 index 000000000..9bc5c2f67 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/UserProfileType.php @@ -0,0 +1,41 @@ +add('phonenumber', ChillPhoneNumberType::class, [ + 'required' => false, + ]) + ->add('notificationFlags', NotificationFlagsType::class, [ + 'label' => false, + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => \Chill\MainBundle\Entity\User::class, + ]); + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationFlagManager.php b/src/Bundle/ChillMainBundle/Notification/NotificationFlagManager.php index e35da5f06..e45621df9 100644 --- a/src/Bundle/ChillMainBundle/Notification/NotificationFlagManager.php +++ b/src/Bundle/ChillMainBundle/Notification/NotificationFlagManager.php @@ -21,9 +21,9 @@ final readonly class NotificationFlagManager private array $notificationFlagProviders; public function __construct( - iterable $notificaitonFlagProviders, + iterable $notificationFlagProviders, ) { - $this->notificationFlagProviders = iterator_to_array($notificaitonFlagProviders); + $this->notificationFlagProviders = iterator_to_array($notificationFlagProviders); } public function getAllNotificationFlagProviders(): array diff --git a/src/Bundle/ChillMainBundle/Resources/views/User/profile.html.twig b/src/Bundle/ChillMainBundle/Resources/views/User/profile.html.twig index 360d748a5..4448cae54 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/User/profile.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/User/profile.html.twig @@ -45,6 +45,32 @@ {{ form_start(form) }} {{ form_row(form.phonenumber) }} +

{{ 'user.profile.notification_preferences'|trans }}

+ + + + + + + + + + {% for flag in form.notificationFlags %} + + + + + + {% endfor %} + +
{{ 'notification.flags.type'|trans }}{{ 'notification.flags.preferences.immediate_email'|trans }}{{ 'notification.flags.preferences.daily_email'|trans }}
+ + + {{ form_widget(flag.immediate_email) }} + + {{ form_widget(flag.daily_email) }} +
+