diff --git a/src/Bundle/ChillMainBundle/Controller/UserProfileController.php b/src/Bundle/ChillMainBundle/Controller/UserProfileController.php index c027da98e..572583ffc 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserProfileController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserProfileController.php @@ -40,10 +40,16 @@ final class UserProfileController extends AbstractController $user = $this->security->getUser(); $editForm = $this->createForm(UserProfileType::class, $user); + + $editForm->get('notificationFlags')->setData($user->getNotificationFlags()); + $editForm->add('submit', SubmitType::class); $editForm->handleRequest($request); if ($editForm->isSubmitted() && $editForm->isValid()) { + $notificationFlagsData = $editForm->get('notificationFlags')->getData(); + $user->setNotificationFlags($notificationFlagsData); + $em = $this->managerRegistry->getManager(); $em->persist($user); $em->flush(); diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/NotificationFlagDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/NotificationFlagDataMapper.php new file mode 100644 index 000000000..e3bfac8c7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/NotificationFlagDataMapper.php @@ -0,0 +1,78 @@ +notificationFlagProviders = $notificationFlagProviders; + } + + public function mapDataToForms($viewData, $forms): void + { + if (null === $viewData) { + $viewData = []; + } + + $formsArray = iterator_to_array($forms); + + foreach ($this->notificationFlagProviders as $flagProvider) { + $flag = $flagProvider->getFlag(); + + if (isset($formsArray[$flag])) { + $flagForm = $formsArray[$flag]; + + $immediateEmailChecked = in_array('immediate-email', $viewData[$flag] ?? []); + $dailyEmailChecked = in_array('daily-email', $viewData[$flag] ?? []); + + if ($flagForm->has('immediate_email')) { + $flagForm->get('immediate_email')->setData($immediateEmailChecked); + } + if ($flagForm->has('daily_email')) { + $flagForm->get('daily_email')->setData($dailyEmailChecked); + } + } + } + } + + public function mapFormsToData($forms, &$viewData): void + { + $formsArray = iterator_to_array($forms); + $viewData = []; + + foreach ($this->notificationFlagProviders as $flagProvider) { + $flag = $flagProvider->getFlag(); + + if (isset($formsArray[$flag])) { + $flagForm = $formsArray[$flag]; + $viewData[$flag] = []; + + if ($flagForm['immediate_email']->getData()) { + $viewData[$flag][] = 'immediate-email'; + } + + if ($flagForm['daily_email']->getData()) { + $viewData[$flag][] = 'daily-email'; + } + + if (empty($viewData[$flag])) { + $viewData[$flag][] = 'no-email'; + } + } + } + } +} diff --git a/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php b/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php index 07cc51454..9c21b59d1 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/NotificationFlagsType.php @@ -12,14 +12,12 @@ declare(strict_types=1); namespace Chill\MainBundle\Form\Type; use Chill\MainBundle\Form\DataMapper\NotificationFlagDataMapper; -use Chill\MainBundle\Form\DataMapper\NotificationPreferenceDataMapper; use Chill\MainBundle\Notification\NotificationFlagManager; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\FormType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Component\Form\DataMapperInterface; class NotificationFlagsType extends AbstractType { @@ -32,13 +30,12 @@ class NotificationFlagsType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { -// $builder->setDataMapper(new NotificationFlagDataMapper($this->notificationFlagProviders)); + $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, ]); @@ -46,10 +43,12 @@ class NotificationFlagsType extends AbstractType ->add('immediate_email', CheckboxType::class, [ 'label' => false, 'required' => false, + 'mapped' => false, // Keep this here for the individual checkboxes ]) ->add('daily_email', CheckboxType::class, [ 'label' => false, 'required' => false, + 'mapped' => false, // Keep this here for the individual checkboxes ]); } } @@ -57,7 +56,7 @@ class NotificationFlagsType extends AbstractType public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ - 'empty_data' => [], + 'data_class' => null, ]); } } diff --git a/src/Bundle/ChillMainBundle/Form/UserProfileType.php b/src/Bundle/ChillMainBundle/Form/UserProfileType.php index 9bc5c2f67..f9fa65991 100644 --- a/src/Bundle/ChillMainBundle/Form/UserProfileType.php +++ b/src/Bundle/ChillMainBundle/Form/UserProfileType.php @@ -16,7 +16,6 @@ use Chill\MainBundle\Form\Type\NotificationFlagsType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Contracts\Translation\TranslatorInterface; class UserProfileType extends AbstractType { @@ -28,6 +27,7 @@ class UserProfileType extends AbstractType ]) ->add('notificationFlags', NotificationFlagsType::class, [ 'label' => false, + 'mapped' => false, ]) ; }