generate document with relatorio: config and driver

This commit is contained in:
2021-12-02 18:02:19 +01:00
parent 2245f83631
commit af6efdd0ba
15 changed files with 286 additions and 363 deletions

View File

@@ -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);
}
}