mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 14:36:13 +00:00
Implement datamapper to handle form data for notification flags
This commit is contained in:
parent
cf780b6e36
commit
33540f58d7
@ -40,10 +40,16 @@ final class UserProfileController extends AbstractController
|
|||||||
|
|
||||||
$user = $this->security->getUser();
|
$user = $this->security->getUser();
|
||||||
$editForm = $this->createForm(UserProfileType::class, $user);
|
$editForm = $this->createForm(UserProfileType::class, $user);
|
||||||
|
|
||||||
|
$editForm->get('notificationFlags')->setData($user->getNotificationFlags());
|
||||||
|
|
||||||
$editForm->add('submit', SubmitType::class);
|
$editForm->add('submit', SubmitType::class);
|
||||||
$editForm->handleRequest($request);
|
$editForm->handleRequest($request);
|
||||||
|
|
||||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||||
|
$notificationFlagsData = $editForm->get('notificationFlags')->getData();
|
||||||
|
$user->setNotificationFlags($notificationFlagsData);
|
||||||
|
|
||||||
$em = $this->managerRegistry->getManager();
|
$em = $this->managerRegistry->getManager();
|
||||||
$em->persist($user);
|
$em->persist($user);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view
|
||||||
|
* the LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Form\DataMapper;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\DataMapperInterface;
|
||||||
|
|
||||||
|
final readonly class NotificationFlagDataMapper implements DataMapperInterface
|
||||||
|
{
|
||||||
|
private array $notificationFlagProviders;
|
||||||
|
|
||||||
|
public function __construct(array $notificationFlagProviders)
|
||||||
|
{
|
||||||
|
$this->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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,14 +12,12 @@ declare(strict_types=1);
|
|||||||
namespace Chill\MainBundle\Form\Type;
|
namespace Chill\MainBundle\Form\Type;
|
||||||
|
|
||||||
use Chill\MainBundle\Form\DataMapper\NotificationFlagDataMapper;
|
use Chill\MainBundle\Form\DataMapper\NotificationFlagDataMapper;
|
||||||
use Chill\MainBundle\Form\DataMapper\NotificationPreferenceDataMapper;
|
|
||||||
use Chill\MainBundle\Notification\NotificationFlagManager;
|
use Chill\MainBundle\Notification\NotificationFlagManager;
|
||||||
use Symfony\Component\Form\AbstractType;
|
use Symfony\Component\Form\AbstractType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\Form\DataMapperInterface;
|
|
||||||
|
|
||||||
class NotificationFlagsType extends AbstractType
|
class NotificationFlagsType extends AbstractType
|
||||||
{
|
{
|
||||||
@ -32,13 +30,12 @@ class NotificationFlagsType extends AbstractType
|
|||||||
|
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
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) {
|
foreach ($this->notificationFlagProviders as $flagProvider) {
|
||||||
$flag = $flagProvider->getFlag();
|
$flag = $flagProvider->getFlag();
|
||||||
$builder->add($flag, FormType::class, [
|
$builder->add($flag, FormType::class, [
|
||||||
'label' => $flagProvider->getLabel(),
|
'label' => $flagProvider->getLabel(),
|
||||||
'mapped' => false,
|
|
||||||
'required' => false,
|
'required' => false,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -46,10 +43,12 @@ class NotificationFlagsType extends AbstractType
|
|||||||
->add('immediate_email', CheckboxType::class, [
|
->add('immediate_email', CheckboxType::class, [
|
||||||
'label' => false,
|
'label' => false,
|
||||||
'required' => false,
|
'required' => false,
|
||||||
|
'mapped' => false, // Keep this here for the individual checkboxes
|
||||||
])
|
])
|
||||||
->add('daily_email', CheckboxType::class, [
|
->add('daily_email', CheckboxType::class, [
|
||||||
'label' => false,
|
'label' => false,
|
||||||
'required' => 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
|
public function configureOptions(OptionsResolver $resolver): void
|
||||||
{
|
{
|
||||||
$resolver->setDefaults([
|
$resolver->setDefaults([
|
||||||
'empty_data' => [],
|
'data_class' => null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ use Chill\MainBundle\Form\Type\NotificationFlagsType;
|
|||||||
use Symfony\Component\Form\AbstractType;
|
use Symfony\Component\Form\AbstractType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
||||||
|
|
||||||
class UserProfileType extends AbstractType
|
class UserProfileType extends AbstractType
|
||||||
{
|
{
|
||||||
@ -28,6 +27,7 @@ class UserProfileType extends AbstractType
|
|||||||
])
|
])
|
||||||
->add('notificationFlags', NotificationFlagsType::class, [
|
->add('notificationFlags', NotificationFlagsType::class, [
|
||||||
'label' => false,
|
'label' => false,
|
||||||
|
'mapped' => false,
|
||||||
])
|
])
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user