WIP add notification preferences table to user profile form

This commit is contained in:
Julie Lenaerts 2025-06-10 15:55:01 +02:00
parent c9c565809a
commit cf780b6e36
8 changed files with 172 additions and 22 deletions

View File

@ -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')]);
}
}

View File

@ -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] ?? [];

View File

@ -0,0 +1,63 @@
<?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\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
{
private array $notificationFlagProviders;
public function __construct(NotificationFlagManager $notificationFlagManager)
{
$this->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' => [],
]);
}
}

View File

@ -0,0 +1,41 @@
<?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;
use Chill\MainBundle\Form\Type\ChillPhoneNumberType;
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
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->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,
]);
}
}

View File

@ -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

View File

@ -45,6 +45,32 @@
{{ form_start(form) }}
{{ form_row(form.phonenumber) }}
<h2 class="mb-4">{{ 'user.profile.notification_preferences'|trans }}</h2>
<table class="table table-bordered border-dark align-middle">
<thead>
<tr>
<th>{{ 'notification.flags.type'|trans }}</th>
<th>{{ 'notification.flags.preferences.immediate_email'|trans }}</th>
<th>{{ 'notification.flags.preferences.daily_email'|trans }}</th>
</tr>
</thead>
<tbody>
{% for flag in form.notificationFlags %}
<tr>
<td class="col-sm-6">
<label>{{ form_label(flag) }}</label>
</td>
<td>
{{ form_widget(flag.immediate_email) }}
</td>
<td>
{{ form_widget(flag.daily_email) }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul class="record_actions">
<li>
{{ form_widget(form.submit, { 'attr': { 'class': 'btn btn-save' } } ) }}

View File

@ -139,6 +139,11 @@ services:
autowire: true
autoconfigure: true
Chill\MainBundle\Form\DataMapper\NotificationFlagDataMapper:
autowire: true
autoconfigure: true
Chill\MainBundle\Form\UserProfileType: ~
Chill\MainBundle\Form\AbsenceType: ~
Chill\MainBundle\Form\DataMapper\RegroupmentDataMapper: ~
Chill\MainBundle\Form\RegroupmentType: ~

View File

@ -51,9 +51,10 @@ Label: Nom
user:
profile:
title: Mon profil
Phonenumber successfully updated!: Numéro de téléphone mis à jour!
Profile successfully updated!: Votre profil a été mis à jour!
no job: Pas de métier assigné
no scope: Pas de cercle assigné
notification_preferences: Préférences pour mes notifications
user_group:
inactive: Inactif
@ -715,6 +716,21 @@ notification:
mark_as_read: Marquer comme lu
mark_as_unread: Marquer comme non-lu
flags:
type: Type de notification
referrer-acc-course: Notification lors de la désignation comme référent
acc-course-work-eval: Notification sur un document d'évaluation
acc-course-work: Notification sur un action d'accompagnement
activity: Notification sur un échange
acc-course: Notification sur un parcours d'accompagnement
person-address-move: Notification lors que l'usager qui localise un parcours a déménagé
person: Notification sur un usager
workflow-trans: Notification sur une transition d'un workflow
preferences:
immediate_email: Recevoir un email immédiatement
daily_email: Recevoir un récapitulatif quotidien
export:
address_helper: