generate document with relatorio: config and driver

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

View File

@ -10,16 +10,6 @@ parameters:
count: 1 count: 1
path: src/Bundle/ChillCustomFieldsBundle/Controller/CustomFieldsGroupController.php 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\\.$#" message: "#^Variable \\$participation might not be defined\\.$#"
count: 3 count: 3

View File

@ -20,12 +20,11 @@ use Symfony\Component\Form\FormBuilderInterface;
*/ */
interface DocGeneratorContextInterface interface DocGeneratorContextInterface
{ {
/** public function adminFormReverseTransform(array $data): array;
* Get the data that will be injected to the generated document.
* public function adminFormTransform(array $data): array;
* @param mixed $entity
*/ public function buildAdminForm(FormBuilderInterface $builder): void;
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array;
/** /**
* Generate the form that display. * Generate the form that display.
@ -34,11 +33,22 @@ interface DocGeneratorContextInterface
*/ */
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; 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 static function getKey(): string;
public function getName(): string; public function getName(): string;
public function getDescription(): string; public function hasAdminForm(): bool;
/** /**
* has form. * has form.
@ -47,15 +57,5 @@ interface DocGeneratorContextInterface
*/ */
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; 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 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;
} }

View File

@ -30,12 +30,24 @@ class AdminDocGeneratorTemplateController extends CRUDController
public function new(Request $request): Response public function new(Request $request): Response
{ {
if (!$request->query->has('context')) { 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); 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 protected function createEntity(string $action, Request $request): object
{ {
/** @var DocGeneratorTemplate $entity */ /** @var DocGeneratorTemplate $entity */
@ -47,19 +59,4 @@ class AdminDocGeneratorTemplateController extends CRUDController
return $entity; 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()
]);
}
} }

View File

@ -1,76 +0,0 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Controller;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlOpenstackGenerator;
use PhpOffice\PhpWord\TemplateProcessor;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class DocGeneratorController.
*/
class DocGeneratorController extends AbstractController
{
/**
* @Route(
* "{_locale}/doc/gen/test",
* name="chill_docgenerator_test"
* )
*/
public function getDoc(Request $request, TempUrlOpenstackGenerator $tempUrlGenerator): Response
{
$p = $tempUrlGenerator->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');
}
}

View File

@ -21,22 +21,22 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Serializer\Model\Collection;
use Exception;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\TransferException; use GuzzleHttp\Exception\TransferException;
use PhpOffice\PhpWord\TemplateProcessor;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
// TODO à mettre dans services // TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
final class DocGeneratorTemplateController extends AbstractController final class DocGeneratorTemplateController extends AbstractController
@ -47,6 +47,8 @@ final class DocGeneratorTemplateController extends AbstractController
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository; private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private DriverInterface $driver;
private KernelInterface $kernel; private KernelInterface $kernel;
private LoggerInterface $logger; private LoggerInterface $logger;
@ -55,8 +57,6 @@ final class DocGeneratorTemplateController extends AbstractController
private TempUrlGeneratorInterface $tempUrlGenerator; private TempUrlGeneratorInterface $tempUrlGenerator;
private DriverInterface $driver;
public function __construct( public function __construct(
ContextManager $contextManager, ContextManager $contextManager,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository, DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
@ -66,7 +66,6 @@ final class DocGeneratorTemplateController extends AbstractController
TempUrlGeneratorInterface $tempUrlGenerator, TempUrlGeneratorInterface $tempUrlGenerator,
KernelInterface $kernel, KernelInterface $kernel,
HttpClientInterface $client HttpClientInterface $client
) { ) {
$this->contextManager = $contextManager; $this->contextManager = $contextManager;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
@ -93,7 +92,7 @@ final class DocGeneratorTemplateController extends AbstractController
$entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId); $entity = $this->getDoctrine()->getRepository($entityClassName)->find($entityId);
if (null === $entity) { 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 { try {
@ -102,6 +101,8 @@ final class DocGeneratorTemplateController extends AbstractController
throw new NotFoundHttpException($e->getMessage(), $e); throw new NotFoundHttpException($e->getMessage(), $e);
} }
$contextGenerationData = [];
if ($context->hasPublicForm($template, $entity)) { if ($context->hasPublicForm($template, $entity)) {
$builder = $this->createFormBuilder(); $builder = $this->createFormBuilder();
$context->buildPublicForm($builder, $template, $entity); $context->buildPublicForm($builder, $template, $entity);
@ -115,8 +116,6 @@ final class DocGeneratorTemplateController extends AbstractController
return $this->render($template, $templateOptions); return $this->render($template, $templateOptions);
} }
} else {
$contextGenerationData = [];
} }
$getUrlGen = $this->tempUrlGenerator->generate( $getUrlGen = $this->tempUrlGenerator->generate(
@ -137,11 +136,11 @@ final class DocGeneratorTemplateController extends AbstractController
$dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat); $dataDecrypted = openssl_decrypt($data->getContent(), $method, $keyGoodFormat, 1, $ivGoodFormat);
if (false === $dataDecrypted) { if (false === $dataDecrypted) {
throw new \Exception('Error during Decrypt ', 1); throw new Exception('Error during Decrypt ', 1);
} }
if (false === $templateResource = fopen('php://memory', 'r+')) { if (false === $templateResource = fopen('php://memory', 'r+b')) {
$this->logger->error("Could not write data to memory"); $this->logger->error('Could not write data to memory');
throw new HttpException(500); throw new HttpException(500);
} }
@ -156,7 +155,7 @@ final class DocGeneratorTemplateController extends AbstractController
fclose($templateResource); fclose($templateResource);
$genDocName = 'doc_' . sprintf('%010d', mt_rand()).'odt'; $genDocName = 'doc_' . sprintf('%010d', mt_rand()) . 'odt';
$getUrlGen = $this->tempUrlGenerator->generate( $getUrlGen = $this->tempUrlGenerator->generate(
'PUT', 'PUT',
@ -181,7 +180,7 @@ final class DocGeneratorTemplateController extends AbstractController
try { try {
$context->storeGenerated($template, $storedObject, $entity, $contextGenerationData); $context->storeGenerated($template, $storedObject, $entity, $contextGenerationData);
} catch (\Exception $e) { } catch (Exception $e) {
$this->logger->error('Could not store the associated document to entity', [ $this->logger->error('Could not store the associated document to entity', [
'entityClassName' => $entityClassName, 'entityClassName' => $entityClassName,
'entityId' => $entityId, 'entityId' => $entityId,
@ -202,7 +201,7 @@ final class DocGeneratorTemplateController extends AbstractController
throw $e; throw $e;
} }
throw new \Exception('Unable to generate document.'); throw new Exception('Unable to generate document.');
} }
/** /**

View File

@ -44,25 +44,6 @@ class ChillDocGeneratorExtension extends Extension implements PrependExtensionIn
$this->prependClientConfig($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) protected function prependCruds(ContainerBuilder $container)
{ {
$container->prependExtensionConfig('chill_main', [ $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'],
],
],
],
]);
}
} }

View File

@ -23,27 +23,26 @@ class Configuration implements ConfigurationInterface
$rootNode $rootNode
->children() ->children()
->arrayNode('driver') ->arrayNode('driver')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children() ->children()
->enumNode('type') ->enumNode('type')
->isRequired() ->isRequired()
->values(['relatorio']) ->values(['relatorio'])
->defaultValue('relatorio') ->defaultValue('relatorio')
->end()
->arrayNode('relatorio')
->addDefaultsIfNotSet()
->children()
->scalarNode('url')
->isRequired()
->defaultValue('http://relatorio:8888/')
->end()
->end()
->end()
->end()
->end()
->end() ->end()
; ->arrayNode('relatorio')
->addDefaultsIfNotSet()
->children()
->scalarNode('url')
->isRequired()
->defaultValue('http://relatorio:8888/')
->end()
->end()
->end()
->end()
->end()
->end();
return $treeBuilder; return $treeBuilder;
} }

View File

@ -25,7 +25,7 @@ use Symfony\Component\Serializer\Annotation as Serializer;
class DocGeneratorTemplate class DocGeneratorTemplate
{ {
/** /**
* @ORM\Column(type="boolean", options={"default":true}) * @ORM\Column(type="boolean", options={"default": true})
*/ */
private bool $active = true; private bool $active = true;
@ -48,16 +48,9 @@ class DocGeneratorTemplate
/** /**
* Class name of the entity for which this template can be used. * 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 = ""; private string $entity = '';
/**
* Options for the template
*
* @ORM\Column(type="json", name="template_options", options={"default":"[]"})
*/
private array $options = [];
/** /**
* @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"})) * @ORM\ManyToOne(targetEntity=StoredObject::class, cascade={"persist"}))
@ -78,17 +71,12 @@ class DocGeneratorTemplate
*/ */
private array $name = []; private array $name = [];
public function isActive(): bool /**
{ * Options for the template.
return $this->active; *
} * @ORM\Column(type="json", name="template_options", options={"default":"[]"})
*/
public function setActive(bool $active): DocGeneratorTemplate private array $options = [];
{
$this->active = $active;
return $this;
}
public function getContext(): ?string public function getContext(): ?string
{ {
@ -100,6 +88,11 @@ class DocGeneratorTemplate
return $this->description; return $this->description;
} }
public function getEntity(): string
{
return $this->entity;
}
public function getFile(): ?StoredObject public function getFile(): ?StoredObject
{ {
return $this->file; return $this->file;
@ -115,6 +108,23 @@ class DocGeneratorTemplate
return $this->name; 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 public function setContext(string $context): self
{ {
$this->context = $context; $this->context = $context;
@ -129,6 +139,13 @@ class DocGeneratorTemplate
return $this; return $this;
} }
public function setEntity(string $entity): self
{
$this->entity = $entity;
return $this;
}
public function setFile(StoredObject $file): self public function setFile(StoredObject $file): self
{ {
$this->file = $file; $this->file = $file;
@ -143,23 +160,6 @@ class DocGeneratorTemplate
return $this; 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 public function setOptions(array $options): self
{ {
$this->options = $options; $this->options = $options;

View File

@ -12,14 +12,11 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Form; namespace Chill\DocGeneratorBundle\Form;
use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
@ -45,17 +42,16 @@ class DocGeneratorTemplateType extends AbstractType
->add('description') ->add('description')
->add('file', StoredObjectType::class, [ ->add('file', StoredObjectType::class, [
'error_bubbling' => true, 'error_bubbling' => true,
]) ]);
;
if ($context->hasAdminForm()) { if ($context->hasAdminForm()) {
$sub = $builder $sub = $builder
->create('options', null, ['compound' => true]) ->create('options', null, ['compound' => true])
->addModelTransformer(new CallbackTransformer( ->addModelTransformer(new CallbackTransformer(
function (array $data) use ($context) { static function (array $data) use ($context) {
return $context->adminFormTransform($data); return $context->adminFormTransform($data);
}, },
function (array $data) use ($context) { static function (array $data) use ($context) {
return $context->adminFormReverseTransform($data); return $context->adminFormReverseTransform($data);
} }
)); ));

View File

@ -1,12 +1,22 @@
<?php <?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\GeneratorDriver; namespace Chill\DocGeneratorBundle\GeneratorDriver;
interface DriverInterface interface DriverInterface
{ {
/** /**
* @param resource $template * @param resource $template
*
* @return resource * @return resource
*/ */
public function generateFromResource($template, string $resourceType, array $data, string $templateName = null); public function generateFromResource($template, string $resourceType, array $data, ?string $templateName = null);
} }

View File

@ -1,11 +1,18 @@
<?php <?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\GeneratorDriver; namespace Chill\DocGeneratorBundle\GeneratorDriver;
use Nelmio\Alice\ParameterBag;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart; use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
@ -31,10 +38,10 @@ class RelatorioDriver implements DriverInterface
$this->url = $parameterBag->get('chill_doc_generator')['driver']['relatorio']['url']; $this->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 = [ $formFields = [
'variables' => \json_encode($data), 'variables' => json_encode($data),
'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType), 'template' => new DataPart($template, $templateName ?? uniqid('template_'), $resourceType),
]; ];
$form = new FormDataPart($formFields); $form = new FormDataPart($formFields);
@ -47,7 +54,7 @@ class RelatorioDriver implements DriverInterface
return $response->toStream(); return $response->toStream();
} catch (HttpExceptionInterface $e) { } catch (HttpExceptionInterface $e) {
$this->logger->error("relatorio: error while generating document", [ $this->logger->error('relatorio: error while generating document', [
'msg' => $e->getMessage(), 'msg' => $e->getMessage(),
'response' => $e->getResponse()->getStatusCode(), 'response' => $e->getResponse()->getStatusCode(),
'content' => $e->getResponse()->getContent(false), 'content' => $e->getResponse()->getContent(false),
@ -55,14 +62,14 @@ class RelatorioDriver implements DriverInterface
throw $e; throw $e;
} catch (TransportExceptionInterface $e) { } catch (TransportExceptionInterface $e) {
$this->logger->error("relatorio: transport exception", [ $this->logger->error('relatorio: transport exception', [
'msg' => $e->getMessage(), 'msg' => $e->getMessage(),
'e' => $e->getTraceAsString(), 'e' => $e->getTraceAsString(),
]); ]);
throw $e; throw $e;
} catch (DecodingExceptionInterface $e) { } catch (DecodingExceptionInterface $e) {
$this->logger->error("relatorio: could not decode response", [ $this->logger->error('relatorio: could not decode response', [
'msg' => $e->getMessage(), 'msg' => $e->getMessage(),
'e' => $e->getTraceAsString(), 'e' => $e->getTraceAsString(),
]); ]);

View File

@ -1,5 +1,12 @@
<?php <?php
/**
* 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.
*/
declare(strict_types=1); declare(strict_types=1);
namespace Chill\Migrations\DocGenerator; namespace Chill\Migrations\DocGenerator;
@ -9,6 +16,15 @@ use Doctrine\Migrations\AbstractMigration;
final class Version20211201191757 extends AbstractMigration final class Version20211201191757 extends AbstractMigration
{ {
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)\'');
}
public function getDescription(): string public function getDescription(): string
{ {
return 'Add options, active and link to entity in docgen_template'; 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('COMMENT ON COLUMN chill_docgen_template.template_options IS \'(DC2Type:json)\'');
$this->addSql('ALTER TABLE chill_docgen_template DROP entities'); $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)\'');
}
} }

View File

@ -15,7 +15,7 @@ use Doctrine\ORM\Mapping as ORM;
/** /**
* @ORM\Table("chill_doc.document_category") * @ORM\Table("chill_doc.document_category")
* @ORM\Entity() * @ORM\Entity
*/ */
class DocumentCategory class DocumentCategory
{ {

View File

@ -14,38 +14,25 @@ namespace Chill\DocStoreBundle\Repository;
use Chill\DocStoreBundle\Entity\DocumentCategory; use Chill\DocStoreBundle\Entity\DocumentCategory;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
use UnexpectedValueException;
/** /**
* Get an available idInsideBUndle. * Get an available idInsideBUndle.
*/ */
class DocumentCategoryRepository implements ObjectRepository class DocumentCategoryRepository implements ObjectRepository
{ {
private EntityRepository $repository;
private EntityManagerInterface $em; private EntityManagerInterface $em;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $em) public function __construct(EntityManagerInterface $em)
{ {
$this->em = $em; $this->em = $em;
$this->repository = $em->getRepository(DocumentCategory::class); $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 public function find($id): ?DocumentCategory
{ {
@ -75,5 +62,14 @@ class DocumentCategoryRepository implements ObjectRepository
return DocumentCategory::class; 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);
}
} }

View File

@ -15,10 +15,8 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
use Chill\DocStoreBundle\Entity\DocumentCategory;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
use Chill\EventBundle\Entity\Participation;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; 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\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function array_key_exists;
class AccompanyingPeriodContext implements DocGeneratorContextInterface class AccompanyingPeriodContext implements DocGeneratorContextInterface
{ {
private DocumentCategoryRepository $documentCategoryRepository;
private EntityManagerInterface $em; private EntityManagerInterface $em;
private NormalizerInterface $normalizer; private NormalizerInterface $normalizer;
private TranslatableStringHelperInterface $translatableStringHelper;
private DocumentCategoryRepository $documentCategoryRepository;
private PersonRender $personRender; private PersonRender $personRender;
private TranslatableStringHelperInterface $translatableStringHelper;
public function __construct( public function __construct(
DocumentCategoryRepository $documentCategoryRepository, DocumentCategoryRepository $documentCategoryRepository,
NormalizerInterface $normalizer, NormalizerInterface $normalizer,
@ -58,94 +57,28 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface
$this->personRender = $personRender; $this->personRender = $personRender;
} }
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array public function adminFormReverseTransform(array $data): array
{ {
if (!$entity instanceof AccompanyingPeriod) { return [
throw new UnexpectedTypeException($entity, AccompanyingPeriod::class); 'mainPerson' => $data['mainPerson'],
} 'person1' => $data['person1'],
$options = $template->getOptions(); 'person2' => $data['person2'],
'category' => [
$data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); 'idInsideBundle' => $data['category']->getIdInsideBundle(),
'bundleId' => $data['category']->getBundleId(),
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 adminFormTransform(array $data): array
* @param AccompanyingPeriod $entity
*/
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
{ {
$options = $template->getOptions(); return [
$persons = $entity->getCurrentParticipations()->map(function (AccompanyingPeriodParticipation $p) { return $p->getPerson(); }) 'mainPerson' => $data['mainPerson'] ?? false,
->toArray(); 'person1' => $data['person1'] ?? false,
'person2' => $data['person2'] ?? false,
foreach (['mainPerson', 'person1', 'person2'] as $key) { 'category' => array_key_exists('category', $data) ?
if ($options[$key] ?? false) { $this->documentCategoryRepository->find($data['category']) : null,
$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;
} }
public function buildAdminForm(FormBuilderInterface $builder): void public function buildAdminForm(FormBuilderInterface $builder): void
@ -172,37 +105,102 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface
return $entity ? $this->translatableStringHelper->localize($entity->getName()) : ''; 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 [ $options = $template->getOptions();
'mainPerson' => $data['mainPerson'] ?? false, $persons = $entity->getCurrentParticipations()->map(static function (AccompanyingPeriodParticipation $p) { return $p->getPerson(); })
'person1' => $data['person1'] ?? false, ->toArray();
'person2' => $data['person2'] ?? false,
'category' => foreach (['mainPerson', 'person1', 'person2'] as $key) {
array_key_exists('category', $data) ? if ($options[$key] ?? false) {
$this->documentCategoryRepository->find($data['category']) : null, $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 [ if (!$entity instanceof AccompanyingPeriod) {
'mainPerson' => $data['mainPerson'], throw new UnexpectedTypeException($entity, AccompanyingPeriod::class);
'person1' => $data['person1'], }
'person2' => $data['person2'], $options = $template->getOptions();
'category' => [
'idInsideBundle' => $data['category']->getIdInsideBundle(), $data = [];
'bundleId' => $data['category']->getBundleId() $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 public function getEntityClass(): string
{ {
return AccompanyingPeriod::class; 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);
}
} }