Add an option "suggest_myself" on PickUserDynamicType and PickUserGroupOrUserDynamicType, and use this option in WorkflowStepType

This will suggest the current user in PickUserDynamicType
This commit is contained in:
Julien Fastré 2024-10-15 11:31:25 +02:00
parent d283d62049
commit d8ded80582
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 35 additions and 2 deletions

View File

@ -18,6 +18,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
@ -29,12 +30,18 @@ use Symfony\Component\Serializer\SerializerInterface;
*
* - `multiple`: pick one or more users
* - `suggested`: a list of suggested users
* - `suggest_myself`: append the current user to the list of suggested
* - `as_id`: only the id will be set in the returned data
* - `submit_on_adding_new_entity`: the browser will immediately submit the form when new users are checked
*/
class PickUserDynamicType extends AbstractType
{
public function __construct(private readonly DenormalizerInterface $denormalizer, private readonly SerializerInterface $serializer, private readonly NormalizerInterface $normalizer) {}
public function __construct(
private readonly DenormalizerInterface $denormalizer,
private readonly SerializerInterface $serializer,
private readonly NormalizerInterface $normalizer,
private readonly Security $security,
) {}
public function buildForm(FormBuilderInterface $builder, array $options)
{
@ -53,6 +60,12 @@ class PickUserDynamicType extends AbstractType
foreach ($options['suggested'] as $user) {
$view->vars['suggested'][] = $this->normalizer->normalize($user, 'json', ['groups' => 'read']);
}
$user = $this->security->getUser();
if ($user instanceof User) {
if (true === $options['suggest_myself'] && !in_array($user, $options['suggested'], true)) {
$view->vars['suggested'][] = $this->normalizer->normalize($user, 'json', ['groups' => 'read']);
}
}
}
public function configureOptions(OptionsResolver $resolver)
@ -61,6 +74,8 @@ class PickUserDynamicType extends AbstractType
->setDefault('multiple', false)
->setAllowedTypes('multiple', ['bool'])
->setDefault('compound', false)
->setDefault('suggest_myself', false)
->setAllowedTypes('suggest_myself', ['bool'])
->setDefault('suggested', [])
// if set to true, only the id will be set inside the content. The denormalization will not work.
->setDefault('as_id', false)

View File

@ -11,12 +11,14 @@ declare(strict_types=1);
namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\Type\DataTransformer\EntityToJsonTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
@ -26,7 +28,12 @@ use Symfony\Component\Serializer\SerializerInterface;
*/
final class PickUserGroupOrUserDynamicType extends AbstractType
{
public function __construct(private readonly DenormalizerInterface $denormalizer, private readonly SerializerInterface $serializer, private readonly NormalizerInterface $normalizer) {}
public function __construct(
private readonly DenormalizerInterface $denormalizer,
private readonly SerializerInterface $serializer,
private readonly NormalizerInterface $normalizer,
private readonly Security $security,
) {}
public function buildForm(FormBuilderInterface $builder, array $options)
{
@ -45,6 +52,12 @@ final class PickUserGroupOrUserDynamicType extends AbstractType
foreach ($options['suggested'] as $userGroup) {
$view->vars['suggested'][] = $this->normalizer->normalize($userGroup, 'json', ['groups' => 'read']);
}
$user = $this->security->getUser();
if ($user instanceof User) {
if (true === $options['suggest_myself'] && !in_array($user, $options['suggested'], true)) {
$view->vars['suggested'][] = $this->normalizer->normalize($user, 'json', ['groups' => 'read']);
}
}
}
public function configureOptions(OptionsResolver $resolver)
@ -54,6 +67,8 @@ final class PickUserGroupOrUserDynamicType extends AbstractType
->setAllowedTypes('multiple', ['bool'])
->setDefault('compound', false)
->setDefault('suggested', [])
->setDefault('suggest_myself', false)
->setAllowedTypes('suggest_myself', ['bool'])
// if set to true, only the id will be set inside the content. The denormalization will not work.
->setDefault('as_id', false)
->setAllowedTypes('as_id', ['bool'])

View File

@ -156,12 +156,14 @@ class WorkflowStepType extends AbstractType
->add('futureUserSignature', PickUserDynamicType::class, [
'label' => 'workflow.signature_zone.user signature',
'multiple' => false,
'suggest_myself' => true,
])
->add('futureDestUsers', PickUserGroupOrUserDynamicType::class, [
'label' => 'workflow.dest for next steps',
'multiple' => true,
'empty_data' => '[]',
'suggested' => $options['suggested_users'],
'suggest_myself' => true,
])
->add('futureCcUsers', PickUserDynamicType::class, [
'label' => 'workflow.cc for next steps',
@ -170,6 +172,7 @@ class WorkflowStepType extends AbstractType
'suggested' => $options['suggested_users'],
'empty_data' => '[]',
'attr' => ['class' => 'future-cc-users'],
'suggest_myself' => true,
])
->add('futureDestineeEmails', ChillCollectionType::class, [
'entry_type' => EmailType::class,