From 21d4e49fcec6ff5cdafacdfc0615b1e4e1a378a4 Mon Sep 17 00:00:00 2001 From: juminet Date: Mon, 24 Jan 2022 13:51:14 +0000 Subject: [PATCH] activity: doc gen --- .../ChillActivityBundle/Entity/Activity.php | 21 +- .../views/Activity/concernedGroups.html.twig | 2 +- .../Resources/views/Activity/edit.html.twig | 10 +- .../Activity/editAccompanyingCourse.html.twig | 2 + .../views/Activity/editPerson.html.twig | 2 + .../Service/DocGenerator/ActivityContext.php | 223 ++++++++++++++++++ .../ChillActivityBundle/config/services.yaml | 5 + .../translations/messages.fr.yml | 4 + 8 files changed, 253 insertions(+), 16 deletions(-) create mode 100644 src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index 77f650ce4..b8534de52 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -13,6 +13,7 @@ namespace Chill\ActivityBundle\Entity; use Chill\ActivityBundle\Validator\Constraints as ActivityValidator; use Chill\DocStoreBundle\Entity\Document; +use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; use Chill\MainBundle\Entity\HasCenterInterface; @@ -61,13 +62,13 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac /** * @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?AccompanyingPeriod $accompanyingPeriod = null; /** * @ORM\ManyToOne(targetEntity="Chill\ActivityBundle\Entity\ActivityType") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) * @SerializedName("activityType") * @ORM\JoinColumn(name="type_id") */ @@ -107,13 +108,13 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location") - * @groups({"read"}) + * @groups({"read", "docgen:read"}) */ private ?Location $location = null; @@ -124,7 +125,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac /** * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\Person") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?Collection $persons = null; @@ -146,20 +147,20 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac /** * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\SocialWork\SocialAction") * @ORM\JoinTable(name="chill_activity_activity_chill_person_socialaction") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private Collection $socialActions; /** * @ORM\ManyToMany(targetEntity="Chill\PersonBundle\Entity\SocialWork\SocialIssue") * @ORM\JoinTable(name="chill_activity_activity_chill_person_socialissue") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private Collection $socialIssues; /** * @ORM\ManyToMany(targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty") - * @Groups({"read"}) + * @Groups({"read", "docgen:read"}) */ private ?Collection $thirdParties = null; @@ -191,7 +192,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac $this->socialActions = new ArrayCollection(); } - public function addDocument(Document $document): self + public function addDocument(StoredObject $document): self { $this->documents[] = $document; @@ -425,7 +426,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this->getEmergency(); } - public function removeDocument(Document $document): void + public function removeDocument(StoredObject $document): void { $this->documents->removeElement($document); } diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig index 33575cea6..d9d72845a 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/concernedGroups.html.twig @@ -8,7 +8,7 @@ action: 'show', displayBadge: true, targetEntity: { name: type, id: entity.id }, buttonText: entity|chill_entity_render_string, - isDead: entity.deathdate is not null, + isDead: entity.deathdate is defined and entity.deathdate is not null, parent: parent } %} {% endmacro %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig index 235a5b7f2..a59c596c3 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig @@ -83,15 +83,15 @@ {{ form_row(edit_form.comment) }} {% endif %} -{%- if edit_form.documents is defined -%} - {{ form_row(edit_form.documents) }} -{% endif %} - {%- if edit_form.attendee is defined -%} {{ form_row(edit_form.attendee) }} {% endif %} -{# TODO .. status #} +{%- if edit_form.documents is defined -%} + {{ form_row(edit_form.documents) }} +{% endif %} + +
{% set person_id = null %} {% if entity.person %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/editAccompanyingCourse.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/editAccompanyingCourse.html.twig index b278c0300..09ff16fec 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/editAccompanyingCourse.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/editAccompanyingCourse.html.twig @@ -24,10 +24,12 @@ window.activity = {{ activity_json|json_encode|raw }}; {{ encore_entry_script_tags('vue_activity') }} + {{ encore_entry_script_tags('mod_docgen_picktemplate') }} {% endblock %} {% block css %} {{ parent() }} {{ encore_entry_link_tags('mod_async_upload') }} {{ encore_entry_link_tags('vue_activity') }} + {{ encore_entry_link_tags('mod_docgen_picktemplate') }} {% endblock %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/editPerson.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/editPerson.html.twig index 82c7403c6..72c74c68e 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/editPerson.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/editPerson.html.twig @@ -39,9 +39,11 @@ window.activity = {{ activity_json|json_encode|raw }}; {{ encore_entry_script_tags('vue_activity') }} + {{ encore_entry_script_tags('mod_docgen_picktemplate') }} {% endblock %} {% block css %} {{ encore_entry_link_tags('mod_async_upload') }} {{ encore_entry_link_tags('vue_activity') }} + {{ encore_entry_link_tags('mod_docgen_picktemplate') }} {% endblock %} diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php new file mode 100644 index 000000000..c23607337 --- /dev/null +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -0,0 +1,223 @@ +documentCategoryRepository = $documentCategoryRepository; + $this->normalizer = $normalizer; + $this->translatableStringHelper = $translatableStringHelper; + $this->em = $em; + $this->personRender = $personRender; + $this->translator = $translator; + $this->baseContextData = $baseContextData; + } + + public function adminFormReverseTransform(array $data): array + { + return $data; + } + + public function adminFormTransform(array $data): array + { + return [ + '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'), + ]; + } + + public function buildAdminForm(FormBuilderInterface $builder): void + { + $builder + ->add('mainPerson', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for main person', + ]) + ->add('mainPersonLabel', TextType::class, [ + 'label' => 'main person label', + 'required' => true, + ]) + ->add('person1', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for person 1', + ]) + ->add('person1Label', TextType::class, [ + 'label' => 'person 1 label', + 'required' => true, + ]) + ->add('person2', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for person 2', + ]) + ->add('person2Label', TextType::class, [ + 'label' => 'person 2 label', + 'required' => true, + ]); + } + + /** + * @param Activity $entity + */ + public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void + { + $options = $template->getOptions(); + $persons = $entity->getPersons(); + + 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, + 'label' => $options[$key . 'Label'], + ]); + } + } + } + + public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array + { + if (!$entity instanceof Activity) { + throw new UnexpectedTypeException($entity, Activity::class); + } + $options = $template->getOptions(); + + $data = []; + $data = array_merge($data, $this->baseContextData->getData()); + $data['activity'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Activity::class, 'groups' => 'docgen:read']); + + $data['course'] = $this->normalizer->normalize($entity->getAccompanyingPeriod(), 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); + $data['person'] = $this->normalizer->normalize($entity->getPerson(), 'docgen', ['docgen:expects' => Person::class, 'groups' => 'docgen:read']); + + foreach (['mainPerson', 'person1', 'person2'] as $k) { + if ($options[$k]) { + $data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', [ + 'docgen:expects' => Person::class, + 'groups' => 'docgen:read', + 'docgen:person:with-household' => true, + 'docgen:person:with-relations' => true, + ]); + } + } + + return $data; + } + + public function getDescription(): string + { + return 'docgen.A basic context for activity'; + } + + public function getEntityClass(): string + { + return Activity::class; + } + + public function getFormData(DocGeneratorTemplate $template, $entity): array + { + return [ + 'activity' => $entity, + ]; + } + + public static function getKey(): string + { + return self::class; + } + + public function getName(): string + { + return 'docgen.Activity 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 Activity $entity + */ + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void + { + $doc = new StoredObject(); + // TODO push document to remote + + $this->em->persist($doc); + + $entity->addDocument($doc); + } +} diff --git a/src/Bundle/ChillActivityBundle/config/services.yaml b/src/Bundle/ChillActivityBundle/config/services.yaml index 1ca413f0e..23f00ed4e 100644 --- a/src/Bundle/ChillActivityBundle/config/services.yaml +++ b/src/Bundle/ChillActivityBundle/config/services.yaml @@ -32,3 +32,8 @@ services: autowire: true autoconfigure: true resource: '../Validator/Constraints/' + + Chill\ActivityBundle\Service\DocGenerator\: + autowire: true + autoconfigure: true + resource: '../Service/DocGenerator/' diff --git a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml index ed7de9c66..81a34d47d 100644 --- a/src/Bundle/ChillActivityBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillActivityBundle/translations/messages.fr.yml @@ -228,3 +228,7 @@ See activity in accompanying course context: Voir l'activité dans le contexte d You get notified of an activity which does not exists any more: Cette notification ne correspond pas à une activité valide. you are not allowed to see it details: La notification fait référence à une activité à laquelle vous n'avez pas accès. This is the minimal activity data: Activité n° + +docgen: + Activity basic: Echange + A basic context for activity: Contexte pour les échanges