From 1c18ba20fc8dc5b6fe4ccee876935ef5249f0431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 27 Nov 2021 01:23:34 +0100 Subject: [PATCH 01/41] docgen: improve listing for templates --- .../DocGeneratorTemplateController.php | 47 ++++++++++++------- .../Entity/DocGeneratorTemplate.php | 16 ++++--- .../DocGeneratorTemplateRepository.php | 23 ++++++++- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index dd9d8e635..fd153fab2 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -14,6 +14,8 @@ use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; +use Chill\MainBundle\Pagination\PaginatorFactory; +use Chill\MainBundle\Serializer\Model\Collection; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; use Chill\PersonBundle\Entity\SocialWork\Evaluation; @@ -22,15 +24,25 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; use PhpOffice\PhpWord\TemplateProcessor; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; class DocGeneratorTemplateController extends AbstractController { + private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; + + private PaginatorFactory $paginatorFactory; + + public function __construct(DocGeneratorTemplateRepository $docGeneratorTemplateRepository, PaginatorFactory $paginatorFactory) + { + $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->paginatorFactory = $paginatorFactory; + } + /** * @Route( * "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}", @@ -130,26 +142,25 @@ class DocGeneratorTemplateController extends AbstractController /** * @Route( - * "{_locale}/doc/gen/templates/for/{entityClassName}", + * "/api/1.0/docgen/templates/by-entity/{entityClassName}", * name="chill_docgenerator_templates_for_entity_api" * ) */ - public function listTemplateApiAction( - string $entityClassName, - DocGeneratorTemplateRepository $templateRepository - ): Response { - $entities = $templateRepository->findByEntity($entityClassName); + public function listTemplateApiAction(string $entityClassName): Response + { + $nb = $this->docGeneratorTemplateRepository->countByEntity($entityClassName); + $paginator = $this->paginatorFactory->create($nb); + $entities = $this->docGeneratorTemplateRepository->findByEntity( + $entityClassName, + $paginator->getCurrentPageFirstItemNumber(), + $paginator->getItemsPerPage() + ); - $ret = []; - - foreach ($entities as $entity) { - $ret[] = [ - 'id' => $entity->getId(), - 'name' => $entity->getName(), - 'description' => $entity->getDescription(), - ]; - } - - return new JsonResponse(['results' => $ret]); + return $this->json( + new Collection($entities, $paginator), + Response::HTTP_OK, + [], + [AbstractNormalizer::GROUPS => ['read']] + ); } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index f38ca139a..43d72a64f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -15,31 +15,35 @@ use Symfony\Component\Serializer\Annotation as Serializer; /** * @ORM\Entity * @ORM\Table(name="chill_docgen_template") + * @Serializer\DiscriminatorMap(typeProperty="type", mapping={ + * "docgen_template": DocGeneratorTemplate::class + * }) */ class DocGeneratorTemplate { /** - * @ORM\Column(type="string", length=255) - * - * Class name of the context to use + * Class name of the context to use. * * so if $context = '' * this template will use '' as context + * + * @ORM\Column(type="string", length=255) */ private string $context; /** * @ORM\Column(type="text", nullable=true) + * @Serializer\Groups({"read"}) */ private string $description; /** - * @ORM\Column(type="simple_array") - * - * Class name of the entities for which this template can be used + * Class name of the entities for which this template can be used. * * so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction'] * this template can be selected for an AccompanyingPeriod or a SocialAction + * + * @ORM\Column(type="simple_array") */ private array $entities = []; diff --git a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php index df0e0bd3d..007f2fabb 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php +++ b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php @@ -25,6 +25,18 @@ final class DocGeneratorTemplateRepository implements ObjectRepository $this->repository = $entityManager->getRepository(DocGeneratorTemplate::class); } + public function countByEntity(string $entity): int + { + $builder = $this->repository->createQueryBuilder('t'); + + $builder + ->select('count(t)') + ->where('t.entities LIKE :entity') + ->setParameter('entity', '%' . addslashes($entity) . '%'); + + return $builder->getQuery()->getSingleScalarResult(); + } + public function find($id, $lockMode = null, $lockVersion = null): ?DocGeneratorTemplate { return $this->repository->find($id, $lockMode, $lockVersion); @@ -49,7 +61,10 @@ final class DocGeneratorTemplateRepository implements ObjectRepository return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } - public function findByEntity($entity) + /** + * @return array|DocGeneratorTemplate[] + */ + public function findByEntity(string $entity, ?int $start = 0, ?int $limit = 50): array { $builder = $this->repository->createQueryBuilder('t'); @@ -57,7 +72,11 @@ final class DocGeneratorTemplateRepository implements ObjectRepository ->where('t.entities LIKE :entity') ->setParameter('entity', '%' . addslashes($entity) . '%'); - return $builder->getQuery()->execute(); + return $builder + ->getQuery() + ->setFirstResult($start) + ->setMaxResults($limit) + ->getResult(); } public function findOneBy(array $criteria, ?array $orderBy = null): ?DocGeneratorTemplate From 9f868bff4f67b2bedfde838d8dbc6a87bba50f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 28 Nov 2021 21:24:18 +0100 Subject: [PATCH 02/41] create module for generating document in twig pages --- .../public/module/PickTemplate/index.js | 24 +++++ .../public/vuejs/_components/PickTemplate.vue | 95 +++++++++++++++++++ .../chill.webpack.config.js | 2 + .../index.html.twig | 9 ++ .../vuejs/AccompanyingCourseWorkEdit/App.vue | 15 +++ 5 files changed, 145 insertions(+) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js b/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js new file mode 100644 index 000000000..8b450a7e1 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js @@ -0,0 +1,24 @@ +import {createApp} from 'vue'; +import PickTemplate from 'ChillDocGeneratorAssets/vuejs/_components/PickTemplate.vue'; +import {_createI18n} from 'ChillMainAssets/vuejs/_js/i18n'; + +const i18n = _createI18n({}); + +document.querySelectorAll('div[data-docgen-template-picker]').forEach(el => { + let + picker = { + template: '', + components: { + PickTemplate, + }, + data() { + return { + entityClass: el.dataset.entityClass, + entityId: el.dataset.entityId, + } + }, + } + ; + + createApp(picker).use(i18n).mount(el); +}); diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue new file mode 100644 index 000000000..bb8f6008a --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue @@ -0,0 +1,95 @@ + + + + + diff --git a/src/Bundle/ChillDocGeneratorBundle/chill.webpack.config.js b/src/Bundle/ChillDocGeneratorBundle/chill.webpack.config.js index d894be9b0..9017c7e08 100644 --- a/src/Bundle/ChillDocGeneratorBundle/chill.webpack.config.js +++ b/src/Bundle/ChillDocGeneratorBundle/chill.webpack.config.js @@ -3,4 +3,6 @@ module.exports = function(encore, entries) { encore.addAliases({ ChillDocGeneratorAssets: __dirname + '/Resources/public' }); + + encore.addEntry('mod_docgen_picktemplate', __dirname + '/Resources/public/module/PickTemplate/index.js'); }; diff --git a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig index dd0c9cc62..ca6869bff 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig +++ b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig @@ -11,6 +11,13 @@ {% block js %} {{ parent() }} {{ encore_entry_script_tags('mod_async_upload') }} + {{ encore_entry_script_tags('mod_docgen_picktemplate') }} +{% endblock %} + +{% block css %} + {{ parent() }} + {{ encore_entry_link_tags('mod_async_upload') }} + {{ encore_entry_link_tags('mod_docgen_picktemplate') }} {% endblock %} {% block content %} @@ -58,6 +65,8 @@ +
+ {% if is_granted('CHILL_ACCOMPANYING_COURSE_DOCUMENT_CREATE', accompanyingCourse) %} + + + +

{{ $t('fix_these_errors') }}

    @@ -232,6 +241,7 @@ import AddEvaluation from './components/AddEvaluation.vue'; import PersonRenderBox from 'ChillPersonAssets/vuejs/_components/Entity/PersonRenderBox.vue'; import AddPersons from 'ChillPersonAssets/vuejs/_components/AddPersons.vue'; import AddressRenderBox from 'ChillMainAssets/vuejs/_components/Entity/AddressRenderBox.vue'; +import PickTemplate from 'ChillDocGeneratorAssets/vuejs/_components/PickTemplate.vue'; const i18n = { messages: { @@ -278,6 +288,7 @@ export default { AddPersons, PersonRenderBox, AddressRenderBox, + PickTemplate, }, i18n, data() { @@ -416,6 +427,10 @@ export default { submit() { this.$store.dispatch('submit'); }, + beforeGenerateTemplate() { + console.log('before generate'); + return Promise.resolve(); + } } }; From bbf6b1412fc62dd549c8036720a998b9fe68259c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 29 Nov 2021 10:42:43 +0100 Subject: [PATCH 03/41] repair admin docgen WIP --- .../Resources/views/Admin/DocGeneratorTemplate/edit.html.twig | 2 +- .../views/Admin/DocGeneratorTemplate/index.html.twig | 2 +- .../Resources/views/Admin/DocGeneratorTemplate/new.html.twig | 2 +- .../Resources/views/Admin/layout.html.twig | 4 ++++ 4 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/layout.html.twig diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig index 040dea1c0..f1ab64888 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig @@ -1,4 +1,4 @@ -{% extends '@ChillPerson/Admin/layout.html.twig' %} +{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} {% block title %} {% include('@ChillMain/CRUD/_edit_title.html.twig') %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig index c601b8836..8d28c5a05 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig @@ -1,4 +1,4 @@ -{% extends '@ChillPerson/Admin/layout.html.twig' %} +{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} {% block layout_wvm_content %} {% embed '@ChillMain/CRUD/_index.html.twig' %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig index f31ac39c9..bdc5144ec 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig @@ -1,4 +1,4 @@ -{% extends '@ChillPerson/Admin/layout.html.twig' %} +{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} {% block title %} {% include('@ChillMain/CRUD/_new_title.html.twig') %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/layout.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/layout.html.twig new file mode 100644 index 000000000..6e7c9387c --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/layout.html.twig @@ -0,0 +1,4 @@ +{% extends '@ChillPerson/Admin/layout.html.twig' %} + +{% block vertical_menu_content %} +{% endblock %} From 7fe2172f050dceec1ae5a5851935ac01f4d45ee4 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 5 Nov 2021 14:53:51 +0100 Subject: [PATCH 04/41] DocGeneratorTemplate use StoredObject for storign the template --- .../Entity/DocGeneratorTemplate.php | 13 ++++--- .../Form/DocGeneratorTemplateType.php | 14 ++++++- .../Admin/DocGeneratorTemplate/edit.html.twig | 9 +++++ .../DocGeneratorTemplate/index.html.twig | 8 +++- .../Admin/DocGeneratorTemplate/new.html.twig | 9 +++++ .../migrations/Version20211103111010.php | 39 +++++++++++++++++++ 6 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index 75edbcbdf..753ca9cd5 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -13,6 +13,7 @@ namespace Chill\DocGeneratorBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation as Serializer; +use Chill\DocStoreBundle\Entity\StoredObject; /** * @ORM\Entity @@ -46,9 +47,9 @@ class DocGeneratorTemplate private array $entities = []; /** - * @ORM\Column(type="string", length=255) + * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"}).) */ - private string $file; + private ?StoredObject $file = null; /** * @ORM\Id @@ -57,6 +58,7 @@ class DocGeneratorTemplate * @Serializer\Groups({"read"}) */ private int $id; + /** * @ORM\Column(type="json") @@ -79,7 +81,7 @@ class DocGeneratorTemplate return $this->entities; } - public function getFile(): ?string + public function getFile(): ?StoredObject { return $this->file; } @@ -89,10 +91,11 @@ class DocGeneratorTemplate return $this->id; } - public function getName(): ?array + public function getName(): ?array { return $this->name; } + public function setContext(string $context): self { @@ -115,7 +118,7 @@ class DocGeneratorTemplate return $this; } - public function setFile(string $file): self + public function setFile(StoredObject $file): self { $this->file = $file; diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 75d33d0e5..3da397be9 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -14,8 +14,10 @@ namespace Chill\DocGeneratorBundle\Form; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Chill\DocStoreBundle\Form\StoredObjectType; class DocGeneratorTemplateType extends AbstractType { @@ -25,8 +27,18 @@ class DocGeneratorTemplateType extends AbstractType ->add('name', TranslatableStringFormType::class, [ 'label' => 'Nom', ]) + ->add('context') + ->add('entities', ChoiceType::class, [ + 'multiple' => true, + 'choices' => [ + 'AccompanyingPeriod' => 'Chill\PersonBundle\Entity\AccompanyingPeriod', + 'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction' + ]]) ->add('description') - ->add('file'); + ->add('file', StoredObjectType::class, [ + 'error_bubbling' => true + ]) + ; } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig index 040dea1c0..81af0037d 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig @@ -10,3 +10,12 @@ {% block content_form_actions_save_and_show %}{% endblock %} {% endembed %} {% endblock %} + +{% block js %} + {{ parent() }} + {{ encore_entry_script_tags('mod_async_upload') }} +{% endblock %} + +{% block css %} + {{ encore_entry_link_tags('mod_async_upload') }} +{% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig index c601b8836..2cedff190 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig @@ -5,7 +5,7 @@ {% block table_entities_thead_tr %} {{ 'Title'|trans }} - {{ 'File'|trans }} + {{ 'Edit'|trans }} {% endblock %} {% block table_entities_tbody %} @@ -13,7 +13,11 @@ {{ entity.id }} {{ entity.name | localize_translatable_string }} - {{ entity.file }} + + + {{ 'Edit'|trans }} + + {% endfor %} {% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig index f31ac39c9..064d45418 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig @@ -9,3 +9,12 @@ {% block content_form_actions_save_and_show %}{% endblock %} {% endembed %} {% endblock %} + +{% block js %} + {{ parent() }} + {{ encore_entry_script_tags('mod_async_upload') }} +{% endblock %} + +{% block css %} + {{ encore_entry_link_tags('mod_async_upload') }} +{% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php new file mode 100644 index 000000000..f3f6b32d0 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php @@ -0,0 +1,39 @@ +addSql('ALTER TABLE chill_docgen_template ADD file_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template DROP file'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER entities SET NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER context SET NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template ADD CONSTRAINT FK_49A347E893CB796C FOREIGN KEY (file_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_49A347E893CB796C ON chill_docgen_template (file_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_docgen_template DROP CONSTRAINT FK_49A347E893CB796C'); + $this->addSql('DROP INDEX IDX_49A347E893CB796C'); + $this->addSql('ALTER TABLE chill_docgen_template ADD file VARCHAR(255) NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template DROP file_id'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER entities DROP NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER context DROP NOT NULL'); + } +} From a3333c33021a3b4d6cc61d7a646696d6d2243d8e Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 19 Nov 2021 11:06:29 +0100 Subject: [PATCH 05/41] Fix LoadDocGeneratorTemplate (using docstore obj) --- .../ORM/LoadDocGeneratorTemplate.php | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php index 082605fd7..293978718 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php @@ -14,7 +14,6 @@ namespace Chill\DocGeneratorBundle\DataFixtures\ORM; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Doctrine\Common\DataFixtures\AbstractFixture; - use Doctrine\Persistence\ObjectManager; /** @@ -28,13 +27,23 @@ class LoadDocGeneratorTemplate extends AbstractFixture [ 'name' => ['fr' => 'FORMULAIRE AEB'], 'desc' => 'stocké sur openstack comedienbe', - 'file' => 'FORMULAIRE_AEB_WITH_DATA_INJ.docx', + 'file' => [ + 'filename' => 'mg01Dz3Cn2Erhi1zK8ACN2', + 'key' => '{"alg":"A256CBC","ext":true,"k":"xTd3HIVeFv2Hm7zdkJ4F6XjCO_e-_7Vg9G07s475hX8","key_ops":["encrypt","decrypt"],"kty":"oct"}', + 'iv' => '[178,160,11,24,15,224,208,42,93,171,74,253,78,105,238,136]', + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + ], 'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext', 'entities' => [AccompanyingPeriodWorkEvaluation::class], ], [ 'name' => ['fr' => 'AIDE ALIMENTAIRE'], 'desc' => 'stocké sur openstack comedienbe', - 'file' => 'AIDE_ALIMENTAIRE.docx', + 'file' => [ + 'filename' => 'pKNlhCrQDCRsAuC8vYHDKa', + 'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}', + 'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]', + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + ], 'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext', 'entities' => ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction', AccompanyingPeriodWorkEvaluation::class], ], @@ -43,10 +52,19 @@ class LoadDocGeneratorTemplate extends AbstractFixture foreach ($templates as $template) { echo 'Adding doc generator templates ' . $template['file'] . ")\n"; + $newStoredObj = (new StoredObject()) + ->setFilename($template['file']['filename']) + ->setKeyInfos(json_decode($template['file']['key'], true)) + ->setIv(json_decode($template['file']['iv'], true)) + ->setCreationDate(new \DateTime('today')) + ->setType($template['file']['type']); + + $manager->persist($newStoredObj); + $newTemplate = (new DocGeneratorTemplate()) ->setName($template['name']) ->setDescription($template['desc']) - ->setFile($template['file']) + ->setFile($newStoredObj) ->setContext($template['context']) ->setEntities($template['entities']); From 1bf595334e4835d28a7e34cb0706cbe4c289d8bb Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 19 Nov 2021 11:08:28 +0100 Subject: [PATCH 06/41] Adding AccompanyingPeriodWorkEvaluation in the choices of DocGenerator config form (admin) --- .../ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 3da397be9..f1f587d76 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -18,6 +18,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Chill\DocStoreBundle\Form\StoredObjectType; +use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; class DocGeneratorTemplateType extends AbstractType { @@ -32,7 +33,8 @@ class DocGeneratorTemplateType extends AbstractType 'multiple' => true, 'choices' => [ 'AccompanyingPeriod' => 'Chill\PersonBundle\Entity\AccompanyingPeriod', - 'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction' + 'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction', + 'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class ]]) ->add('description') ->add('file', StoredObjectType::class, [ From a7af7e7874d38d0e00181102380c685dc600e830 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 19 Nov 2021 14:19:41 +0100 Subject: [PATCH 07/41] php-decrypt data crypted using js --- .../DocGeneratorTemplateController.php | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index f517a7cc6..803da218e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -26,6 +26,10 @@ use PhpOffice\PhpWord\TemplateProcessor; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\HeaderUtils; +use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator; +use Jose\Component\Core\JWK; +use Base64Url\Base64Url; // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; @@ -48,11 +52,39 @@ class DocGeneratorTemplateController extends AbstractController ): Response { $getUrlGen = $tempUrlGenerator->generate( 'GET', - $template->getFile() - ); + $template->getFile()->getFilename()); - $tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE'); - file_put_contents($tmpfname, file_get_contents($getUrlGen->url)); + $data = file_get_contents($getUrlGen->url); + + $iv = $template->getFile()->getIv(); // iv as an Array + $ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt) + + $method = 'AES-256-CBC'; + + $key = $template->getFile()->getKeyInfos()['k']; + $keyGoodFormat = Base64Url::decode($key); + + $dataDecrypted = openssl_decrypt($data, $method, $keyGoodFormat, 1, $ivGoodFormat); + + if ($dataDecrypted === FALSE) { + throw new \Exception("Error during Decrypt ", 1); + } + + $tmpfnameDeCrypted = tempnam(sys_get_temp_dir(), 'DECRYPT_DOC_TEMPLATE'); + + if (!$handle = fopen($tmpfnameDeCrypted, 'a')) { + echo "Cannot open file ($tmpfnameDeCrypted)"; + exit; + } + + if (fwrite($handle, $dataDecrypted) === FALSE) { + echo "Cannot write to file ($tmpfnameDeCrypted)"; + exit; + } + + dump("Success, wrote ($dataDecrypted) to file ($tmpfnameDeCrypted)"); + + fclose($handle); $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); @@ -63,7 +95,7 @@ class DocGeneratorTemplateController extends AbstractController throw new Exception('Not implemented', 1); } - $templateProcessor = new TemplateProcessor($tmpfname); + $templateProcessor = new TemplateProcessor($tmpfnameDeCrypted); foreach ($datas['setValues'] as $setValuesConf) { $templateProcessor->setValues($setValuesConf); @@ -73,12 +105,10 @@ class DocGeneratorTemplateController extends AbstractController $templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]); } - $tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED'); - $templateProcessor->saveAs($tmpfname2); + $tmpfnameGenerated = tempnam(sys_get_temp_dir(), 'DOC_GENERATED'); + $templateProcessor->saveAs($tmpfnameGenerated); - unlink($tmpfname); - - $fileContent = fopen($tmpfname2, 'rb'); // the generated file content + $fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx'; @@ -87,7 +117,8 @@ class DocGeneratorTemplateController extends AbstractController $genDocName ); - unlink($tmpfname2); + unlink($tmpfnameDeCrypted); + unlink($tmpfnameGenerated); $client = new Client(); @@ -128,7 +159,7 @@ class DocGeneratorTemplateController extends AbstractController } throw new Exception('Unable to generate document.'); - } + } /** * @Route( From 847e610362a93a5cf1764997d139bfde2b4438ea Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 19 Nov 2021 14:20:12 +0100 Subject: [PATCH 08/41] Good values to point to a doc file --- .../DataFixtures/ORM/LoadDocGeneratorTemplate.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php index 293978718..cb7e7b027 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php @@ -28,9 +28,9 @@ class LoadDocGeneratorTemplate extends AbstractFixture 'name' => ['fr' => 'FORMULAIRE AEB'], 'desc' => 'stocké sur openstack comedienbe', 'file' => [ - 'filename' => 'mg01Dz3Cn2Erhi1zK8ACN2', - 'key' => '{"alg":"A256CBC","ext":true,"k":"xTd3HIVeFv2Hm7zdkJ4F6XjCO_e-_7Vg9G07s475hX8","key_ops":["encrypt","decrypt"],"kty":"oct"}', - 'iv' => '[178,160,11,24,15,224,208,42,93,171,74,253,78,105,238,136]', + 'filename' => 'pKNlhCrQDCRsAuC8vYHDKa', + 'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}', + 'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]', 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ], 'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext', From 2b01179967abaaafff997794ab4d699bf6867aef Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 5 Nov 2021 14:53:51 +0100 Subject: [PATCH 09/41] DocGeneratorTemplate use StoredObject for storign the template --- .../ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php | 5 +++++ .../Form/DocGeneratorTemplateType.php | 1 + 2 files changed, 6 insertions(+) diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index 753ca9cd5..c1cdeca74 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -66,6 +66,11 @@ class DocGeneratorTemplate */ private array $name = []; + /** + * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"}).) + */ + private ?StoredObject $file = null; + public function getContext(): ?string { return $this->context; diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index f1f587d76..6323d8ebf 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -19,6 +19,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; +use Chill\MainBundle\Form\Type\TranslatableStringFormType; class DocGeneratorTemplateType extends AbstractType { From 4e56075fdcb8a6e741333749d25238d5c311fd26 Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Fri, 19 Nov 2021 14:24:37 +0100 Subject: [PATCH 10/41] Removing unused deps --- .../Controller/DocGeneratorTemplateController.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 803da218e..d609995da 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -18,9 +18,9 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; -use Chill\PersonBundle\Entity\SocialWork\Evaluation; -use Exception; + use GuzzleHttp\Client; + use GuzzleHttp\Exception\TransferException; use PhpOffice\PhpWord\TemplateProcessor; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -92,7 +92,7 @@ class DocGeneratorTemplateController extends AbstractController $context = new HouseholdMemberSelectionContext(); $datas = $context->getData($entity); } else { - throw new Exception('Not implemented', 1); + throw new \Exception('Not implemented', 1); } $templateProcessor = new TemplateProcessor($tmpfnameDeCrypted); From 9fa2e80d8162a6cfda081d03555973d54177e575 Mon Sep 17 00:00:00 2001 From: marcu Date: Fri, 26 Nov 2021 17:25:21 +0100 Subject: [PATCH 11/41] Debug : redeclaration of a varaible --- .../ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index c1cdeca74..753ca9cd5 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -66,11 +66,6 @@ class DocGeneratorTemplate */ private array $name = []; - /** - * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"}).) - */ - private ?StoredObject $file = null; - public function getContext(): ?string { return $this->context; From 59a0b692ac18872d2aa209daa549e7bf4361a56d Mon Sep 17 00:00:00 2001 From: marcu Date: Fri, 26 Nov 2021 17:39:51 +0100 Subject: [PATCH 12/41] Fix load fixtures (LoadDocGeneratorTemplates) --- .../DataFixtures/ORM/LoadDocGeneratorTemplate.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php index cb7e7b027..af8915df7 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php @@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Persistence\ObjectManager; +use Chill\DocStoreBundle\Entity\StoredObject; /** * Load DocGeneratorTemplate. From d026be3041dcedfd219e9bf99af3b23a5215babb Mon Sep 17 00:00:00 2001 From: marcu Date: Tue, 30 Nov 2021 10:42:54 +0100 Subject: [PATCH 13/41] Create new layer + convert in good crs --- .../Controller/DocGeneratorTemplateController.php | 13 +++++++++++-- .../ChillDocGeneratorBundle/config/services.yaml | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index d609995da..35ea454d2 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -31,12 +31,21 @@ use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator; use Jose\Component\Core\JWK; use Base64Url\Base64Url; +use Symfony\Component\HttpKernel\HttpKernelInterface; + // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class DocGeneratorTemplateController extends AbstractController { + protected $kernel; + + public function __construct(HttpKernelInterface $kernel) + { + $this->kernel = $kernel; + } + /** * @Route( * "{_locale}/doc/gen/generate/from/{template}/for/{entityClassName}/{entityId}", @@ -70,7 +79,7 @@ class DocGeneratorTemplateController extends AbstractController throw new \Exception("Error during Decrypt ", 1); } - $tmpfnameDeCrypted = tempnam(sys_get_temp_dir(), 'DECRYPT_DOC_TEMPLATE'); + $tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins if (!$handle = fopen($tmpfnameDeCrypted, 'a')) { echo "Cannot open file ($tmpfnameDeCrypted)"; @@ -105,7 +114,7 @@ class DocGeneratorTemplateController extends AbstractController $templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]); } - $tmpfnameGenerated = tempnam(sys_get_temp_dir(), 'DOC_GENERATED'); + $tmpfnameGenerated = tempnam($this->kernel->getCacheDir(), 'DOC_GENERATED'); $templateProcessor->saveAs($tmpfnameGenerated); $fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 6d87dbd25..6e66115fc 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -15,3 +15,8 @@ services: resource: '../Serializer/Normalizer/' tags: - { name: 'serializer.normalizer', priority: -152 } + + Chill\DocGeneratorBundle\Controller\: + resource: "../Controller" + autowire: true + autoconfigure: true From 2993dab3f7b19274a1cd23a1315a405643e9a64f Mon Sep 17 00:00:00 2001 From: marcu Date: Tue, 30 Nov 2021 12:30:04 +0100 Subject: [PATCH 14/41] Debug using KernelInterface instead HttpKernelInterface --- .../Controller/DocGeneratorTemplateController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 35ea454d2..510d5a483 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -31,17 +31,17 @@ use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator; use Jose\Component\Core\JWK; use Base64Url\Base64Url; -use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\HttpKernel\KernelInterface; // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -class DocGeneratorTemplateController extends AbstractController +final class DocGeneratorTemplateController extends AbstractController { - protected $kernel; + private KernelInterface $kernel; - public function __construct(HttpKernelInterface $kernel) + public function __construct(KernelInterface $kernel) { $this->kernel = $kernel; } From 0e88d7c549a4a46dd69f4148c16033858220e25f Mon Sep 17 00:00:00 2001 From: marcu Date: Tue, 30 Nov 2021 14:46:21 +0100 Subject: [PATCH 15/41] Using HttpClientInterface instead of file_get_contents --- .../Controller/DocGeneratorTemplateController.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 510d5a483..fc494390e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -37,13 +37,17 @@ use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Contracts\HttpClient\HttpClientInterface; + final class DocGeneratorTemplateController extends AbstractController { private KernelInterface $kernel; + private HttpClientInterface $client; - public function __construct(KernelInterface $kernel) + public function __construct(KernelInterface $kernel, HttpClientInterface $client) { $this->kernel = $kernel; + $this->client = $client; } /** @@ -63,7 +67,7 @@ final class DocGeneratorTemplateController extends AbstractController 'GET', $template->getFile()->getFilename()); - $data = file_get_contents($getUrlGen->url); + $data = $this->client->request('GET', $getUrlGen->url); $iv = $template->getFile()->getIv(); // iv as an Array $ivGoodFormat = pack('C*', ...$iv); // iv as a String (ok for openssl_decrypt) @@ -73,7 +77,7 @@ final class DocGeneratorTemplateController extends AbstractController $key = $template->getFile()->getKeyInfos()['k']; $keyGoodFormat = Base64Url::decode($key); - $dataDecrypted = openssl_decrypt($data, $method, $keyGoodFormat, 1, $ivGoodFormat); + $dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat); if ($dataDecrypted === FALSE) { throw new \Exception("Error during Decrypt ", 1); From 3404d3669ce60048e42c503d17724ae101bbfd22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 30 Nov 2021 18:35:05 +0100 Subject: [PATCH 16/41] create contextmanager and refactor docgeneratorTemplateType --- .../ChillDocGeneratorBundle.php | 10 ++++ .../Context/ContextManager.php | 21 ++++++++ .../Context/DocGeneratorContextInterface.php | 5 ++ .../Exception/UnexpectedTypeException.php | 11 +++++ .../HouseholdMemberSelectionContext.php | 10 ++++ .../Form/DocGeneratorTemplateType.php | 20 +++++++- .../config/services.yaml | 9 ++++ .../AccompanyingPeriodContext.php | 48 +++++++++++++++++++ 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php create mode 100644 src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php diff --git a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php index d52f0f4b1..d85b1f7ee 100644 --- a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php +++ b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php @@ -11,8 +11,18 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; +use Chill\MainBundle\Routing\LocalMenuBuilderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class ChillDocGeneratorBundle extends Bundle { + public function build(ContainerBuilder $container) + { + parent::build($container); + + $container->registerForAutoconfiguration(DocGeneratorContextInterface::class) + ->addTag('chill_docgen.context'); + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php new file mode 100644 index 000000000..ec065732c --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php @@ -0,0 +1,21 @@ +contexts = $contexts; + } + + public function getContext(): array + { + return iterator_to_array($this->contexts); + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index 7493f7000..df8c55042 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -16,6 +16,11 @@ namespace Chill\DocGeneratorBundle\Context; */ interface DocGeneratorContextInterface { + + public static function getKey(): string; + + public function getName(): string; + /** * Get the data that will be injected to the generated document. * diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php new file mode 100644 index 000000000..727c12050 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php @@ -0,0 +1,11 @@ +contextManager = $contextManager; + } + public function buildForm(FormBuilderInterface $builder, array $options) { + $contexts = \array_flip(\array_map(function (DocGeneratorContextInterface $c) { + return $c->getName(); + }, $this->contextManager->getContext())); + $builder ->add('name', TranslatableStringFormType::class, [ 'label' => 'Nom', ]) - ->add('context') + ->add('context', ChoiceType::class, [ + 'required' => true, + 'label' => 'Context', + 'choices' => $contexts + ]) ->add('entities', ChoiceType::class, [ 'multiple' => true, 'choices' => [ diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 6e66115fc..25ed037f7 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -20,3 +20,12 @@ services: resource: "../Controller" autowire: true autoconfigure: true + + Chill\DocGeneratorBundle\Form\: + resource: "../Form/" + autowire: true + autoconfigure: true + + Chill\DocGeneratorBundle\Context\ContextManager: + arguments: + $contexts: !tagged_iterator { tag: chill_docgen.context, default_index_method: getKey } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php new file mode 100644 index 000000000..2f286351e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -0,0 +1,48 @@ +normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + } + + public function getForm($entity) + { + // TODO: Implement getForm() method. + } + + public function hasForm(): bool + { + return false; + } + + public function supports(string $entityClass): bool + { + return $entityClass === AccompanyingPeriod::class; + } +} From de4e83b3fb25103e4f4f55e42adc15674526698b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 30 Nov 2021 23:23:02 +0100 Subject: [PATCH 17/41] Generate a context for docgen, on accompanying period --- .../ChillDocGeneratorBundle.php | 1 - .../Context/ContextManager.php | 12 +- .../Context/DocGeneratorContextInterface.php | 9 +- .../Exception/UnexpectedTypeException.php | 18 ++- .../HouseholdMemberSelectionContext.php | 20 +-- .../DocGeneratorTemplateController.php | 38 ++--- .../ORM/LoadDocGeneratorTemplate.php | 9 +- .../Entity/DocGeneratorTemplate.php | 6 +- .../Form/DocGeneratorTemplateType.php | 19 ++- .../migrations/Version20211103111010.php | 29 ++-- .../Resolver/ScopeResolverDispatcher.php | 9 +- .../Templating/TranslatableStringHelper.php | 12 +- .../Controller/HouseholdMemberController.php | 2 +- .../Entity/AccompanyingPeriod.php | 74 +++++---- .../Entity/AccompanyingPeriod/Origin.php | 10 +- .../AccompanyingPeriodDocGenNormalizer.php | 147 ++++++++++++++++++ .../AccompanyingPeriodContext.php | 32 ++-- ...AccompanyingPeriodDocGenNormalizerTest.php | 109 +++++++++++++ .../translations/messages.fr.yml | 5 + 19 files changed, 433 insertions(+), 128 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php diff --git a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php index d85b1f7ee..aa02a6101 100644 --- a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php +++ b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php @@ -12,7 +12,6 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; -use Chill\MainBundle\Routing\LocalMenuBuilderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php index ec065732c..a2eb3742c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php @@ -1,14 +1,20 @@ contexts = $contexts; diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index df8c55042..7118b8eab 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -16,11 +16,6 @@ namespace Chill\DocGeneratorBundle\Context; */ interface DocGeneratorContextInterface { - - public static function getKey(): string; - - public function getName(): string; - /** * Get the data that will be injected to the generated document. * @@ -35,6 +30,10 @@ interface DocGeneratorContextInterface */ public function getForm($entity); + public static function getKey(): string; + + public function getName(): string; + /** * has form. */ diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php index 727c12050..617ec537a 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php @@ -1,11 +1,25 @@ generate( 'GET', - $template->getFile()->getFilename()); + $template->getFile()->getFilename() + ); $data = $this->client->request('GET', $getUrlGen->url); @@ -92,23 +90,25 @@ final class DocGeneratorTemplateController extends AbstractController $dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat); - if ($dataDecrypted === FALSE) { - throw new \Exception("Error during Decrypt ", 1); + if (false === $dataDecrypted) { + throw new \Exception('Error during Decrypt ', 1); } $tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins - if (!$handle = fopen($tmpfnameDeCrypted, 'a')) { - echo "Cannot open file ($tmpfnameDeCrypted)"; + if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) { + echo "Cannot open file ({$tmpfnameDeCrypted})"; + exit; } - if (fwrite($handle, $dataDecrypted) === FALSE) { - echo "Cannot write to file ($tmpfnameDeCrypted)"; + if (fwrite($handle, $dataDecrypted) === false) { + echo "Cannot write to file ({$tmpfnameDeCrypted})"; + exit; } - dump("Success, wrote ($dataDecrypted) to file ($tmpfnameDeCrypted)"); + dump("Success, wrote ({$dataDecrypted}) to file ({$tmpfnameDeCrypted})"); fclose($handle); @@ -185,7 +185,7 @@ final class DocGeneratorTemplateController extends AbstractController } throw new Exception('Unable to generate document.'); - } + } /** * @Route( diff --git a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php index af8915df7..0db7bbd44 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/DataFixtures/ORM/LoadDocGeneratorTemplate.php @@ -12,10 +12,11 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\DataFixtures\ORM; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocStoreBundle\Entity\StoredObject; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; +use DateTime; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Persistence\ObjectManager; -use Chill\DocStoreBundle\Entity\StoredObject; /** * Load DocGeneratorTemplate. @@ -32,7 +33,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture 'filename' => 'pKNlhCrQDCRsAuC8vYHDKa', 'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}', 'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]', - 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', ], 'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext', 'entities' => [AccompanyingPeriodWorkEvaluation::class], @@ -43,7 +44,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture 'filename' => 'pKNlhCrQDCRsAuC8vYHDKa', 'key' => '{"alg":"A256CBC","ext":true,"k":"_VihnD41-VDHlpS-ouwtbMPnu-OXVdtA7ENQWWtAQYM","key_ops":["encrypt","decrypt"],"kty":"oct"}', 'iv' => '[86,231,83,148,117,107,149,173,130,19,105,194,224,145,8,48]', - 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', ], 'context' => 'Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext', 'entities' => ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction', AccompanyingPeriodWorkEvaluation::class], @@ -57,7 +58,7 @@ class LoadDocGeneratorTemplate extends AbstractFixture ->setFilename($template['file']['filename']) ->setKeyInfos(json_decode($template['file']['key'], true)) ->setIv(json_decode($template['file']['iv'], true)) - ->setCreationDate(new \DateTime('today')) + ->setCreationDate(new DateTime('today')) ->setType($template['file']['type']); $manager->persist($newStoredObj); diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index 751fd75cb..93be974ad 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -11,9 +11,9 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Entity; +use Chill\DocStoreBundle\Entity\StoredObject; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation as Serializer; -use Chill\DocStoreBundle\Entity\StoredObject; /** * @ORM\Entity @@ -62,7 +62,6 @@ class DocGeneratorTemplate * @Serializer\Groups({"read"}) */ private int $id; - /** * @ORM\Column(type="json") @@ -95,11 +94,10 @@ class DocGeneratorTemplate return $this->id; } - public function getName(): ?array + public function getName(): ?array { return $this->name; } - public function setContext(string $context): self { diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 4ffbbd1e3..f453ffa35 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -14,13 +14,13 @@ namespace Chill\DocGeneratorBundle\Form; use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; +use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -use Chill\DocStoreBundle\Form\StoredObjectType; -use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; class DocGeneratorTemplateType extends AbstractType { @@ -33,7 +33,7 @@ class DocGeneratorTemplateType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - $contexts = \array_flip(\array_map(function (DocGeneratorContextInterface $c) { + $contexts = array_flip(array_map(static function (DocGeneratorContextInterface $c) { return $c->getName(); }, $this->contextManager->getContext())); @@ -44,20 +44,19 @@ class DocGeneratorTemplateType extends AbstractType ->add('context', ChoiceType::class, [ 'required' => true, 'label' => 'Context', - 'choices' => $contexts + 'choices' => $contexts, ]) ->add('entities', ChoiceType::class, [ 'multiple' => true, - 'choices' => [ + 'choices' => [ 'AccompanyingPeriod' => 'Chill\PersonBundle\Entity\AccompanyingPeriod', 'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction', - 'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class - ]]) + 'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class, + ], ]) ->add('description') ->add('file', StoredObjectType::class, [ - 'error_bubbling' => true - ]) - ; + 'error_bubbling' => true, + ]); } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php index f3f6b32d0..1530fd065 100644 --- a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php +++ b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211103111010.php @@ -1,5 +1,12 @@ addSql('ALTER TABLE chill_docgen_template DROP CONSTRAINT FK_49A347E893CB796C'); + $this->addSql('DROP INDEX IDX_49A347E893CB796C'); + $this->addSql('ALTER TABLE chill_docgen_template ADD file VARCHAR(255) NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template DROP file_id'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER entities DROP NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template ALTER context DROP NOT NULL'); + } + public function getDescription(): string { return 'Using DocStore objects inside the DocGenTemplate'; @@ -26,14 +43,4 @@ final class Version20211103111010 extends AbstractMigration $this->addSql('ALTER TABLE chill_docgen_template ADD CONSTRAINT FK_49A347E893CB796C FOREIGN KEY (file_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('CREATE INDEX IDX_49A347E893CB796C ON chill_docgen_template (file_id)'); } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE chill_docgen_template DROP CONSTRAINT FK_49A347E893CB796C'); - $this->addSql('DROP INDEX IDX_49A347E893CB796C'); - $this->addSql('ALTER TABLE chill_docgen_template ADD file VARCHAR(255) NOT NULL'); - $this->addSql('ALTER TABLE chill_docgen_template DROP file_id'); - $this->addSql('ALTER TABLE chill_docgen_template ALTER entities DROP NOT NULL'); - $this->addSql('ALTER TABLE chill_docgen_template ALTER context DROP NOT NULL'); - } } diff --git a/src/Bundle/ChillMainBundle/Security/Resolver/ScopeResolverDispatcher.php b/src/Bundle/ChillMainBundle/Security/Resolver/ScopeResolverDispatcher.php index 1cd03e2b7..0e4cfdc78 100644 --- a/src/Bundle/ChillMainBundle/Security/Resolver/ScopeResolverDispatcher.php +++ b/src/Bundle/ChillMainBundle/Security/Resolver/ScopeResolverDispatcher.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Security\Resolver; use Chill\MainBundle\Entity\Scope; +use Doctrine\Common\Collections\Collection; final class ScopeResolverDispatcher { @@ -45,7 +46,13 @@ final class ScopeResolverDispatcher { foreach ($this->resolvers as $resolver) { if ($resolver->supports($entity, $options)) { - return $resolver->resolveScope($entity, $options); + $scopes = $resolver->resolveScope($entity, $options); + + if ($scopes instanceof Collection) { + return $scopes->toArray(); + } + + return $scopes; } } diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php index 5da53f515..e354fe9f0 100644 --- a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Templating; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Contracts\Translation\TranslatorInterface; use function array_key_exists; @@ -21,10 +22,13 @@ final class TranslatableStringHelper implements TranslatableStringHelperInterfac private TranslatorInterface $translator; - public function __construct(RequestStack $requestStack, TranslatorInterface $translator) + private string $defaultLocale; + + public function __construct(RequestStack $requestStack, TranslatorInterface $translator, ParameterBagInterface $parameterBag) { $this->requestStack = $requestStack; $this->translator = $translator; + $this->defaultLocale = $parameterBag->get('kernel.default_locale'); } public function localize(array $translatableStrings): ?string @@ -35,11 +39,7 @@ final class TranslatableStringHelper implements TranslatableStringHelperInterfac $request = $this->requestStack->getCurrentRequest(); - if (null === $request) { - return null; - } - - $language = $request->getLocale(); + $language = null === $request ? $this->defaultLocale : $request->getLocale(); if (array_key_exists($language, $translatableStrings)) { return $translatableStrings[$language]; diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php index 802f9fd6c..012d0fd68 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php @@ -186,7 +186,7 @@ class HouseholdMemberController extends ApiController $_format, ['groups' => ['read']] ); - } catch (Exception\InvalidArgumentException|Exception\UnexpectedValueException $e) { + } catch (Exception\InvalidArgumentException | Exception\UnexpectedValueException $e) { throw new BadRequestException("Deserialization error: {$e->getMessage()}", 45896, $e); } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 13109cc98..d3595932c 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -117,11 +117,11 @@ class AccompanyingPeriod implements * @var DateTime * * @ORM\Column(type="date", nullable=true) - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) * @Assert\NotBlank(groups={AccompanyingPeriod::STEP_CLOSED}) * @Assert\GreaterThan(propertyPath="openingDate", groups={AccompanyingPeriod::STEP_CLOSED}) */ - private $closingDate; + private ?DateTime $closingDate = null; /** * @var AccompanyingPeriod\ClosingMotive @@ -132,7 +132,7 @@ class AccompanyingPeriod implements * @Groups({"read", "write"}) * @Assert\NotBlank(groups={AccompanyingPeriod::STEP_CLOSED}) */ - private $closingMotive; + private ?ClosingMotive $closingMotive = null; /** * @var Collection @@ -144,33 +144,34 @@ class AccompanyingPeriod implements * ) * @Assert\NotBlank(groups={AccompanyingPeriod::STEP_DRAFT}) */ - private $comments; + private Collection $comments; /** * @var bool * @ORM\Column(type="boolean", options={"default": false}) - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) */ - private $confidential = false; + private bool $confidential = false; /** * @ORM\Column(type="datetime", nullable=true, options={"default": NULL}) + * @Groups({"docgen:read"}) */ - private DateTimeInterface $createdAt; + private ?DateTimeInterface $createdAt = null; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ - private $createdBy; + private ?User $createdBy = null; /** * @var bool * @ORM\Column(type="boolean", options={"default": false}) - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) */ - private $emergency = false; + private bool $emergency = false; /** * @var int @@ -178,9 +179,9 @@ class AccompanyingPeriod implements * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ - private $id; + private ?int $id = null; /** * @ORM\ManyToOne( @@ -202,9 +203,9 @@ class AccompanyingPeriod implements * @var DateTime * * @ORM\Column(type="date") - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) */ - private $openingDate; + private ?DateTime $openingDate = null; /** * @ORM\ManyToOne(targetEntity=Origin::class) @@ -212,7 +213,7 @@ class AccompanyingPeriod implements * @Groups({"read", "write"}) * @Assert\NotBlank(groups={AccompanyingPeriod::STEP_CONFIRMED}) */ - private $origin; + private ?Origin $origin = null; /** * @var Collection @@ -220,10 +221,10 @@ class AccompanyingPeriod implements * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class, * mappedBy="accompanyingPeriod", orphanRemoval=true, * cascade={"persist", "refresh", "remove", "merge", "detach"}) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) * @ParticipationOverlap(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED}) */ - private $participations; + private Collection $participations; /** * @ORM\ManyToOne( @@ -239,26 +240,26 @@ class AccompanyingPeriod implements * @ORM\Column(type="text") * @Groups({"read", "write"}) */ - private $remark = ''; + private string $remark = ''; /** * @var bool * @ORM\Column(type="boolean", options={"default": false}) - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) */ - private $requestorAnonymous = false; + private bool $requestorAnonymous = false; /** * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="accompanyingPeriodRequested") * @ORM\JoinColumn(nullable=true) */ - private $requestorPerson; + private ?Person $requestorPerson = null; /** * @ORM\ManyToOne(targetEntity=ThirdParty::class) * @ORM\JoinColumn(nullable=true) */ - private $requestorThirdParty; + private ?ThirdParty $requestorThirdParty = null; /** * @var Collection @@ -272,7 +273,7 @@ class AccompanyingPeriod implements * @Groups({"read"}) * @ResourceDuplicateCheck(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED, "Default", "default"}) */ - private $resources; + private Collection $resources; /** * @var Collection @@ -288,7 +289,7 @@ class AccompanyingPeriod implements * @Groups({"read"}) * @Assert\Count(min=1, groups={AccompanyingPeriod::STEP_CONFIRMED}, minMessage="A course must be associated to at least one scope") */ - private $scopes; + private Collection $scopes; /** * @ORM\ManyToMany( @@ -297,7 +298,7 @@ class AccompanyingPeriod implements * @ORM\JoinTable( * name="chill_person_accompanying_period_social_issues" * ) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) * @Assert\Count(min=1, groups={AccompanyingPeriod::STEP_CONFIRMED}, minMessage="A course must contains at least one social issue") */ private Collection $socialIssues; @@ -307,26 +308,26 @@ class AccompanyingPeriod implements * @ORM\Column(type="string", length=32, nullable=true) * @Groups({"read"}) */ - private $step = self::STEP_DRAFT; + private string $step = self::STEP_DRAFT; /** * @ORM\Column(type="datetime", nullable=true, options={"default": NULL}) */ - private DateTimeInterface $updatedAt; + private ?DateTimeInterface $updatedAt = null; /** * @ORM\ManyToOne( * targetEntity=User::class * ) */ - private User $updatedBy; + private ?User $updatedBy = null; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) - * @Groups({"read", "write"}) + * @Groups({"read", "write", "docgen:read"}) */ - private $user; + private ?User $user = null; /** * @ORM\OneToMany( @@ -577,6 +578,11 @@ class AccompanyingPeriod implements }); } + public function getCreatedAt(): ?DateTime + { + return $this->createdAt; + } + public function getCreatedBy(): ?User { return $this->createdBy; @@ -605,7 +611,7 @@ class AccompanyingPeriod implements * * @return int */ - public function getId() + public function getId(): ?int { return $this->id; } @@ -631,7 +637,7 @@ class AccompanyingPeriod implements public function getLocation(?DateTimeImmutable $at = null): ?Address { if ($this->getPersonLocation() instanceof Person) { - return $this->getPersonLocation()->getCurrentHouseholdAddress($at); + return $this->getPersonLocation()->getCurrentPersonAddress(); } return $this->getAddressLocation(); @@ -660,7 +666,7 @@ class AccompanyingPeriod implements * * @return DateTime */ - public function getOpeningDate() + public function getOpeningDate(): ?\DateTime { return $this->openingDate; } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php index 7d35b8684..09ebc7d31 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php @@ -33,26 +33,26 @@ class Origin * @ORM\Column(type="integer") * @Groups({"read"}) */ - private $id; + private ?int $id = null; /** * @ORM\Column(type="json") * @Groups({"read"}) */ - private $label; + private array $label = []; /** * @ORM\Column(type="date_immutable", nullable=true) * @Groups({"read"}) */ - private $noActiveAfter; + private ?\DateTimeImmutable $noActiveAfter = null; public function getId(): ?int { return $this->id; } - public function getLabel() + public function getLabel(): array { return $this->label; } @@ -62,7 +62,7 @@ class Origin return $this->noActiveAfter; } - public function setLabel(string $label): self + public function setLabel(array $label): self { $this->label = $label; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php new file mode 100644 index 000000000..d3243c48b --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php @@ -0,0 +1,147 @@ + "", + 'closingDate' => \DateTime::class, + 'confidential' => "", + 'confidentialText' => '', + 'createdAt' => \DateTime::class, + 'createdBy' => User::class, + 'emergency' => "", + 'emergencyText' => '', + 'openingDate' => \DateTime::class, + 'originText' => '', + 'requestorAnonymous' => false, + 'socialIssues' => [], + 'intensity' => '', + 'step' => '', + 'closingMotiveText' => '', + 'socialIssuesText' => '', + 'scopes' => [], + 'scopesText' => '', + 'ref' => User::class, + 'participations' => [], + ]; + + public function __construct( + TranslatorInterface $translator, + TranslatableStringHelper $translatableStringHelper, + SocialIssueRender $socialIssueRender, + ClosingMotiveRender $closingMotiveRender, + ScopeResolverDispatcher $scopeResolverDispatcher + ) { + $this->translator = $translator; + $this->translatableStringHelper = $translatableStringHelper; + $this->socialIssueRender = $socialIssueRender; + $this->closingMotiveRender = $closingMotiveRender; + $this->scopeResolverDispatcher = $scopeResolverDispatcher; + } + + + public function supportsNormalization($data, string $format = null, array $context = []): bool + { + if ('docgen' !== $format) { + return false; + } + + if ($data instanceof AccompanyingPeriod) { + if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context) + && in_array(spl_object_hash($data), $context[self::IGNORE_FIRST_PASS_KEY])) { + return false; + } + + return true; + } elseif (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriod::class) { + return true; + } + + return false; + } + + /** + * @param AccompanyingPeriod|null $period + */ + public function normalize($period, string $format = null, array $context = []) + { + if ($period instanceof AccompanyingPeriod) { + $ignored = $context[self::IGNORE_FIRST_PASS_KEY] ?? []; + $ignored[] = spl_object_hash($period); + $initial = + $this->normalizer->normalize($period, $format, \array_merge($context, + [self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read'])); + + // some transformation + $user = $initial['user']; + unset($initial['user']); + + $scopes = $this->scopeResolverDispatcher->isConcerned($period) ? $this->scopeResolverDispatcher->resolveScope($period) : []; + + if (!is_array($scopes)) { + $scopes = [$scopes]; + } + + return array_merge( + // get a first default data + $initial, + // and add data custom + [ + 'intensity' => $this->translator->trans($period->getIntensity()), + 'step' => $this->translator->trans('accompanying_period.'.$period->getStep()), + 'emergencyText' => $period->isEmergency() ? $this->translator->trans('accompanying_period.emergency') : '', + 'confidentialText' => $period->isConfidential() ? $this->translator->trans('confidential') : '', + 'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '', + 'closingMotiveText' => null !== $period->getClosingMotive() ? + $this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '', + 'ref' => $user, + 'socialIssuesText' => implode(', ', array_map(function(SocialIssue $s) { + return $this->socialIssueRender->renderString($s, []); + }, $period->getSocialIssues()->toArray())), + 'scopesText' => implode(', ', array_map(function (Scope $s) { + return $this->translatableStringHelper->localize($s->getName()); + }, $scopes)), + 'scopes' => $scopes, + ] + ); + } elseif (null === $period) { + return self::PERIOD_NULL; + } + + throw new InvalidArgumentException("this neither an accompanying period or null"); + } +} diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 2f286351e..2a3fb10f4 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -1,10 +1,18 @@ normalizer = self::$container->get(NormalizerInterface::class); + } + + public function testNormalize() + { + $period = new AccompanyingPeriod(); + $period->setConfidential(true); + $period->setEmergency(true); + $period->setOrigin((new AccompanyingPeriod\Origin())->setLabel(['fr' => 'origin'])); + $period->setClosingMotive((new AccompanyingPeriod\ClosingMotive())->setName(['closing'])); + $period->addScope((new Scope())->setName(['fr' => 'scope1'])); + $period->addScope((new Scope())->setName(['fr' =>'scope2'])); + $period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue1'])); + $period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue2'])); + $data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + $expected = [ + 'id' => null, + 'closingDate' => '@ignored', + 'confidential' => true, + 'confidentialText' => 'confidentiel', + 'createdAt' => '@ignored', + 'createdBy' => '@ignored', + 'emergency' => true, + 'emergencyText' => 'Urgent', + 'openingDate' => '@ignored', + 'originText' => 'origin', + 'requestorAnonymous' => false, + 'socialIssues' => '@ignored', + 'intensity' => 'ponctuel', + 'step' => 'Brouillon', + 'closingMotiveText' => 'closing', + 'socialIssuesText' => 'issue1, issue2', + 'scopes' => '@ignored', + 'scopesText' => 'scope1, scope2', + 'ref' => '@ignored', + 'participations' => '@ignored', + ]; + + $this->assertIsArray($data); + $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data)); + + foreach ($expected as $key => $item) { + if ($item === '@ignored') { + continue; + } + + $this->assertEquals($item, $data[$key]); + } + } + + public function testNormalizeNull() + { + $data = $this->normalizer->normalize(null, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + $expected = [ + 'id' => "", + 'closingDate' => '@ignored', + 'confidential' => "", + 'confidentialText' => '', + 'createdAt' => '@ignored', + 'createdBy' => '@ignored', + 'emergency' => "", + 'emergencyText' => '', + 'openingDate' => '@ignored', + 'originText' => '', + 'requestorAnonymous' => '', + 'socialIssues' => '@ignored', + 'intensity' => '', + 'step' => '', + 'closingMotiveText' => '', + 'socialIssuesText' => '', + 'scopes' => '@ignored', + 'scopesText' => '', + 'ref' => '@ignored', + 'participations' => '@ignored', + ]; + + $this->assertIsArray($data); + $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data)); + + foreach ($expected as $key => $item) { + if ($item === '@ignored') { + continue; + } + + $this->assertEquals($item, $data[$key]); + } + } + +} diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 46077fdf0..904ec8811 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -377,9 +377,14 @@ accompanying_period: dates: Période dates_from_%opening_date%: Ouvert depuis le %opening_date% dates_from_%opening_date%_to_%closing_date%: Ouvert du %opening_date% au %closing_date% + DRAFT: Brouillon + CONFIRMED: Confirmé + CLOSED: Clotûré + emergency: Urgent occasional: ponctuel regular: régulier Confidential: confidentiel +confidential: confidentiel Draft: brouillon Confirmed: en file active Closed: Cloturé From 9f43c99accc3d98a1d70d411504a4b5e27fe5a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 1 Dec 2021 15:39:44 +0100 Subject: [PATCH 18/41] handle finding returnType on property type on parent classes --- .../Normalizer/DocGenObjectNormalizer.php | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index 040738571..b57577848 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -86,19 +86,34 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte private function getExpectedType(AttributeMetadata $attribute, ReflectionClass $reflection): string { - // we have to get the expected content - if ($reflection->hasProperty($attribute->getName())) { - $type = $reflection->getProperty($attribute->getName())->getType(); - } elseif ($reflection->hasMethod($attribute->getName())) { - $type = $reflection->getMethod($attribute->getName())->getReturnType(); - } else { - throw new \LogicException(sprintf( - 'Could not determine how the content is determined for the attribute %s. Add attribute property only on property or method', - $attribute->getName() - )); - } + $type = null; - if (null === $type) { + do { + // we have to get the expected content + if ($reflection->hasProperty($attribute->getName())) { + if (!$reflection->getProperty($attribute->getName())->hasType()) { + throw new \LogicException(sprintf( + 'Could not determine how the content is determined for the attribute %s. Add a type on this property', + $attribute->getName() + )); + } + + $type = $reflection->getProperty($attribute->getName())->getType(); + } elseif ($reflection->hasMethod($attribute->getName())) { + if (!$reflection->getMethod($attribute->getName())->hasReturnType()) { + throw new \LogicException(sprintf( + 'Could not determine how the content is determined for the attribute %s. Add a return type on the method', + $attribute->getName() + )); + } + + $type = $reflection->getMethod($attribute->getName())->getReturnType(); + } else { + $reflection = $reflection->getParentClass(); + } + } while (null === $type && $reflection instanceof ReflectionClass); + + if (null === $type ?? null) { throw new \LogicException(sprintf( 'Could not determine the type for this attribute: %s. Add a return type to the method or property declaration', $attribute->getName() From 9d0e1a82e71f96129671e00c95382e3eab96fe26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 1 Dec 2021 15:41:59 +0100 Subject: [PATCH 19/41] add deps to base64 --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 254486913..6e18ea69a 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "phpoffice/phpspreadsheet": "^1.16", "ramsey/uuid-doctrine": "^1.7", "sensio/framework-extra-bundle": "^5.5", + "spomky-labs/base64url": "^2.0", "symfony/asset": "4.*", "symfony/browser-kit": "^5.2", "symfony/css-selector": "^5.2", From 7719d2b073bf578f38123c577fb43d51241814b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 1 Dec 2021 15:43:34 +0100 Subject: [PATCH 20/41] full generation for accompanying period --- .../Context/ContextManager.php | 22 +++- .../Context/DocGeneratorContextInterface.php | 5 + .../Exception/ContextNotFoundException.php | 22 ++++ .../HouseholdMemberSelectionContext.php | 24 ++++ .../DocGeneratorTemplateController.php | 74 +++++++----- .../Form/DocGeneratorTemplateType.php | 2 +- .../index.html.twig | 2 +- .../AccompanyingCourseDocument/show.html.twig | 6 +- .../Templating/TranslatableStringHelper.php | 4 +- .../Entity/AccompanyingPeriod.php | 15 +-- .../Entity/AccompanyingPeriod/Origin.php | 8 +- .../AccompanyingPeriodDocGenNormalizer.php | 107 ++++++++++-------- .../AccompanyingPeriodContext.php | 36 +++++- ...AccompanyingPeriodDocGenNormalizerTest.php | 29 +++-- 14 files changed, 248 insertions(+), 108 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/Exception/ContextNotFoundException.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php index a2eb3742c..78b59fdc6 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php @@ -11,8 +11,14 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Context; +use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; +use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; + class ContextManager { + /** + * @var DocGeneratorContextInterface[]|iterable + */ private iterable $contexts; public function __construct(iterable $contexts) @@ -20,7 +26,21 @@ class ContextManager $this->contexts = $contexts; } - public function getContext(): array + /** + * @throw ContextNotFoundException when the context is not found + */ + public function getContextByDocGeneratorTemplate(DocGeneratorTemplate $docGeneratorTemplate): DocGeneratorContextInterface + { + foreach ($this->contexts as $key => $context) { + if ($docGeneratorTemplate->getContext() === $key) { + return $context; + } + } + + throw new ContextNotFoundException($docGeneratorTemplate->getContext()); + } + + public function getContexts(): array { return iterator_to_array($this->contexts); } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index 7118b8eab..f70aa0abf 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -11,6 +11,9 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Context; +use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocStoreBundle\Entity\StoredObject; + /** * Interface for context for for document generation. */ @@ -39,6 +42,8 @@ interface DocGeneratorContextInterface */ public function hasForm(): bool; + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void; + /** * True of false which entity supports. */ diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/Exception/ContextNotFoundException.php b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/ContextNotFoundException.php new file mode 100644 index 000000000..d4bbe856a --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/ContextNotFoundException.php @@ -0,0 +1,22 @@ +em = $em; + } + /** * Get the data that will be injected to the generated document. * @@ -107,6 +118,19 @@ class HouseholdMemberSelectionContext implements DocGeneratorContextInterface return true; } + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void + { + // Only for evaluation + if ($entity instanceof AccompanyingPeriodWorkEvaluation) { + $doc = new AccompanyingPeriodWorkEvaluationDocument(); + $doc + ->setStoredObject($storedObject) + ->setTemplate($template); + $entity->addDocument($doc); + $this->em->persist($doc); + } + } + /** * True of false which entity supports. */ diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index a0a50fb35..2a048d421 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -13,25 +13,24 @@ namespace Chill\DocGeneratorBundle\Controller; use Base64Url\Base64Url; use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; -use Chill\DocGeneratorBundle\Context\HouseholdMemberSelectionContext; +use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; -use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; - -use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; - use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; use PhpOffice\PhpWord\TemplateProcessor; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // TODO à mettre dans services +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; @@ -42,20 +41,32 @@ final class DocGeneratorTemplateController extends AbstractController { private HttpClientInterface $client; + private ContextManager $contextManager; + private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; private KernelInterface $kernel; + private LoggerInterface $logger; + private PaginatorFactory $paginatorFactory; + private TempUrlGeneratorInterface $tempUrlGenerator; + public function __construct( + ContextManager $contextManager, DocGeneratorTemplateRepository $docGeneratorTemplateRepository, + LoggerInterface $logger, PaginatorFactory $paginatorFactory, + TempUrlGeneratorInterface $tempUrlGenerator, KernelInterface $kernel, HttpClientInterface $client ) { + $this->contextManager = $contextManager; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->logger = $logger; $this->paginatorFactory = $paginatorFactory; + $this->tempUrlGenerator = $tempUrlGenerator; $this->kernel = $kernel; $this->client = $client; } @@ -67,13 +78,12 @@ final class DocGeneratorTemplateController extends AbstractController * ) */ public function generateDocFromTemplateAction( - TempUrlGeneratorInterface $tempUrlGenerator, DocGeneratorTemplate $template, string $entityClassName, int $entityId, Request $request ): Response { - $getUrlGen = $tempUrlGenerator->generate( + $getUrlGen = $this->tempUrlGenerator->generate( 'GET', $template->getFile()->getFilename() ); @@ -102,25 +112,29 @@ final class DocGeneratorTemplateController extends AbstractController exit; } - if (fwrite($handle, $dataDecrypted) === false) { + if (false === $ftemplate = fwrite($handle, $dataDecrypted)) { echo "Cannot write to file ({$tmpfnameDeCrypted})"; exit; } - dump("Success, wrote ({$dataDecrypted}) to file ({$tmpfnameDeCrypted})"); + dump("Success, wrote (to file ({$tmpfnameDeCrypted})"); fclose($handle); $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); - if ($template->getContext() === HouseholdMemberSelectionContext::class) { - $context = new HouseholdMemberSelectionContext(); - $datas = $context->getData($entity); - } else { - throw new \Exception('Not implemented', 1); + try { + $context = $this->contextManager->getContextByDocGeneratorTemplate($template); + } catch (ContextNotFoundException $e) { + throw new NotFoundHttpException($e->getMessage(), $e); } + $datas = $context->getData($entity); + + dump('process the data', $datas); + + /* $templateProcessor = new TemplateProcessor($tmpfnameDeCrypted); foreach ($datas['setValues'] as $setValuesConf) { @@ -130,28 +144,32 @@ final class DocGeneratorTemplateController extends AbstractController foreach ($datas['cloneRowAndSetValues'] as $cloneRowAndSetValues) { $templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]); } - $tmpfnameGenerated = tempnam($this->kernel->getCacheDir(), 'DOC_GENERATED'); - $templateProcessor->saveAs($tmpfnameGenerated); $fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content + */ $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx'; - $getUrlGen = $tempUrlGenerator->generate( + $getUrlGen = $this->tempUrlGenerator->generate( 'PUT', $genDocName ); unlink($tmpfnameDeCrypted); - unlink($tmpfnameGenerated); + //unlink($tmpfnameGenerated); $client = new Client(); try { + /* $putResponse = $client->request('PUT', $getUrlGen->url, [ 'body' => $fileContent, ]); + */ + $putResponse = $client->request('PUT', $getUrlGen->url, [ + 'body' => $ftemplate, + ]); if ($putResponse->getStatusCode() === 201) { $em = $this->getDoctrine()->getManager(); @@ -163,14 +181,16 @@ final class DocGeneratorTemplateController extends AbstractController $em->persist($storedObject); - // Only for evaluation - if ($entity instanceof AccompanyingPeriodWorkEvaluation) { - $doc = new AccompanyingPeriodWorkEvaluationDocument(); - $doc - ->setStoredObject($storedObject) - ->setTemplate($template); - $entity->addDocument($doc); - $em->persist($doc); + try { + $context->storeGenerated($template, $storedObject, $entity); + } catch (\Exception $e) { + $this->logger->error('Could not store the associated document to entity', [ + 'entityClassName' => $entityClassName, + 'entityId' => $entityId, + 'contextKey' => $context->getName(), + ]); + + throw $e; } $em->flush(); @@ -184,7 +204,7 @@ final class DocGeneratorTemplateController extends AbstractController throw $e; } - throw new Exception('Unable to generate document.'); + throw new \Exception('Unable to generate document.'); } /** diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index f453ffa35..e661c6d9e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -35,7 +35,7 @@ class DocGeneratorTemplateType extends AbstractType { $contexts = array_flip(array_map(static function (DocGeneratorContextInterface $c) { return $c->getName(); - }, $this->contextManager->getContext())); + }, $this->contextManager->getContexts())); $builder ->add('name', TranslatableStringFormType::class, [ diff --git a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig index ca6869bff..893f504bc 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig +++ b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/index.html.twig @@ -36,7 +36,7 @@ {% for document in documents %} {{ document.title }} - {{ document.category.name|localize_translatable_string }} + {% if document.category %}{{ document.category.name|localize_translatable_string }}{% endif %}
      {% if is_granted('CHILL_ACCOMPANYING_COURSE_DOCUMENT_SEE_DETAILS', document) %} diff --git a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/show.html.twig b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/show.html.twig index 3bf9cac43..90bb6289d 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/show.html.twig +++ b/src/Bundle/ChillDocStoreBundle/Resources/views/AccompanyingCourseDocument/show.html.twig @@ -22,8 +22,10 @@
      {{ 'Title'|trans }}
      {{ document.title }}
      -
      {{ 'Category'|trans }}
      -
      {{ document.category.name|localize_translatable_string }}
      + {% if document.category is not null %} +
      {{ 'Category'|trans }}
      +
      {{ document.category.name|localize_translatable_string }}
      + {% endif %}
      {{ 'Description' | trans }}
      diff --git a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php index e354fe9f0..88bb8f89d 100644 --- a/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php +++ b/src/Bundle/ChillMainBundle/Templating/TranslatableStringHelper.php @@ -18,12 +18,12 @@ use function array_key_exists; final class TranslatableStringHelper implements TranslatableStringHelperInterface { + private string $defaultLocale; + private RequestStack $requestStack; private TranslatorInterface $translator; - private string $defaultLocale; - public function __construct(RequestStack $requestStack, TranslatorInterface $translator, ParameterBagInterface $parameterBag) { $this->requestStack = $requestStack; diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index d3595932c..92d84610f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -135,8 +135,6 @@ class AccompanyingPeriod implements private ?ClosingMotive $closingMotive = null; /** - * @var Collection - * * @ORM\OneToMany(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Comment", * mappedBy="accompanyingPeriod", * cascade={"persist", "remove"}, @@ -147,7 +145,6 @@ class AccompanyingPeriod implements private Collection $comments; /** - * @var bool * @ORM\Column(type="boolean", options={"default": false}) * @Groups({"read", "write", "docgen:read"}) */ @@ -167,7 +164,6 @@ class AccompanyingPeriod implements private ?User $createdBy = null; /** - * @var bool * @ORM\Column(type="boolean", options={"default": false}) * @Groups({"read", "write", "docgen:read"}) */ @@ -216,8 +212,6 @@ class AccompanyingPeriod implements private ?Origin $origin = null; /** - * @var Collection - * * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class, * mappedBy="accompanyingPeriod", orphanRemoval=true, * cascade={"persist", "refresh", "remove", "merge", "detach"}) @@ -235,15 +229,12 @@ class AccompanyingPeriod implements private ?Person $personLocation = null; /** - * @var string - * * @ORM\Column(type="text") * @Groups({"read", "write"}) */ private string $remark = ''; /** - * @var bool * @ORM\Column(type="boolean", options={"default": false}) * @Groups({"read", "write", "docgen:read"}) */ @@ -262,8 +253,6 @@ class AccompanyingPeriod implements private ?ThirdParty $requestorThirdParty = null; /** - * @var Collection - * * @ORM\OneToMany( * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Resource", * mappedBy="accompanyingPeriod", @@ -276,7 +265,6 @@ class AccompanyingPeriod implements private Collection $resources; /** - * @var Collection * @ORM\ManyToMany( * targetEntity=Scope::class, * cascade={} @@ -304,7 +292,6 @@ class AccompanyingPeriod implements private Collection $socialIssues; /** - * @var string * @ORM\Column(type="string", length=32, nullable=true) * @Groups({"read"}) */ @@ -666,7 +653,7 @@ class AccompanyingPeriod implements * * @return DateTime */ - public function getOpeningDate(): ?\DateTime + public function getOpeningDate(): ?DateTime { return $this->openingDate; } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php index 09ebc7d31..afcfc4228 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php @@ -39,21 +39,23 @@ class Origin * @ORM\Column(type="json") * @Groups({"read"}) */ - private array $label = []; + private $label; /** * @ORM\Column(type="date_immutable", nullable=true) * @Groups({"read"}) */ - private ?\DateTimeImmutable $noActiveAfter = null; + private ?DateTimeImmutable $noActiveAfter = null; public function getId(): ?int { return $this->id; } - public function getLabel(): array + public function getLabel() { + dump($this->label); + return $this->label; } diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php index d3243c48b..473f6d75d 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizer.php @@ -1,5 +1,14 @@ "", - 'closingDate' => \DateTime::class, - 'confidential' => "", + 'id' => '', + 'closingDate' => DateTime::class, + 'confidential' => '', 'confidentialText' => '', - 'createdAt' => \DateTime::class, + 'createdAt' => DateTime::class, 'createdBy' => User::class, - 'emergency' => "", + 'emergency' => '', 'emergencyText' => '', - 'openingDate' => \DateTime::class, + 'openingDate' => DateTime::class, 'originText' => '', 'requestorAnonymous' => false, 'socialIssues' => [], @@ -59,6 +59,16 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf 'participations' => [], ]; + private ClosingMotiveRender $closingMotiveRender; + + private ScopeResolverDispatcher $scopeResolverDispatcher; + + private SocialIssueRender $socialIssueRender; + + private TranslatableStringHelper $translatableStringHelper; + + private TranslatorInterface $translator; + public function __construct( TranslatorInterface $translator, TranslatableStringHelper $translatableStringHelper, @@ -73,38 +83,19 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf $this->scopeResolverDispatcher = $scopeResolverDispatcher; } - - public function supportsNormalization($data, string $format = null, array $context = []): bool - { - if ('docgen' !== $format) { - return false; - } - - if ($data instanceof AccompanyingPeriod) { - if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context) - && in_array(spl_object_hash($data), $context[self::IGNORE_FIRST_PASS_KEY])) { - return false; - } - - return true; - } elseif (null === $data && ($context['docgen:expects'] ?? null) === AccompanyingPeriod::class) { - return true; - } - - return false; - } - /** * @param AccompanyingPeriod|null $period */ - public function normalize($period, string $format = null, array $context = []) + public function normalize($period, ?string $format = null, array $context = []) { if ($period instanceof AccompanyingPeriod) { $ignored = $context[self::IGNORE_FIRST_PASS_KEY] ?? []; $ignored[] = spl_object_hash($period); $initial = - $this->normalizer->normalize($period, $format, \array_merge($context, - [self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read'])); + $this->normalizer->normalize($period, $format, array_merge( + $context, + [self::IGNORE_FIRST_PASS_KEY => $ignored, AbstractNormalizer::GROUPS => 'docgen:read'] + )); // some transformation $user = $initial['user']; @@ -122,14 +113,14 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf // and add data custom [ 'intensity' => $this->translator->trans($period->getIntensity()), - 'step' => $this->translator->trans('accompanying_period.'.$period->getStep()), + 'step' => $this->translator->trans('accompanying_period.' . $period->getStep()), 'emergencyText' => $period->isEmergency() ? $this->translator->trans('accompanying_period.emergency') : '', 'confidentialText' => $period->isConfidential() ? $this->translator->trans('confidential') : '', - 'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '', + //'originText' => null !== $period->getOrigin() ? $this->translatableStringHelper->localize($period->getOrigin()->getLabel()) : '', 'closingMotiveText' => null !== $period->getClosingMotive() ? $this->closingMotiveRender->renderString($period->getClosingMotive(), []) : '', 'ref' => $user, - 'socialIssuesText' => implode(', ', array_map(function(SocialIssue $s) { + 'socialIssuesText' => implode(', ', array_map(function (SocialIssue $s) { return $this->socialIssueRender->renderString($s, []); }, $period->getSocialIssues()->toArray())), 'scopesText' => implode(', ', array_map(function (Scope $s) { @@ -142,6 +133,28 @@ class AccompanyingPeriodDocGenNormalizer implements ContextAwareNormalizerInterf return self::PERIOD_NULL; } - throw new InvalidArgumentException("this neither an accompanying period or null"); + throw new InvalidArgumentException('this neither an accompanying period or null'); + } + + public function supportsNormalization($data, ?string $format = null, array $context = []): bool + { + if ('docgen' !== $format) { + return false; + } + + if ($data instanceof AccompanyingPeriod) { + if (array_key_exists(self::IGNORE_FIRST_PASS_KEY, $context) + && in_array(spl_object_hash($data), $context[self::IGNORE_FIRST_PASS_KEY], true)) { + return false; + } + + return true; + } + + if (null === $data && AccompanyingPeriod::class === ($context['docgen:expects'] ?? null)) { + return true; + } + + return false; } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 2a3fb10f4..9c7ee70a6 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -13,12 +13,32 @@ namespace Chill\PersonBundle\Service\DocGenerator; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; +use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; +use Chill\DocStoreBundle\Entity\StoredObject; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use DateTime; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class AccompanyingPeriodContext implements DocGeneratorContextInterface { - public NormalizerInterface $normalizer; + private EntityManagerInterface $em; + + private NormalizerInterface $normalizer; + + private TranslatableStringHelperInterface $translatableStringHelper; + + public function __construct( + NormalizerInterface $normalizer, + TranslatableStringHelperInterface $translatableStringHelper, + EntityManagerInterface $em + ) { + $this->normalizer = $normalizer; + $this->translatableStringHelper = $translatableStringHelper; + $this->em = $em; + } public function getData($entity): array { @@ -49,6 +69,20 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface return false; } + /** + * @param AccompanyingPeriod $entity + */ + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void + { + $doc = new AccompanyingCourseDocument(); + $doc->setTitle($this->translatableStringHelper->localize($template->getName())) + ->setDate(new DateTime()) + ->setDescription($this->translatableStringHelper->localize($template->getName())) + ->setCourse($entity) + ->setObject($storedObject); + $this->em->persist($doc); + } + public function supports(string $entityClass): bool { return AccompanyingPeriod::class === $entityClass; diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php index 5f5598a37..4c08db326 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php @@ -1,15 +1,27 @@ setOrigin((new AccompanyingPeriod\Origin())->setLabel(['fr' => 'origin'])); $period->setClosingMotive((new AccompanyingPeriod\ClosingMotive())->setName(['closing'])); $period->addScope((new Scope())->setName(['fr' => 'scope1'])); - $period->addScope((new Scope())->setName(['fr' =>'scope2'])); + $period->addScope((new Scope())->setName(['fr' => 'scope2'])); $period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue1'])); $period->addSocialIssue((new SocialIssue())->setTitle(['fr' => 'issue2'])); $data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); @@ -59,7 +71,7 @@ class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data)); foreach ($expected as $key => $item) { - if ($item === '@ignored') { + if ('@ignored' === $item) { continue; } @@ -72,13 +84,13 @@ class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase $data = $this->normalizer->normalize(null, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); $expected = [ - 'id' => "", + 'id' => '', 'closingDate' => '@ignored', - 'confidential' => "", + 'confidential' => '', 'confidentialText' => '', 'createdAt' => '@ignored', 'createdBy' => '@ignored', - 'emergency' => "", + 'emergency' => '', 'emergencyText' => '', 'openingDate' => '@ignored', 'originText' => '', @@ -98,12 +110,11 @@ class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($data)); foreach ($expected as $key => $item) { - if ($item === '@ignored') { + if ('@ignored' === $item) { continue; } $this->assertEquals($item, $data[$key]); } } - } From 475b40e896e0863ccb7292068893622e501dbec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 1 Dec 2021 23:00:02 +0100 Subject: [PATCH 21/41] add form to doc generation and custom form to admin template configuration --- .../Context/ContextManager.php | 11 ++ .../Context/DocGeneratorContextInterface.php | 28 ++-- .../HouseholdMemberSelectionContext.php | 2 +- .../AdminDocGeneratorTemplateController.php | 47 ++++++ .../DocGeneratorTemplateController.php | 50 ++++-- .../Entity/DocGeneratorTemplate.php | 73 ++++++--- .../Form/DocGeneratorTemplateType.php | 37 +++-- .../DocGeneratorTemplateRepository.php | 8 +- .../pick-context.html.twig | 27 ++++ .../views/Generator/basic_form.html.twig | 21 +++ .../migrations/Version20211201191757.php | 34 +++++ .../Entity/DocumentCategory.php | 2 +- .../DocumentCategoryRepository.php | 31 ---- .../Repository/DocumentCategoryRepository.php | 79 ++++++++++ .../AccompanyingPeriodContext.php | 142 ++++++++++++++++-- 15 files changed, 484 insertions(+), 108 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig create mode 100644 src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php delete mode 100644 src/Bundle/ChillDocStoreBundle/EntityRepository/DocumentCategoryRepository.php create mode 100644 src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php index 78b59fdc6..8dfecb730 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php @@ -40,6 +40,17 @@ class ContextManager throw new ContextNotFoundException($docGeneratorTemplate->getContext()); } + public function getContextByKey(string $searchedKey): DocGeneratorContextInterface + { + foreach ($this->contexts as $key => $context) { + if ($searchedKey === $key) { + return $context; + } + } + + throw new ContextNotFoundException($searchedKey); + } + public function getContexts(): array { return iterator_to_array($this->contexts); diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index f70aa0abf..d9faeeb3f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -13,9 +13,10 @@ namespace Chill\DocGeneratorBundle\Context; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\StoredObject; +use Symfony\Component\Form\FormBuilderInterface; /** - * Interface for context for for document generation. + * Interface for context for document generation. */ interface DocGeneratorContextInterface { @@ -24,28 +25,37 @@ interface DocGeneratorContextInterface * * @param mixed $entity */ - public function getData($entity): array; + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array; /** * Generate the form that display. * * @param mixed $entity */ - public function getForm($entity); + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; public static function getKey(): string; public function getName(): string; + public function getDescription(): string; + /** * has form. + * + * @param mixed $entity */ - public function hasForm(): bool; + public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void; + public function hasAdminForm(): bool; - /** - * True of false which entity supports. - */ - public function supports(string $entityClass): bool; + public function buildAdminForm(FormBuilderInterface $builder): void; + + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void; + + public function getEntityClass(): string; + + public function adminFormTransform(array $data): array; + + public function adminFormReverseTransform(array $data): array; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php b/src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php index a9734d9cf..2197995a0 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php @@ -25,7 +25,7 @@ use function get_class; /** * Context that display a form to select a member of a houseHold. */ -class HouseholdMemberSelectionContext implements DocGeneratorContextInterface +class HouseholdMemberSelectionContext //implements DocGeneratorContextInterface { private EntityManagerInterface $em; diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index a7d24f127..aa0e34476 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -11,8 +11,55 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Controller; +use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\MainBundle\CRUD\Controller\CRUDController; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; class AdminDocGeneratorTemplateController extends CRUDController { + private ContextManager $contextManager; + + public function __construct(ContextManager $contextManager) + { + $this->contextManager = $contextManager; + } + + public function new(Request $request): Response + { + if (!$request->query->has('context')) { + return $this->redirectToRoute("chill_docgen_admin_template_pick-context"); + } + + return parent::new($request); + } + + protected function createEntity(string $action, Request $request): object + { + /** @var DocGeneratorTemplate $entity */ + $entity = parent::createEntity($action, $request); + $key = $request->query->get('context'); + $context = $this->contextManager->getContextByKey($key); + + $entity->setContext($key)->setEntity($context->getEntityClass()); + + return $entity; + } + + /** + * @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context") + * @param Request $request + * @return Response + */ + public function pickContext(Request $request): Response + { + $this->denyAccessUnlessGranted('ROLE_ADMIN'); + + return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [ + 'contexts' => $this->contextManager->getContexts() + ]); + } + } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 2a048d421..493bcb92e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -30,6 +30,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // TODO à mettre dans services +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Annotation\Route; @@ -83,6 +84,35 @@ final class DocGeneratorTemplateController extends AbstractController int $entityId, Request $request ): Response { + $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); + + if (null === $entity) { + throw new NotFoundHttpException("Entity with classname $entityClassName and id $entityId is not found"); + } + + try { + $context = $this->contextManager->getContextByDocGeneratorTemplate($template); + } catch (ContextNotFoundException $e) { + throw new NotFoundHttpException($e->getMessage(), $e); + } + + if ($context->hasPublicForm($template, $entity)) { + $builder = $this->createFormBuilder(); + $context->buildPublicForm($builder, $template, $entity); + $form = $builder->getForm()->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $contextGenerationData = $form->getData(); + } elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) { + $template = '@ChillDocGenerator/Generator/basic_form.html.twig'; + $templateOptions = ['entity' => $entity, 'form' => $form->createView(), 'template' => $template]; + + return $this->render($template, $templateOptions); + } + } else { + $contextGenerationData = []; + } + $getUrlGen = $this->tempUrlGenerator->generate( 'GET', $template->getFile()->getFilename() @@ -107,30 +137,22 @@ final class DocGeneratorTemplateController extends AbstractController $tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) { - echo "Cannot open file ({$tmpfnameDeCrypted})"; + $this->logger->error("Cannot open file ({$tmpfnameDeCrypted})"); - exit; + throw new HttpException(500); } if (false === $ftemplate = fwrite($handle, $dataDecrypted)) { - echo "Cannot write to file ({$tmpfnameDeCrypted})"; + $this->logger->error("Cannot write to file ({$tmpfnameDeCrypted})"); - exit; + throw new HttpException(500); } dump("Success, wrote (to file ({$tmpfnameDeCrypted})"); fclose($handle); - $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); - - try { - $context = $this->contextManager->getContextByDocGeneratorTemplate($template); - } catch (ContextNotFoundException $e) { - throw new NotFoundHttpException($e->getMessage(), $e); - } - - $datas = $context->getData($entity); + $datas = $context->getData($template, $entity, $contextGenerationData); dump('process the data', $datas); @@ -182,7 +204,7 @@ final class DocGeneratorTemplateController extends AbstractController $em->persist($storedObject); try { - $context->storeGenerated($template, $storedObject, $entity); + $context->storeGenerated($template, $storedObject, $entity, $contextGenerationData); } catch (\Exception $e) { $this->logger->error('Could not store the associated document to entity', [ 'entityClassName' => $entityClassName, diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index 93be974ad..61e604757 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -24,6 +24,11 @@ use Symfony\Component\Serializer\Annotation as Serializer; */ class DocGeneratorTemplate { + /** + * @ORM\Column(type="boolean", options={"default":true}) + */ + private bool $active = true; + /** * Class name of the context to use. * @@ -38,20 +43,24 @@ class DocGeneratorTemplate * @ORM\Column(type="text", nullable=true) * @Serializer\Groups({"read"}) */ - private string $description; + private ?string $description = null; /** - * Class name of the entities for which this template can be used. + * Class name of the entity for which this template can be used. * - * so if $entities = ['Chill\PersonBundle\Entity\AccompanyingPeriod', 'Chill\PersonBundle\Entity\SocialWork\SocialAction'] - * this template can be selected for an AccompanyingPeriod or a SocialAction - * - * @ORM\Column(type="simple_array") + * @ORM\Column(type="string", options={"default":""}) */ - private array $entities = []; + private string $entity = ""; /** - * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"}).) + * Options for the template + * + * @ORM\Column(type="json", name="template_options", options={"default":"[]"}) + */ + private array $options = []; + + /** + * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"})) */ private ?StoredObject $file = null; @@ -69,6 +78,18 @@ class DocGeneratorTemplate */ private array $name = []; + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): DocGeneratorTemplate + { + $this->active = $active; + + return $this; + } + public function getContext(): ?string { return $this->context; @@ -79,11 +100,6 @@ class DocGeneratorTemplate return $this->description; } - public function getEntities(): ?array - { - return $this->entities; - } - public function getFile(): ?StoredObject { return $this->file; @@ -113,13 +129,6 @@ class DocGeneratorTemplate return $this; } - public function setEntities(array $entities): self - { - $this->entities = $entities; - - return $this; - } - public function setFile(StoredObject $file): self { $this->file = $file; @@ -133,4 +142,28 @@ class DocGeneratorTemplate return $this; } + + public function getEntity(): string + { + return $this->entity; + } + + public function setEntity(string $entity): self + { + $this->entity = $entity; + + return $this; + } + + public function getOptions(): array + { + return $this->options; + } + + public function setOptions(array $options): self + { + $this->options = $options; + + return $this; + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index e661c6d9e..474ac0571 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -18,6 +18,7 @@ use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -33,30 +34,34 @@ class DocGeneratorTemplateType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - $contexts = array_flip(array_map(static function (DocGeneratorContextInterface $c) { - return $c->getName(); - }, $this->contextManager->getContexts())); + /** @var DocGeneratorTemplate $template */ + $template = $options['data']; + $context = $this->contextManager->getContextByKey($template->getContext()); $builder ->add('name', TranslatableStringFormType::class, [ 'label' => 'Nom', ]) - ->add('context', ChoiceType::class, [ - 'required' => true, - 'label' => 'Context', - 'choices' => $contexts, - ]) - ->add('entities', ChoiceType::class, [ - 'multiple' => true, - 'choices' => [ - 'AccompanyingPeriod' => 'Chill\PersonBundle\Entity\AccompanyingPeriod', - 'SocialWork\SocialAction' => 'Chill\PersonBundle\Entity\SocialWork\SocialAction', - 'AccompanyingPeriod\AccompanyingPeriodWorkEvaluation' => AccompanyingPeriodWorkEvaluation::class, - ], ]) ->add('description') ->add('file', StoredObjectType::class, [ 'error_bubbling' => true, - ]); + ]) + ; + + if ($context->hasAdminForm()) { + $sub = $builder + ->create('options', null, ['compound' => true]) + ->addModelTransformer(new CallbackTransformer( + function (array $data) use ($context) { + return $context->adminFormTransform($data); + }, + function (array $data) use ($context) { + return $context->adminFormReverseTransform($data); + } + )); + $context->buildAdminForm($sub); + $builder->add($sub); + } } public function configureOptions(OptionsResolver $resolver) diff --git a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php index 007f2fabb..5d99efa94 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php +++ b/src/Bundle/ChillDocGeneratorBundle/Repository/DocGeneratorTemplateRepository.php @@ -31,8 +31,8 @@ final class DocGeneratorTemplateRepository implements ObjectRepository $builder ->select('count(t)') - ->where('t.entities LIKE :entity') - ->setParameter('entity', '%' . addslashes($entity) . '%'); + ->where('t.entity LIKE :entity') + ->setParameter('entity', addslashes($entity)); return $builder->getQuery()->getSingleScalarResult(); } @@ -69,8 +69,8 @@ final class DocGeneratorTemplateRepository implements ObjectRepository $builder = $this->repository->createQueryBuilder('t'); $builder - ->where('t.entities LIKE :entity') - ->setParameter('entity', '%' . addslashes($entity) . '%'); + ->where('t.entity LIKE :entity') + ->setParameter('entity', addslashes($entity)); return $builder ->getQuery() diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig new file mode 100644 index 000000000..d43e88f8d --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig @@ -0,0 +1,27 @@ +{% extends '@ChillDocGenerator/Admin/layout.html.twig' %} + +{% block title 'Pick template context'|trans %} + +{% block layout_wvm_content %} +
      + +

      {{ block('title') }}

      +
      + {% for key, context in contexts %} +
      + +
      + {{ context.description|trans|nl2br }} +
      +
      + {% endfor %} +
      +
      + +{% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig new file mode 100644 index 000000000..58d4b059f --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig @@ -0,0 +1,21 @@ +{% extends 'ChillMainBundle::layout.html.twig' %} + +{% block title 'Generate document'|trans %} + +{% block content %} +
      +

      {{ block('title') }}

      + + {{ form_start(form, { 'attr': { 'id': 'generate_doc_form' }}) }} + {{ form(form) }} + {{ form_end(form) }} + +
        +
      • + +
      • +
      +
      +{% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php new file mode 100644 index 000000000..2ce380d69 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php @@ -0,0 +1,34 @@ +addSql('ALTER TABLE chill_docgen_template ADD active BOOLEAN DEFAULT true NOT NULL'); + $this->addSql('ALTER TABLE chill_docgen_template ADD entity VARCHAR(255) NOT NULL DEFAULT \'\''); + $this->addSql('ALTER TABLE chill_docgen_template ADD template_options JSONB NOT NULL DEFAULT \'[]\'::jsonb '); + $this->addSql('COMMENT ON COLUMN chill_docgen_template.template_options IS \'(DC2Type:json)\''); + $this->addSql('ALTER TABLE chill_docgen_template DROP entities'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_docgen_template DROP active'); + $this->addSql('ALTER TABLE chill_docgen_template DROP entity'); + $this->addSql('ALTER TABLE chill_docgen_template DROP template_options'); + $this->addSql('ALTER TABLE chill_docgen_template ADD entities TEXT'); + $this->addSql('COMMENT ON COLUMN chill_docgen_template.entities IS \'(DC2Type:simple_array)\''); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php b/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php index 1ded5ccb2..cfe6e9643 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php @@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table("chill_doc.document_category") - * @ORM\Entity(repositoryClass="Chill\DocStoreBundle\EntityRepository\DocumentCategoryRepository") + * @ORM\Entity() */ class DocumentCategory { diff --git a/src/Bundle/ChillDocStoreBundle/EntityRepository/DocumentCategoryRepository.php b/src/Bundle/ChillDocStoreBundle/EntityRepository/DocumentCategoryRepository.php deleted file mode 100644 index fbf4acb15..000000000 --- a/src/Bundle/ChillDocStoreBundle/EntityRepository/DocumentCategoryRepository.php +++ /dev/null @@ -1,31 +0,0 @@ -getEntityManager() - ->createQuery( - 'SELECT MAX(c.idInsideBundle) + 1 FROM ChillDocStoreBundle:DocumentCategory c' - ) - ->getSingleResult(); - - return reset($array_res); - } -} diff --git a/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php b/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php new file mode 100644 index 000000000..09ae5075a --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php @@ -0,0 +1,79 @@ +em = $em; + $this->repository = $em->getRepository(DocumentCategory::class); + } + + public function nextIdInsideBundle() + { + $array_res = $this->em + ->createQuery( + 'SELECT MAX(c.idInsideBundle) + 1 FROM ChillDocStoreBundle:DocumentCategory c' + ) + ->getSingleResult(); + + return reset($array_res); + } + + /** + * @return DocumentCategory|null + */ + public function find($id): ?DocumentCategory + { + return $this->repository->find($id); + } + + /** + * @return array|DocumentCategory[] + */ + public function findAll(): array + { + return $this->repository->findAll(); + } + + public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null) + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria): ?DocumentCategory + { + return $this->findOneBy($criteria); + } + + public function getClassName() + { + return DocumentCategory::class; + } + + +} diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 9c7ee70a6..31d237f1c 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -15,11 +15,21 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; +use Chill\DocStoreBundle\Entity\DocumentCategory; use Chill\DocStoreBundle\Entity\StoredObject; +use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; +use Chill\EventBundle\Entity\Participation; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; +use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Templating\Entity\PersonRender; use DateTime; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class AccompanyingPeriodContext implements DocGeneratorContextInterface @@ -30,28 +40,62 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface private TranslatableStringHelperInterface $translatableStringHelper; + private DocumentCategoryRepository $documentCategoryRepository; + + private PersonRender $personRender; + public function __construct( + DocumentCategoryRepository $documentCategoryRepository, NormalizerInterface $normalizer, TranslatableStringHelperInterface $translatableStringHelper, - EntityManagerInterface $em + EntityManagerInterface $em, + PersonRender $personRender ) { + $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; $this->translatableStringHelper = $translatableStringHelper; $this->em = $em; + $this->personRender = $personRender; } - public function getData($entity): array + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array { if (!$entity instanceof AccompanyingPeriod) { throw new UnexpectedTypeException($entity, AccompanyingPeriod::class); } + $options = $template->getOptions(); - return $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + foreach (['mainPerson', 'person1', 'person2'] as $k) { + if ($options[$k]) { + $data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', ['docgen:expects' => Person::class]); + } + } + + return $data; } - public function getForm($entity) + /** + * @param AccompanyingPeriod $entity + */ + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { - // TODO: Implement getForm() method. + $options = $template->getOptions(); + $persons = $entity->getCurrentParticipations()->map(function (AccompanyingPeriodParticipation $p) { return $p->getPerson(); }) + ->toArray(); + + foreach (['mainPerson', 'person1', 'person2'] as $key) { + if ($options[$key] ?? false) { + $builder->add($key, EntityType::class, [ + 'class' => Person::class, + 'choices' => $persons, + 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, + 'multiple' => false, + 'expanded' => true, + ]); + } + } } public static function getKey(): string @@ -61,30 +105,104 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface public function getName(): string { - return 'Accompanying Period'; + return 'Accompanying Period basic'; } - public function hasForm(): bool + public function getDescription(): string { - return false; + return "A basic context for accompanying period"; + } + + public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool + { + $options = $template->getOptions(); + + return $options['mainPerson'] || $options['person1'] || $options['person2']; } /** * @param AccompanyingPeriod $entity */ - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void { $doc = new AccompanyingCourseDocument(); $doc->setTitle($this->translatableStringHelper->localize($template->getName())) ->setDate(new DateTime()) ->setDescription($this->translatableStringHelper->localize($template->getName())) ->setCourse($entity) - ->setObject($storedObject); + ->setObject($storedObject) + ; + + if (array_key_exists('category', $template->getOptions()['category'])) { + $doc + ->setCategory($this->documentCategoryRepository->find( + $template->getOptions()['category']) + ); + } + $this->em->persist($doc); } - public function supports(string $entityClass): bool + public function hasAdminForm(): bool { - return AccompanyingPeriod::class === $entityClass; + return true; + } + + public function buildAdminForm(FormBuilderInterface $builder): void + { + $builder + ->add('mainPerson', CheckboxType::class, [ + 'required' => false, + ]) + ->add('person1', CheckboxType::class, [ + 'required' => false, + ]) + ->add('person2', CheckboxType::class, [ + 'required' => false, + ]) + ->add('category', EntityType::class, [ + 'placeholder' => 'Choose a document category', + 'class' => 'ChillDocStoreBundle:DocumentCategory', + 'query_builder' => static function (EntityRepository $er) { + return $er->createQueryBuilder('c') + ->where('c.documentClass = :docClass') + ->setParameter('docClass', AccompanyingCourseDocument::class); + }, + 'choice_label' => function ($entity = null) { + return $entity ? $this->translatableStringHelper->localize($entity->getName()) : ''; + }, + ]); + ; + } + + public function adminFormTransform(array $data): array + { + return [ + 'mainPerson' => $data['mainPerson'] ?? false, + 'person1' => $data['person1'] ?? false, + 'person2' => $data['person2'] ?? false, + 'category' => + array_key_exists('category', $data) ? + $this->documentCategoryRepository->find($data['category']) : null, + ]; + } + + public function adminFormReverseTransform(array $data): array + { + return [ + 'mainPerson' => $data['mainPerson'], + 'person1' => $data['person1'], + 'person2' => $data['person2'], + 'category' => [ + 'idInsideBundle' => $data['category']->getIdInsideBundle(), + 'bundleId' => $data['category']->getBundleId() + ], + ]; + } + + + public function getEntityClass(): string + { + return AccompanyingPeriod::class; } } From 2245f836319a150b162553d4baac5cc2652a1cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 2 Dec 2021 17:00:09 +0100 Subject: [PATCH 22/41] generate document with relatorio: config and driver --- .../DocGeneratorTemplateController.php | 56 ++++---------- .../ChillDocGeneratorExtension.php | 24 ++++++ .../DependencyInjection/Configuration.php | 35 ++++++--- .../GeneratorDriver/DriverInterface.php | 12 +++ .../GeneratorDriver/RelatorioDriver.php | 73 +++++++++++++++++++ .../config/services.yaml | 7 ++ .../public/module/async_upload/downloader.js | 44 +++++------ 7 files changed, 177 insertions(+), 74 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/RelatorioDriver.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 493bcb92e..a0768cdca 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -16,6 +16,7 @@ use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Pagination\PaginatorFactory; @@ -54,17 +55,22 @@ final class DocGeneratorTemplateController extends AbstractController private TempUrlGeneratorInterface $tempUrlGenerator; + private DriverInterface $driver; + public function __construct( ContextManager $contextManager, DocGeneratorTemplateRepository $docGeneratorTemplateRepository, + DriverInterface $driver, LoggerInterface $logger, PaginatorFactory $paginatorFactory, TempUrlGeneratorInterface $tempUrlGenerator, KernelInterface $kernel, HttpClientInterface $client + ) { $this->contextManager = $contextManager; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->driver = $driver; $this->logger = $logger; $this->paginatorFactory = $paginatorFactory; $this->tempUrlGenerator = $tempUrlGenerator; @@ -134,71 +140,41 @@ final class DocGeneratorTemplateController extends AbstractController throw new \Exception('Error during Decrypt ', 1); } - $tmpfnameDeCrypted = tempnam($this->kernel->getCacheDir(), 'DECRYPT_DOC_TEMPLATE'); // plus ou moins - - if (!$handle = fopen($tmpfnameDeCrypted, 'ab')) { - $this->logger->error("Cannot open file ({$tmpfnameDeCrypted})"); + if (false === $templateResource = fopen('php://memory', 'r+')) { + $this->logger->error("Could not write data to memory"); throw new HttpException(500); } - if (false === $ftemplate = fwrite($handle, $dataDecrypted)) { - $this->logger->error("Cannot write to file ({$tmpfnameDeCrypted})"); - - throw new HttpException(500); - } - - dump("Success, wrote (to file ({$tmpfnameDeCrypted})"); - - fclose($handle); + fwrite($templateResource, $dataDecrypted); + rewind($templateResource); $datas = $context->getData($template, $entity, $contextGenerationData); + dump('datas compiled', $datas); - dump('process the data', $datas); + $generatedResource = $this->driver->generateFromResource($templateResource, $template->getFile()->getType(), $datas, $template->getFile()->getFilename()); - /* - $templateProcessor = new TemplateProcessor($tmpfnameDeCrypted); + fclose($templateResource); - foreach ($datas['setValues'] as $setValuesConf) { - $templateProcessor->setValues($setValuesConf); - } - - foreach ($datas['cloneRowAndSetValues'] as $cloneRowAndSetValues) { - $templateProcessor->cloneRowAndSetValues($cloneRowAndSetValues[0], $cloneRowAndSetValues[1]); - } - $tmpfnameGenerated = tempnam($this->kernel->getCacheDir(), 'DOC_GENERATED'); - - $fileContent = fopen($tmpfnameGenerated, 'rb'); // the generated file content - */ - - $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . '.docx'; + $genDocName = 'doc_' . sprintf('%010d', mt_rand()).'odt'; $getUrlGen = $this->tempUrlGenerator->generate( 'PUT', $genDocName ); - unlink($tmpfnameDeCrypted); - //unlink($tmpfnameGenerated); - $client = new Client(); try { - /* $putResponse = $client->request('PUT', $getUrlGen->url, [ - 'body' => $fileContent, - ]); - */ - $putResponse = $client->request('PUT', $getUrlGen->url, [ - 'body' => $ftemplate, + 'body' => $generatedResource, ]); if ($putResponse->getStatusCode() === 201) { $em = $this->getDoctrine()->getManager(); $storedObject = new StoredObject(); $storedObject - // currently, only docx is supported - ->setType('application/vnd.openxmlformats-officedocument.wordprocessingml.document') + ->setType($template->getFile()->getType()) ->setFilename($genDocName); $em->persist($storedObject); diff --git a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php index 15fd49140..0622377f9 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php +++ b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php @@ -26,6 +26,10 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn { public function load(array $configs, ContainerBuilder $container) { + $configuration = new Configuration(); + $config = $this->processConfiguration($configuration, $configs); + $container->setParameter('chill_doc_generator', $config); + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../config')); $loader->load('services.yaml'); $loader->load('services/controller.yaml'); @@ -37,6 +41,26 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn { $this->preprendRoutes($container); $this->prependCruds($container); + $this->prependClientConfig($container); + } + + private function prependClientConfig(ContainerBuilder $container) + { + $configs = $container->getExtensionConfig($this->getAlias()); + $resolvingBag = $container->getParameterBag(); + $configs = $resolvingBag->resolveValue($configs); + + $config = $this->processConfiguration(new Configuration(), $configs); + + $container->prependExtensionConfig('framework', [ + 'http_client' => [ + 'scoped_clients' => [ + 'relatorio.client' => [ + 'scope' => $config['driver']['relatorio']['url'], + ] + ] + ] + ]); } protected function prependCruds(ContainerBuilder $container) diff --git a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php index 161886d87..678a871f7 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php @@ -14,21 +14,36 @@ namespace Chill\DocGeneratorBundle\DependencyInjection; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -/** - * This is the class that validates and merges configuration from your app/config files. - * - * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html} - */ class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { - $treeBuilder = new TreeBuilder('chill_calendar'); - $rootNode = $treeBuilder->getRootNode('chill_calendar'); + $treeBuilder = new TreeBuilder('chill_doc_generator'); + $rootNode = $treeBuilder->getRootNode(); - // Here you should define the parameters that are allowed to - // configure your bundle. See the documentation linked above for - // more information on that topic. + $rootNode + ->children() + ->arrayNode('driver') + ->addDefaultsIfNotSet() + ->children() + ->enumNode('type') + ->isRequired() + ->values(['relatorio']) + ->defaultValue('relatorio') + ->end() + ->arrayNode('relatorio') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('url') + ->isRequired() + ->defaultValue('http://relatorio:8888/') + ->end() + ->end() + ->end() + ->end() + ->end() + ->end() + ; return $treeBuilder; } diff --git a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php new file mode 100644 index 000000000..0e1828f3e --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php @@ -0,0 +1,12 @@ +relatorioClient = $relatorioClient; + $this->logger = $logger; + $this->url = $parameterBag->get('chill_doc_generator')['driver']['relatorio']['url']; + } + + public function generateFromResource($template, string $resourceType, array $data, string $templateName = null) + { + $formFields = [ + 'variables' => \json_encode($data), + 'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType), + ]; + $form = new FormDataPart($formFields); + + try { + $response = $this->relatorioClient->request('POST', $this->url, [ + 'headers' => $form->getPreparedHeaders()->toArray(), + 'body' => $form->bodyToIterable(), + ]); + + return $response->toStream(); + } catch (HttpExceptionInterface $e) { + $this->logger->error("relatorio: error while generating document", [ + 'msg' => $e->getMessage(), + 'response' => $e->getResponse()->getStatusCode(), + 'content' => $e->getResponse()->getContent(false), + ]); + + throw $e; + } catch (TransportExceptionInterface $e) { + $this->logger->error("relatorio: transport exception", [ + 'msg' => $e->getMessage(), + 'e' => $e->getTraceAsString(), + ]); + + throw $e; + } catch (DecodingExceptionInterface $e) { + $this->logger->error("relatorio: could not decode response", [ + 'msg' => $e->getMessage(), + 'e' => $e->getTraceAsString(), + ]); + + throw $e; + } + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 25ed037f7..011317959 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -26,6 +26,13 @@ services: autowire: true autoconfigure: true + Chill\DocGeneratorBundle\GeneratorDriver\: + resource: "../GeneratorDriver/" + autowire: true + autoconfigure: true + + Chill\DocGeneratorBundle\Driver\RelatorioDriver: '@Chill\DocGeneratorBundle\Driver\DriverInterface' + Chill\DocGeneratorBundle\Context\ContextManager: arguments: $contexts: !tagged_iterator { tag: chill_docgen.context, default_index_method: getKey } diff --git a/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js b/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js index deeb2828f..b8abcae62 100644 --- a/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js +++ b/src/Bundle/ChillDocStoreBundle/Resources/public/module/async_upload/downloader.js @@ -5,7 +5,7 @@ var algo = 'AES-CBC'; var initializeButtons = (root) => { var buttons = root.querySelectorAll('a[data-download-button]'); - + for (let i = 0; i < buttons.length; i ++) { initialize(buttons[i]); } @@ -33,9 +33,12 @@ var download = (button) => { fetchError = "Error while fetching file", key, url ; - + + console.log('keyData', keyData); + console.log('ivData', ivData); + button.textContent = labelPreparing; - + window.fetch(urlGenerator) .then((r) => { if (r.ok) { @@ -46,26 +49,19 @@ var download = (button) => { }) .then(data => { url = data.url; - - return window.crypto.subtle.importKey('jwk', keyData, { name: algo, iv: iv}, false, ['decrypt']); - }) - .catch(e => { - console.error("error while importing key"); - console.error(e); - button.appendChild(document.createTextNode(decryptError)); + + if (keyData.length > 0) { + return window.crypto.subtle.importKey('jwk', keyData, { name: algo, iv: iv}, false, ['decrypt']); + } + return Promise.resolve(undefined); }) .then(nKey => { key = nKey; return window.fetch(url); }) - .catch(e => { - console.error("error while fetching data"); - console.error(e); - button.textContent = ""; - button.appendChild(document.createTextNode(fetchError)); - }) .then(r => { + console.log('r', r); if (r.ok) { return r.arrayBuffer(); } else { @@ -73,16 +69,16 @@ var download = (button) => { } }) .then(buffer => { - return window.crypto.subtle.decrypt({ name: algo, iv: iv }, key, buffer); - }) - .catch(e => { - console.error("error while importing key"); - console.error(e); - button.textContent = ""; - button.appendChild(document.createTextNode(decryptError)); + console.log('buffer', buffer); + if (keyData.length > 0) { + return window.crypto.subtle.decrypt({ name: algo, iv: iv }, key, buffer); + } + + return Promise.resolve(buffer); }) .then(decrypted => { - var + console.log('decrypted', decrypted); + var blob = new Blob([decrypted], { type: mimeType }), url = window.URL.createObjectURL(blob) ; From af6efdd0ba8a392776d8e2a58cf376db7357d74d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 2 Dec 2021 18:02:19 +0100 Subject: [PATCH 23/41] generate document with relatorio: config and driver --- phpstan-baseline.neon | 10 - .../Context/DocGeneratorContextInterface.php | 34 +-- .../AdminDocGeneratorTemplateController.php | 29 ++- .../Controller/DocGeneratorController.php | 76 ------ .../DocGeneratorTemplateController.php | 31 ++- .../ChillDocGeneratorExtension.php | 38 +-- .../DependencyInjection/Configuration.php | 39 ++-- .../Entity/DocGeneratorTemplate.php | 76 +++--- .../Form/DocGeneratorTemplateType.php | 10 +- .../GeneratorDriver/DriverInterface.php | 12 +- .../GeneratorDriver/RelatorioDriver.php | 21 +- .../migrations/Version20211201191757.php | 25 +- .../Entity/DocumentCategory.php | 2 +- .../Repository/DocumentCategoryRepository.php | 28 +-- .../AccompanyingPeriodContext.php | 218 +++++++++--------- 15 files changed, 286 insertions(+), 363 deletions(-) delete mode 100644 src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorController.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a3a2f6e04..d996ff3b8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -10,16 +10,6 @@ parameters: count: 1 path: src/Bundle/ChillCustomFieldsBundle/Controller/CustomFieldsGroupController.php - - - message: "#^Instantiated class PhpOffice\\\\PhpWord\\\\TemplateProcessor not found\\.$#" - count: 1 - path: src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorController.php - - - - message: "#^Instantiated class PhpOffice\\\\PhpWord\\\\TemplateProcessor not found\\.$#" - count: 1 - path: src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php - - message: "#^Variable \\$participation might not be defined\\.$#" count: 3 diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index d9faeeb3f..9bca0444d 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -20,12 +20,11 @@ use Symfony\Component\Form\FormBuilderInterface; */ interface DocGeneratorContextInterface { - /** - * Get the data that will be injected to the generated document. - * - * @param mixed $entity - */ - public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array; + public function adminFormReverseTransform(array $data): array; + + public function adminFormTransform(array $data): array; + + public function buildAdminForm(FormBuilderInterface $builder): void; /** * Generate the form that display. @@ -34,11 +33,22 @@ interface DocGeneratorContextInterface */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; + /** + * Get the data that will be injected to the generated document. + * + * @param mixed $entity + */ + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array; + + public function getDescription(): string; + + public function getEntityClass(): string; + public static function getKey(): string; public function getName(): string; - public function getDescription(): string; + public function hasAdminForm(): bool; /** * has form. @@ -47,15 +57,5 @@ interface DocGeneratorContextInterface */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; - public function hasAdminForm(): bool; - - public function buildAdminForm(FormBuilderInterface $builder): void; - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void; - - public function getEntityClass(): string; - - public function adminFormTransform(array $data): array; - - public function adminFormReverseTransform(array $data): array; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index aa0e34476..e41c0c186 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -30,12 +30,24 @@ class AdminDocGeneratorTemplateController extends CRUDController public function new(Request $request): Response { if (!$request->query->has('context')) { - return $this->redirectToRoute("chill_docgen_admin_template_pick-context"); + return $this->redirectToRoute('chill_docgen_admin_template_pick-context'); } return parent::new($request); } + /** + * @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context") + */ + public function pickContext(Request $request): Response + { + $this->denyAccessUnlessGranted('ROLE_ADMIN'); + + return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [ + 'contexts' => $this->contextManager->getContexts(), + ]); + } + protected function createEntity(string $action, Request $request): object { /** @var DocGeneratorTemplate $entity */ @@ -47,19 +59,4 @@ class AdminDocGeneratorTemplateController extends CRUDController return $entity; } - - /** - * @Route("{_locale}/admin/docgen/template/pick-context", name="chill_docgen_admin_template_pick-context") - * @param Request $request - * @return Response - */ - public function pickContext(Request $request): Response - { - $this->denyAccessUnlessGranted('ROLE_ADMIN'); - - return $this->render('ChillDocGeneratorBundle:Admin/DocGeneratorTemplate:pick-context.html.twig', [ - 'contexts' => $this->contextManager->getContexts() - ]); - } - } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorController.php deleted file mode 100644 index 50201f6b0..000000000 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorController.php +++ /dev/null @@ -1,76 +0,0 @@ -generate( - 'GET', - 'FORMULAIRE_AEB.docx', - $request->query->has('expires_delay') ? $request->query->getInt('expires_delay', 0) : null - ); - - $tmpfname = tempnam(sys_get_temp_dir(), 'DOC_TEMPLATE'); - file_put_contents($tmpfname, file_get_contents($p->url)); - - $templateProcessor = new TemplateProcessor($tmpfname); - $templateProcessor->setValues(['firstname' => 'John', 'lastname' => 'Doe']); - - $tmpfname2 = tempnam(sys_get_temp_dir(), 'DOC_GENERATED'); - $templateProcessor->saveAs($tmpfname2); - - unlink($tmpfname); - - $fileContent = fopen($tmpfname2, 'rb'); // the generated file content - $response = new Response(fread($fileContent, filesize($tmpfname2))); - - $disposition = HeaderUtils::makeDisposition( - HeaderUtils::DISPOSITION_ATTACHMENT, - 'foo.docx' - ); - - $response->headers->set('Content-Disposition', $disposition); - unlink($tmpfname2); - - return $response; - } - - /** - * @Route( - * "{_locale}/doc/gen/test", - * name="chill_docgenerator_test" - * ) - */ - public function testAction(): Response - { - return (new Response())->setContent('Test'); - } -} diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index a0768cdca..a4dd27f2e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -21,22 +21,22 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; +use Exception; use GuzzleHttp\Client; use GuzzleHttp\Exception\TransferException; -use PhpOffice\PhpWord\TemplateProcessor; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; - // TODO à mettre dans services +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Annotation\Route; -use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Contracts\HttpClient\HttpClientInterface; final class DocGeneratorTemplateController extends AbstractController @@ -47,6 +47,8 @@ final class DocGeneratorTemplateController extends AbstractController private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; + private DriverInterface $driver; + private KernelInterface $kernel; private LoggerInterface $logger; @@ -55,8 +57,6 @@ final class DocGeneratorTemplateController extends AbstractController private TempUrlGeneratorInterface $tempUrlGenerator; - private DriverInterface $driver; - public function __construct( ContextManager $contextManager, DocGeneratorTemplateRepository $docGeneratorTemplateRepository, @@ -66,7 +66,6 @@ final class DocGeneratorTemplateController extends AbstractController TempUrlGeneratorInterface $tempUrlGenerator, KernelInterface $kernel, HttpClientInterface $client - ) { $this->contextManager = $contextManager; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; @@ -93,7 +92,7 @@ final class DocGeneratorTemplateController extends AbstractController $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); if (null === $entity) { - throw new NotFoundHttpException("Entity with classname $entityClassName and id $entityId is not found"); + throw new NotFoundHttpException("Entity with classname {$entityClassName} and id {$entityId} is not found"); } try { @@ -102,6 +101,8 @@ final class DocGeneratorTemplateController extends AbstractController throw new NotFoundHttpException($e->getMessage(), $e); } + $contextGenerationData = []; + if ($context->hasPublicForm($template, $entity)) { $builder = $this->createFormBuilder(); $context->buildPublicForm($builder, $template, $entity); @@ -115,8 +116,6 @@ final class DocGeneratorTemplateController extends AbstractController return $this->render($template, $templateOptions); } - } else { - $contextGenerationData = []; } $getUrlGen = $this->tempUrlGenerator->generate( @@ -137,11 +136,11 @@ final class DocGeneratorTemplateController extends AbstractController $dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat); if (false === $dataDecrypted) { - throw new \Exception('Error during Decrypt ', 1); + throw new Exception('Error during Decrypt ', 1); } - if (false === $templateResource = fopen('php://memory', 'r+')) { - $this->logger->error("Could not write data to memory"); + if (false === $templateResource = fopen('php://memory', 'r+b')) { + $this->logger->error('Could not write data to memory'); throw new HttpException(500); } @@ -156,7 +155,7 @@ final class DocGeneratorTemplateController extends AbstractController fclose($templateResource); - $genDocName = 'doc_' . sprintf('%010d', mt_rand()).'odt'; + $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . 'odt'; $getUrlGen = $this->tempUrlGenerator->generate( 'PUT', @@ -181,7 +180,7 @@ final class DocGeneratorTemplateController extends AbstractController try { $context->storeGenerated($template, $storedObject, $entity, $contextGenerationData); - } catch (\Exception $e) { + } catch (Exception $e) { $this->logger->error('Could not store the associated document to entity', [ 'entityClassName' => $entityClassName, 'entityId' => $entityId, @@ -202,7 +201,7 @@ final class DocGeneratorTemplateController extends AbstractController throw $e; } - throw new \Exception('Unable to generate document.'); + throw new Exception('Unable to generate document.'); } /** diff --git a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php index 0622377f9..9712ffac8 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php +++ b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/ChillDocGeneratorExtension.php @@ -44,25 +44,6 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn $this->prependClientConfig($container); } - private function prependClientConfig(ContainerBuilder $container) - { - $configs = $container->getExtensionConfig($this->getAlias()); - $resolvingBag = $container->getParameterBag(); - $configs = $resolvingBag->resolveValue($configs); - - $config = $this->processConfiguration(new Configuration(), $configs); - - $container->prependExtensionConfig('framework', [ - 'http_client' => [ - 'scoped_clients' => [ - 'relatorio.client' => [ - 'scope' => $config['driver']['relatorio']['url'], - ] - ] - ] - ]); - } - protected function prependCruds(ContainerBuilder $container) { $container->prependExtensionConfig('chill_main', [ @@ -102,4 +83,23 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn ], ]); } + + private function prependClientConfig(ContainerBuilder $container) + { + $configs = $container->getExtensionConfig($this->getAlias()); + $resolvingBag = $container->getParameterBag(); + $configs = $resolvingBag->resolveValue($configs); + + $config = $this->processConfiguration(new Configuration(), $configs); + + $container->prependExtensionConfig('framework', [ + 'http_client' => [ + 'scoped_clients' => [ + 'relatorio.client' => [ + 'scope' => $config['driver']['relatorio']['url'], + ], + ], + ], + ]); + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php index 678a871f7..1117a5fc0 100644 --- a/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillDocGeneratorBundle/DependencyInjection/Configuration.php @@ -23,27 +23,26 @@ class Configuration implements ConfigurationInterface $rootNode ->children() - ->arrayNode('driver') - ->addDefaultsIfNotSet() - ->children() - ->enumNode('type') - ->isRequired() - ->values(['relatorio']) - ->defaultValue('relatorio') - ->end() - ->arrayNode('relatorio') - ->addDefaultsIfNotSet() - ->children() - ->scalarNode('url') - ->isRequired() - ->defaultValue('http://relatorio:8888/') - ->end() - ->end() - ->end() - ->end() - ->end() + ->arrayNode('driver') + ->addDefaultsIfNotSet() + ->children() + ->enumNode('type') + ->isRequired() + ->values(['relatorio']) + ->defaultValue('relatorio') ->end() - ; + ->arrayNode('relatorio') + ->addDefaultsIfNotSet() + ->children() + ->scalarNode('url') + ->isRequired() + ->defaultValue('http://relatorio:8888/') + ->end() + ->end() + ->end() + ->end() + ->end() + ->end(); return $treeBuilder; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php index 61e604757..ee93f74fb 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php +++ b/src/Bundle/ChillDocGeneratorBundle/Entity/DocGeneratorTemplate.php @@ -25,7 +25,7 @@ use Symfony\Component\Serializer\Annotation as Serializer; class DocGeneratorTemplate { /** - * @ORM\Column(type="boolean", options={"default":true}) + * @ORM\Column(type="boolean", options={"default": true}) */ private bool $active = true; @@ -48,16 +48,9 @@ class DocGeneratorTemplate /** * Class name of the entity for which this template can be used. * - * @ORM\Column(type="string", options={"default":""}) + * @ORM\Column(type="string", options={"default": ""}) */ - private string $entity = ""; - - /** - * Options for the template - * - * @ORM\Column(type="json", name="template_options", options={"default":"[]"}) - */ - private array $options = []; + private string $entity = ''; /** * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"})) @@ -78,17 +71,12 @@ class DocGeneratorTemplate */ private array $name = []; - public function isActive(): bool - { - return $this->active; - } - - public function setActive(bool $active): DocGeneratorTemplate - { - $this->active = $active; - - return $this; - } + /** + * Options for the template. + * + * @ORM\Column(type="json", name="template_options", options={"default":"[]"}) + */ + private array $options = []; public function getContext(): ?string { @@ -100,6 +88,11 @@ class DocGeneratorTemplate return $this->description; } + public function getEntity(): string + { + return $this->entity; + } + public function getFile(): ?StoredObject { return $this->file; @@ -115,6 +108,23 @@ class DocGeneratorTemplate return $this->name; } + public function getOptions(): array + { + return $this->options; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): DocGeneratorTemplate + { + $this->active = $active; + + return $this; + } + public function setContext(string $context): self { $this->context = $context; @@ -129,6 +139,13 @@ class DocGeneratorTemplate return $this; } + public function setEntity(string $entity): self + { + $this->entity = $entity; + + return $this; + } + public function setFile(StoredObject $file): self { $this->file = $file; @@ -143,23 +160,6 @@ class DocGeneratorTemplate return $this; } - public function getEntity(): string - { - return $this->entity; - } - - public function setEntity(string $entity): self - { - $this->entity = $entity; - - return $this; - } - - public function getOptions(): array - { - return $this->options; - } - public function setOptions(array $options): self { $this->options = $options; diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 474ac0571..158c7bddc 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -12,14 +12,11 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Form; use Chill\DocGeneratorBundle\Context\ContextManager; -use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; -use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -45,17 +42,16 @@ class DocGeneratorTemplateType extends AbstractType ->add('description') ->add('file', StoredObjectType::class, [ 'error_bubbling' => true, - ]) - ; + ]); if ($context->hasAdminForm()) { $sub = $builder ->create('options', null, ['compound' => true]) ->addModelTransformer(new CallbackTransformer( - function (array $data) use ($context) { + static function (array $data) use ($context) { return $context->adminFormTransform($data); }, - function (array $data) use ($context) { + static function (array $data) use ($context) { return $context->adminFormReverseTransform($data); } )); diff --git a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php index 0e1828f3e..2572484e1 100644 --- a/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/GeneratorDriver/DriverInterface.php @@ -1,12 +1,22 @@ url = $parameterBag->get('chill_doc_generator')['driver']['relatorio']['url']; } - public function generateFromResource($template, string $resourceType, array $data, string $templateName = null) + public function generateFromResource($template, string $resourceType, array $data, ?string $templateName = null) { $formFields = [ - 'variables' => \json_encode($data), + 'variables' => json_encode($data), 'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType), ]; $form = new FormDataPart($formFields); @@ -47,7 +54,7 @@ class RelatorioDriver implements DriverInterface return $response->toStream(); } catch (HttpExceptionInterface $e) { - $this->logger->error("relatorio: error while generating document", [ + $this->logger->error('relatorio: error while generating document', [ 'msg' => $e->getMessage(), 'response' => $e->getResponse()->getStatusCode(), 'content' => $e->getResponse()->getContent(false), @@ -55,14 +62,14 @@ class RelatorioDriver implements DriverInterface throw $e; } catch (TransportExceptionInterface $e) { - $this->logger->error("relatorio: transport exception", [ + $this->logger->error('relatorio: transport exception', [ 'msg' => $e->getMessage(), 'e' => $e->getTraceAsString(), ]); throw $e; } catch (DecodingExceptionInterface $e) { - $this->logger->error("relatorio: could not decode response", [ + $this->logger->error('relatorio: could not decode response', [ 'msg' => $e->getMessage(), 'e' => $e->getTraceAsString(), ]); diff --git a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php index 2ce380d69..814974d7b 100644 --- a/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php +++ b/src/Bundle/ChillDocGeneratorBundle/migrations/Version20211201191757.php @@ -1,5 +1,12 @@ addSql('ALTER TABLE chill_docgen_template DROP active'); + $this->addSql('ALTER TABLE chill_docgen_template DROP entity'); + $this->addSql('ALTER TABLE chill_docgen_template DROP template_options'); + $this->addSql('ALTER TABLE chill_docgen_template ADD entities TEXT'); + $this->addSql('COMMENT ON COLUMN chill_docgen_template.entities IS \'(DC2Type:simple_array)\''); + } + public function getDescription(): string { return 'Add options, active and link to entity in docgen_template'; @@ -22,13 +38,4 @@ final class Version20211201191757 extends AbstractMigration $this->addSql('COMMENT ON COLUMN chill_docgen_template.template_options IS \'(DC2Type:json)\''); $this->addSql('ALTER TABLE chill_docgen_template DROP entities'); } - - public function down(Schema $schema): void - { - $this->addSql('ALTER TABLE chill_docgen_template DROP active'); - $this->addSql('ALTER TABLE chill_docgen_template DROP entity'); - $this->addSql('ALTER TABLE chill_docgen_template DROP template_options'); - $this->addSql('ALTER TABLE chill_docgen_template ADD entities TEXT'); - $this->addSql('COMMENT ON COLUMN chill_docgen_template.entities IS \'(DC2Type:simple_array)\''); - } } diff --git a/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php b/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php index cfe6e9643..55850b157 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/DocumentCategory.php @@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table("chill_doc.document_category") - * @ORM\Entity() + * @ORM\Entity */ class DocumentCategory { diff --git a/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php b/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php index 09ae5075a..f930e8ece 100644 --- a/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php +++ b/src/Bundle/ChillDocStoreBundle/Repository/DocumentCategoryRepository.php @@ -14,38 +14,25 @@ namespace Chill\DocStoreBundle\Repository; use Chill\DocStoreBundle\Entity\DocumentCategory; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\Mapping\Entity; use Doctrine\Persistence\ObjectRepository; -use UnexpectedValueException; /** * Get an available idInsideBUndle. */ class DocumentCategoryRepository implements ObjectRepository { - private EntityRepository $repository; - private EntityManagerInterface $em; + private EntityRepository $repository; + public function __construct(EntityManagerInterface $em) { $this->em = $em; $this->repository = $em->getRepository(DocumentCategory::class); } - public function nextIdInsideBundle() - { - $array_res = $this->em - ->createQuery( - 'SELECT MAX(c.idInsideBundle) + 1 FROM ChillDocStoreBundle:DocumentCategory c' - ) - ->getSingleResult(); - - return reset($array_res); - } - /** - * @return DocumentCategory|null + * @param mixed $id */ public function find($id): ?DocumentCategory { @@ -75,5 +62,14 @@ class DocumentCategoryRepository implements ObjectRepository return DocumentCategory::class; } + public function nextIdInsideBundle() + { + $array_res = $this->em + ->createQuery( + 'SELECT MAX(c.idInsideBundle) + 1 FROM ChillDocStoreBundle:DocumentCategory c' + ) + ->getSingleResult(); + return reset($array_res); + } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 31d237f1c..182f7aab9 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -15,10 +15,8 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; -use Chill\DocStoreBundle\Entity\DocumentCategory; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; -use Chill\EventBundle\Entity\Participation; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; @@ -31,19 +29,20 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use function array_key_exists; class AccompanyingPeriodContext implements DocGeneratorContextInterface { + private DocumentCategoryRepository $documentCategoryRepository; + private EntityManagerInterface $em; private NormalizerInterface $normalizer; - private TranslatableStringHelperInterface $translatableStringHelper; - - private DocumentCategoryRepository $documentCategoryRepository; - private PersonRender $personRender; + private TranslatableStringHelperInterface $translatableStringHelper; + public function __construct( DocumentCategoryRepository $documentCategoryRepository, NormalizerInterface $normalizer, @@ -58,94 +57,28 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface $this->personRender = $personRender; } - public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array + public function adminFormReverseTransform(array $data): array { - if (!$entity instanceof AccompanyingPeriod) { - throw new UnexpectedTypeException($entity, AccompanyingPeriod::class); - } - $options = $template->getOptions(); - - $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); - - foreach (['mainPerson', 'person1', 'person2'] as $k) { - if ($options[$k]) { - $data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', ['docgen:expects' => Person::class]); - } - } - - return $data; + return [ + 'mainPerson' => $data['mainPerson'], + 'person1' => $data['person1'], + 'person2' => $data['person2'], + 'category' => [ + 'idInsideBundle' => $data['category']->getIdInsideBundle(), + 'bundleId' => $data['category']->getBundleId(), + ], + ]; } - /** - * @param AccompanyingPeriod $entity - */ - public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void + public function adminFormTransform(array $data): array { - $options = $template->getOptions(); - $persons = $entity->getCurrentParticipations()->map(function (AccompanyingPeriodParticipation $p) { return $p->getPerson(); }) - ->toArray(); - - foreach (['mainPerson', 'person1', 'person2'] as $key) { - if ($options[$key] ?? false) { - $builder->add($key, EntityType::class, [ - 'class' => Person::class, - 'choices' => $persons, - 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, - 'multiple' => false, - 'expanded' => true, - ]); - } - } - } - - public static function getKey(): string - { - return self::class; - } - - public function getName(): string - { - return 'Accompanying Period basic'; - } - - public function getDescription(): string - { - return "A basic context for accompanying period"; - } - - public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool - { - $options = $template->getOptions(); - - return $options['mainPerson'] || $options['person1'] || $options['person2']; - } - - /** - * @param AccompanyingPeriod $entity - */ - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void - { - $doc = new AccompanyingCourseDocument(); - $doc->setTitle($this->translatableStringHelper->localize($template->getName())) - ->setDate(new DateTime()) - ->setDescription($this->translatableStringHelper->localize($template->getName())) - ->setCourse($entity) - ->setObject($storedObject) - ; - - if (array_key_exists('category', $template->getOptions()['category'])) { - $doc - ->setCategory($this->documentCategoryRepository->find( - $template->getOptions()['category']) - ); - } - - $this->em->persist($doc); - } - - public function hasAdminForm(): bool - { - return true; + return [ + 'mainPerson' => $data['mainPerson'] ?? false, + 'person1' => $data['person1'] ?? false, + 'person2' => $data['person2'] ?? false, + 'category' => array_key_exists('category', $data) ? + $this->documentCategoryRepository->find($data['category']) : null, + ]; } public function buildAdminForm(FormBuilderInterface $builder): void @@ -172,37 +105,102 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface return $entity ? $this->translatableStringHelper->localize($entity->getName()) : ''; }, ]); - ; } - public function adminFormTransform(array $data): array + /** + * @param AccompanyingPeriod $entity + */ + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { - return [ - 'mainPerson' => $data['mainPerson'] ?? false, - 'person1' => $data['person1'] ?? false, - 'person2' => $data['person2'] ?? false, - 'category' => - array_key_exists('category', $data) ? - $this->documentCategoryRepository->find($data['category']) : null, - ]; + $options = $template->getOptions(); + $persons = $entity->getCurrentParticipations()->map(static function (AccompanyingPeriodParticipation $p) { return $p->getPerson(); }) + ->toArray(); + + foreach (['mainPerson', 'person1', 'person2'] as $key) { + if ($options[$key] ?? false) { + $builder->add($key, EntityType::class, [ + 'class' => Person::class, + 'choices' => $persons, + 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, + 'multiple' => false, + 'expanded' => true, + ]); + } + } } - public function adminFormReverseTransform(array $data): array + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array { - return [ - 'mainPerson' => $data['mainPerson'], - 'person1' => $data['person1'], - 'person2' => $data['person2'], - 'category' => [ - 'idInsideBundle' => $data['category']->getIdInsideBundle(), - 'bundleId' => $data['category']->getBundleId() - ], - ]; + if (!$entity instanceof AccompanyingPeriod) { + throw new UnexpectedTypeException($entity, AccompanyingPeriod::class); + } + $options = $template->getOptions(); + + $data = []; + $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + foreach (['mainPerson', 'person1', 'person2'] as $k) { + if ($options[$k]) { + $data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', ['docgen:expects' => Person::class]); + } + } + + return $data; } + public function getDescription(): string + { + return 'A basic context for accompanying period'; + } public function getEntityClass(): string { return AccompanyingPeriod::class; } + + public static function getKey(): string + { + return self::class; + } + + public function getName(): string + { + return 'Accompanying Period basic'; + } + + public function hasAdminForm(): bool + { + return true; + } + + public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool + { + $options = $template->getOptions(); + + return $options['mainPerson'] || $options['person1'] || $options['person2']; + } + + /** + * @param AccompanyingPeriod $entity + */ + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void + { + $doc = new AccompanyingCourseDocument(); + $doc->setTitle($this->translatableStringHelper->localize($template->getName())) + ->setDate(new DateTime()) + ->setDescription($this->translatableStringHelper->localize($template->getName())) + ->setCourse($entity) + ->setObject($storedObject); + + if (array_key_exists('category', $template->getOptions()['category'])) { + $doc + ->setCategory( + $this->documentCategoryRepository->find( + $template->getOptions()['category'] + ) + ); + } + + $this->em->persist($doc); + } } From be626079d0c98ee5aee6b1936fdfbeceda2426d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 2 Dec 2021 18:18:11 +0100 Subject: [PATCH 24/41] move logic of context to different interfaces --- .../Context/DocGeneratorContextInterface.php | 23 --- ...GeneratorContextWithAdminFormInterface.php | 25 +++ ...eneratorContextWithPublicFormInterface.php | 32 ++++ .../HouseholdMemberSelectionContext.php | 143 ------------------ .../DocGeneratorTemplateController.php | 4 +- .../Form/DocGeneratorTemplateType.php | 4 +- .../AccompanyingPeriodContext.php | 11 +- 7 files changed, 71 insertions(+), 171 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php delete mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index 9bca0444d..df148f6e1 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -13,26 +13,12 @@ namespace Chill\DocGeneratorBundle\Context; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\StoredObject; -use Symfony\Component\Form\FormBuilderInterface; /** * Interface for context for document generation. */ interface DocGeneratorContextInterface { - public function adminFormReverseTransform(array $data): array; - - public function adminFormTransform(array $data): array; - - public function buildAdminForm(FormBuilderInterface $builder): void; - - /** - * Generate the form that display. - * - * @param mixed $entity - */ - public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; - /** * Get the data that will be injected to the generated document. * @@ -48,14 +34,5 @@ interface DocGeneratorContextInterface public function getName(): string; - public function hasAdminForm(): bool; - - /** - * has form. - * - * @param mixed $entity - */ - public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php new file mode 100644 index 000000000..28ad70170 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php @@ -0,0 +1,25 @@ +em = $em; - } - - /** - * Get the data that will be injected to the generated document. - * - * @param mixed $entity - */ - public function getData($entity): array - { - $datas = [ - 'setValues' => [], - 'cloneRowAndSetValues' => [], - ]; - - $persons = $entity->getAccompanyingPeriodWork()->getPersons(); - - if (count($persons) > 0) { - $firstPerson = $persons[0]; - - $datas['setValues'][] = [ - 'firstPersonFirstName' => $firstPerson->getFirstName(), - 'firstPersonLastName' => $firstPerson->getLastName(), ]; - } - - if (get_class($entity) === AccompanyingPeriodWorkEvaluation::class) { - $values = []; - - foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) { - $i = 1; - $values[] = [ - 'personRowId' => $i, - 'personFirstName' => $person->getFirstName(), - 'personLastName' => $person->getLastName(), - ]; - } - - $datas['cloneRowAndSetValues'][] = [ - 'personRowId', $values, ]; - } - - return $datas; - } - - /** - * Generate the form that display. - * - * @param mixed $entity - */ - public function getForm($entity) - { - throw new Exception('No implemented yet', 1); - $choices = []; - - if (get_class($entity) === AccompanyingPeriodWorkEvaluation::class) { - foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) { - $choices[$person->getId()] = $person->getName(); - } - } - - $builder->add('members', ChoiceType::class, [ - 'choices' => $choices, - 'placeholder' => 'Choose a person', - 'label' => 'Person to add', - ]); - - return $builder; - } - - public static function getKey(): string - { - return self::class; - } - - public function getName(): string - { - return 'household member'; - } - - /** - * has form. - */ - public function hasForm(): bool - { - return true; - } - - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void - { - // Only for evaluation - if ($entity instanceof AccompanyingPeriodWorkEvaluation) { - $doc = new AccompanyingPeriodWorkEvaluationDocument(); - $doc - ->setStoredObject($storedObject) - ->setTemplate($template); - $entity->addDocument($doc); - $this->em->persist($doc); - } - } - - /** - * True of false which entity supports. - */ - public function supports(string $entityClass): bool - { - return - (AccompanyingPeriod::class === $entityClass) - || (SocialAction::class === $entityClass); - } -} diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index a4dd27f2e..1f5043d82 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -14,6 +14,7 @@ namespace Chill\DocGeneratorBundle\Controller; use Base64Url\Base64Url; use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; @@ -103,7 +104,8 @@ final class DocGeneratorTemplateController extends AbstractController $contextGenerationData = []; - if ($context->hasPublicForm($template, $entity)) { + if ($context instanceof DocGeneratorContextWithPublicFormInterface + && $context->hasPublicForm($template, $entity)) { $builder = $this->createFormBuilder(); $context->buildPublicForm($builder, $template, $entity); $form = $builder->getForm()->handleRequest($request); diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 158c7bddc..f52962a41 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Form; use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; @@ -44,7 +45,8 @@ class DocGeneratorTemplateType extends AbstractType 'error_bubbling' => true, ]); - if ($context->hasAdminForm()) { + if ($context instanceof DocGeneratorContextWithAdminFormInterface + && $context->hasAdminForm()) { $sub = $builder ->create('options', null, ['compound' => true]) ->addModelTransformer(new CallbackTransformer( diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 182f7aab9..26dc29b25 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -12,6 +12,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Service\DocGenerator; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; @@ -31,7 +33,10 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use function array_key_exists; -class AccompanyingPeriodContext implements DocGeneratorContextInterface +class AccompanyingPeriodContext implements + DocGeneratorContextInterface, + DocGeneratorContextWithAdminFormInterface, + DocGeneratorContextWithPublicFormInterface { private DocumentCategoryRepository $documentCategoryRepository; @@ -196,8 +201,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface $doc ->setCategory( $this->documentCategoryRepository->find( - $template->getOptions()['category'] - ) + $template->getOptions()['category'] + ) ); } From e053529afb6df88089fc320d0d924d248f8e3554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 12:21:36 +0100 Subject: [PATCH 25/41] add docgen context for AccompanyingPeriodWork --- .../AccompanyingPeriodWork.php | 36 ++++--- .../AccompanyingPeriodParticipation.php | 6 +- .../AccompanyingPeriodWorkContext.php | 100 ++++++++++++++++++ ...mpanyingPeriodWorkDocGenNormalizerTest.php | 92 ++++++++++++++++ 4 files changed, 214 insertions(+), 20 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php index 2d0bfe918..0e7b27cef 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php @@ -53,7 +53,7 @@ use Symfony\Component\Validator\Constraints as Assert; * cascade={"remove", "persist"}, * orphanRemoval=true * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * * @internal /!\ the serialization for write evaluations is handled in `AccompanyingPeriodWorkDenormalizer` */ @@ -61,24 +61,26 @@ use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\Column(type="datetime_immutable") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?DateTimeImmutable $createdAt = null; /** * @ORM\Column(type="boolean") + * @Serializer\Groups({"read", "docgen:read"}) */ private bool $createdAutomatically = false; /** * @ORM\Column(type="text") + * @Serializer\Groups({"read", "docgen:read"}) */ private string $createdAutomaticallyReason = ''; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=false) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?User $createdBy = null; @@ -86,7 +88,7 @@ use Symfony\Component\Validator\Constraints as Assert; * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) * @Serializer\Groups({"accompanying_period_work:create"}) * @Serializer\Groups({"accompanying_period_work:edit"}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\GreaterThan(propertyPath="startDate", * message="accompanying_course_work.The endDate should be greater than the start date" * ) @@ -100,14 +102,14 @@ use Symfony\Component\Validator\Constraints as Assert; * cascade={"persist"}, * orphanRemoval=true * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:edit"}) */ private Collection $goals; /** * @ORM\ManyToOne(targetEntity=ThirdParty::class) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:edit"}) * * In schema : traitant @@ -118,20 +120,20 @@ use Symfony\Component\Validator\Constraints as Assert; * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ - private ?int $id; + private ?int $id = null; /** * @ORM\Column(type="text") - * @Serializer\Groups({"read", "accompanying_period_work:edit"}) + * @Serializer\Groups({"read", "accompanying_period_work:edit", "docgen:read"}) */ private string $note = ''; /** * @ORM\ManyToMany(targetEntity=Person::class) * @ORM\JoinTable(name="chill_person_accompanying_period_work_person") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:edit"}) * @Serializer\Groups({"accompanying_period_work:create"}) */ @@ -140,14 +142,14 @@ use Symfony\Component\Validator\Constraints as Assert; /** * @ORM\ManyToMany(targetEntity=Result::class, inversedBy="accompanyingPeriodWorks") * @ORM\JoinTable(name="chill_person_accompanying_period_work_result") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:edit"}) */ private Collection $results; /** * @ORM\ManyToOne(targetEntity=SocialAction::class) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:create"}) */ private ?SocialAction $socialAction = null; @@ -156,30 +158,30 @@ use Symfony\Component\Validator\Constraints as Assert; * @ORM\Column(type="date_immutable") * @Serializer\Groups({"accompanying_period_work:create"}) * @Serializer\Groups({"accompanying_period_work:edit"}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ - private DateTimeImmutable $startDate; + private ?DateTimeImmutable $startDate = null; /** * @ORM\ManyToMany(targetEntity=ThirdParty::class) * @ORM\JoinTable(name="chill_person_accompanying_period_work_third_party") * * In schema : intervenants - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work:edit"}) */ private Collection $thirdParties; /** * @ORM\Column(type="datetime_immutable") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?DateTimeImmutable $updatedAt = null; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=false) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?User $updatedBy = null; diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php index 3478b2631..862d268d2 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php @@ -36,7 +36,7 @@ class AccompanyingPeriodParticipation /** * @ORM\Column(type="date", nullable=true) - * @Groups({"read"}) + * @Groups({"read", "read:docgen"}) */ private $endDate; @@ -44,14 +44,14 @@ class AccompanyingPeriodParticipation * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Groups({"read"}) + * @Groups({"read", "read:docgen"}) */ private $id; /** * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="accompanyingPeriodParticipations") * @ORM\JoinColumn(name="person_id", referencedColumnName="id", nullable=false) - * @Groups({"read"}) + * @Groups({"read", "read:docgen"}) */ private $person; diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php new file mode 100644 index 000000000..7f6b47971 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -0,0 +1,100 @@ +periodContext = $periodContext; + $this->normalizer = $normalizer; + } + + /** + * @param AccompanyingPeriodWork $entity + */ + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array + { + $data = $this->periodContext->getData($template, $entity->getAccompanyingPeriod(), $contextGenerationData); + $data['work'] = $this->normalizer->normalize($entity, 'docgen', [ + AbstractNormalizer::GROUPS => ['docgen:read'], + 'docgen:expects' => AccompanyingPeriodWork::class, + ]); + + return $data; + } + + public function getDescription(): string + { + return "A context for work"; + } + + public function getEntityClass(): string + { + return AccompanyingPeriodWork::class; + } + + public static function getKey(): string + { + return 'accompanying_period_work_regular'; + } + + public function getName(): string + { + return "Accompanying period work"; + } + + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void + { + // TODO: Implement storeGenerated() method. + } + + public function adminFormReverseTransform(array $data): array + { + return $this->periodContext->adminFormReverseTransform($data); + } + + public function adminFormTransform(array $data): array + { + return $this->periodContext->adminFormTransform($data); + } + + public function buildAdminForm(FormBuilderInterface $builder): void + { + $this->periodContext->buildAdminForm($builder); + } + + public function hasAdminForm(): bool + { + return $this->periodContext->hasAdminForm(); + } + + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void + { + $this->periodContext->buildPublicForm($builder, $template, $entity); + } + + public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool + { + return $this->periodContext->hasPublicForm($template, $entity); + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php new file mode 100644 index 000000000..025dc73ae --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php @@ -0,0 +1,92 @@ +normalizer = self::$container->get(NormalizerInterface::class); + } + + public function testNormlalize() + { + $work = new AccompanyingPeriodWork(); + $work + ->addPerson((new Person())->setFirstName('hello')->setLastName('name')) + ->addGoal($g = new AccompanyingPeriodWorkGoal()) + ->addResult($r = new Result()) + ->setCreatedAt(new \DateTimeImmutable()) + ->setUpdatedAt(new \DateTimeImmutable()) + ->setCreatedBy($user = new User()) + ->setUpdatedBy($user) + ; + $g->addResult($r)->setGoal($goal = new Goal()); + $goal->addResult($r); + + $actual = $this->normalizer->normalize($work, 'docgen', [ + 'docgen:expects' => AccompanyingPeriodWork::class, + AbstractNormalizer::GROUPS => ['docgen:read'] + ]); + + var_dump($actual); + + $expected = [ + 'id' => 0, + + ]; + + $this->assertIsArray($actual); + $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($actual)); + + foreach ($expected as $key => $item) { + if ('@ignored' === $item) { + continue; + } + + $this->assertEquals($item, $actual[$key]); + } + } + + public function testNormalizationNull() + { + $actual = $this->normalizer->normalize(null, 'docgen', [ + 'docgen:expects' => AccompanyingPeriodWork::class, + AbstractNormalizer::GROUPS => ['docgen:read'] + ]); + + dump($actual); + + $expected = [ + 'id' => "" + ]; + + $this->assertIsArray($actual); + $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($actual)); + + foreach ($expected as $key => $item) { + if ('@ignored' === $item) { + continue; + } + + $this->assertEquals($item, $actual[$key]); + } + + + + } + +} From 23eff9f6d43381d91938086f2ef8d30438879901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 13:50:35 +0100 Subject: [PATCH 26/41] add basic context for AccompanyingPeriodWorkEvalution WIP --- ...GeneratorContextWithAdminFormInterface.php | 2 +- ...eneratorContextWithPublicFormInterface.php | 2 +- .../Form/DocGeneratorTemplateType.php | 9 ++ .../AccompanyingPeriodContext.php | 24 ++- .../AccompanyingPeriodWorkContext.php | 67 ++++---- ...ccompanyingPeriodWorkEvaluationContext.php | 143 ++++++++++++++++++ ...mpanyingPeriodWorkDocGenNormalizerTest.php | 80 +++++----- 7 files changed, 253 insertions(+), 74 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php index 28ad70170..3b606ccb5 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php @@ -13,7 +13,7 @@ namespace Chill\DocGeneratorBundle\Context; use Symfony\Component\Form\FormBuilderInterface; -interface DocGeneratorContextWithAdminFormInterface +interface DocGeneratorContextWithAdminFormInterface extends DocGeneratorContextInterface { public function adminFormReverseTransform(array $data): array; diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php index 391dff580..402802501 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php @@ -14,7 +14,7 @@ namespace Chill\DocGeneratorBundle\Context; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Symfony\Component\Form\FormBuilderInterface; -interface DocGeneratorContextWithPublicFormInterface +interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContextInterface { /** * Generate the form that display. diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index f52962a41..4275cf53e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -18,6 +18,7 @@ use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -43,6 +44,14 @@ class DocGeneratorTemplateType extends AbstractType ->add('description') ->add('file', StoredObjectType::class, [ 'error_bubbling' => true, + ]) + ->add('active', ChoiceType::class, [ + 'label' => 'Active', + 'choices' => [ + 'Yes' => true, + 'No' => false, + ], + 'required' => true, ]); if ($context instanceof DocGeneratorContextWithAdminFormInterface diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 26dc29b25..5227770eb 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -64,26 +64,36 @@ class AccompanyingPeriodContext implements public function adminFormReverseTransform(array $data): array { - return [ + $data = [ 'mainPerson' => $data['mainPerson'], 'person1' => $data['person1'], 'person2' => $data['person2'], - 'category' => [ + ]; + + if (array_key_exists('category', $data)) { + $data['category'] = [ 'idInsideBundle' => $data['category']->getIdInsideBundle(), 'bundleId' => $data['category']->getBundleId(), - ], - ]; + ]; + } + + return $data; } public function adminFormTransform(array $data): array { - return [ + $data = [ 'mainPerson' => $data['mainPerson'] ?? false, 'person1' => $data['person1'] ?? false, 'person2' => $data['person2'] ?? false, - 'category' => array_key_exists('category', $data) ? - $this->documentCategoryRepository->find($data['category']) : null, ]; + + if (array_key_exists('category', $data)) { + $data['category'] = array_key_exists('category', $data) ? + $this->documentCategoryRepository->find($data['category']) : null; + } + + return $data; } public function buildAdminForm(FormBuilderInterface $builder): void diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php index 7f6b47971..22c898f9d 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -1,5 +1,14 @@ normalizer = $normalizer; } + public function adminFormReverseTransform(array $data): array + { + return $this->periodContext->adminFormReverseTransform($data); + } + + public function adminFormTransform(array $data): array + { + return $this->periodContext->adminFormTransform($data); + } + + public function buildAdminForm(FormBuilderInterface $builder): void + { + $this->periodContext->buildAdminForm($builder); + } + + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void + { + $this->periodContext->buildPublicForm($builder, $template, $entity); + } + /** * @param AccompanyingPeriodWork $entity */ @@ -45,7 +74,7 @@ class AccompanyingPeriodWorkContext implements public function getDescription(): string { - return "A context for work"; + return 'A context for work'; } public function getEntityClass(): string @@ -60,27 +89,7 @@ class AccompanyingPeriodWorkContext implements public function getName(): string { - return "Accompanying period work"; - } - - public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void - { - // TODO: Implement storeGenerated() method. - } - - public function adminFormReverseTransform(array $data): array - { - return $this->periodContext->adminFormReverseTransform($data); - } - - public function adminFormTransform(array $data): array - { - return $this->periodContext->adminFormTransform($data); - } - - public function buildAdminForm(FormBuilderInterface $builder): void - { - $this->periodContext->buildAdminForm($builder); + return 'Accompanying period work'; } public function hasAdminForm(): bool @@ -88,13 +97,13 @@ class AccompanyingPeriodWorkContext implements return $this->periodContext->hasAdminForm(); } - public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void - { - $this->periodContext->buildPublicForm($builder, $template, $entity); - } - public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool { return $this->periodContext->hasPublicForm($template, $entity); } + + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void + { + // TODO: Implement storeGenerated() method. + } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php new file mode 100644 index 000000000..1f17ce48a --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -0,0 +1,143 @@ +accompanyingPeriodWorkContext = $accompanyingPeriodWorkContext; + $this->em = $em; + $this->evaluationRepository = $evaluationRepository; + $this->translatableStringHelper = $translatableStringHelper; + } + + public function adminFormReverseTransform(array $data): array + { + return array_merge( + $this->accompanyingPeriodWorkContext->adminFormReverseTransform($data), + [ + 'evaluations' => array_map( + static function (Evaluation $e) { return $e->getId(); }, + $data['evaluations'] + ), + ] + ); + } + + public function adminFormTransform(array $data): array + { + return array_merge( + $this->accompanyingPeriodWorkContext->adminFormTransform($data), + [ + 'evaluations' => array_map( + function ($id) { return $this->evaluationRepository->find($id); }, + $data['evaluations'] ?? [] + ), + ] + ); + } + + public function buildAdminForm(FormBuilderInterface $builder): void + { + $this->accompanyingPeriodWorkContext->buildAdminForm($builder); + + $builder->remove('category'); + + $builder->add('evaluations', EntityType::class, [ + 'class' => Evaluation::class, + 'label' => 'Linked evaluations', + 'choices' => $this->evaluationRepository->findAll(), + 'choice_label' => function (Evaluation $e) { + return $this->translatableStringHelper->localize($e->getTitle()); + }, + 'multiple' => true, + 'expanded' => true, + ]); + } + + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void + { + // TODO: Implement buildPublicForm() method. + } + + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array + { + // TODO: Implement getData() method. + } + + public function getDescription(): string + { + return 'Context for accompanying period work'; + } + + public function getEntityClass(): string + { + return AccompanyingPeriodWorkEvaluation::class; + } + + public static function getKey(): string + { + return 'accompanying_period_work_evaluation_regular'; + } + + public function getName(): string + { + return 'Accompanying period work context'; + } + + public function hasAdminForm(): bool + { + return true; + } + + public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool + { + // TODO: Implement hasPublicForm() method. + } + + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void + { + $doc = new AccompanyingPeriodWorkEvaluationDocument(); + $doc->setStoredObject($storedObject) + ->setAccompanyingPeriodWorkEvaluation($entity) + ->setTemplate($template); + $this->em->persist($doc); + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php index 025dc73ae..2417e93da 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodWorkDocGenNormalizerTest.php @@ -1,5 +1,14 @@ normalizer = self::$container->get(NormalizerInterface::class); } + public function testNormalizationNull() + { + $actual = $this->normalizer->normalize(null, 'docgen', [ + 'docgen:expects' => AccompanyingPeriodWork::class, + AbstractNormalizer::GROUPS => ['docgen:read'], + ]); + + dump($actual); + + $expected = [ + 'id' => '', + ]; + + $this->assertIsArray($actual); + $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($actual)); + + foreach ($expected as $key => $item) { + if ('@ignored' === $item) { + continue; + } + + $this->assertEquals($item, $actual[$key]); + } + } + public function testNormlalize() { $work = new AccompanyingPeriodWork(); @@ -29,24 +68,22 @@ class AccompanyingPeriodWorkDocGenNormalizerTest extends KernelTestCase ->addPerson((new Person())->setFirstName('hello')->setLastName('name')) ->addGoal($g = new AccompanyingPeriodWorkGoal()) ->addResult($r = new Result()) - ->setCreatedAt(new \DateTimeImmutable()) - ->setUpdatedAt(new \DateTimeImmutable()) + ->setCreatedAt(new DateTimeImmutable()) + ->setUpdatedAt(new DateTimeImmutable()) ->setCreatedBy($user = new User()) - ->setUpdatedBy($user) - ; + ->setUpdatedBy($user); $g->addResult($r)->setGoal($goal = new Goal()); $goal->addResult($r); $actual = $this->normalizer->normalize($work, 'docgen', [ 'docgen:expects' => AccompanyingPeriodWork::class, - AbstractNormalizer::GROUPS => ['docgen:read'] + AbstractNormalizer::GROUPS => ['docgen:read'], ]); var_dump($actual); $expected = [ 'id' => 0, - ]; $this->assertIsArray($actual); @@ -60,33 +97,4 @@ class AccompanyingPeriodWorkDocGenNormalizerTest extends KernelTestCase $this->assertEquals($item, $actual[$key]); } } - - public function testNormalizationNull() - { - $actual = $this->normalizer->normalize(null, 'docgen', [ - 'docgen:expects' => AccompanyingPeriodWork::class, - AbstractNormalizer::GROUPS => ['docgen:read'] - ]); - - dump($actual); - - $expected = [ - 'id' => "" - ]; - - $this->assertIsArray($actual); - $this->assertEqualsCanonicalizing(array_keys($expected), array_keys($actual)); - - foreach ($expected as $key => $item) { - if ('@ignored' === $item) { - continue; - } - - $this->assertEquals($item, $actual[$key]); - } - - - - } - } From 43702ded77449ee08b8d2c5c494c220d457373a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 15:06:06 +0100 Subject: [PATCH 27/41] fix pagination problems --- src/Bundle/ChillMainBundle/Pagination/Paginator.php | 8 ++++++++ .../ChillMainBundle/Tests/Pagination/PaginatorTest.php | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Bundle/ChillMainBundle/Pagination/Paginator.php b/src/Bundle/ChillMainBundle/Pagination/Paginator.php index d6da55618..54fa36084 100644 --- a/src/Bundle/ChillMainBundle/Pagination/Paginator.php +++ b/src/Bundle/ChillMainBundle/Pagination/Paginator.php @@ -109,6 +109,10 @@ class Paginator implements PaginatorInterface return 1; } + if (0 === $this->totalItems) { + return 1; + } + $nb = floor($this->totalItems / $this->itemPerPage); if ($this->totalItems % $this->itemPerPage > 0) { @@ -211,6 +215,10 @@ class Paginator implements PaginatorInterface public function hasPage($number) { + if (0 === $this->totalItems) { + return 1 === $number; + } + return 0 < $number && $this->countPages() >= $number; } diff --git a/src/Bundle/ChillMainBundle/Tests/Pagination/PaginatorTest.php b/src/Bundle/ChillMainBundle/Tests/Pagination/PaginatorTest.php index 219669825..2a92ef222 100644 --- a/src/Bundle/ChillMainBundle/Tests/Pagination/PaginatorTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Pagination/PaginatorTest.php @@ -204,6 +204,14 @@ final class PaginatorTest extends KernelTestCase ); } + public function testPagesWithoutResult() + { + $paginator = $this->generatePaginator(0, 10); + + $this->assertEquals(0, $paginator->getCurrentPageFirstItemNumber()); + $this->assertEquals(10, $paginator->getItemsPerPage()); + } + /** * @param int $itemPerPage * @param string $route From a86ba6faf53bc7fe70db95c833a13ca88e96cb3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 15:18:45 +0100 Subject: [PATCH 28/41] fix code style --- src/Bundle/ChillActivityBundle/Entity/Activity.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index 419892f9b..77f650ce4 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -46,7 +46,7 @@ use Symfony\Component\Serializer\Annotation\SerializedName; * "activity": Activity::class * }) * @ActivityValidator\ActivityValidity - * + * * TODO see if necessary * UserCircleConsistency( * "CHILL_ACTIVITY_SEE_DETAILS", From 2d319fcc42f28cfc3acc9c468e0acf9cb5aa6417 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 20:09:24 +0100 Subject: [PATCH 29/41] adapting vuejs form for accompanyingPeriodWork: include template for specific evaluation --- .../Resources/public/api/pickTemplate.js | 10 ++ .../public/module/PickTemplate/index.js | 33 ++-- .../public/vuejs/_components/PickTemplate.vue | 43 +++-- ...nyingPeriodWorkEvaluationApiController.php | 74 +++++++++ .../Entity/AccompanyingPeriod/Origin.php | 2 - .../vuejs/AccompanyingCourseWorkEdit/App.vue | 6 +- .../components/FormEvaluation.vue | 48 +++--- .../vuejs/AccompanyingCourseWorkEdit/store.js | 147 ++++++++---------- 8 files changed, 224 insertions(+), 139 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Resources/public/api/pickTemplate.js create mode 100644 src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/api/pickTemplate.js b/src/Bundle/ChillDocGeneratorBundle/Resources/public/api/pickTemplate.js new file mode 100644 index 000000000..a668fe29d --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/api/pickTemplate.js @@ -0,0 +1,10 @@ +import { fetchResults } from "ChillMainAssets/lib/api/apiMethods.js"; + +const fetchTemplates = (entityClass) => { + let fqdnEntityClass = encodeURI(entityClass); + return fetchResults(`/api/1.0/docgen/templates/by-entity/${fqdnEntityClass}`); +} + +export { + fetchTemplates +}; diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js b/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js index 8b450a7e1..9d26d1009 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/module/PickTemplate/index.js @@ -1,24 +1,27 @@ import {createApp} from 'vue'; import PickTemplate from 'ChillDocGeneratorAssets/vuejs/_components/PickTemplate.vue'; +import {fetchTemplates} from 'ChillDocGeneratorAssets/api/pickTemplate.js'; import {_createI18n} from 'ChillMainAssets/vuejs/_js/i18n'; const i18n = _createI18n({}); document.querySelectorAll('div[data-docgen-template-picker]').forEach(el => { - let - picker = { - template: '', - components: { - PickTemplate, - }, - data() { - return { - entityClass: el.dataset.entityClass, - entityId: el.dataset.entityId, - } - }, - } - ; + fetchTemplates(el.dataset.entityClass).then(templates => { + let + picker = { + template: '', + components: { + PickTemplate, + }, + data() { + return { + templates: templates, + entityId: el.dataset.entityId, + } + }, + } + ; + createApp(picker).use(i18n).mount(el); + }) - createApp(picker).use(i18n).mount(el); }); diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue index bb8f6008a..1052fe75f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue @@ -20,7 +20,6 @@ diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php new file mode 100644 index 000000000..29b00cf4f --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php @@ -0,0 +1,74 @@ +docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->serializer = $serializer; + $this->paginatorFactory = $paginatorFactory; + } + + /** + * @Route("/api/1.0/person/docgen/template/by-evaluation/{id}.{_format}", + * requirements={"format": "json"}) + */ + public function listTemplateByEvaluation(Evaluation $evaluation, string $_format): JsonResponse + { + if ('json' !== $_format) { + throw new BadRequestHttpException("format not supported"); + } + + $evaluations = + array_filter( + $this->docGeneratorTemplateRepository + ->findByEntity(AccompanyingPeriodWorkEvaluation::class), + function (DocGeneratorTemplate $t) use ($evaluation) { + $ids = $t->getOptions()['evaluations'] ?? []; + return in_array($evaluation->getId(), $ids); + } + ); + + $paginator = $this->paginatorFactory->create(count($evaluations)); + $paginator->setItemsPerPage(count($evaluations)); + + return new JsonResponse($this->serializer->serialize( + new Collection($evaluations, $paginator), 'json', [ + AbstractNormalizer::GROUPS => ['read'] + ] + ), JsonResponse::HTTP_OK, [], true); + } +} diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php index afcfc4228..ec22e806e 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Origin.php @@ -54,8 +54,6 @@ class Origin public function getLabel() { - dump($this->label); - return $this->label; } diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue index 58675b54d..b7a137de2 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue @@ -196,7 +196,8 @@
@@ -85,6 +82,7 @@ import {dateToISO, ISOToDate, ISOToDatetime} from 'ChillMainAssets/chill/js/date import CKEditor from '@ckeditor/ckeditor5-vue'; import ClassicEditor from 'ChillMainAssets/module/ckeditor5/index.js'; import { mapGetters, mapState } from 'vuex'; +import PickTemplate from 'ChillDocGeneratorAssets/vuejs/_components/PickTemplate.vue'; const i18n = { messages: { @@ -111,6 +109,7 @@ export default { props: ['evaluation'], components: { ckeditor: CKEditor.component, + PickTemplate, }, i18n, data() { @@ -120,12 +119,12 @@ export default { } }, computed: { - ...mapGetters([ - 'getTemplatesAvailaibleForEvaluation' - ]), ...mapState([ 'isPosting' ]), + getTemplatesAvailables() { + return this.$store.getters.getTemplatesAvailablesForEvaluation(this.evaluation.evaluation); + }, canGenerate() { return !this.$store.state.isPosting && this.template !== null; }, @@ -176,13 +175,14 @@ export default { }) ; }, - generateDocument() { - console.log('template picked', this.template); - this.$store.dispatch('generateDocument', { key: this.evaluation.key, templateId: this.template}) + submitBeforeGenerate() { + const callback = (data) => { + let evaluationId = data.accompanyingPeriodWorkEvaluations.find(e => e.key === this.evaluation.key).id; + return Promise.resolve({entityId: evaluationId}); + }; + + return this.$store.dispatch('submit', callback).catch(e => { console.log(e); throw e; }); } }, - mounted() { - //this.listAllStatus(); - } } diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/store.js b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/store.js index 22218f04f..92ee414a7 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/store.js +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/store.js @@ -2,6 +2,8 @@ import { createStore } from 'vuex'; import { datetimeToISO, ISOToDatetime, intervalDaysToISO, intervalISOToDays } from 'ChillMainAssets/chill/js/date.js'; import { findSocialActionsBySocialIssue } from 'ChillPersonAssets/vuejs/_api/SocialWorkSocialAction.js'; import { create } from 'ChillPersonAssets/vuejs/_api/AccompanyingCourseWork.js'; +import { fetchResults, makeFetch } from 'ChillMainAssets/lib/api/apiMethods.js'; +import { fetchTemplates } from 'ChillDocGeneratorAssets/api/pickTemplate.js'; const debug = process.env.NODE_ENV !== 'production'; const evalFQDN = encodeURIComponent("Chill\\PersonBundle\\Entity\\AccompanyingPeriod\\AccompanyingPeriodWorkEvaluation"); @@ -20,20 +22,10 @@ const store = createStore({ resultsPicked: window.accompanyingCourseWork.results, resultsForAction: [], resultsForGoal: [], - evaluationsPicked: window.accompanyingCourseWork.accompanyingPeriodWorkEvaluations.map((e, index) => { - var k = Object.assign(e, { - key: index, - editEvaluation: false, - startDate: e.startDate !== null ? ISOToDatetime(e.startDate.datetime) : null, - endDate: e.endDate !== null ? ISOToDatetime(e.endDate.datetime) : null, - maxDate: e.maxDate !== null ? ISOToDatetime(e.maxDate.datetime) : null, - warningInterval: e.warningInterval !== null ? intervalISOToDays(e.warningInterval) : null, - }); - - return k; - }), + evaluationsPicked: [], evaluationsForAction: [], - templatesAvailableForEvaluation: [], + templatesAvailablesForAction: [], + templatesAvailablesForEvaluation: new Map([]), personsPicked: window.accompanyingCourseWork.persons, personsReachables: window.accompanyingCourseWork.accompanyingPeriod.participations.filter(p => p.endDate == null) .map(p => p.person), @@ -65,8 +57,8 @@ const store = createStore({ hasThirdParties(state) { return state.thirdParties.length > 0; }, - getTemplatesAvailaibleForEvaluation(state) { - return state.templatesAvailableForEvaluation; + getTemplatesAvailablesForEvaluation: (state) => (evaluation) => { + return state.templatesAvailablesForEvaluation.get(evaluation.id) || []; }, buildPayload(state) { return { @@ -125,6 +117,20 @@ const store = createStore({ } }, mutations: { + setEvaluationsPicked(state, evaluations) { + state.evaluationsPicked = evaluations.map((e, index) => { + var k = Object.assign(e, { + key: index, + editEvaluation: false, + startDate: e.startDate !== null ? ISOToDatetime(e.startDate.datetime) : null, + endDate: e.endDate !== null ? ISOToDatetime(e.endDate.datetime) : null, + maxDate: e.maxDate !== null ? ISOToDatetime(e.maxDate.datetime) : null, + warningInterval: e.warningInterval !== null ? intervalISOToDays(e.warningInterval) : null, + }); + + return k; + }); + }, setStartDate(state, date) { state.startDate = date; }, @@ -222,10 +228,11 @@ const store = createStore({ let evaluation = state.evaluationsPicked.find(e => e.key === key); evaluation.editEvaluation = !evaluation.editEvaluation; }, - setTemplatesAvailableForEvaluation(state, templates) { - for (let i in templates) { - state.templatesAvailableForEvaluation.push(templates[i]); - } + setTemplatesForEvaluation(state, {templates, evaluation}) { + state.templatesAvailablesForEvaluation.set(evaluation.id, templates); + }, + setTemplatesAvailablesForAction(state, templates) { + state.templatesAvailablesForAction = templates; }, setPersonsPickedIds(state, ids) { state.personsPicked = state.personsReachables @@ -328,36 +335,19 @@ const store = createStore({ commit('setEvaluationsForAction', data.results); }); }, - getReachableTemplatesForEvaluation({commit}) { - const - url = `/fr/doc/gen/templates/for/${evalFQDN}` - ; - window.fetch(url).then(r => { - if (r.ok) { - return r.json(); - } - throw new Error("not possible to load templates for evaluations") - }).then(data => { - commit('setTemplatesAvailableForEvaluation', data.results); - }).catch(e => { - console.error(e); - }) + addEvaluation({commit, dispatch}, evaluation) { + commit('addEvaluation', evaluation); + dispatch('fetchTemplatesAvailablesForEvaluation', evaluation); }, - generateDocument({ dispatch }, {key, templateId}) { - const callback = function(data) { - // get the evaluation id from the data - const - evaluationId = data.accompanyingPeriodWorkEvaluations.find(e => e.key === key).id, - returnPath = encodeURIComponent(window.location.pathname + window.location.search + window.location.hash), - url = `/fr/doc/gen/generate/from/${templateId}/for/${evalFQDN}/${evaluationId}?returnPath=${returnPath}` - ; - //http://localhost:8001/fr/doc/gen/generate/from/12/for/Chill%5CPersonBundle%5CEntity%5CAccompanyingPeriod%5CAccompanyingPeriodWorkEvaluation/41 - - console.log('I will generate your doc at', url); - window.location.assign(url); - }; - - dispatch('submit', callback); + fetchTemplatesAvailablesForEvaluation({commit, state}, evaluation) { + if (!state.templatesAvailablesForEvaluation.has(evaluation.id)) { + // commit an empty array to avoid parallel fetching for same evaluation id + commit('setTemplatesForEvaluation', {templates: [], evaluation}); + fetchResults(`/api/1.0/person/docgen/template/by-evaluation/${evaluation.id}.json`) + .then(templates => { + commit('setTemplatesForEvaluation', {templates, evaluation}); + }); + } }, submit({ getters, state, commit }, callback) { let @@ -368,47 +358,36 @@ const store = createStore({ commit('setIsPosting', true); - window.fetch(url, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(payload) - }).then(response => { - if (response.ok || response.status === 422) { - return response.json().then(data => ({data, status: response.status})); - } - - throw new Error(response.status); - }).then(({data, status}) => { - if (status === 422) { - for (let i in data.violations) { - errors.push(data.violations[i].title); + return makeFetch('PUT', url, payload) + .then(data => { + console.log('data received', data); + if (typeof(callback) !== 'undefined') { + return callback(data); + } else { + console.info('nothing to do here, bye bye');window.location.assign(`/fr/person/accompanying-period/${state.work.accompanyingPeriod.id}/work`); } - commit('setErrors', errors); + }).catch(error => { + console.log('error on submit', error); commit('setIsPosting', false); - } else if (typeof(callback) !== 'undefined') { - callback(data); - } else { - console.info('nothing to do here, bye bye'); - window.location.assign(`/fr/person/accompanying-period/${state.work.accompanyingPeriod.id}/work`); - } - }).catch(e => { - commit('setErrors', [ - 'Erreur serveur ou réseau: veuillez ré-essayer. Code erreur: ' + e - ]); - commit('setIsPosting', false); - }); - }, - initAsync({ dispatch }) { - dispatch('getReachablesResultsForAction'); - dispatch('getReachablesGoalsForAction'); - dispatch('getReachablesEvaluationsForAction'); - dispatch('getReachableTemplatesForEvaluation'); + commit('setErrors', error.violations); + }); }, } }); -store.dispatch('initAsync'); +store.commit('setEvaluationsPicked', window.accompanyingCourseWork.accompanyingPeriodWorkEvaluations); +store.dispatch('getReachablesResultsForAction'); +store.dispatch('getReachablesGoalsForAction'); +store.dispatch('getReachablesEvaluationsForAction'); + +store.state.evaluationsPicked.forEach(evaluation => { + store.dispatch('fetchTemplatesAvailablesForEvaluation', evaluation.evaluation) +}); + +fetchTemplates('Chill\\PersonBundle\\Entity\\AccompanyingPeriod\\AccompanyingPeriodWork') + .then(templates => { + store.commit('setTemplatesAvailablesForAction', templates); + } +) export { store }; From 29134f0f11431f85f8684be2c9f0c029a5617d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 20:24:31 +0100 Subject: [PATCH 30/41] continue context for evaluations --- .../AccompanyingPeriodWorkEvaluation.php | 32 +++++++++++------ .../Entity/SocialWork/Evaluation.php | 14 ++++---- ...ccompanyingPeriodWorkEvaluationContext.php | 34 +++++++++++++++++-- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php index cdcad98bd..9fcf492f6 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php @@ -44,7 +44,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU /** * @ORM\Column(type="text", nullable=false, options={"default": ""}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"write"}) * @Serializer\Groups({"accompanying_period_work_evaluation:create"}) */ @@ -52,7 +52,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?DateTimeImmutable $createdAt = null; @@ -60,7 +60,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU * @ORM\ManyToOne( * targetEntity=User::class * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?User $createdBy = null; @@ -76,7 +76,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"write"}) * @Serializer\Groups({"accompanying_period_work_evaluation:create"}) */ @@ -86,7 +86,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU * @ORM\ManyToOne( * targetEntity=Evaluation::class * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work_evaluation:create"}) */ private ?Evaluation $evaluation = null; @@ -95,7 +95,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; @@ -116,14 +116,14 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"accompanying_period_work_evaluation:create"}) */ private ?DateTimeImmutable $maxDate = null; /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\Groups({"write"}) * @Serializer\Groups({"accompanying_period_work_evaluation:create"}) */ @@ -131,7 +131,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?DateTimeImmutable $updatedAt = null; @@ -139,7 +139,7 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU * @ORM\ManyToOne( * targetEntity=User::class * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?User $updatedBy = null; @@ -244,6 +244,18 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU return $this->warningInterval; } + /** + * @Serializer\Groups({"docgen:read"}) + */ + public function getWarningDate(): ?DateTimeImmutable + { + if (null === $this->getWarningDate() || null === $this->getWarningInterval()) { + return null; + } + + return $this->getWarningDate()->sub($this->getWarningInterval()); + } + public function removeDocument(AccompanyingPeriodWorkEvaluationDocument $document): self { $this->documents->removeElement($document); diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php index 6b1d81700..fd846fbe9 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php @@ -28,21 +28,21 @@ class Evaluation * @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @Serializer\Groups({"read"}) */ - private $delay; + private ?\DateInterval $delay = null; /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ - private $id; + private ?int $id = null; /** * @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @Serializer\Groups({"read"}) */ - private $notificationDelay; + private ?\DateInterval $notificationDelay = null; /** * @ORM\ManyToOne( @@ -50,13 +50,13 @@ class Evaluation * inversedBy="evaluations" * ) */ - private $socialAction; + private ?SocialAction $socialAction = null; /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ - private $title = []; + private array $title = []; public function getDelay(): ?DateInterval { diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 1f17ce48a..39dfcd493 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -23,6 +23,8 @@ use Chill\PersonBundle\Repository\SocialWork\EvaluationRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; class AccompanyingPeriodWorkEvaluationContext implements DocGeneratorContextWithAdminFormInterface, @@ -34,17 +36,21 @@ class AccompanyingPeriodWorkEvaluationContext implements private EvaluationRepository $evaluationRepository; + private NormalizerInterface $normalizer; + private TranslatableStringHelperInterface $translatableStringHelper; public function __construct( AccompanyingPeriodWorkContext $accompanyingPeriodWorkContext, EntityManagerInterface $em, EvaluationRepository $evaluationRepository, + NormalizerInterface $normalizer, TranslatableStringHelperInterface $translatableStringHelper ) { $this->accompanyingPeriodWorkContext = $accompanyingPeriodWorkContext; $this->em = $em; $this->evaluationRepository = $evaluationRepository; + $this->normalizer = $normalizer; $this->translatableStringHelper = $translatableStringHelper; } @@ -92,14 +98,32 @@ class AccompanyingPeriodWorkEvaluationContext implements ]); } + /** + * @param AccompanyingPeriodWorkEvaluation $entity + * @return void + */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { - // TODO: Implement buildPublicForm() method. + $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); } + /** + * @param AccompanyingPeriodWorkEvaluation $entity + */ public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array { - // TODO: Implement getData() method. + $data = $this->accompanyingPeriodWorkContext + ->getData($template, $entity->getAccompanyingPeriodWork(), $contextGenerationData); + $data['evaluation'] = $this->normalizer->normalize( + $entity, + 'docgen', + [ + 'docgen:expect' => AccompanyingPeriodWorkEvaluation::class, + AbstractNormalizer::GROUPS => ['docgen:read'], + ] + ); + + return $data; } public function getDescription(): string @@ -127,9 +151,13 @@ class AccompanyingPeriodWorkEvaluationContext implements return true; } + /** + * @param AccompanyingPeriodWorkEvaluation $entity + */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool { - // TODO: Implement hasPublicForm() method. + return $this->accompanyingPeriodWorkContext + ->hasPublicForm($template, $entity->getAccompanyingPeriodWork()); } public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void From 6c1a9466086540c1742ff067482a15316ef62989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Dec 2021 23:31:19 +0100 Subject: [PATCH 31/41] generate context for evaluations --- ...eneratorContextWithPublicFormInterface.php | 2 ++ .../DocGeneratorTemplateController.php | 2 +- .../Normalizer/DocGenObjectNormalizer.php | 22 ++++++++++++++++++- .../AccompanyingPeriodWorkEvaluation.php | 4 ++-- .../AccompanyingPeriodContext.php | 8 ++++++- .../AccompanyingPeriodWorkContext.php | 13 ++++++++++- ...ccompanyingPeriodWorkEvaluationContext.php | 9 ++++++++ 7 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php index 402802501..894bf058c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php @@ -29,4 +29,6 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext * @param mixed $entity */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; + + public function getFormData(DocGeneratorTemplate $template, $entity): array; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 1f5043d82..1f729e066 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -106,7 +106,7 @@ final class DocGeneratorTemplateController extends AbstractController if ($context instanceof DocGeneratorContextWithPublicFormInterface && $context->hasPublicForm($template, $entity)) { - $builder = $this->createFormBuilder(); + $builder = $this->createFormBuilder($context->getFormData($template, $entity)); $context->buildPublicForm($builder, $template, $entity); $form = $builder->getForm()->handleRequest($request); diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index b57577848..c081a63c2 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -99,6 +99,24 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte } $type = $reflection->getProperty($attribute->getName())->getType(); + } elseif ($reflection->hasMethod($method = 'get'.ucfirst($attribute->getName()))) { + if (!$reflection->getMethod($method)->hasReturnType()) { + throw new \LogicException(sprintf( + 'Could not determine how the content is determined for the attribute %s. Add a return type on the method', + $attribute->getName() + )); + } + + $type = $reflection->getMethod($method)->getReturnType(); + } elseif ($reflection->hasMethod($method = 'is'.ucfirst($attribute->getName()))) { + if (!$reflection->getMethod($method)->hasReturnType()) { + throw new \LogicException(sprintf( + 'Could not determine how the content is determined for the attribute %s. Add a return type on the method', + $attribute->getName() + )); + } + + $type = $reflection->getMethod($method)->getReturnType(); } elseif ($reflection->hasMethod($attribute->getName())) { if (!$reflection->getMethod($attribute->getName())->hasReturnType()) { throw new \LogicException(sprintf( @@ -149,11 +167,13 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte switch ($type) { case 'array': + return []; case 'bool': case 'double': case 'float': case 'int': case 'resource': + return null; case 'string': return ''; @@ -197,7 +217,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte } elseif (null === $value) { $data[$key] = $this->normalizeNullOutputValue($format, $context, $attribute, $reflection); } else { - $data[$key] = (string) $value; + $data[$key] = $value; } } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php index 9fcf492f6..d5f883e75 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php @@ -249,11 +249,11 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU */ public function getWarningDate(): ?DateTimeImmutable { - if (null === $this->getWarningDate() || null === $this->getWarningInterval()) { + if (null === $this->getEndDate() || null === $this->getWarningInterval()) { return null; } - return $this->getWarningDate()->sub($this->getWarningInterval()); + return $this->getEndDate()->sub($this->getWarningInterval()); } public function removeDocument(AccompanyingPeriodWorkEvaluationDocument $document): self diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 5227770eb..cab195530 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -34,7 +34,6 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use function array_key_exists; class AccompanyingPeriodContext implements - DocGeneratorContextInterface, DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface { @@ -173,6 +172,13 @@ class AccompanyingPeriodContext implements return AccompanyingPeriod::class; } + public function getFormData(DocGeneratorTemplate $template, $entity): array + { + return [ + 'course' => $entity + ]; + } + public static function getKey(): string { return self::class; diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php index 22c898f9d..3ade3ac4f 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -53,9 +53,12 @@ class AccompanyingPeriodWorkContext implements $this->periodContext->buildAdminForm($builder); } + /** + * @param AccompanyingPeriodWork $entity + */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { - $this->periodContext->buildPublicForm($builder, $template, $entity); + $this->periodContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriod()); } /** @@ -82,6 +85,14 @@ class AccompanyingPeriodWorkContext implements return AccompanyingPeriodWork::class; } + /** + * @param AccompanyingPeriodWork $entity + */ + public function getFormData(DocGeneratorTemplate $template, $entity): array + { + return $this->periodContext->getFormData($template, $entity->getAccompanyingPeriod()); + } + public static function getKey(): string { return 'accompanying_period_work_regular'; diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 39dfcd493..e1aeed231 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -136,6 +136,15 @@ class AccompanyingPeriodWorkEvaluationContext implements return AccompanyingPeriodWorkEvaluation::class; } + /** + * @param AccompanyingPeriodWorkEvaluation $entity + */ + public function getFormData(DocGeneratorTemplate $template, $entity): array + { + return $this->accompanyingPeriodWorkContext->getFormData($template, + $entity->getAccompanyingPeriodWork()); + } + public static function getKey(): string { return 'accompanying_period_work_evaluation_regular'; From 063ceb2c5330060b0a43d75cd1ca746e8ce9f707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 4 Dec 2021 01:13:55 +0100 Subject: [PATCH 32/41] various improvements --- ...eneratorContextWithPublicFormInterface.php | 4 +- .../AdminDocGeneratorTemplateController.php | 19 ++++++ .../DocGeneratorTemplateController.php | 7 ++- .../public/vuejs/_components/PickTemplate.vue | 59 ++++++++++++++----- .../Admin/DocGeneratorTemplate/edit.html.twig | 10 +++- .../Admin/DocGeneratorTemplate/new.html.twig | 11 +++- .../views/Generator/basic_form.html.twig | 7 ++- .../Normalizer/DocGenObjectNormalizer.php | 6 +- .../translations/messages.fr.yml | 3 + ...nyingPeriodWorkEvaluationApiController.php | 22 ++++--- .../AccompanyingPeriodWorkEvaluation.php | 10 ++-- .../Entity/SocialWork/Evaluation.php | 4 +- .../vuejs/AccompanyingCourseWorkEdit/App.vue | 20 ++++--- .../AccompanyingPeriodContext.php | 36 ++++++++--- .../AccompanyingPeriodWorkContext.php | 2 + ...ccompanyingPeriodWorkEvaluationContext.php | 7 ++- .../translations/messages.fr.yml | 5 ++ 17 files changed, 163 insertions(+), 69 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php index 894bf058c..e207cfb25 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php @@ -23,12 +23,12 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; + public function getFormData(DocGeneratorTemplate $template, $entity): array; + /** * has form. * * @param mixed $entity */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; - - public function getFormData(DocGeneratorTemplate $template, $entity): array; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index e41c0c186..f1db9e16f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -59,4 +59,23 @@ class AdminDocGeneratorTemplateController extends CRUDController 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] + ); + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 1f729e066..02f164a78 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -113,10 +113,11 @@ final class DocGeneratorTemplateController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $contextGenerationData = $form->getData(); } elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) { - $template = '@ChillDocGenerator/Generator/basic_form.html.twig'; - $templateOptions = ['entity' => $entity, 'form' => $form->createView(), 'template' => $template]; + $templatePath = '@ChillDocGenerator/Generator/basic_form.html.twig'; + $templateOptions = ['entity' => $entity, 'form' => $form->createView(), + 'template' => $template, 'context' => $context]; - return $this->render($template, $templateOptions); + return $this->render($templatePath, $templateOptions); } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue index 1052fe75f..f1fb6c84b 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/public/vuejs/_components/PickTemplate.vue @@ -1,21 +1,39 @@ @@ -106,6 +124,15 @@ export default { window.location.assign(url); }, }, + i18n: { + messages: { + fr: { + generate_document: 'Générer un document', + select_a_template: 'Choisir un gabarit', + choose_a_template: 'Choisir', + } + } + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig index 77c9f3e10..93b08f688 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/edit.html.twig @@ -1,11 +1,15 @@ {% extends '@ChillDocGenerator/Admin/layout.html.twig' %} -{% block title %} - {% include('@ChillMain/CRUD/_edit_title.html.twig') %} -{% endblock %} +{% block title 'docgen.Edit template'|trans %} {% block layout_wvm_content %} {% embed '@ChillMain/CRUD/_edit_content.html.twig' %} + + {% block crud_content_header %} +

{{ 'docgen.Edit template'|trans }}

+

{{ 'docgen.With context %name%'|trans({'%name%': context.name|trans }) }}

+ {% endblock crud_content_header %} + {% block content_form_actions_view %}{% endblock %} {% block content_form_actions_save_and_show %}{% endblock %} {% endembed %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig index baf409800..fe4991ffa 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/new.html.twig @@ -1,12 +1,17 @@ {% extends '@ChillDocGenerator/Admin/layout.html.twig' %} -{% block title %} - {% include('@ChillMain/CRUD/_new_title.html.twig') %} -{% endblock %} +{% block title 'docgen.New template'|trans %} {% block layout_wvm_content %} {% embed '@ChillMain/CRUD/_new_content.html.twig' %} + + {% block crud_content_header %} +

{{ 'docgen.New template'|trans }}

+

{{ 'docgen.With context %name%'|trans({'%name%': context.name|trans }) }}

+ {% endblock crud_content_header %} + {% block content_form_actions_save_and_show %}{% endblock %} + {% endembed %} {% endblock %} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig index 58d4b059f..7b24eae0d 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Generator/basic_form.html.twig @@ -1,10 +1,11 @@ {% extends 'ChillMainBundle::layout.html.twig' %} -{% block title 'Generate document'|trans %} +{% block title 'docgen.Generate a document'|trans %} {% block content %}

{{ block('title') }}

+

{{ template.name|localize_translatable_string }}

{{ form_start(form, { 'attr': { 'id': 'generate_doc_form' }}) }} {{ form(form) }} @@ -12,8 +13,8 @@
  • -
diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index c081a63c2..0a29af940 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -99,7 +99,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte } $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()) { throw new \LogicException(sprintf( '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(); - } elseif ($reflection->hasMethod($method = 'is'.ucfirst($attribute->getName()))) { + } elseif ($reflection->hasMethod($method = 'is' . ucfirst($attribute->getName()))) { if (!$reflection->getMethod($method)->hasReturnType()) { throw new \LogicException(sprintf( '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) { case 'array': return []; + case 'bool': case 'double': case 'float': case 'int': case 'resource': return null; + case 'string': return ''; diff --git a/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml b/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml index e69de29bb..ebfe5320f 100644 --- a/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml @@ -0,0 +1,3 @@ +docgen: + Generate a document: Génerer un document + Generate: Génerer diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php index 29b00cf4f..ddd3445a4 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingPeriodWorkEvaluationApiController.php @@ -15,7 +15,6 @@ use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; -use Chill\MainBundle\Serializer\Normalizer\CollectionNormalizer; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Symfony\Component\HttpFoundation\JsonResponse; @@ -23,15 +22,17 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\SerializerInterface; +use function count; +use function in_array; class AccompanyingPeriodWorkEvaluationApiController { private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; - private SerializerInterface $serializer; - private PaginatorFactory $paginatorFactory; + private SerializerInterface $serializer; + public function __construct( DocGeneratorTemplateRepository $docGeneratorTemplateRepository, SerializerInterface $serializer, @@ -44,21 +45,22 @@ class AccompanyingPeriodWorkEvaluationApiController /** * @Route("/api/1.0/person/docgen/template/by-evaluation/{id}.{_format}", - * requirements={"format": "json"}) + * requirements={"format": "json"}) */ public function listTemplateByEvaluation(Evaluation $evaluation, string $_format): JsonResponse { if ('json' !== $_format) { - throw new BadRequestHttpException("format not supported"); + throw new BadRequestHttpException('format not supported'); } $evaluations = array_filter( $this->docGeneratorTemplateRepository ->findByEntity(AccompanyingPeriodWorkEvaluation::class), - function (DocGeneratorTemplate $t) use ($evaluation) { + static function (DocGeneratorTemplate $t) use ($evaluation) { $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)); return new JsonResponse($this->serializer->serialize( - new Collection($evaluations, $paginator), 'json', [ - AbstractNormalizer::GROUPS => ['read'] + new Collection($evaluations, $paginator), + 'json', + [ + AbstractNormalizer::GROUPS => ['read'], ] ), JsonResponse::HTTP_OK, [], true); } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php index d5f883e75..479bbfb87 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWorkEvaluation.php @@ -239,11 +239,6 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU return $this->updatedBy; } - public function getWarningInterval(): ?DateInterval - { - return $this->warningInterval; - } - /** * @Serializer\Groups({"docgen:read"}) */ @@ -256,6 +251,11 @@ class AccompanyingPeriodWorkEvaluation implements TrackCreationInterface, TrackU return $this->getEndDate()->sub($this->getWarningInterval()); } + public function getWarningInterval(): ?DateInterval + { + return $this->warningInterval; + } + public function removeDocument(AccompanyingPeriodWorkEvaluationDocument $document): self { $this->documents->removeElement($document); diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php index fd846fbe9..4c21cd348 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Evaluation.php @@ -28,7 +28,7 @@ class Evaluation * @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @Serializer\Groups({"read"}) */ - private ?\DateInterval $delay = null; + private ?DateInterval $delay = null; /** * @ORM\Id @@ -42,7 +42,7 @@ class Evaluation * @ORM\Column(type="dateinterval", nullable=true, options={"default": null}) * @Serializer\Groups({"read"}) */ - private ?\DateInterval $notificationDelay = null; + private ?DateInterval $notificationDelay = null; /** * @ORM\ManyToOne( diff --git a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue index b7a137de2..0bdb91ab5 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue +++ b/src/Bundle/ChillPersonBundle/Resources/public/vuejs/AccompanyingCourseWorkEdit/App.vue @@ -195,15 +195,17 @@
- - - +
+ + + +

{{ $t('fix_these_errors') }}

diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index cab195530..136cb751b 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -11,7 +11,6 @@ declare(strict_types=1); namespace Chill\PersonBundle\Service\DocGenerator; -use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; @@ -29,8 +28,10 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Contracts\Translation\TranslatorInterface; use function array_key_exists; class AccompanyingPeriodContext implements @@ -47,28 +48,26 @@ class AccompanyingPeriodContext implements private TranslatableStringHelperInterface $translatableStringHelper; + private TranslatorInterface $translator; + public function __construct( DocumentCategoryRepository $documentCategoryRepository, NormalizerInterface $normalizer, TranslatableStringHelperInterface $translatableStringHelper, EntityManagerInterface $em, - PersonRender $personRender + PersonRender $personRender, + TranslatorInterface $translator ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; $this->translatableStringHelper = $translatableStringHelper; $this->em = $em; $this->personRender = $personRender; + $this->translator = $translator; } public function adminFormReverseTransform(array $data): array { - $data = [ - 'mainPerson' => $data['mainPerson'], - 'person1' => $data['person1'], - 'person2' => $data['person2'], - ]; - if (array_key_exists('category', $data)) { $data['category'] = [ 'idInsideBundle' => $data['category']->getIdInsideBundle(), @@ -83,8 +82,11 @@ class AccompanyingPeriodContext implements { $data = [ 'mainPerson' => $data['mainPerson'] ?? false, + 'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'), 'person1' => $data['person1'] ?? false, + 'person1Label' => $data['person1Label'] ?? $this->translator->trans('docgen.person 1'), 'person2' => $data['person2'] ?? false, + 'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'), ]; if (array_key_exists('category', $data)) { @@ -100,12 +102,27 @@ class AccompanyingPeriodContext implements $builder ->add('mainPerson', CheckboxType::class, [ 'required' => false, + 'label' => 'Ask for main person', + ]) + ->add('mainPersonLabel', TextType::class, [ + 'label' => 'main person label', + 'required' => true, ]) ->add('person1', CheckboxType::class, [ 'required' => false, + 'label' => 'Ask for person 1' + ]) + ->add('person1Label', TextType::class, [ + 'label' => 'person 1 label', + 'required' => true, ]) ->add('person2', CheckboxType::class, [ 'required' => false, + 'label' => 'Ask for person 2' + ]) + ->add('person2Label', TextType::class, [ + 'label' => 'person 2 label', + 'required' => true, ]) ->add('category', EntityType::class, [ 'placeholder' => 'Choose a document category', @@ -138,6 +155,7 @@ class AccompanyingPeriodContext implements 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, 'multiple' => false, 'expanded' => true, + 'label' => $options[$key.'Label'], ]); } } @@ -175,7 +193,7 @@ class AccompanyingPeriodContext implements public function getFormData(DocGeneratorTemplate $template, $entity): array { return [ - 'course' => $entity + 'course' => $entity, ]; } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php index 3ade3ac4f..5041d9180 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -51,6 +51,8 @@ class AccompanyingPeriodWorkContext implements public function buildAdminForm(FormBuilderInterface $builder): void { $this->periodContext->buildAdminForm($builder); + + $builder->remove('category'); } /** diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index e1aeed231..2ffcaf818 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -100,7 +100,6 @@ class AccompanyingPeriodWorkEvaluationContext implements /** * @param AccompanyingPeriodWorkEvaluation $entity - * @return void */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { @@ -141,8 +140,10 @@ class AccompanyingPeriodWorkEvaluationContext implements */ public function getFormData(DocGeneratorTemplate $template, $entity): array { - return $this->accompanyingPeriodWorkContext->getFormData($template, - $entity->getAccompanyingPeriodWork()); + return $this->accompanyingPeriodWorkContext->getFormData( + $template, + $entity->getAccompanyingPeriodWork() + ); } public static function getKey(): string diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 904ec8811..f2125d5c6 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -448,3 +448,8 @@ Household addresses: Adresses de domicile Insert an address: Insérer une adresse see social issues: Voir les problématiques sociales see persons associated: Voir les usagers concernés + +docgen: + Main person: Personne principale + person 1: Première personne + person 2: Deuxième personne From 51fd81c6611b7ffdf472331cefc892873639b5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sat, 4 Dec 2021 01:14:37 +0100 Subject: [PATCH 33/41] code style --- .../AdminDocGeneratorTemplateController.php | 42 ++++++++++--------- .../DocGeneratorTemplateController.php | 2 +- .../AccompanyingPeriodContext.php | 6 +-- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index f1db9e16f..57d4cd487 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -27,6 +27,29 @@ class AdminDocGeneratorTemplateController extends CRUDController $this->contextManager = $contextManager; } + public 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] + ); + } + public function new(Request $request): Response { if (!$request->query->has('context')) { @@ -59,23 +82,4 @@ class AdminDocGeneratorTemplateController extends CRUDController 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] - ); - } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 02f164a78..2a175eb27 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -115,7 +115,7 @@ final class DocGeneratorTemplateController extends AbstractController } elseif (!$form->isSubmitted() || ($form->isSubmitted() && !$form->isValid())) { $templatePath = '@ChillDocGenerator/Generator/basic_form.html.twig'; $templateOptions = ['entity' => $entity, 'form' => $form->createView(), - 'template' => $template, 'context' => $context]; + 'template' => $template, 'context' => $context, ]; return $this->render($templatePath, $templateOptions); } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 136cb751b..5bc218ce9 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -110,7 +110,7 @@ class AccompanyingPeriodContext implements ]) ->add('person1', CheckboxType::class, [ 'required' => false, - 'label' => 'Ask for person 1' + 'label' => 'Ask for person 1', ]) ->add('person1Label', TextType::class, [ 'label' => 'person 1 label', @@ -118,7 +118,7 @@ class AccompanyingPeriodContext implements ]) ->add('person2', CheckboxType::class, [ 'required' => false, - 'label' => 'Ask for person 2' + 'label' => 'Ask for person 2', ]) ->add('person2Label', TextType::class, [ 'label' => 'person 2 label', @@ -155,7 +155,7 @@ class AccompanyingPeriodContext implements 'choice_label' => function (Person $p) { return $this->personRender->renderString($p, []); }, 'multiple' => false, 'expanded' => true, - 'label' => $options[$key.'Label'], + 'label' => $options[$key . 'Label'], ]); } } From c4998f4ac1051164c322cc78a33866cb93bd6484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 13:18:59 +0100 Subject: [PATCH 34/41] normalization for docgen:read: add groups and so on (wip) --- .../DocGeneratorTemplateController.php | 11 ++-- .../Normalizer/DocGenObjectNormalizer.php | 12 ++++- src/Bundle/ChillMainBundle/Entity/Scope.php | 4 +- .../Serializer/Normalizer/UserNormalizer.php | 2 +- .../Entity/AccompanyingPeriod.php | 8 ++- .../Entity/AccompanyingPeriod/Resource.php | 13 ++--- .../AccompanyingPeriodParticipation.php | 24 ++++----- .../Entity/Household/Household.php | 13 +++-- .../Entity/Household/HouseholdMember.php | 14 ++--- .../Entity/Household/Position.php | 8 +-- .../Entity/SocialWork/Goal.php | 4 +- .../Entity/SocialWork/Result.php | 4 +- .../Normalizer/SocialActionNormalizer.php | 51 +++++++++++++++---- .../Normalizer/SocialIssueNormalizer.php | 51 +++++++++++++++---- ...AccompanyingPeriodDocGenNormalizerTest.php | 17 +++++++ 15 files changed, 166 insertions(+), 70 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 2a175eb27..3267b2225 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -90,11 +90,6 @@ final class DocGeneratorTemplateController extends AbstractController int $entityId, Request $request ): Response { - $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); - - if (null === $entity) { - throw new NotFoundHttpException("Entity with classname {$entityClassName} and id {$entityId} is not found"); - } try { $context = $this->contextManager->getContextByDocGeneratorTemplate($template); @@ -102,6 +97,12 @@ final class DocGeneratorTemplateController extends AbstractController throw new NotFoundHttpException($e->getMessage(), $e); } + $entity = $this->getDoctrine()->getRepository($context->getEntityClass())->find($entityId); + + if (null === $entity) { + throw new NotFoundHttpException("Entity with classname {$entityClassName} and id {$entityId} is not found"); + } + $contextGenerationData = []; if ($context instanceof DocGeneratorContextWithPublicFormInterface diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index 0a29af940..4f0a9f1ab 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -210,7 +210,17 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte $value = $this->propertyAccess->getValue($object, $attribute->getName()); $key = $attribute->getSerializedName() ?? $attribute->getName(); - if (is_object($value)) { + if (is_iterable($value)) { + $arr = []; + foreach ($value as $k => $v) { + $arr[$k] = + $this->normalizer->normalize($v, $format, array_merge( + $context, + $attribute->getNormalizationContextForGroups($expectedGroups) + )); + } + $data[$key] = $arr; + } elseif (is_object($value)) { $data[$key] = $this->normalizer->normalize($value, $format, array_merge( $context, diff --git a/src/Bundle/ChillMainBundle/Entity/Scope.php b/src/Bundle/ChillMainBundle/Entity/Scope.php index 3a6cbd953..fc6084883 100644 --- a/src/Bundle/ChillMainBundle/Entity/Scope.php +++ b/src/Bundle/ChillMainBundle/Entity/Scope.php @@ -33,7 +33,7 @@ class Scope * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private $id; @@ -43,7 +43,7 @@ class Scope * @var array * * @ORM\Column(type="json") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private $name = []; diff --git a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php index f046cb34f..22219b108 100644 --- a/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php +++ b/src/Bundle/ChillMainBundle/Serializer/Normalizer/UserNormalizer.php @@ -45,6 +45,6 @@ class UserNormalizer implements NormalizerAwareInterface, NormalizerInterface public function supportsNormalization($data, ?string $format = null): bool { - return 'json' === $format && $data instanceof User; + return $data instanceof User && ('json' === $format || 'docgen' === $format); } } diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index 92d84610f..dcd3c2df0 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -259,7 +259,7 @@ class AccompanyingPeriod implements * cascade={"persist", "remove"}, * orphanRemoval=true * ) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) * @ResourceDuplicateCheck(groups={AccompanyingPeriod::STEP_DRAFT, AccompanyingPeriod::STEP_CONFIRMED, "Default", "default"}) */ private Collection $resources; @@ -274,7 +274,7 @@ class AccompanyingPeriod implements * joinColumns={@ORM\JoinColumn(name="accompanying_period_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="id")} * ) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) * @Assert\Count(min=1, groups={AccompanyingPeriod::STEP_CONFIRMED}, minMessage="A course must be associated to at least one scope") */ private Collection $scopes; @@ -340,6 +340,7 @@ class AccompanyingPeriod implements $this->socialIssues = new ArrayCollection(); $this->comments = new ArrayCollection(); $this->works = new ArrayCollection(); + $this->resources = new ArrayCollection(); } /** @@ -575,6 +576,9 @@ class AccompanyingPeriod implements return $this->createdBy; } + /** + * @Groups({"docgen:read"}) + */ public function getCurrentParticipations(): Collection { return $this->getOpenParticipations(); diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Resource.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Resource.php index ea20666fb..a11dc9b3d 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Resource.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/Resource.php @@ -41,11 +41,10 @@ class Resource * ) * @ORM\JoinColumn(nullable=false) */ - private $accompanyingPeriod; + private ?AccompanyingPeriod $accompanyingPeriod = null; /** * @ORM\ManyToOne(targetEntity=Comment::class) - * @ORM\JoinColumn(nullable=true) */ private $comment; @@ -53,21 +52,23 @@ class Resource * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ - private $id; + private ?int $id = null; /** * @ORM\ManyToOne(targetEntity=Person::class) * @ORM\JoinColumn(nullable=true) + * @Groups({"docgen:read"}) */ - private $person; + private ?Person $person = null; /** * @ORM\ManyToOne(targetEntity=ThirdParty::class) * @ORM\JoinColumn(nullable=true) + * @Groups({"docgen:read"}) */ - private $thirdParty; + private ?ThirdParty $thirdParty = null; public function getAccompanyingPeriod(): ?AccompanyingPeriod { diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php index 862d268d2..ee3ac7dc9 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php @@ -32,38 +32,38 @@ class AccompanyingPeriodParticipation * @ORM\ManyToOne(targetEntity=AccompanyingPeriod::class, inversedBy="participations", cascade={"persist"}) * @ORM\JoinColumn(name="accompanyingperiod_id", referencedColumnName="id", nullable=false) */ - private $accompanyingPeriod; + private ?AccompanyingPeriod $accompanyingPeriod = null; /** * @ORM\Column(type="date", nullable=true) - * @Groups({"read", "read:docgen"}) + * @Groups({"read", "docgen:read"}) */ - private $endDate; + private ?\DateTime $endDate = null; /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Groups({"read", "read:docgen"}) + * @Groups({"read", "docgen:read"}) */ - private $id; + private ?int $id = null; /** * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="accompanyingPeriodParticipations") * @ORM\JoinColumn(name="person_id", referencedColumnName="id", nullable=false) - * @Groups({"read", "read:docgen"}) + * @Groups({"read", "docgen:read"}) */ - private $person; + private ?Person $person = null; /** * @ORM\Column(type="date", nullable=false) - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ - private $startDate; + private ?\DateTime $startDate = null; public function __construct(AccompanyingPeriod $accompanyingPeriod, Person $person) { - $this->startDate = new DateTimeImmutable('now'); + $this->startDate = new \DateTime('now'); $this->accompanyingPeriod = $accompanyingPeriod; $this->person = $person; } @@ -73,10 +73,6 @@ class AccompanyingPeriodParticipation return $this->accompanyingPeriod; } - /* - * public function setStartDate(\DateTimeInterface $startDate): self { $this->startDate = $startDate; return $this; } - */ - public function getEndDate(): ?DateTimeInterface { return $this->endDate; diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index c03644214..c5adbbd6a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -59,7 +59,7 @@ class Household * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; @@ -68,17 +68,19 @@ class Household * targetEntity=HouseholdMember::class, * mappedBy="household" * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private Collection $members; /** * @ORM\Column(type="boolean", name="waiting_for_birth", options={"default": false}) + * @Serializer\Groups({"docgen:read"}) */ private bool $waitingForBirth = false; /** * @ORM\Column(type="date_immutable", name="waiting_for_birth_date", nullable=true, options={"default": null}) + * @Serializer\Groups({"docgen:read"}) */ private ?DateTimeImmutable $waitingForBirthDate = null; @@ -134,7 +136,7 @@ class Household } /** - * @Serializer\Groups({ "read" }) + * @Serializer\Groups({"read", "docgen:read"}) * @Serializer\SerializedName("current_address") */ public function getCurrentAddress(?DateTime $at = null): ?Address @@ -154,6 +156,11 @@ class Household return null; } + /** + * @param DateTimeImmutable|null $now + * @return Collection + * @Serializer\Groups({"docgen:read"}) + */ public function getCurrentMembers(?DateTimeImmutable $now = null): Collection { return $this->getMembers()->matching($this->buildCriteriaCurrentMembers($now)); diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php index af3ead806..829bc3ade 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php @@ -28,13 +28,13 @@ class HouseholdMember { /** * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?string $comment = null; /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\GreaterThan( * propertyPath="startDate", * message="household_membership.The end date must be after start date", @@ -45,7 +45,7 @@ class HouseholdMember /** * @ORM\Column(type="boolean", options={"default": false}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private bool $holder = false; @@ -63,7 +63,7 @@ class HouseholdMember * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private $id; @@ -72,7 +72,7 @@ class HouseholdMember * @ORM\ManyToOne( * targetEntity="\Chill\PersonBundle\Entity\Person" * ) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\Valid(groups={"household_memberships"}) * @Assert\NotNull(groups={"household_memberships"}) */ @@ -80,7 +80,7 @@ class HouseholdMember /** * @ORM\ManyToOne(targetEntity=Position::class) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\NotNull(groups={"household_memberships_created"}) */ private ?Position $position = null; @@ -92,7 +92,7 @@ class HouseholdMember /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) * @Assert\NotNull(groups={"household_memberships"}) */ private ?DateTimeImmutable $startDate = null; diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Position.php b/src/Bundle/ChillPersonBundle/Entity/Household/Position.php index 721c35433..b8f707126 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Position.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Position.php @@ -33,25 +33,25 @@ class Position * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({ "read" }) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id; /** * @ORM\Column(type="json") - * @Serializer\Groups({ "read" }) + * @Serializer\Groups({"read", "docgen:read"}) */ private array $label = []; /** * @ORM\Column(type="float") - * @Serializer\Groups({ "read" }) + * @Serializer\Groups({"read"}) */ private float $ordering = 0.00; /** * @ORM\Column(type="boolean") - * @Serializer\Groups({ "read" }) + * @Serializer\Groups({"read"}) */ private bool $shareHouseHold = true; diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Goal.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Goal.php index 9c02a129d..f3d8f5289 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Goal.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Goal.php @@ -38,7 +38,7 @@ class Goal * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private $id; @@ -55,7 +55,7 @@ class Goal /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private $title = []; diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Result.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Result.php index b2894d210..17d4ddd0a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/Result.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/Result.php @@ -55,7 +55,7 @@ class Result * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private $id; @@ -66,7 +66,7 @@ class Result /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private $title = []; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php index 94341197b..96af56e95 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Serializer\Normalizer; use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -30,18 +31,48 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte public function normalize($socialAction, ?string $format = null, array $context = []) { - return [ - 'id' => $socialAction->getId(), - 'type' => 'social_work_social_action', - 'text' => $this->render->renderString($socialAction, []), - 'parent' => $this->normalizer->normalize($socialAction->getParent()), - 'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()), - 'title' => $socialAction->getTitle(), - ]; + switch ($format) { + case 'json': + return [ + 'id' => $socialAction->getId(), + 'type' => 'social_work_social_action', + 'text' => $this->render->renderString($socialAction, []), + 'parent' => $this->normalizer->normalize($socialAction->getParent()), + 'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()), + 'title' => $socialAction->getTitle(), + ]; + case 'docgen': + + if (null === $socialAction) { + return ['id' => 0, 'title' => '', 'text' => '']; + } + + return [ + 'id' => $socialAction->getId(), + 'text' => $this->render->renderString($socialAction, []), + 'title' => $socialAction->getTitle(), + ]; + default: + throw new \Symfony\Component\Serializer\Exception\RuntimeException("format not supported"); + } } - public function supportsNormalization($data, ?string $format = null) + public function supportsNormalization($data, string $format = null, array $context = []) { - return $data instanceof SocialAction; + if ($data instanceof SocialAction && 'json' === $format) { + return true; + } + + if ('docgen' === $format) { + if ($data instanceof SocialAction) { + return true; + } + + if (null === $data && ($context['docgen:expects'] ?? null) === SocialAction::class) { + return true; + } + } + + return false; } } diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php index f255e6f8c..cfe7c0895 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php @@ -13,11 +13,12 @@ namespace Chill\PersonBundle\Serializer\Normalizer; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; +use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -class SocialIssueNormalizer implements NormalizerAwareInterface, NormalizerInterface +class SocialIssueNormalizer implements NormalizerAwareInterface, ContextAwareNormalizerInterface { use NormalizerAwareTrait; @@ -31,18 +32,46 @@ class SocialIssueNormalizer implements NormalizerAwareInterface, NormalizerInter public function normalize($socialIssue, ?string $format = null, array $context = []) { /** @var SocialIssue $socialIssue */ - return [ - 'type' => 'social_issue', - 'id' => $socialIssue->getId(), - 'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null, - 'children_ids' => $socialIssue->getChildren()->map(static function (SocialIssue $si) { return $si->getId(); }), - 'title' => $socialIssue->getTitle(), - 'text' => $this->render->renderString($socialIssue, []), - ]; + switch ($format) { + case 'json': + return [ + 'type' => 'social_issue', + 'id' => $socialIssue->getId(), + 'parent_id' => $socialIssue->hasParent() ? $socialIssue->getParent()->getId() : null, + 'children_ids' => $socialIssue->getChildren()->map(static function (SocialIssue $si) { return $si->getId(); }), + 'title' => $socialIssue->getTitle(), + 'text' => $this->render->renderString($socialIssue, []), + ]; + case 'docgen': + + if (null === $socialIssue) { + return ['id' => 0, 'title' => '', 'text' => '']; + } + + return [ + 'id' => $socialIssue->getId(), + 'title' => $socialIssue->getTitle(), + 'text' => $this->render->renderString($socialIssue, []), + ]; + } } - public function supportsNormalization($data, ?string $format = null): bool + public function supportsNormalization($data, string $format = null, array $context = []) { - return $data instanceof SocialIssue; + if ($data instanceof SocialIssue && 'json' === $format) { + return true; + } + + if ('docgen' === $format) { + if ($data instanceof SocialIssue) { + return true; + } + + if (null === $data && ($context['docgen:expects'] ?? null) === SocialIssue::class) { + return true; + } + } + + return false; } } diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php index 4c08db326..bd104bbe8 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php @@ -13,6 +13,7 @@ namespace Serializer\Normalizer; use Chill\MainBundle\Entity\Scope; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; @@ -79,6 +80,22 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase } } + public function testNormalizeParticipations() + { + $period = new AccompanyingPeriod(); + $period->addPerson($person = new Person()); + $person->setFirstName('test'); + + $data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + $this->assertIsArray($data); + $this->assertArrayHasKey('participations', $data); + $this->assertCount(1, $data['participations']); + + $this->assertArrayHasKey('currentParticipations', $data); + $this->assertCount(1, $data['currentParticipations']); + } + public function testNormalizeNull() { $data = $this->normalizer->normalize(null, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); From 2d1e8b2acde74475d4adb09393598d713ee094f0 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 6 Dec 2021 14:45:44 +0000 Subject: [PATCH 35/41] =?UTF-8?q?Coh=C3=A9rence=20graphique=20des=20listes?= =?UTF-8?q?=20d'activit=C3=A9s=20et=20d'actions=20dans=20le=20contexte=20d?= =?UTF-8?q?'un=20parcours?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 +- .../Resources/public/chill/chillactivity.scss | 19 +- .../Activity/activity-badge-title.html.twig | 110 --------- .../views/Activity/concernedGroups.html.twig | 2 +- .../Resources/views/Activity/list.html.twig | 177 ++++++++++++--- .../Activity/listAccompanyingCourse.html.twig | 4 +- .../views/Activity/list_recent.html.twig | 84 ++++++- .../translations/messages.fr.yml | 2 + .../public/chill/scss/flex_table.scss | 14 ++ .../chill/scss/accompanying_period_work.scss | 49 +--- .../Resources/public/chill/scss/badge.scss | 129 ++++++----- .../public/chill/scss/flex_table.scss | 6 - .../Resources/public/chill/scss/mixins.scss | 5 +- .../views/AccompanyingCourse/index.html.twig | 4 +- .../AccompanyingCourseWork/delete.html.twig | 2 +- .../AccompanyingCourseWork/index.html.twig | 212 +++++++++++++++++- .../list_by_accompanying_period.html.twig | 118 ---------- ...st_recent_by_accompanying_period.html.twig | 13 +- .../translations/messages.fr.yml | 5 + 19 files changed, 561 insertions(+), 400 deletions(-) delete mode 100644 src/Bundle/ChillActivityBundle/Resources/views/Activity/activity-badge-title.html.twig delete mode 100644 src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_by_accompanying_period.html.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index ba2c90409..65b9864c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ and this project adheres to * [main] address: in edit mode, select the encoded values in multiselect for address reference and city (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/316) * [person search] fix bug when using birthdate after and birthdate before * [person search] increase pertinence when lastname begins with search pattern +* [activity/actions] Améliore la cohérence du design entre + * la page résumé d'un parcours (liste d'actions récentes et liste d'activités récentes) + * la page liste des actions + * la page liste des activités (contexte personne / contexte parcours) * [household] field to edit wheter person is titulaire of household or not removed (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/322) * [activity] create work if a work with same social action is not associated to the activity * [visgraph] improve and fix bugs on vis-network relationship graph @@ -174,7 +178,7 @@ and this project adheres to * fast creation buttons * add ordering for types -* [AccompanyingCourse Resume page] badge-title for AccompanyingCourseWork and for Activities; +* [AccompanyingCourse Resume page] dashboard for AccompanyingCourseWork and for Activities; * Improve badges behaviour with small screens; * [ThirdParty]: diff --git a/src/Bundle/ChillActivityBundle/Resources/public/chill/chillactivity.scss b/src/Bundle/ChillActivityBundle/Resources/public/chill/chillactivity.scss index 28c02e23e..275f67950 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/chill/chillactivity.scss +++ b/src/Bundle/ChillActivityBundle/Resources/public/chill/chillactivity.scss @@ -24,14 +24,16 @@ div.new-activity-select-type { } //// ACTIVITY LIST PAGE -// precise badge-title specific details +// precise dashboard specific details +p.date-label { + display: inline-block; + margin: 0 0.5em 0 0; + font-weight: 700; + font-size: 18pt; +} +div.dashboard, h2.badge-title { - div.duration { - font-size: smaller; - padding-left: 1em; - margin-top: 1em; - } ul.list-content { font-size: 70%; list-style-type: none; @@ -39,16 +41,13 @@ h2.badge-title { margin: 0; li { margin-bottom: 0.2em; - // exception: change bg color for action badges above badge-title + // exception: change bg color for action badges above dashboard .bg-light { background-color: $chill-light-gray !important; } } } } -div.main { - padding: 1em; -} //// ACTIVITY SHOW AND FORM PAGES // Exceptions for flex-bloc in concerned-groups diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/activity-badge-title.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/activity-badge-title.html.twig deleted file mode 100644 index f51854163..000000000 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/activity-badge-title.html.twig +++ /dev/null @@ -1,110 +0,0 @@ -

- - - {% if activity.date %} -

{{ activity.date|format_date('short') }}

- {% endif %} - -
- {% if activity.durationTime and t.durationTimeVisible %} -

- - {{ activity.durationTime|date('H:i') }} -

- {% endif %} - - {% if activity.travelTime and t.travelTimeVisible %} -

- - {{ activity.travelTime|date('H:i') }} -

- {% endif %} -
- -
- - - {{ activity.type.name | localize_translatable_string }} - - {% if activity.emergency %} - {{ 'Emergency'|trans|upper }} - {% endif %} - -
    - - {% if activity.sentReceived is not empty and t.sentReceivedVisible %} -
  • - {{ 'Sent received'|trans ~ ' : ' }} - {{ activity.sentReceived|capitalize|trans }} -
  • - {% endif %} - - {% if activity.location and t.locationVisible %} -
  • - {{ 'location'|trans ~ ': ' }} - - {{ activity.location.locationType.title|localize_translatable_string }} - {{ activity.location.name }} - -
  • - {% endif %} - - {% if activity.user and t.userVisible %} -
  • - {{ 'Referrer'|trans ~ ': ' }} - {{ activity.user.usernameCanonical }} -
  • - {% endif %} - -
  • - {{ 'Participants'|trans ~ ' : ' }} - {% for p in activity.personsAssociated %} - {{ p|chill_entity_render_box }} - {% endfor %} -
  • -
- -
    - {%- if t.reasonsVisible -%} - {%- if activity.reasons is not empty -%} -
  • - {% for r in activity.reasons %} - {{ r|chill_entity_render_box }} - {% endfor %} -
  • - {%- endif -%} - {% endif %} - {%- if t.socialIssuesVisible %} - {%- if activity.socialIssues is not empty -%} - - {%- endif -%} - {% endif %} - {%- if t.socialActionsVisible -%} - {%- if activity.socialActions is not empty -%} - - {%- endif -%} - {% endif %} -
-
-

- -{% if context == 'person' and activity.accompanyingPeriod is not empty %} -
- - -
-{% endif %} - diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig index f6b052265..2808cca60 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig @@ -126,7 +126,7 @@
{% if bloc.items|length > 0 %}
-

{{ bloc.title }}

+

{{ bloc.title }}

{% for item in bloc.items %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/list.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/list.html.twig index 7c75f5bf0..83d8cbcdb 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/list.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/list.html.twig @@ -10,49 +10,174 @@ {% for activity in activities %} {% set t = activity.type %}
+
- {% include '@ChillActivity/Activity/activity-badge-title.html.twig' %} +
+
+
+ + {% if activity.date %} +

+ {{ activity.date|format_date('short') }} +

+ {% endif %} + +
+
+ +

+ + + {{ activity.type.name | localize_translatable_string }} + + {% if activity.emergency %} + {{ 'Emergency'|trans|upper }} + {% endif %} + +

+ +
+
+
- {% if activity.comment.comment is not empty - or activity.persons|length > 0 - or activity.thirdParties|length > 0 - or activity.users|length > 0 - %} -
- {% if activity.comment.comment is not empty %} - {{ activity.comment|chill_entity_render_box({ - 'disable_markdown': false, - 'limit_lines': 3, - 'metadata': false, - }) }} - {% endif %} +
+
+ {% if activity.location and t.locationVisible %} +
+

{{ 'location'|trans }}

+
+

+ {{ activity.location.locationType.title|localize_translatable_string }} + {{ activity.location.name }} +

+
+
+ {% endif %} + + {% if activity.sentReceived is not empty and t.sentReceivedVisible %} +
+

{{ 'Sent received'|trans }}

+
+

+ {{ activity.sentReceived|capitalize|trans }} +

+
+
+ {% endif %} + + {% if activity.user and t.userVisible %} +
+

{{ 'Referrer'|trans }}

+
+

+ {{ activity.user.usernameCanonical|chill_entity_render_string|capitalize }} +

+
+
+ {% endif %} +
{% include 'ChillActivityBundle:Activity:concernedGroups.html.twig' with { 'context': context, - 'with_display': 'row', + 'with_display': 'wrap-list', 'entity': activity, 'badge_person': true } %} + +
+ {%- if activity.reasons is not empty and t.reasonsVisible -%} +
+
+

{{ 'Reasons'|trans }}

+
+
+ {% for r in activity.reasons %} +

+ {{ r|chill_entity_render_box }} +

+ {% endfor %} +
+
+ {% endif %} + + {%- if activity.socialIssues is not empty and t.socialIssuesVisible -%} +
+
+

{{ 'Social issues'|trans }}

+
+
+ {% for r in activity.socialIssues %} + + {% endfor %} +
+
+ {% endif %} + + {%- if activity.socialActions is not empty and t.socialActionsVisible -%} +
+
+

{{ 'Social actions'|trans }}

+
+
+ {% for r in activity.socialActions %} + + {% endfor %} +
+
+ {% endif %} + + {# SEULEMENT SI DÉTAILLÉ + {% if activity.comment.comment is not empty %} +
+
+

{{ 'Comment'|trans }}

+
+
+ {{ activity.comment|chill_entity_render_box({ + 'disable_markdown': false, + 'limit_lines': 3, + 'metadata': false + }) }} +
+
+ {% endif %} + #} +
- {% endif %}
-
diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/listAccompanyingCourse.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/listAccompanyingCourse.html.twig index c5de72308..431a06ba6 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/listAccompanyingCourse.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/listAccompanyingCourse.html.twig @@ -23,8 +23,8 @@ {% if is_granted('CHILL_ACTIVITY_CREATE', accompanyingCourse) %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/list_recent.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/list_recent.html.twig index 2829d8153..0f426c5a9 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/list_recent.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/list_recent.html.twig @@ -3,10 +3,88 @@ {% set t = activity.type %} + class="dashboard-link" title="{{ 'Show the activity'|trans }}"> - {% include '@ChillActivity/Activity/activity-badge-title.html.twig' %} +
+ + + + {%- if activity.date -%} +

{{ activity.date|format_date('short') }}

+ {%- endif -%} + + + + {% if activity.emergency %} + {{ 'Emergency'|trans|upper }} + {% endif %} + +
    + + {% if activity.sentReceived is not empty and t.sentReceivedVisible %} +
  • + {{ 'Sent received'|trans ~ ' : ' }} + {{ activity.sentReceived|capitalize|trans }} +
  • + {% endif %} + + {% if activity.location and t.locationVisible %} +
  • + {{ 'location'|trans ~ ': ' }} + + {{ activity.location.locationType.title|localize_translatable_string }} + {{ activity.location.name }} + +
  • + {% endif %} + + {% if activity.user and t.userVisible %} +
  • + {{ 'Referrer'|trans ~ ': ' }} + {{ activity.user.usernameCanonical }} +
  • + {% endif %} + +
  • + {{ 'Participants'|trans ~ ' : ' }} + {% for p in activity.personsAssociated %} + {{ p|chill_entity_render_box }} + {% endfor %} +
  • +
+ +
    + {%- if t.reasonsVisible -%} + {%- if activity.reasons is not empty -%} +
  • + {% for r in activity.reasons %} + {{ r|chill_entity_render_box }} + {% endfor %} +
  • + {%- endif -%} + {% endif %} + {%- if t.socialIssuesVisible %} + {%- if activity.socialIssues is not empty -%} + + {%- endif -%} + {% endif %} + {%- if t.socialActionsVisible -%} + {%- if activity.socialActions is not empty -%} + + {%- endif -%} + {% endif %} +
+ +
+
- {% endfor %}
diff --git a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml index 098a6f6df..3de1cc600 100644 --- a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml @@ -222,3 +222,5 @@ Aggregate by activity type: Aggréger par type d'activité Aggregate by activity reason: Aggréger par sujet de l'activité Last activities: Les dernières activités + +See activity in accompanying course context: Voir l'activité dans le contexte du parcours d'accompagnement diff --git a/src/Bundle/ChillMainBundle/Resources/public/chill/scss/flex_table.scss b/src/Bundle/ChillMainBundle/Resources/public/chill/scss/flex_table.scss index 8e3a9121b..6b22302b2 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/chill/scss/flex_table.scss +++ b/src/Bundle/ChillMainBundle/Resources/public/chill/scss/flex_table.scss @@ -101,6 +101,10 @@ div.flex-table { div.item-row { flex-direction: row; + &.column { // exception + flex-direction: column + } + div.item-col { &:first-child { flex-grow: 0; flex-shrink: 0; flex-basis: auto; @@ -160,6 +164,12 @@ div.wrap-list { & > * { padding-right: 1em; } + + h3, h4 { + font-weight: 700; + font-size: 100%; + font-family: 'Open Sans'; + } } div.wl-col.list { @@ -171,6 +181,10 @@ div.wrap-list { padding: 0em; display: inline-block; } + + blockquote.chill-user-quote { + margin: 0.7em 0; + } } } diff --git a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/accompanying_period_work.scss b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/accompanying_period_work.scss index d246b3c92..daaae9e28 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/accompanying_period_work.scss +++ b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/accompanying_period_work.scss @@ -1,51 +1,6 @@ /// AccompanyingCourse Work list Page div.accompanying_course_work-list { - div.timeline { - width: 100%; - ul { - display: flex; - align-items: center; - justify-content: center; - - padding: 0; - list-style-type: none; - - > li { - flex-grow: 1; flex-shrink: 1; flex-basis: auto; - - div { - display: flex; - flex-direction: column; - align-items: center; - - &.date { - margin-bottom: 1em; - } - &.label { - border-top: 3px solid $chill-green; - - &:before { - content: ''; - display: inline-block; - position: relative; - width: 15px; - height: 15px; - top: -9px; - - background-color: $white; - border-radius: 12px; - border: 2px solid $chill-green; - } - &.no-label:before { - display: none; - } - } - } - } - } - } - div.objective_results { width: 100%; display: grid; @@ -69,8 +24,10 @@ div.accompanying_course_work-list { //&:nth-child(even) { background-color: $chill-llight-gray; } &.without-objectives {} &.with-objectives {} + } - + div.objective_results, + div.evaluations { h4.title_label { display: block; margin: 0.4em 0; diff --git a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/badge.scss b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/badge.scss index 6ffe0ac9d..39dbb4e55 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/badge.scss +++ b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/badge.scss @@ -39,69 +39,15 @@ span.fa-holder { } /* -* BADGE_TITLE -* Display Title like a badge (with background-colored label) +* DASHBOARDS */ -h2.badge-title { - display: flex; - flex-direction: row; - width: 100%; - color: $dark; - - span.title_label { - border-radius: 0.35rem 0 0 0.35rem; - color: $white; - font-size: 80%; - padding: 0.5em; - padding-right: 0; - h3 { - margin-bottom: 0.5rem; - } - - //position: relative; - span { - display: none; - //position: absolute; - //top: 0; - //left: 0; - //transform: rotate(270deg); - //transform-origin: 0 0; - } - } - span.title_action { - flex-grow: 1; - margin: 0 0 0 auto; - border-radius: 0 0.35rem 0.35rem 0; - background-color: $chill-llight-gray; - padding: 0.2em 1em; - - ul.small_in_title { - margin: 0; - //margin-top: 0.5em; - font-size: 70%; - padding-left: 1rem; - &.evaluations { - @include list_marker_triangle($orange); - } - } - ul.columns { // XS:1 SM:2 MD:1 LG:2 XL:2 XXL:2 - @include media-breakpoint-only(sm) { - columns: 2; -webkit-columns: 2; -moz-columns: 2; - } - @include media-breakpoint-up(lg) { - columns: 2; -webkit-columns: 2; -moz-columns: 2; - } - } - } -} - -/// Theses links apply on badge as parent tag. +/// Theses links apply on dashboards as parent tag. /// They don't look like button, picto or simple text links -a.badge-link { +a.dashboard-link { color: unset; text-decoration: unset; - & > h2.badge-title { + & > div.dashboard { &:hover { //box-shadow: 0 0 7px 0 $chill-gray; //opacity: 0.8; @@ -114,21 +60,80 @@ a.badge-link { } } -/// badge_title in AccompanyingCourse Work list Page +div.dashboard { + font-weight: 700; + font-size: 1.5rem; + margin-bottom: 0.5rem; + line-height: 1.2; + span.like-h3 { + color: #334d5c; + } +} +div.dashboard, +h2.badge-title { + display: flex; + flex-direction: row; + width: 100%; + color: $dark; + span.title_label { + color: $white; + font-size: 80%; + padding: 0.5em; + padding-right: 0; + border-radius: 0.35rem 0 0 0.35rem; + h3 { + margin-bottom: 0.5rem; + } + } + span.title_action { + flex-grow: 1; + margin: 0 0 0 auto; + background-color: $chill-llight-gray; + padding: 0.2em 1em; + border-radius: 0 0.35rem 0.35rem 0; + + ul.small_in_title { + font-size: 70%; + } + } +} + +ul.small_in_title { + margin: 0; + //margin-top: 0.5em; + padding-left: 1rem; + &.evaluations { + @include list_marker_triangle($orange); + } +} +ul.columns { // XS:1 SM:2 MD:1 LG:2 XL:2 XXL:2 + @include media-breakpoint-only(sm) { + columns: 2; -webkit-columns: 2; -moz-columns: 2; + } + @include media-breakpoint-up(lg) { + columns: 2; -webkit-columns: 2; -moz-columns: 2; + } +} + + + +/// dashboard_like_badge in AccompanyingCourse Work list Page div.accompanying_course_work-list { + div.dashboard, h2.badge-title { span.title_label { // Calculate same color then border:groove background-color: shade-color($social-action-color, 34%); } span.title_action { - @include badge_title($social-action-color); + @include dashboard_like_badge($social-action-color); } } } -/// badge_title in Activities on resume page +/// dashboard_like_badge in Activities on resume page div.activity-list { + div.dashboard, h2.badge-title { span.title_label { // Calculate same color then border:groove @@ -138,7 +143,7 @@ div.activity-list { } } span.title_action { - @include badge_title($activity-color); + @include dashboard_like_badge($activity-color); } span.title_label { div.duration { diff --git a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/flex_table.scss b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/flex_table.scss index 0229f53b1..2383e43b1 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/flex_table.scss +++ b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/flex_table.scss @@ -18,12 +18,6 @@ div.accompanyingcourse-list { //&:nth-child(2) { flex-direction: row; } //&:last-child { flex-direction: column; } } - div.title h3 { - font-weight: 700; - font-size: 100%; - font-family: 'Open Sans'; - } - div.list {} } /// Search Page (list_with_period.html.twig) diff --git a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/mixins.scss b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/mixins.scss index 8a3d21ece..228f10178 100644 --- a/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/mixins.scss +++ b/src/Bundle/ChillPersonBundle/Resources/public/chill/scss/mixins.scss @@ -27,11 +27,10 @@ } /// -/// Generic mixin for titles like badge -// define visual badge used in title area +/// Mixin for dashboards (with design like badge_social) /// -@mixin badge_title($color) { +@mixin dashboard_like_badge($color) { @include chill_badge($color); &:before { margin: 0 0.3em 0 -1.05em; diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig index 96e2cb534..cdd0363b4 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourse/index.html.twig @@ -103,7 +103,7 @@
@@ -121,7 +121,7 @@ {% set accompanying_course_id = accompanyingCourse.id %} {% endif %} -

{{ 'Last activities' |trans }}

+

{{ 'Last activities' |trans }}

{% include 'ChillActivityBundle:Activity:list_recent.html.twig' with { 'context': 'accompanyingCourse', 'no_action': true } %}
{% endblock %} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/delete.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/delete.html.twig index c66e5aa3d..37a6812bb 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/delete.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/delete.html.twig @@ -8,7 +8,7 @@

- {{ 'accompanying_course_work.action'|trans }} + {{ work.socialAction|chill_entity_render_string }}

diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/index.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/index.html.twig index 92b1919af..844b2d611 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/index.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/index.html.twig @@ -7,7 +7,217 @@

{{ block('title') }}

- {% include 'ChillPersonBundle:AccompanyingCourseWork:list_by_accompanying_period.html.twig' %} + {% if works|length == 0 %} +

{{ 'accompanying_course_work.Any work'|trans }} + +

+ + {% else %} +
+ {% for w in works %} +
+ +
+

+ + {{ w.socialAction|chill_entity_render_string }} + +
    +
  • + {{ 'accompanying_course_work.start_date'|trans ~ ' : ' }} + {{ w.startDate|format_date('short') }} +
  • + {% if w.endDate %} +
  • + {{ 'accompanying_course_work.end_date'|trans ~ ' : ' }} + {{ w.endDate|format_date('short') }} +
  • + {% endif %} +
+ +
+

+
+ +
+
+ + {% if w.createdBy %} +
+
+

{{ 'Referrer'|trans }}

+
+
+

+ {{ w.createdBy.usernameCanonical|chill_entity_render_string|capitalize }} +

+
+
+ {% endif %} + + {%- if w.persons -%} +
+
+

{{ 'Persons in accompanying course'|trans }}

+
+
+ {% for p in w.persons %} + + {{ p|chill_entity_render_box({ + 'render': 'raw', + 'addAltNames': false + }) }} + + {% endfor %} +
+
+ {% endif %} + + {%- if w.handlingThierParty -%} +
+
+

{{ 'Thirdparty handling'|trans }}

+
+
+ + {{ w.handlingThierParty|chill_entity_render_box({ + 'render': 'raw', + 'addAltNames': false + }) }} + +
+
+ {% endif %} + + {%- if w.socialAction.issue -%} +
+
+

{{ 'Social issue'|trans }}

+
+
+ +
+
+ {% endif %} + + {% if w.accompanyingPeriodWorkEvaluations|length > 0 %} +
+
+

{{ 'accompanying_course_work.evaluations'|trans }}

+
+
+
    + {% for e in w.accompanyingPeriodWorkEvaluations %} +
  • + {{ e.evaluation.title|localize_translatable_string }} + +
      +
    • + {{ 'accompanying_course_work.start_date'|trans ~ ' : ' }} + {{ e.startDate|format_date('short') }} +
    • + {% if e.endDate %} +
    • + {{ 'accompanying_course_work.end_date'|trans ~ ' : ' }} + {{ e.endDate|format_date('short') }} +
    • + {% endif %} + {% if e.maxDate %} +
    • + {{ 'accompanying_course_work.max_date'|trans ~ ' : ' }} + {{ e.maxDate|format_date('short') }} +
    • + {% endif %} + {% if e.warningInterval and e.warningInterval.d > 0 %} +
    • + {% set days = (e.warningInterval.d + e.warningInterval.m * 30) %} + {{ 'accompanying_course_work.warning_interval'|trans ~ ' : ' }} + {{ 'accompanying_course_work.%days% days before max_date'|trans({'%days%': days }) }} +
    • + {% endif %} +
    + +
  • + {% endfor %} +
+
+
+ {% endif %} + +
+
+ +
+ {# SEULEMENT SI DÉTAILLÉ + {% if w.results|length > 0 %} +
+
+

{{ 'accompanying_course_work.goal'|trans }}

+

{{ 'accompanying_course_work.results without objective'|trans }}

+
+
+

{{ 'accompanying_course_work.results'|trans }}

+
    + {% for r in w.results %} +
  • {{ r.title|localize_translatable_string }}
  • + {% endfor %} +
+
+
+ {% endif %} + {% if w.goals|length > 0 %} + {% for g in w.goals %} +
+
+

{{ 'accompanying_course_work.goal'|trans }}

+
  • {{ g.goal.title|localize_translatable_string }}
+
+
+

{{ 'accompanying_course_work.results'|trans }}

+ {% if g.results|length == 0 %} +

{{ 'accompanying_course_work.no_results'|trans }}

+ {% else %} +
    + {% for r in g.results %} +
  • {{ r.title|localize_translatable_string }}
  • + {% endfor %} +
+ {% endif %} +
+
+ {% endfor %} + {% endif %} + #} +
+ +
+
+ {{ 'Last updated by'|trans}} {{ w.updatedBy|chill_entity_render_box }},
+ {{ 'le ' ~ w.updatedAt|format_datetime('long', 'short') }} +
+
    +
  • + +
  • +
  • + +
  • +
+
+ +
+ {% endfor %} +
+ {% endif %}
  • diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_by_accompanying_period.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_by_accompanying_period.html.twig deleted file mode 100644 index 797f1bcc8..000000000 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_by_accompanying_period.html.twig +++ /dev/null @@ -1,118 +0,0 @@ - {% if works|length == 0 %} -

    {{ 'accompanying_course_work.Any work'|trans }} - {# TODO link #} -

    - {% endif %} - -
    - {% for w in works %} -
    -
    - -

    - {{ 'accompanying_course_work.action'|trans }} - {{ w.socialAction|chill_entity_render_string }} -

    - -
    -
    - -
    -
      -
    • -
      - {{ w.startDate|format_date('long') }} -
      -
      - {{ 'accompanying_course_work.start_date'|trans }} -
      -
    • - {% if w.endDate == null %} -
    • -
      -
    • - {% else %} -
    • -
      - {{ w.endDate|format_date('long') }} -
      -
      - {{ 'accompanying_course_work.end_date'|trans }} -
      -
    • - {% endif %} -
    -
    - -
    -
    - - {% if w.results|length > 0 and w.goals|length > 0 %} - - {% endif %} - {% if w.results|length > 0 %} -
    -
    -

    {{ 'accompanying_course_work.goal'|trans }}

    -

    {{ 'accompanying_course_work.results without objective'|trans }}

    -
    -
    -

    {{ 'accompanying_course_work.results'|trans }}

    -
      - {% for r in w.results %} -
    • {{ r.title|localize_translatable_string }}
    • - {% endfor %} -
    -
    -
    - {% endif %} - {% if w.goals|length > 0 %} - {% for g in w.goals %} -
    -
    -

    {{ 'accompanying_course_work.goal'|trans }}

    -
    • {{ g.goal.title|localize_translatable_string }}
    -
    -
    -

    {{ 'accompanying_course_work.results'|trans }}

    - {% if g.results|length == 0 %} -

    {{ 'accompanying_course_work.no_results'|trans }}

    - {% else %} -
      - {% for r in g.results %} -
    • {{ r.title|localize_translatable_string }}
    • - {% endfor %} -
    - {% endif %} -
    -
    - {% endfor %} - {% endif %} - -
    -
    - -
    - {{ 'Last updated by'|trans}} {{ w.updatedBy|chill_entity_render_box }},
    - {{ 'le ' ~ w.updatedAt|format_datetime('long', 'short') }} -
    - - -
    -
    - {% endfor %} -
    - - diff --git a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig index f33718910..b5fcb6c40 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/AccompanyingCourseWork/list_recent_by_accompanying_period.html.twig @@ -2,14 +2,11 @@ {% for w in works | slice(0,5) %} + class="dashboard-link" title="{{ 'crud.social_action.title_link'|trans }}"> -

    - - {{ 'accompanying_course_work.action'|trans }} - - - {{ w.socialAction|chill_entity_render_string }} +
    + +
    • @@ -75,7 +72,7 @@
    -

    +
{# {{ dump(w) }} #} {% endfor %} diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 46077fdf0..0898ec611 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -198,6 +198,7 @@ Resources: Interlocuteurs privilégiés Any requestor to this accompanying course: Aucun demandeur pour ce parcours Social actions: Actions d'accompagnement Last social actions: Les dernières actions d'accompagnement +Social issue: Problématique sociale Social issues: Problématiques sociales Last events on accompanying course: Dernières actions de suivi Edit & activate accompanying course: Modifier et valider @@ -429,10 +430,14 @@ accompanying_course_work: create_date: Date de création start_date: Date de début end_date: Date de fin + max_date: Date d'échéance + warning_interval: Rappel + '%days% days before max_date': "%days% jour(s) avant l'échéance" results without objective: Aucun objectif - motif - dispositif no_results: Aucun résultat - orientation results: Résultats - orientations goal: Objectif - motif - dispositif + evaluations: Évaluations Any work: Aucune action d'accompagnement remove: Supprimer une action d'accompagnement social_evaluation: Évaluation From b120e47786e5b671e0e10b96acefba55b21a587a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 16:17:30 +0100 Subject: [PATCH 36/41] add translations --- .../AdminDocGeneratorTemplateController.php | 17 ++++---- .../Menu/AdminMenuBuilder.php | 42 +++++++++++++++++++ .../DocGeneratorTemplate/index.html.twig | 2 + .../pick-context.html.twig | 2 +- .../config/services.yaml | 5 +++ .../translations/messages.fr.yml | 16 +++++++ .../AccompanyingPeriodContext.php | 8 ++-- .../AccompanyingPeriodWorkContext.php | 2 +- ...ccompanyingPeriodWorkEvaluationContext.php | 2 +- .../translations/messages.fr.yml | 9 +++- 10 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index 57d4cd487..d6ec9c19c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -32,22 +32,23 @@ class AdminDocGeneratorTemplateController extends CRUDController switch ($action) { case 'new': $context = $this->contextManager->getContextByKey($request->get('context')); - - break; - case 'edit': $context = $this->contextManager->getContextByDocGeneratorTemplate($entity); - break; + return array_merge( + $defaultTemplateParameters, + ['context' => $context] + ); + case 'index': + return array_merge( + $defaultTemplateParameters, + ['contextManager' => $this->contextManager] + ); default: return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters); // TODO: Change the autogenerated stub } - return array_merge( - $defaultTemplateParameters, - ['context' => $context] - ); } public function new(Request $request): Response diff --git a/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php new file mode 100644 index 000000000..f59389352 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php @@ -0,0 +1,42 @@ +translator = $translator; + $this->security = $security; + } + + public function buildMenu($menuId, MenuItem $menu, array $parameters) + { + if ($this->security->isGranted('ROLE_ADMIN')) { + if (in_array($menuId, ['admin_index', 'admin_section'])) { + $menu->addChild($this->translator->trans('docgen.Document generation'), [ + 'route' => 'chill_crud_docgen_template_index' + ])->setExtras([ + 'order' => 350, + 'explain' => 'docgen.Manage templates and document generation' + ]); + } + } + } + + public static function getMenuIds(): array + { + return ['admin_index', 'admin_section', ['docgen_admin']]; + } + + +} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig index d98bd4811..1cdad36ff 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/index.html.twig @@ -5,6 +5,7 @@ {% block table_entities_thead_tr %} {{ 'Title'|trans }} + {{ 'docgen.Context'|trans }} {{ 'Edit'|trans }} {% endblock %} @@ -13,6 +14,7 @@ {{ entity.id }} {{ entity.name | localize_translatable_string }} + {{ contextManager.getContextByKey(entity.context).name|trans }} {{ 'Edit'|trans }} diff --git a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig index d43e88f8d..5e3ff2049 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig +++ b/src/Bundle/ChillDocGeneratorBundle/Resources/views/Admin/DocGeneratorTemplate/pick-context.html.twig @@ -1,6 +1,6 @@ {% extends '@ChillDocGenerator/Admin/layout.html.twig' %} -{% block title 'Pick template context'|trans %} +{% block title 'docgen.Pick template context'|trans %} {% block layout_wvm_content %}
diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 011317959..10ab6d172 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -9,6 +9,11 @@ services: autoconfigure: true resource: '../Repository/' + Chill\DocGeneratorBundle\Menu\: + autoconfigure: true + autowire: true + resource: '../Menu/' + Chill\DocGeneratorBundle\Serializer\Normalizer\: autowire: true autoconfigure: true diff --git a/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml b/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml index ebfe5320f..af08cf1c8 100644 --- a/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillDocGeneratorBundle/translations/messages.fr.yml @@ -1,3 +1,19 @@ docgen: Generate a document: Génerer un document Generate: Génerer + Document generation: Génération de documents + Manage templates and document generation: Gestion des documents générés et de leurs gabarits + Pick template context: Choisir un contexte + Context: Contexte + New template: Nouveau gabarit + Edit template: Modifier gabarit + With context: 'Avec le contexte :' + + +crud: + docgen_template: + index: + title: Génération de documents + add_new: Créer + + diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 5bc218ce9..10431d7aa 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -102,7 +102,7 @@ class AccompanyingPeriodContext implements $builder ->add('mainPerson', CheckboxType::class, [ 'required' => false, - 'label' => 'Ask for main person', + 'label' => 'docgen.Ask for main person', ]) ->add('mainPersonLabel', TextType::class, [ 'label' => 'main person label', @@ -110,7 +110,7 @@ class AccompanyingPeriodContext implements ]) ->add('person1', CheckboxType::class, [ 'required' => false, - 'label' => 'Ask for person 1', + 'label' => 'docgen.Ask for person 1', ]) ->add('person1Label', TextType::class, [ 'label' => 'person 1 label', @@ -118,7 +118,7 @@ class AccompanyingPeriodContext implements ]) ->add('person2', CheckboxType::class, [ 'required' => false, - 'label' => 'Ask for person 2', + 'label' => 'docgen.Ask for person 2', ]) ->add('person2Label', TextType::class, [ 'label' => 'person 2 label', @@ -182,7 +182,7 @@ class AccompanyingPeriodContext implements public function getDescription(): string { - return 'A basic context for accompanying period'; + return 'docgen.A basic context for accompanying period'; } public function getEntityClass(): string diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php index 5041d9180..09d154900 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -79,7 +79,7 @@ class AccompanyingPeriodWorkContext implements public function getDescription(): string { - return 'A context for work'; + return 'docgen.A context for accompanying period work'; } public function getEntityClass(): string diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 2ffcaf818..9c8d6172a 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -127,7 +127,7 @@ class AccompanyingPeriodWorkEvaluationContext implements public function getDescription(): string { - return 'Context for accompanying period work'; + return 'docgen.A context for accompanying period work evaluation'; } public function getEntityClass(): string diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index af2009f3a..0ca5ce19d 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -457,4 +457,11 @@ see persons associated: Voir les usagers concernés docgen: Main person: Personne principale person 1: Première personne - person 2: Deuxième personne + person 2: Seconde personne + Ask for main person: Demander à l'utilisateur de préciser la personne principale + Ask for person 1: Demander à l'utilisateur de préciser la première personne + Ask for person 2: Demander à l'utilisateur de préciser la seconde personne + Accompanying period work: Actions + A basic context for accompanying period: Contexte pour les parcours + A context for accompanying period work: Contexte pour les actions d'accompagnement + A context for accompanying period work evaluation: Contexte pour les évaluations dans les actions d'accompagnement From fabe32371f551c4bbe05bb87f439081dc884f03a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 17:19:22 +0100 Subject: [PATCH 37/41] fix menu doc generator --- src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php index f59389352..bfe8ba66c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php +++ b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php @@ -35,7 +35,7 @@ class AdminMenuBuilder implements LocalMenuBuilderInterface public static function getMenuIds(): array { - return ['admin_index', 'admin_section', ['docgen_admin']]; + return ['admin_index', 'admin_section', 'docgen_admin']; } From 2788149db45a81f166c7ead2256000f449c78cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 17:24:35 +0100 Subject: [PATCH 38/41] fix error when using twilio format --- src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php index e807efebe..0bf1cc8f4 100644 --- a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php +++ b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php @@ -204,7 +204,7 @@ class PhonenumberHelper return null; } - $format = json_decode($response->getBody())->national_format; + $format = json_decode($response->getBody()->getContents())->national_format; $item ->set($format) From 25147704ad517b9f0f298df558e20aae45365c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 17:24:58 +0100 Subject: [PATCH 39/41] fix uppercase in person form with strict types --- src/Bundle/ChillPersonBundle/Form/PersonType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Form/PersonType.php b/src/Bundle/ChillPersonBundle/Form/PersonType.php index eddf8c751..de9b3bbb1 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonType.php @@ -114,10 +114,10 @@ class PersonType extends AbstractType $builder->get('placeOfBirth')->addModelTransformer(new CallbackTransformer( static function ($string) { - return strtoupper($string); + return strtoupper((string) $string); }, static function ($string) { - return strtoupper($string); + return strtoupper((string) $string); } )); } From ebd817f61e07928e1af208c4c17d50bd023cf6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 6 Dec 2021 17:27:57 +0100 Subject: [PATCH 40/41] fix cs --- .../AdminDocGeneratorTemplateController.php | 3 +- .../DocGeneratorTemplateController.php | 1 - .../Menu/AdminMenuBuilder.php | 22 +++++++++---- .../Normalizer/DocGenObjectNormalizer.php | 1 + .../AccompanyingPeriodParticipation.php | 8 ++--- .../Entity/Household/Household.php | 2 -- .../Normalizer/SocialActionNormalizer.php | 10 +++--- .../Normalizer/SocialIssueNormalizer.php | 19 ++++++----- ...AccompanyingPeriodDocGenNormalizerTest.php | 32 +++++++++---------- 9 files changed, 52 insertions(+), 46 deletions(-) diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php index d6ec9c19c..c92aff527 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/AdminDocGeneratorTemplateController.php @@ -32,6 +32,7 @@ class AdminDocGeneratorTemplateController extends CRUDController switch ($action) { case 'new': $context = $this->contextManager->getContextByKey($request->get('context')); + // no break case 'edit': $context = $this->contextManager->getContextByDocGeneratorTemplate($entity); @@ -39,6 +40,7 @@ class AdminDocGeneratorTemplateController extends CRUDController $defaultTemplateParameters, ['context' => $context] ); + case 'index': return array_merge( $defaultTemplateParameters, @@ -48,7 +50,6 @@ class AdminDocGeneratorTemplateController extends CRUDController default: return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters); // TODO: Change the autogenerated stub } - } public function new(Request $request): Response diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index 3267b2225..21a830bfd 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -90,7 +90,6 @@ final class DocGeneratorTemplateController extends AbstractController int $entityId, Request $request ): Response { - try { $context = $this->contextManager->getContextByDocGeneratorTemplate($template); } catch (ContextNotFoundException $e) { diff --git a/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php index bfe8ba66c..39b21ed0e 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php +++ b/src/Bundle/ChillDocGeneratorBundle/Menu/AdminMenuBuilder.php @@ -1,18 +1,28 @@ translator = $translator; @@ -22,12 +32,12 @@ class AdminMenuBuilder implements LocalMenuBuilderInterface public function buildMenu($menuId, MenuItem $menu, array $parameters) { if ($this->security->isGranted('ROLE_ADMIN')) { - if (in_array($menuId, ['admin_index', 'admin_section'])) { + if (in_array($menuId, ['admin_index', 'admin_section'], true)) { $menu->addChild($this->translator->trans('docgen.Document generation'), [ - 'route' => 'chill_crud_docgen_template_index' + 'route' => 'chill_crud_docgen_template_index', ])->setExtras([ 'order' => 350, - 'explain' => 'docgen.Manage templates and document generation' + 'explain' => 'docgen.Manage templates and document generation', ]); } } @@ -37,6 +47,4 @@ class AdminMenuBuilder implements LocalMenuBuilderInterface { return ['admin_index', 'admin_section', 'docgen_admin']; } - - } diff --git a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php index 4f0a9f1ab..c32be9913 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php +++ b/src/Bundle/ChillDocGeneratorBundle/Serializer/Normalizer/DocGenObjectNormalizer.php @@ -212,6 +212,7 @@ class DocGenObjectNormalizer implements NormalizerAwareInterface, NormalizerInte if (is_iterable($value)) { $arr = []; + foreach ($value as $k => $v) { $arr[$k] = $this->normalizer->normalize($v, $format, array_merge( diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php index ee3ac7dc9..055da93e9 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriodParticipation.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Entity; -use DateTimeImmutable; +use DateTime; use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; @@ -38,7 +38,7 @@ class AccompanyingPeriodParticipation * @ORM\Column(type="date", nullable=true) * @Groups({"read", "docgen:read"}) */ - private ?\DateTime $endDate = null; + private ?DateTime $endDate = null; /** * @ORM\Id @@ -59,11 +59,11 @@ class AccompanyingPeriodParticipation * @ORM\Column(type="date", nullable=false) * @Groups({"read", "docgen:read"}) */ - private ?\DateTime $startDate = null; + private ?DateTime $startDate = null; public function __construct(AccompanyingPeriod $accompanyingPeriod, Person $person) { - $this->startDate = new \DateTime('now'); + $this->startDate = new DateTime('now'); $this->accompanyingPeriod = $accompanyingPeriod; $this->person = $person; } diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index c5adbbd6a..95aa83872 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -157,8 +157,6 @@ class Household } /** - * @param DateTimeImmutable|null $now - * @return Collection * @Serializer\Groups({"docgen:read"}) */ public function getCurrentMembers(?DateTimeImmutable $now = null): Collection diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php index 96af56e95..5fa12f741 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialActionNormalizer.php @@ -12,7 +12,6 @@ declare(strict_types=1); namespace Chill\PersonBundle\Serializer\Normalizer; use Chill\PersonBundle\Entity\SocialWork\SocialAction; -use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; @@ -41,8 +40,8 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte 'desactivationDate' => $this->normalizer->normalize($socialAction->getDesactivationDate()), 'title' => $socialAction->getTitle(), ]; - case 'docgen': + case 'docgen': if (null === $socialAction) { return ['id' => 0, 'title' => '', 'text' => '']; } @@ -52,12 +51,13 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte 'text' => $this->render->renderString($socialAction, []), 'title' => $socialAction->getTitle(), ]; + default: - throw new \Symfony\Component\Serializer\Exception\RuntimeException("format not supported"); + throw new \Symfony\Component\Serializer\Exception\RuntimeException('format not supported'); } } - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, ?string $format = null, array $context = []) { if ($data instanceof SocialAction && 'json' === $format) { return true; @@ -68,7 +68,7 @@ class SocialActionNormalizer implements NormalizerAwareInterface, NormalizerInte return true; } - if (null === $data && ($context['docgen:expects'] ?? null) === SocialAction::class) { + if (null === $data && SocialAction::class === ($context['docgen:expects'] ?? null)) { return true; } } diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php index cfe7c0895..5be7df4bc 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/SocialIssueNormalizer.php @@ -16,9 +16,8 @@ use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait; -use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -class SocialIssueNormalizer implements NormalizerAwareInterface, ContextAwareNormalizerInterface +class SocialIssueNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface { use NormalizerAwareTrait; @@ -42,8 +41,8 @@ class SocialIssueNormalizer implements NormalizerAwareInterface, ContextAwareNor 'title' => $socialIssue->getTitle(), 'text' => $this->render->renderString($socialIssue, []), ]; - case 'docgen': + case 'docgen': if (null === $socialIssue) { return ['id' => 0, 'title' => '', 'text' => '']; } @@ -56,20 +55,20 @@ class SocialIssueNormalizer implements NormalizerAwareInterface, ContextAwareNor } } - public function supportsNormalization($data, string $format = null, array $context = []) + public function supportsNormalization($data, ?string $format = null, array $context = []) { if ($data instanceof SocialIssue && 'json' === $format) { return true; } if ('docgen' === $format) { - if ($data instanceof SocialIssue) { - return true; - } + if ($data instanceof SocialIssue) { + return true; + } - if (null === $data && ($context['docgen:expects'] ?? null) === SocialIssue::class) { - return true; - } + if (null === $data && SocialIssue::class === ($context['docgen:expects'] ?? null)) { + return true; + } } return false; diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php index bd104bbe8..af2dca1f3 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/AccompanyingPeriodDocGenNormalizerTest.php @@ -80,22 +80,6 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase } } - public function testNormalizeParticipations() - { - $period = new AccompanyingPeriod(); - $period->addPerson($person = new Person()); - $person->setFirstName('test'); - - $data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); - - $this->assertIsArray($data); - $this->assertArrayHasKey('participations', $data); - $this->assertCount(1, $data['participations']); - - $this->assertArrayHasKey('currentParticipations', $data); - $this->assertCount(1, $data['currentParticipations']); - } - public function testNormalizeNull() { $data = $this->normalizer->normalize(null, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); @@ -134,4 +118,20 @@ final class AccompanyingPeriodDocGenNormalizerTest extends KernelTestCase $this->assertEquals($item, $data[$key]); } } + + public function testNormalizeParticipations() + { + $period = new AccompanyingPeriod(); + $period->addPerson($person = new Person()); + $person->setFirstName('test'); + + $data = $this->normalizer->normalize($period, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + + $this->assertIsArray($data); + $this->assertArrayHasKey('participations', $data); + $this->assertCount(1, $data['participations']); + + $this->assertArrayHasKey('currentParticipations', $data); + $this->assertCount(1, $data['currentParticipations']); + } } From 6cc0f1f98b156ed779aa7f946039815bebe1c4b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 7 Dec 2021 16:41:30 +0100 Subject: [PATCH 41/41] update changelog [ci-skip] --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65b9864c4..da1e32cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to ## Unreleased + +## Test releases + +### test release 2021-12-06 + * [main] address: use search API end points for getting postal code and reference address (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/316) * [main] address: in edit mode, select the encoded values in multiselect for address reference and city (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/316) * [person search] fix bug when using birthdate after and birthdate before @@ -24,8 +29,6 @@ and this project adheres to * [visgraph] improve and fix bugs on vis-network relationship graph * [bugfix] posting of birth- and deathdate through api fixed. -## Test releases - ### Test release 2021-11-19 - bis * [household] do not allow to create two addresses on the same date