Move logic from twig template to controller and refactor workflow controller

This commit is contained in:
Julie Lenaerts 2024-07-18 15:00:22 +02:00
parent 7f144da1a7
commit 8973b7c20b
4 changed files with 26 additions and 59 deletions

View File

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

View File

@ -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,
]);
}*/
}

View File

@ -4,18 +4,9 @@
<div class="item-bloc">
{% for s in signatures %}
<div class="item-row mb-2">
{% if signaturesByPerson %}
<div class="col-sm-6"><span class="chill-entity entity-person badge-person">{{ s.getSigner|chill_entity_render_box }}</span></div>
{% else %}
<div class="col-sm-6"><span>{{ s.getSigner.username }}</span></div>
{% endif %}
<div class="col-sm-6"><span>{{ s.signer|chill_entity_render_box }}</span></div>
<div class="col-sm-6">
{% if signaturesByPerson %}
<a class="btn btn-show" href="{{ chill_path_add_return_path('chill_main_workflow_signature_metadata', { 'id': entity_workflow.id, 'signature_id': s.id}) }}">{{ 'workflow.signature_zone.button_sign'|trans }}</a>
{% else %}
<a class="btn btn-show" href="#">{{ 'workflow.signature_zone.button_sign'|trans }}</a>
{% endif %}
<a class="btn btn-show" href="{{ chill_path_add_return_path('chill_main_workflow_signature_metadata', { 'signature_id': s.id}) }}">{{ 'workflow.signature_zone.button_sign'|trans }}</a>
{% if s.state is same as('signed') %}
<p class="updatedBy">{{ s.stateDate }}</p>
{% endif %}

View File

@ -57,7 +57,7 @@
</section>
<section class="step my-4">{% include '@ChillMain/Workflow/_follow.html.twig' %}</section>
{% if signatures is not empty %}
{% if signatures|length > 0 %}
<section class="step my-4">{% include '@ChillMain/Workflow/_signature.html.twig' %}</section>
{% endif %}
<section class="step my-4">{% include '@ChillMain/Workflow/_decision.html.twig' %}</section>{#