Refactor user suggestion logic in workflow

Removed duplicate user suggestion handling from `WorkflowController` and centralized it in `WorkflowStepType`. This change simplifies the controller and makes user suggestion logic more maintainable.
This commit is contained in:
Julien Fastré 2024-10-22 23:51:48 +02:00
parent 85dc9bdb2f
commit 968835a262
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 7 additions and 11 deletions

View File

@ -300,19 +300,12 @@ class WorkflowController extends AbstractController
if (\count($workflow->getEnabledTransitions($entityWorkflow)) > 0) { if (\count($workflow->getEnabledTransitions($entityWorkflow)) > 0) {
// possible transition // possible transition
$stepDTO = new WorkflowTransitionContextDTO($entityWorkflow); $stepDTO = new WorkflowTransitionContextDTO($entityWorkflow);
$usersInvolved = $entityWorkflow->getUsersInvolved();
$currentUserFound = array_search($this->security->getUser(), $usersInvolved, true);
if (false !== $currentUserFound) {
unset($usersInvolved[$currentUserFound]);
}
$transitionForm = $this->createForm( $transitionForm = $this->createForm(
WorkflowStepType::class, WorkflowStepType::class,
$stepDTO, $stepDTO,
[ [
'entity_workflow' => $entityWorkflow, 'entity_workflow' => $entityWorkflow,
'suggested_users' => $usersInvolved,
] ]
); );

View File

@ -17,6 +17,7 @@ use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Form\Type\PickUserDynamicType; use Chill\MainBundle\Form\Type\PickUserDynamicType;
use Chill\MainBundle\Form\Type\PickUserGroupOrUserDynamicType; use Chill\MainBundle\Form\Type\PickUserGroupOrUserDynamicType;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO; use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO;
use Chill\PersonBundle\Form\Type\PickPersonDynamicType; use Chill\PersonBundle\Form\Type\PickPersonDynamicType;
use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType; use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType;
@ -34,6 +35,7 @@ class WorkflowStepType extends AbstractType
public function __construct( public function __construct(
private readonly Registry $registry, private readonly Registry $registry,
private readonly TranslatableStringHelperInterface $translatableStringHelper, private readonly TranslatableStringHelperInterface $translatableStringHelper,
private readonly EntityWorkflowManager $entityWorkflowManager,
) {} ) {}
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
@ -43,6 +45,7 @@ class WorkflowStepType extends AbstractType
$workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName());
$place = $workflow->getMarking($entityWorkflow); $place = $workflow->getMarking($entityWorkflow);
$placeMetadata = $workflow->getMetadataStore()->getPlaceMetadata(array_keys($place->getPlaces())[0]); $placeMetadata = $workflow->getMetadataStore()->getPlaceMetadata(array_keys($place->getPlaces())[0]);
$suggestedUsers = $this->entityWorkflowManager->getSuggestedUsers($entityWorkflow);
if (null === $options['entity_workflow']) { if (null === $options['entity_workflow']) {
throw new \LogicException('if transition is true, entity_workflow should be defined'); throw new \LogicException('if transition is true, entity_workflow should be defined');
@ -157,19 +160,20 @@ class WorkflowStepType extends AbstractType
'label' => 'workflow.signature_zone.user signature', 'label' => 'workflow.signature_zone.user signature',
'multiple' => false, 'multiple' => false,
'suggest_myself' => true, 'suggest_myself' => true,
'suggested' => $suggestedUsers,
]) ])
->add('futureDestUsers', PickUserGroupOrUserDynamicType::class, [ ->add('futureDestUsers', PickUserGroupOrUserDynamicType::class, [
'label' => 'workflow.dest for next steps', 'label' => 'workflow.dest for next steps',
'multiple' => true, 'multiple' => true,
'empty_data' => '[]', 'empty_data' => '[]',
'suggested' => $options['suggested_users'], 'suggested' => $suggestedUsers,
'suggest_myself' => true, 'suggest_myself' => true,
]) ])
->add('futureCcUsers', PickUserDynamicType::class, [ ->add('futureCcUsers', PickUserDynamicType::class, [
'label' => 'workflow.cc for next steps', 'label' => 'workflow.cc for next steps',
'multiple' => true, 'multiple' => true,
'required' => false, 'required' => false,
'suggested' => $options['suggested_users'], 'suggested' => $suggestedUsers,
'empty_data' => '[]', 'empty_data' => '[]',
'attr' => ['class' => 'future-cc-users'], 'attr' => ['class' => 'future-cc-users'],
'suggest_myself' => true, 'suggest_myself' => true,
@ -207,7 +211,6 @@ class WorkflowStepType extends AbstractType
$resolver $resolver
->setDefault('data_class', WorkflowTransitionContextDTO::class) ->setDefault('data_class', WorkflowTransitionContextDTO::class)
->setRequired('entity_workflow') ->setRequired('entity_workflow')
->setAllowedTypes('entity_workflow', EntityWorkflow::class) ->setAllowedTypes('entity_workflow', EntityWorkflow::class);
->setDefault('suggested_users', []);
} }
} }