various improvements

This commit is contained in:
Julien Fastré 2021-12-04 01:13:55 +01:00
parent 6c1a946608
commit 063ceb2c53
17 changed files with 163 additions and 69 deletions

View File

@ -23,12 +23,12 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext
*/ */
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void;
public function getFormData(DocGeneratorTemplate $template, $entity): array;
/** /**
* has form. * has form.
* *
* @param mixed $entity * @param mixed $entity
*/ */
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
public function getFormData(DocGeneratorTemplate $template, $entity): array;
} }

View File

@ -59,4 +59,23 @@ class AdminDocGeneratorTemplateController extends CRUDController
return $entity; return $entity;
} }
function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = [])
{
switch ($action) {
case 'new':
$context = $this->contextManager->getContextByKey($request->get('context'));
break;
case 'edit':
$context = $this->contextManager->getContextByDocGeneratorTemplate($entity);
break;
default:
return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters); // TODO: Change the autogenerated stub
}
return array_merge(
$defaultTemplateParameters,
['context' => $context]
);
}
} }

View File

@ -113,10 +113,11 @@ final class DocGeneratorTemplateController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$contextGenerationData = $form->getData(); $contextGenerationData = $form->getData();
} elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) { } elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) {
$template = '@ChillDocGenerator/Generator/basic_form.html.twig'; $templatePath = '@ChillDocGenerator/Generator/basic_form.html.twig';
$templateOptions = ['entity' => $entity, 'form' => $form->createView(), 'template' => $template]; $templateOptions = ['entity' => $entity, 'form' => $form->createView(),
'template' => $template, 'context' => $context];
return $this->render($template, $templateOptions); return $this->render($templatePath, $templateOptions);
} }
} }

View File

@ -1,21 +1,39 @@
<template> <template>
<div>
<template v-if="templates.length > 0"> <template v-if="templates.length > 0">
<slot name="title"> <slot name="title">
<h2>{{ $t('Generate document from template')}}</h2> <h2>{{ $t('generate_document')}}</h2>
</slot> </slot>
<select class="form-select form-select-sm" v-model="template">
<option disabled value="">{{ $t('evaluation_choose_a_template') }}</option> <div class="container">
<div class="row">
<div class="col-md-4">
<slot name="label">
<label>{{ $t('select_a_template') }}</label>
</slot>
</div>
<div class="col-md-8">
<div class="input-group mb-3">
<select class="form-select" v-model="template">
<option disabled selected value="">{{ $t('choose_a_template') }}</option>
<template v-for="t in templates"> <template v-for="t in templates">
<option v-bind:value="t.id">{{ t.name.fr }}</option> <option v-bind:value="t.id">{{ t.name.fr }}</option>
</template> </template>
</select> </select>
<button v-if="canGenerate" class="btn btn-update btn-sm change-icon" type="button" @click="generateDocument"><i class="fa fa-fw fa-cog"></i></button> <button v-if="canGenerate" class="btn btn-update btn-sm change-icon" type="button" @click="generateDocument"><i class="fa fa-fw fa-cog"></i></button>
<button v-else class="btn btn-update btn-sm change-icon" type="button" disabled ><i class="fa fa-fw fa-cog"></i></button> <button v-else class="btn btn-update btn-sm change-icon" type="button" disabled ><i class="fa fa-fw fa-cog"></i></button>
</div>
<div v-if="hasDescription"> </div>
</div>
<div class="row" v-if="hasDescription">
<div class="col-md-8 align-self-end">
<p>{{ getDescription }}</p> <p>{{ getDescription }}</p>
</div> </div>
</div>
</div>
</template> </template>
</div>
</template> </template>
@ -106,6 +124,15 @@ export default {
window.location.assign(url); window.location.assign(url);
}, },
}, },
i18n: {
messages: {
fr: {
generate_document: 'Générer un document',
select_a_template: 'Choisir un gabarit',
choose_a_template: 'Choisir',
}
}
}
} }
</script> </script>

View File

@ -1,11 +1,15 @@
{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} {% extends '@ChillDocGenerator/Admin/layout.html.twig' %}
{% block title %} {% block title 'docgen.Edit template'|trans %}
{% include('@ChillMain/CRUD/_edit_title.html.twig') %}
{% endblock %}
{% block layout_wvm_content %} {% block layout_wvm_content %}
{% embed '@ChillMain/CRUD/_edit_content.html.twig' %} {% embed '@ChillMain/CRUD/_edit_content.html.twig' %}
{% block crud_content_header %}
<h1>{{ 'docgen.Edit template'|trans }}</h1>
<h2>{{ 'docgen.With context %name%'|trans({'%name%': context.name|trans }) }}</h2>
{% endblock crud_content_header %}
{% block content_form_actions_view %}{% endblock %} {% block content_form_actions_view %}{% endblock %}
{% block content_form_actions_save_and_show %}{% endblock %} {% block content_form_actions_save_and_show %}{% endblock %}
{% endembed %} {% endembed %}

View File

@ -1,12 +1,17 @@
{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} {% extends '@ChillDocGenerator/Admin/layout.html.twig' %}
{% block title %} {% block title 'docgen.New template'|trans %}
{% include('@ChillMain/CRUD/_new_title.html.twig') %}
{% endblock %}
{% block layout_wvm_content %} {% block layout_wvm_content %}
{% embed '@ChillMain/CRUD/_new_content.html.twig' %} {% embed '@ChillMain/CRUD/_new_content.html.twig' %}
{% block crud_content_header %}
<h1>{{ 'docgen.New template'|trans }}</h1>
<h2>{{ 'docgen.With context %name%'|trans({'%name%': context.name|trans }) }}</h2>
{% endblock crud_content_header %}
{% block content_form_actions_save_and_show %}{% endblock %} {% block content_form_actions_save_and_show %}{% endblock %}
{% endembed %} {% endembed %}
{% endblock %} {% endblock %}

View File

@ -1,10 +1,11 @@
{% extends 'ChillMainBundle::layout.html.twig' %} {% extends 'ChillMainBundle::layout.html.twig' %}
{% block title 'Generate document'|trans %} {% block title 'docgen.Generate a document'|trans %}
{% block content %} {% block content %}
<div class="col-md-10 col-xxl"> <div class="col-md-10 col-xxl">
<h1>{{ block('title') }}</h1> <h1>{{ block('title') }}</h1>
<h2>{{ template.name|localize_translatable_string }}</h2>
{{ form_start(form, { 'attr': { 'id': 'generate_doc_form' }}) }} {{ form_start(form, { 'attr': { 'id': 'generate_doc_form' }}) }}
{{ form(form) }} {{ form(form) }}
@ -12,8 +13,8 @@
<ul class="record_actions sticky-form-buttons"> <ul class="record_actions sticky-form-buttons">
<li> <li>
<button type="submit" class="btn btn-edit" form="generate_doc_form"> <button type="submit" class="btn btn-edit change-icon" form="generate_doc_form">
<i class="fa fa-cog"></i> {{ 'Generate'|trans }} <i class="fa fa-cog"></i> {{ 'docgen.Generate'|trans }}
</button> </button>
</li> </li>
</ul> </ul>

View File

@ -99,7 +99,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
} }
$type = $reflection->getProperty($attribute->getName())->getType(); $type = $reflection->getProperty($attribute->getName())->getType();
} elseif ($reflection->hasMethod($method = 'get'.ucfirst($attribute->getName()))) { } elseif ($reflection->hasMethod($method = 'get' . ucfirst($attribute->getName()))) {
if (!$reflection->getMethod($method)->hasReturnType()) { if (!$reflection->getMethod($method)->hasReturnType()) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add a return type on the method', 'Could not determine how the content is determined for the attribute %s. Add a return type on the method',
@ -108,7 +108,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
} }
$type = $reflection->getMethod($method)->getReturnType(); $type = $reflection->getMethod($method)->getReturnType();
} elseif ($reflection->hasMethod($method = 'is'.ucfirst($attribute->getName()))) { } elseif ($reflection->hasMethod($method = 'is' . ucfirst($attribute->getName()))) {
if (!$reflection->getMethod($method)->hasReturnType()) { if (!$reflection->getMethod($method)->hasReturnType()) {
throw new \LogicException(sprintf( throw new \LogicException(sprintf(
'Could not determine how the content is determined for the attribute %s. Add a return type on the method', 'Could not determine how the content is determined for the attribute %s. Add a return type on the method',
@ -168,12 +168,14 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte
switch ($type) { switch ($type) {
case 'array': case 'array':
return []; return [];
case 'bool': case 'bool':
case 'double': case 'double':
case 'float': case 'float':
case 'int': case 'int':
case 'resource': case 'resource':
return null; return null;
case 'string': case 'string':
return ''; return '';

View File

@ -0,0 +1,3 @@
docgen:
Generate a document: Génerer un document
Generate: Génerer

View File

@ -15,7 +15,6 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Serializer\Model\Collection;
use Chill\MainBundle\Serializer\Normalizer\CollectionNormalizer;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Chill\PersonBundle\Entity\SocialWork\Evaluation;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
@ -23,15 +22,17 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Serializer\SerializerInterface;
use function count;
use function in_array;
class AccompanyingPeriodWorkEvaluationApiController class AccompanyingPeriodWorkEvaluationApiController
{ {
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private SerializerInterface $serializer;
private PaginatorFactory $paginatorFactory; private PaginatorFactory $paginatorFactory;
private SerializerInterface $serializer;
public function __construct( public function __construct(
DocGeneratorTemplateRepository $docGeneratorTemplateRepository, DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
SerializerInterface $serializer, SerializerInterface $serializer,
@ -49,16 +50,17 @@ class AccompanyingPeriodWorkEvaluationApiController
public function listTemplateByEvaluation(Evaluation $evaluation, string $_format): JsonResponse public function listTemplateByEvaluation(Evaluation $evaluation, string $_format): JsonResponse
{ {
if ('json' !== $_format) { if ('json' !== $_format) {
throw new BadRequestHttpException("format not supported"); throw new BadRequestHttpException('format not supported');
} }
$evaluations = $evaluations =
array_filter( array_filter(
$this->docGeneratorTemplateRepository $this->docGeneratorTemplateRepository
->findByEntity(AccompanyingPeriodWorkEvaluation::class), ->findByEntity(AccompanyingPeriodWorkEvaluation::class),
function (DocGeneratorTemplate $t) use ($evaluation) { static function (DocGeneratorTemplate $t) use ($evaluation) {
$ids = $t->getOptions()['evaluations'] ?? []; $ids = $t->getOptions()['evaluations'] ?? [];
return in_array($evaluation->getId(), $ids);
return in_array($evaluation->getId(), $ids, true);
} }
); );
@ -66,8 +68,10 @@ class AccompanyingPeriodWorkEvaluationApiController
$paginator->setItemsPerPage(count($evaluations)); $paginator->setItemsPerPage(count($evaluations));
return new JsonResponse($this->serializer->serialize( return new JsonResponse($this->serializer->serialize(
new Collection($evaluations, $paginator), 'json', [ new Collection($evaluations, $paginator),
AbstractNormalizer::GROUPS => ['read'] 'json',
[
AbstractNormalizer::GROUPS => ['read'],
] ]
), JsonResponse::HTTP_OK, [], true); ), JsonResponse::HTTP_OK, [], true);
} }

View File

@ -239,11 +239,6 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU
return $this->updatedBy; return $this->updatedBy;
} }
public function getWarningInterval(): ?DateInterval
{
return $this->warningInterval;
}
/** /**
* @Serializer\Groups({"docgen:read"}) * @Serializer\Groups({"docgen:read"})
*/ */
@ -256,6 +251,11 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU
return $this->getEndDate()->sub($this->getWarningInterval()); return $this->getEndDate()->sub($this->getWarningInterval());
} }
public function getWarningInterval(): ?DateInterval
{
return $this->warningInterval;
}
public function removeDocument(AccompanyingPeriodWorkEvaluationDocument $document): self public function removeDocument(AccompanyingPeriodWorkEvaluationDocument $document): self
{ {
$this->documents->removeElement($document); $this->documents->removeElement($document);

View File

@ -28,7 +28,7 @@ class Evaluation
* @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @ORM\Column(type="dateinterval", nullable=true, options={"default": null})
* @Serializer\Groups({"read"}) * @Serializer\Groups({"read"})
*/ */
private ?\DateInterval $delay = null; private ?DateInterval $delay = null;
/** /**
* @ORM\Id * @ORM\Id
@ -42,7 +42,7 @@ class Evaluation
* @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @ORM\Column(type="dateinterval", nullable=true, options={"default": null})
* @Serializer\Groups({"read"}) * @Serializer\Groups({"read"})
*/ */
private ?\DateInterval $notificationDelay = null; private ?DateInterval $notificationDelay = null;
/** /**
* @ORM\ManyToOne( * @ORM\ManyToOne(

View File

@ -195,6 +195,7 @@
</ul> </ul>
</div> </div>
<div>
<pick-template <pick-template
entityClass="Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork" entityClass="Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork"
:templates="this.templatesAvailablesForAction" :templates="this.templatesAvailablesForAction"
@ -204,6 +205,7 @@
<h3>{{ $t('Generate doc') }}</h3> <h3>{{ $t('Generate doc') }}</h3>
</template> </template>
</pick-template> </pick-template>
</div>
<div v-if="errors.length > 0" id="errors" class="alert alert-danger flashbag"> <div v-if="errors.length > 0" id="errors" class="alert alert-danger flashbag">
<p>{{ $t('fix_these_errors') }}</p> <p>{{ $t('fix_these_errors') }}</p>

View File

@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Service\DocGenerator; namespace Chill\PersonBundle\Service\DocGenerator;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
@ -29,8 +28,10 @@ use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists; use function array_key_exists;
class AccompanyingPeriodContext implements class AccompanyingPeriodContext implements
@ -47,28 +48,26 @@ class AccompanyingPeriodContext implements
private TranslatableStringHelperInterface $translatableStringHelper; private TranslatableStringHelperInterface $translatableStringHelper;
private TranslatorInterface $translator;
public function __construct( public function __construct(
DocumentCategoryRepository $documentCategoryRepository, DocumentCategoryRepository $documentCategoryRepository,
NormalizerInterface $normalizer, NormalizerInterface $normalizer,
TranslatableStringHelperInterface $translatableStringHelper, TranslatableStringHelperInterface $translatableStringHelper,
EntityManagerInterface $em, EntityManagerInterface $em,
PersonRender $personRender PersonRender $personRender,
TranslatorInterface $translator
) { ) {
$this->documentCategoryRepository = $documentCategoryRepository; $this->documentCategoryRepository = $documentCategoryRepository;
$this->normalizer = $normalizer; $this->normalizer = $normalizer;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->em = $em; $this->em = $em;
$this->personRender = $personRender; $this->personRender = $personRender;
$this->translator = $translator;
} }
public function adminFormReverseTransform(array $data): array public function adminFormReverseTransform(array $data): array
{ {
$data = [
'mainPerson' => $data['mainPerson'],
'person1' => $data['person1'],
'person2' => $data['person2'],
];
if (array_key_exists('category', $data)) { if (array_key_exists('category', $data)) {
$data['category'] = [ $data['category'] = [
'idInsideBundle' => $data['category']->getIdInsideBundle(), 'idInsideBundle' => $data['category']->getIdInsideBundle(),
@ -83,8 +82,11 @@ class AccompanyingPeriodContext implements
{ {
$data = [ $data = [
'mainPerson' => $data['mainPerson'] ?? false, 'mainPerson' => $data['mainPerson'] ?? false,
'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'),
'person1' => $data['person1'] ?? false, 'person1' => $data['person1'] ?? false,
'person1Label' => $data['person1Label'] ?? $this->translator->trans('docgen.person 1'),
'person2' => $data['person2'] ?? false, 'person2' => $data['person2'] ?? false,
'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'),
]; ];
if (array_key_exists('category', $data)) { if (array_key_exists('category', $data)) {
@ -100,12 +102,27 @@ class AccompanyingPeriodContext implements
$builder $builder
->add('mainPerson', CheckboxType::class, [ ->add('mainPerson', CheckboxType::class, [
'required' => false, 'required' => false,
'label' => 'Ask for main person',
])
->add('mainPersonLabel', TextType::class, [
'label' => 'main person label',
'required' => true,
]) ])
->add('person1', CheckboxType::class, [ ->add('person1', CheckboxType::class, [
'required' => false, 'required' => false,
'label' => 'Ask for person 1'
])
->add('person1Label', TextType::class, [
'label' => 'person 1 label',
'required' => true,
]) ])
->add('person2', CheckboxType::class, [ ->add('person2', CheckboxType::class, [
'required' => false, 'required' => false,
'label' => 'Ask for person 2'
])
->add('person2Label', TextType::class, [
'label' => 'person 2 label',
'required' => true,
]) ])
->add('category', EntityType::class, [ ->add('category', EntityType::class, [
'placeholder' => 'Choose a document category', 'placeholder' => 'Choose a document category',
@ -138,6 +155,7 @@ class AccompanyingPeriodContext implements
'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); },
'multiple' => false, 'multiple' => false,
'expanded' => true, 'expanded' => true,
'label' => $options[$key.'Label'],
]); ]);
} }
} }
@ -175,7 +193,7 @@ class AccompanyingPeriodContext implements
public function getFormData(DocGeneratorTemplate $template, $entity): array public function getFormData(DocGeneratorTemplate $template, $entity): array
{ {
return [ return [
'course' => $entity 'course' => $entity,
]; ];
} }

View File

@ -51,6 +51,8 @@ class AccompanyingPeriodWorkContext implements
public function buildAdminForm(FormBuilderInterface $builder): void public function buildAdminForm(FormBuilderInterface $builder): void
{ {
$this->periodContext->buildAdminForm($builder); $this->periodContext->buildAdminForm($builder);
$builder->remove('category');
} }
/** /**

View File

@ -100,7 +100,6 @@ class AccompanyingPeriodWorkEvaluationContext implements
/** /**
* @param AccompanyingPeriodWorkEvaluation $entity * @param AccompanyingPeriodWorkEvaluation $entity
* @return void
*/ */
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
{ {
@ -141,8 +140,10 @@ class AccompanyingPeriodWorkEvaluationContext implements
*/ */
public function getFormData(DocGeneratorTemplate $template, $entity): array public function getFormData(DocGeneratorTemplate $template, $entity): array
{ {
return $this->accompanyingPeriodWorkContext->getFormData($template, return $this->accompanyingPeriodWorkContext->getFormData(
$entity->getAccompanyingPeriodWork()); $template,
$entity->getAccompanyingPeriodWork()
);
} }
public static function getKey(): string public static function getKey(): string

View File

@ -448,3 +448,8 @@ Household addresses: Adresses de domicile
Insert an address: Insérer une adresse Insert an address: Insérer une adresse
see social issues: Voir les problématiques sociales see social issues: Voir les problématiques sociales
see persons associated: Voir les usagers concernés see persons associated: Voir les usagers concernés
docgen:
Main person: Personne principale
person 1: Première personne
person 2: Deuxième personne