diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php index 2d6c5972a..0bb5ecee3 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserDynamicType.php @@ -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) diff --git a/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php index c4e91cce4..6d24e145e 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserGroupOrUserDynamicType.php @@ -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']) diff --git a/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php b/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php index 1984437f8..e5769a28e 100644 --- a/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php +++ b/src/Bundle/ChillMainBundle/Form/WorkflowStepType.php @@ -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,