Feature: [calendar][docgen] generation context for Calendar

This commit is contained in:
2022-10-20 21:36:44 +02:00
parent 2b1d9cabff
commit 63f3010395
5 changed files with 561 additions and 2 deletions

View File

@@ -0,0 +1,249 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CalendarBundle\Service\DocGenerator;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\CalendarBundle\Entity\CalendarDoc;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Templating\Entity\PersonRender;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender;
use Doctrine\ORM\EntityManagerInterface;
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 function count;
final class CalendarContext implements CalendarContextInterface
{
private BaseContextData $baseContextData;
private EntityManagerInterface $entityManager;
private NormalizerInterface $normalizer;
private PersonRender $personRender;
private ThirdPartyRender $thirdPartyRender;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct(
BaseContextData $baseContextData,
EntityManagerInterface $entityManager,
NormalizerInterface $normalizer,
PersonRender $personRender,
ThirdPartyRender $thirdPartyRender,
TranslatableStringHelperInterface $translatableStringHelper
) {
$this->baseContextData = $baseContextData;
$this->entityManager = $entityManager;
$this->normalizer = $normalizer;
$this->personRender = $personRender;
$this->thirdPartyRender = $thirdPartyRender;
$this->translatableStringHelper = $translatableStringHelper;
}
public function adminFormReverseTransform(array $data): array
{
return array_merge(
[
'trackDatetime' => true,
'askMainPerson' => true,
'mainPersonLabel' => 'docgen.calendar.Destinee',
'askThirdParty' => false,
'thirdPartyLabel' => 'Third party',
],
$data
);
}
public function adminFormTransform(array $data): array
{
return $data;
}
public function buildAdminForm(FormBuilderInterface $builder): void
{
$builder
->add('trackDatetime', CheckboxType::class, [
'required' => false,
'label' => 'docgen.calendar.Track changes on datetime and warn user if date time is updated after the doc generation',
])
->add('askMainPerson', CheckboxType::class, [
'required' => false,
'label' => 'docgen.calendar.Ask main person',
])
->add('mainPersonLabel', TextType::class, [
'required' => false,
'label' => 'docgen.calendar.Main person label',
])
->add('askThirdParty', CheckboxType::class, [
'required' => false,
'label' => 'docgen.calendar.Ask third party',
])
->add('thirdPartyLabel', TextType::class, [
'required' => false,
'label' => 'docgen.calendar.Third party label',
]);
}
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
{
$options = $this->getOptions($template);
$builder->add('title', TextType::class, [
'required' => true,
'label' => 'docgen.calendar.title of the generated document',
'data' => $this->translatableStringHelper->localize($template->getName()),
]);
if ($options['askMainPerson']) {
$builder->add('mainPerson', EntityType::class, [
'class' => Person::class,
'multiple' => false,
'label' => $options['mainPersonLabel'] ?? 'docgen.calendar.Main person label',
'required' => false,
'choices' => $entity->getPersons(),
'choice_label' => fn (Person $p) => $this->personRender->renderString($p, []),
'expanded' => false,
]);
}
if ($options['askThirdParty']) {
$builder->add('thirdParty', EntityType::class, [
'class' => ThirdParty::class,
'multiple' => false,
'label' => $options['thirdPartyLabel'] ?? 'Third party',
'choices' => $entity->getProfessionals(),
'choice_label' => fn (ThirdParty $tp) => $this->thirdPartyRender->renderString($tp, []),
'expanded' => false,
]);
}
}
/**
* @param array{mainPerson?: Person, thirdParty?: ThirdParty, title: string} $contextGenerationData
* @param mixed $entity
*/
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
{
$options = $this->getOptions($template);
$data = array_merge(
$this->baseContextData->getData(),
[
'calendar' => $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Calendar::class, 'groups' => ['docgen:read']]),
]
);
if ($options['askMainPerson']) {
$data['mainPerson'] = $this->normalizer->normalize($contextGenerationData['mainPerson'] ?? null, 'docgen', [
'docgen:expects' => Person::class,
'groups' => ['docgen:read'],
'docgen:person:with-household' => true,
'docgen:person:with-relations' => true,
'docgen:person:with-budget' => true,
]);
}
if ($options['askThirdParty']) {
$data['thirdParty'] = $this->normalizer->normalize($contextGenerationData['thirdParty'] ?? null, 'docgen', [
'docgen:expects' => ThirdParty::class,
'groups' => ['docgen:read'],
]);
}
return $data;
}
public function getDescription(): string
{
return 'docgen.calendar.A base context for generating document on calendar';
}
public function getEntityClass(): string
{
return Calendar::class;
}
public function getFormData(DocGeneratorTemplate $template, $entity): array
{
$options = $this->getOptions($template);
$data = [];
if ($options['askMainPerson']) {
$data['mainPerson'] = null;
if (1 === count($entity->getPersons())) {
$data['mainPerson'] = $entity->getPersons()->first();
}
}
if ($options['askThirdParty']) {
$data['thirdParty'] = null;
if (1 === count($entity->getProfessionals())) {
$data['thirdParty'] = $entity->getProfessionals()->first();
}
}
return $data;
}
public static function getKey(): string
{
return self::class;
}
public function getName(): string
{
return 'docgen.calendar.Base context for calendar';
}
public function hasAdminForm(): bool
{
return true;
}
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool
{
return true;
}
/**
* @param array{mainPerson?: Person, thirdParty?: ThirdParty, title: string} $contextGenerationData
*/
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$options = $this->getOptions($template);
$storedObject->setTitle($contextGenerationData['title']);
$doc = new CalendarDoc($entity, $storedObject);
$doc->setTrackDateTimeVersion($options['trackDatetime']);
$this->entityManager->persist($doc);
}
/**
* @return array{askMainPerson: bool, mainPersonLabel: ?string, askThirdParty: bool, thirdPartyLabel: ?string, trackDateTime: bool} $options
*/
private function getOptions(DocGeneratorTemplate $template): array
{
return $template->getOptions();
}
}

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CalendarBundle\Service\DocGenerator;
use Chill\CalendarBundle\Entity\Calendar;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\Form\FormBuilderInterface;
interface CalendarContextInterface extends DocGeneratorContextWithPublicFormInterface, DocGeneratorContextWithAdminFormInterface
{
public function adminFormReverseTransform(array $data): array;
public function adminFormTransform(array $data): array;
public function buildAdminForm(FormBuilderInterface $builder): void;
/**
* @param Calendar $entity
*/
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void;
/**
* @param Calendar $entity
*/
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array;
public function getDescription(): string;
public function getEntityClass(): string;
/**
* @param Calendar $entity
*/
public function getFormData(DocGeneratorTemplate $template, $entity): array;
public static function getKey(): string;
public function getName(): string;
public function hasAdminForm(): bool;
/**
* @param Calendar $entity
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
/**
* @param Calendar $entity
*/
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void;
}