From 8973b7c20bec50d2e929c1fe801f3950a03c47ae Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Thu, 18 Jul 2024 15:00:22 +0200 Subject: [PATCH] Move logic from twig template to controller and refactor workflow controller --- .../Controller/WorkflowController.php | 42 ++++++------------- .../Form/WorkflowSignatureMetadataType.php | 28 +++++-------- .../views/Workflow/_signature.html.twig | 13 +----- .../Resources/views/Workflow/index.html.twig | 2 +- 4 files changed, 26 insertions(+), 59 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php index 6575d6c93..ed83d2c74 100644 --- a/src/Bundle/ChillMainBundle/Controller/WorkflowController.php +++ b/src/Bundle/ChillMainBundle/Controller/WorkflowController.php @@ -13,10 +13,8 @@ namespace Chill\MainBundle\Controller; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\Workflow\EntityWorkflow; -use Chill\MainBundle\Entity\Workflow\EntityWorkflowComment; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep; use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature; -use Chill\MainBundle\Form\EntityWorkflowCommentType; use Chill\MainBundle\Form\WorkflowSignatureMetadataType; use Chill\MainBundle\Form\WorkflowStepType; use Chill\MainBundle\Pagination\PaginatorFactory; @@ -25,7 +23,6 @@ use Chill\MainBundle\Security\Authorization\EntityWorkflowVoter; use Chill\MainBundle\Security\ChillSecurity; use Chill\MainBundle\Workflow\EntityWorkflowManager; use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO; -use Chill\PersonBundle\Entity\Person; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\FormType; @@ -38,7 +35,6 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\TransitionBlocker; -use Symfony\Component\Workflow\Workflow; use Symfony\Contracts\Translation\TranslatorInterface; class WorkflowController extends AbstractController @@ -281,12 +277,7 @@ class WorkflowController extends AbstractController $handler = $this->entityWorkflowManager->getHandler($entityWorkflow); $workflow = $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()); $errors = []; - $isSignaturePerson = false; - $signatures = $entityWorkflow->getCurrentStep()->getSignatures(); - if (!$signatures->isEmpty()) { - $isSignaturePerson = $signatures[0]->getSigner() instanceof Person; - } if (\count($workflow->getEnabledTransitions($entityWorkflow)) > 0) { // possible transition @@ -332,22 +323,6 @@ class WorkflowController extends AbstractController } } - /* - $commentForm = $this->createForm(EntityWorkflowCommentType::class, $newComment = new EntityWorkflowComment()); - $commentForm->handleRequest($request); - - if ($commentForm->isSubmitted() && $commentForm->isValid()) { - $this->entityManager->persist($newComment); - $this->entityManager->flush(); - - $this->addFlash('success', $this->translator->trans('workflow.Comment added')); - - return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]); - } elseif ($commentForm->isSubmitted() && !$commentForm->isValid()) { - $this->addFlash('error', $this->translator->trans('This form contains errors')); - } - */ - return $this->render( '@ChillMain/Workflow/index.html.twig', [ @@ -358,8 +333,6 @@ class WorkflowController extends AbstractController 'entity_workflow' => $entityWorkflow, 'transition_form_errors' => $errors, 'signatures' => $signatures, - 'signaturesByPerson' => $isSignaturePerson, - // 'comment_form' => $commentForm->createView(), ] ); } @@ -379,9 +352,18 @@ class WorkflowController extends AbstractController return $lines; } - #[Route(path: '/{_locale}/main/workflow/{id}/signature/{signature_id}/metadata', name: 'chill_main_workflow_signature_metadata')] - public function addSignatureMetadata(EntityWorkflow $entityWorkflow, EntityWorkflowStepSignature $signature, Request $request): Response + /** + * @ParamConverter("signature", options={"id": "signature_id"}) + */ + #[Route(path: '/{_locale}/main/workflow/signature/{signature_id}/metadata', name: 'chill_main_workflow_signature_metadata')] + public function addSignatureMetadata(int $signature_id, Request $request): Response { + $signature = $this->entityManager->getRepository(EntityWorkflowStepSignature::class)->find($signature_id); + + if ($signature->getSigner() instanceof User) { + return $this->redirectToRoute('signature_route_user'); + } + $metadataForm = $this->createForm(WorkflowSignatureMetadataType::class); $metadataForm->add('submit', SubmitType::class, ['label' => $this->translator->trans('Save')]); @@ -404,7 +386,7 @@ class WorkflowController extends AbstractController $this->entityManager->flush(); // Todo should redirect to document for actual signing? To be adjusted still - return $this->redirectToRoute('chill_main_workflow_show', ['id' => $entityWorkflow->getId()]); + return $this->redirectToRoute('chill_main_workflow_show', ['id' => $signature->getStep()->getEntityWorkflow()->getId()]); } return $this->render( diff --git a/src/Bundle/ChillMainBundle/Form/WorkflowSignatureMetadataType.php b/src/Bundle/ChillMainBundle/Form/WorkflowSignatureMetadataType.php index d6da62eeb..47be9b88c 100644 --- a/src/Bundle/ChillMainBundle/Form/WorkflowSignatureMetadataType.php +++ b/src/Bundle/ChillMainBundle/Form/WorkflowSignatureMetadataType.php @@ -11,33 +11,34 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; -use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature; use Chill\MainBundle\Form\Type\ChillDateType; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\OptionsResolver\OptionsResolver; class WorkflowSignatureMetadataType extends AbstractType { - public function __construct(private ParameterBagInterface $parameterBag, private RequestStack $requestStack) {} + public function __construct(private readonly ParameterBagInterface $parameterBag, private readonly TranslatableStringHelperInterface $translatableStringHelper) {} - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { $documentTypeChoices = $this->parameterBag->get('chill_main.id_document_kinds'); - $locale = $this->requestStack->getCurrentRequest()->getLocale(); $choices = []; foreach ($documentTypeChoices as $documentType) { + $labels = []; + foreach ($documentType['labels'] as $label) { - if ($label['lang'] === $locale) { - $choices[$label['label']] = $documentType['key']; - break; - } + $labels[$label['lang']] = $label['label']; + } + + $localizedLabel = $this->translatableStringHelper->localize($labels); + if (null !== $localizedLabel) { + $choices[$localizedLabel] = $documentType['key']; } } @@ -58,11 +59,4 @@ class WorkflowSignatureMetadataType extends AbstractType 'label' => 'workflow.signature_zone.metadata.docExpiration', ]); } - - /* public function configureOptions(OptionsResolver $resolver): void - { - $resolver->setDefaults([ - 'data_class' => EntityWorkflowStepSignature::class, - ]); - }*/ } diff --git a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig index 325868fc2..a61808500 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Workflow/_signature.html.twig @@ -4,18 +4,9 @@
{% for s in signatures %}
- {% if signaturesByPerson %} -
{{ s.getSigner|chill_entity_render_box }}
- {% else %} -
{{ s.getSigner.username }}
- {% endif %} +
{{ s.signer|chill_entity_render_box }}
- {% if signaturesByPerson %} - {{ 'workflow.signature_zone.button_sign'|trans }} - {% else %} - {{ 'workflow.signature_zone.button_sign'|trans }} - {% endif %} - + {{ 'workflow.signature_zone.button_sign'|trans }} {% if s.state is same as('signed') %}

{{ s.stateDate }}

{% endif %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Workflow/index.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Workflow/index.html.twig index 730d0808e..da4d073b2 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/Workflow/index.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/Workflow/index.html.twig @@ -57,7 +57,7 @@
{% include '@ChillMain/Workflow/_follow.html.twig' %}
- {% if signatures is not empty %} + {% if signatures|length > 0 %}
{% include '@ChillMain/Workflow/_signature.html.twig' %}
{% endif %}
{% include '@ChillMain/Workflow/_decision.html.twig' %}
{#