From 91d21ba939e39af504888be3cced98d0b95a92bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 14 Feb 2023 23:26:00 +0100 Subject: [PATCH] Feature: [docgen][stored object] handler for request generator and required fixes --- .../Service/DocGenerator/ActivityContext.php | 10 ++ ...tActivitiesByAccompanyingPeriodContext.php | 10 ++ .../Service/DocGenerator/CalendarContext.php | 10 ++ .../DocGenerator/CalendarContextInterface.php | 4 + ...eneratorContextWithPublicFormInterface.php | 13 ++ .../DocGeneratorTemplateController.php | 130 ++++++++---------- .../Service/Generator/Generator.php | 35 ++--- .../Service/Generator/GeneratorInterface.php | 28 ++++ .../Messenger/RequestGenerationHandler.php | 50 +++++++ .../Messenger/RequestGenerationMessage.php | 27 +++- .../config/services.yaml | 16 ++- .../Phonenumber/PhonenumberHelper.php | 10 +- .../AccompanyingPeriodContext.php | 10 ++ .../AccompanyingPeriodWorkContext.php | 2 + ...ccompanyingPeriodWorkEvaluationContext.php | 10 ++ .../Service/DocGenerator/PersonContext.php | 38 +++++ .../DocGenerator/PersonContextInterface.php | 4 + .../PersonContextWithThirdParty.php | 10 ++ 18 files changed, 307 insertions(+), 110 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationHandler.php diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index 4e2970138..f7f30713b 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -206,6 +206,16 @@ class ActivityContext implements return $options['mainPerson'] || $options['person1'] || $options['person2']; } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + /** * @param Activity $entity */ diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ListActivitiesByAccompanyingPeriodContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ListActivitiesByAccompanyingPeriodContext.php index 3189307f9..3621ed80b 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ListActivitiesByAccompanyingPeriodContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ListActivitiesByAccompanyingPeriodContext.php @@ -146,6 +146,16 @@ class ListActivitiesByAccompanyingPeriodContext implements return $this->accompanyingPeriodContext->hasPublicForm($template, $entity); } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void { $this->accompanyingPeriodContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData); diff --git a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php index 9ba9e36b4..e7be965a5 100644 --- a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php +++ b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php @@ -226,6 +226,16 @@ final class CalendarContext implements CalendarContextInterface return true; } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + /** * @param array{mainPerson?: Person, thirdParty?: ThirdParty, title: string} $contextGenerationData */ diff --git a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContextInterface.php b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContextInterface.php index d02cdc2c2..692d6991e 100644 --- a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContextInterface.php +++ b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContextInterface.php @@ -56,6 +56,10 @@ interface CalendarContextInterface extends DocGeneratorContextWithPublicFormInte */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array; + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array; + /** * @param Calendar $entity */ diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php index f013c8435..1ad3dfa51 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php @@ -23,6 +23,9 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void; + /** + * Fill the form with initial data + */ public function getFormData(DocGeneratorTemplate $template, $entity): array; /** @@ -31,4 +34,14 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext * @param mixed $entity */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; + + /** + * Transform the data from the form into serializable data, storable into messenger's message + */ + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array; + + /** + * Reverse the data from the messenger's message into data usable for doc's generation + */ + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index d618f758a..ecb27c455 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -16,30 +16,28 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; -use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; +use Chill\DocGeneratorBundle\Service\Generator\GeneratorInterface; +use Chill\DocGeneratorBundle\Service\Messenger\RequestGenerationMessage; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; use Chill\MainBundle\Pagination\PaginatorFactory; use Chill\MainBundle\Serializer\Model\Collection; use Doctrine\ORM\EntityManagerInterface; -use Exception; use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\FileType; -use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; // TODO à mettre dans services use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Contracts\HttpClient\HttpClientInterface; -use Throwable; use function strlen; final class DocGeneratorTemplateController extends AbstractController @@ -54,17 +52,24 @@ final class DocGeneratorTemplateController extends AbstractController private EntityManagerInterface $entityManager; + private GeneratorInterface $generator; + private LoggerInterface $logger; + private MessageBusInterface $messageBus; + private PaginatorFactory $paginatorFactory; private StoredObjectManagerInterface $storedObjectManager; + public function __construct( ContextManager $contextManager, DocGeneratorTemplateRepository $docGeneratorTemplateRepository, DriverInterface $driver, + GeneratorInterface $generator, LoggerInterface $logger, + MessageBusInterface $messageBus, PaginatorFactory $paginatorFactory, HttpClientInterface $client, StoredObjectManagerInterface $storedObjectManager, @@ -73,7 +78,9 @@ final class DocGeneratorTemplateController extends AbstractController $this->contextManager = $contextManager; $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->driver = $driver; + $this->generator = $generator; $this->logger = $logger; + $this->messageBus = $messageBus; $this->paginatorFactory = $paginatorFactory; $this->client = $client; $this->storedObjectManager = $storedObjectManager; @@ -259,99 +266,72 @@ final class DocGeneratorTemplateController extends AbstractController } } - $document = $template->getFile(); - - if ($isTest && ($contextGenerationData['test_file'] instanceof File)) { - $dataDecrypted = file_get_contents($contextGenerationData['test_file']->getPathname()); - } else { - try { - $dataDecrypted = $this->storedObjectManager->read($document); - } catch (Throwable $exception) { - throw $exception; - } - } + // transform context generation data + $contextGenerationDataSanitized = array_merge( + $contextGenerationData, + $context instanceof DocGeneratorContextWithPublicFormInterface ? + $context->publicFormTransform($template, $entity, $contextGenerationData) + : [] + ); + // if is test, render the data or generate the doc if ($isTest && isset($form) && $form['show_data']->getData()) { return $this->render('@ChillDocGenerator/Generator/debug_value.html.twig', [ 'datas' => json_encode($context->getData($template, $entity, $contextGenerationData), JSON_PRETTY_PRINT) ]); - } - - try { - $generatedResource = $this - ->driver - ->generateFromString( - $dataDecrypted, - $template->getFile()->getType(), - $context->getData($template, $entity, $contextGenerationData), - $template->getFile()->getFilename() - ); - } catch (TemplateException $e) { - return new Response( - implode("\n", $e->getErrors()), - 400, - [ - 'Content-Type' => 'text/plain', - ] + } elseif ($isTest) { + $generated = $this->generator->generateDocFromTemplate( + $template, + $entityClassName, + $entityId, + $contextGenerationDataSanitized, + null, + true, + isset($form) ? $form['test_file']->getData() : null ); - } - if ($isTest) { return new Response( - $generatedResource, + $generated, Response::HTTP_OK, [ 'Content-Transfer-Encoding', 'binary', 'Content-Type' => 'application/vnd.oasis.opendocument.text', 'Content-Disposition' => 'attachment; filename="generated.odt"', - 'Content-Length' => strlen($generatedResource), + 'Content-Length' => strlen($generated), ], ); } - /** @var StoredObject $storedObject */ - $storedObject = (new ObjectNormalizer()) - ->denormalize( - [ - 'type' => $template->getFile()->getType(), - 'filename' => sprintf('%s_odt', uniqid('doc_', true)), - ], - StoredObject::class - ); - - try { - $this->storedObjectManager->write($storedObject, $generatedResource); - } catch (Throwable $exception) { - throw $exception; - } + // this is not a test + // we prepare the object to store the document + $storedObject = (new StoredObject()) + ->setStatus(StoredObject::STATUS_PENDING) + ; $this->entityManager->persist($storedObject); - try { - $context - ->storeGenerated( - $template, - $storedObject, - $entity, - $contextGenerationData - ); - } catch (Exception $e) { - $this - ->logger - ->error( - 'Unable to store the associated document to entity', - [ - 'entityClassName' => $entityClassName, - 'entityId' => $entityId, - 'contextKey' => $context->getName(), - ] - ); - - throw $e; - } + // we store the generated document + $context + ->storeGenerated( + $template, + $storedObject, + $entity, + $contextGenerationData + ); $this->entityManager->flush(); + $this->messageBus->dispatch( + new RequestGenerationMessage( + $this->getUser(), + $template, + $entityId, + $entityClassName, + $storedObject, + $contextGenerationDataSanitized, + ) + ); + return $this ->redirectToRoute( 'chill_wopi_file_edit', diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php index 755e608ba..376cfc268 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php @@ -3,6 +3,7 @@ namespace Chill\DocGeneratorBundle\Service\Generator; use Chill\DocGeneratorBundle\Context\ContextManagerInterface; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; @@ -12,7 +13,7 @@ use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\File\File; -class Generator +class Generator implements GeneratorInterface { private ContextManagerInterface $contextManager; @@ -50,6 +51,7 @@ class Generator DocGeneratorTemplate $template, string $entityClassName, int $entityId, + array $contextGenerationData, ?StoredObject $destinationStoredObject = null, bool $isTest = false, ?File $testFile = null @@ -59,7 +61,6 @@ class Generator } $context = $this->contextManager->getContextByDocGeneratorTemplate($template); - $contextGenerationData = ['test_file' => $testFile]; $entity = $this ->entityManager @@ -70,6 +71,13 @@ class Generator throw new RelatedEntityNotFoundException($entityClassName, $entityId); } + $contextGenerationData = array_merge( + $contextGenerationData, + $context instanceof DocGeneratorContextWithPublicFormInterface ? + $context->publicFormReverseTransform($template, $entity, $contextGenerationData) + : [] + ); + if ($isTest && ($testFile instanceof File)) { $dataDecrypted = file_get_contents($testFile->getPathname()); } else { @@ -102,29 +110,6 @@ class Generator $this->storedObjectManager->write($destinationStoredObject, $generatedResource); - try { - $context - ->storeGenerated( - $template, - $destinationStoredObject, - $entity, - $contextGenerationData - ); - } catch (\Exception $e) { - $this - ->logger - ->error( - 'Unable to store the associated document to entity', - [ - 'entityClassName' => $entityClassName, - 'entityId' => $entityId, - 'contextKey' => $context->getName(), - ] - ); - - throw $e; - } - $this->entityManager->flush(); return null; diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php new file mode 100644 index 000000000..c18dc3bc2 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php @@ -0,0 +1,28 @@ +storedObjectRepository = $storedObjectRepository; + $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; + $this->generator = $generator; + } + + public function __invoke(RequestGenerationMessage $message) + { + if (null === $template = $this->docGeneratorTemplateRepository->find($message->getTemplateId())) { + throw new \RuntimeException('template not found: ' . $message->getTemplateId()); + } + + if (null === $destinationStoredObject = $this->storedObjectRepository->find($message->getDestinationStoredObjectId())) { + throw new \RuntimeException('destination stored object not found : ' . $message->getDestinationStoredObjectId()); + } + + $this->generator->generateDocFromTemplate( + $template, + $message->getEntityClassName(), + $message->getEntityId(), + $message->getContextGenerationData(), + $destinationStoredObject + ); + } + +} diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php index 1489e2686..535e569d7 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php @@ -3,6 +3,7 @@ namespace Chill\DocGeneratorBundle\Service\Messenger; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Entity\User; class RequestGenerationMessage @@ -15,12 +16,24 @@ class RequestGenerationMessage private string $entityClassName; - public function __construct(User $creator, DocGeneratorTemplate $template, int $entityId, string $entityClassName) - { + private int $destinationStoredObjectId; + + private array $contextGenerationData; + + public function __construct( + User $creator, + DocGeneratorTemplate $template, + int $entityId, + string $entityClassName, + StoredObject $destinationStoredObject, + array $contextGenerationData + ) { $this->creatorId = $creator->getId(); $this->templateId = $template->getId(); $this->entityId = $entityId; $this->entityClassName = $entityClassName; + $this->destinationStoredObjectId = $destinationStoredObject->getId(); + $this->contextGenerationData = $contextGenerationData; } public function getCreatorId(): int @@ -28,6 +41,11 @@ class RequestGenerationMessage return $this->creatorId; } + public function getDestinationStoredObjectId(): int + { + return $this->destinationStoredObjectId; + } + public function getTemplateId(): int { return $this->templateId; @@ -42,4 +60,9 @@ class RequestGenerationMessage { return $this->entityClassName; } + + public function getContextGenerationData(): array + { + return $this->contextGenerationData; + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 5bdfe2a11..5fef6fb22 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -20,10 +20,14 @@ services: resource: '../Serializer/Normalizer/' tags: - { name: 'serializer.normalizer', priority: -152 } + Chill\DocGeneratorBundle\Serializer\Normalizer\CollectionDocGenNormalizer: tags: - { name: 'serializer.normalizer', priority: -126 } + Chill\DocGeneratorBundle\Service\Context\: + resource: "../Service/Context" + Chill\DocGeneratorBundle\Controller\: resource: "../Controller" autowire: true @@ -34,18 +38,20 @@ services: autowire: true autoconfigure: true - Chill\DocGeneratorBundle\Service\Context\: - resource: "../Service/Context/" - autowire: true - autoconfigure: true - Chill\DocGeneratorBundle\GeneratorDriver\: resource: "../GeneratorDriver/" autowire: true autoconfigure: true + Chill\DocGeneratorBundle\Service\Messenger\: + resource: "../Service/Messenger/" + + Chill\DocGeneratorBundle\Service\Generator\Generator: ~ + Chill\DocGeneratorBundle\Service\Generator\GeneratorInterface: '@Chill\DocGeneratorBundle\Service\Generator\Generator' + Chill\DocGeneratorBundle\Driver\RelatorioDriver: '@Chill\DocGeneratorBundle\Driver\DriverInterface' Chill\DocGeneratorBundle\Context\ContextManager: arguments: $contexts: !tagged_iterator { tag: chill_docgen.context, default_index_method: getKey } + Chill\DocGeneratorBundle\Context\ContextManagerInterface: '@Chill\DocGeneratorBundle\Context\ContextManager' diff --git a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php index 5b52f6819..94a09cafb 100644 --- a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php +++ b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php @@ -118,7 +118,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface * Return true if the phonenumber is a landline or voip phone. Return always true * if the validation is not configured. * - * @param string $phonenumber + * @param string|PhoneNumber $phonenumber */ public function isValidPhonenumberAny($phonenumber): bool { @@ -138,7 +138,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface * Return true if the phonenumber is a landline or voip phone. Return always true * if the validation is not configured. * - * @param string $phonenumber + * @param string|PhoneNumber $phonenumber */ public function isValidPhonenumberLandOrVoip($phonenumber): bool { @@ -159,7 +159,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface * REturn true if the phonenumber is a mobile phone. Return always true * if the validation is not configured. * - * @param string $phonenumber + * @param string|PhoneNumber $phonenumber */ public function isValidPhonenumberMobile($phonenumber): bool { @@ -182,6 +182,10 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface return null; } + if ($phonenumber instanceof PhoneNumber) { + $phonenumber = (string) $phonenumber; + } + // filter only number $filtered = preg_replace('/[^0-9]/', '', $phonenumber); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 23bfac56f..dac850446 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -256,6 +256,16 @@ class AccompanyingPeriodContext implements return $options['mainPerson'] || $options['person1'] || $options['person2']; } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + /** * @param AccompanyingPeriod $entity */ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php index 7e9280e5d..d73d1aa7b 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkContext.php @@ -109,6 +109,8 @@ class AccompanyingPeriodWorkContext 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 index 4dd14faab..3b9d958cb 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -174,6 +174,16 @@ class AccompanyingPeriodWorkEvaluationContext implements ->hasPublicForm($template, $entity->getAccompanyingPeriodWork()); } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void { $doc = new AccompanyingPeriodWorkEvaluationDocument(); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index af6c6e114..5f98f7798 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -21,11 +21,13 @@ use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Form\Type\ScopePickerType; +use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Repository\PersonRepository; use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; @@ -55,6 +57,8 @@ final class PersonContext implements PersonContextInterface private NormalizerInterface $normalizer; + private ScopeRepositoryInterface $scopeRepository; + private Security $security; private bool $showScopes; @@ -71,6 +75,7 @@ final class PersonContext implements PersonContextInterface EntityManagerInterface $em, NormalizerInterface $normalizer, ParameterBagInterface $parameterBag, + ScopeRepositoryInterface $scopeRepository, Security $security, TranslatorInterface $translator, TranslatableStringHelperInterface $translatableStringHelper @@ -81,6 +86,7 @@ final class PersonContext implements PersonContextInterface $this->documentCategoryRepository = $documentCategoryRepository; $this->em = $em; $this->normalizer = $normalizer; + $this->scopeRepository = $scopeRepository; $this->security = $security; $this->showScopes = $parameterBag->get('chill_main')['acl']['form_show_scopes']; $this->translator = $translator; @@ -211,6 +217,38 @@ final class PersonContext implements PersonContextInterface return true; } + /** + * @param Person $entity + */ + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + $scope = $data['scope'] ?? null; + + return [ + 'title' => $data['title'] ?? '', + 'scope_id' => $scope instanceof Scope ? $scope->getId() : null, + ]; + } + + /** + * @param Person $entity + */ + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + if (!isset($data['scope'])) { + $scope = null; + } else { + if (null === $scope = $this->scopeRepository->find($data['scope'])) { + throw new \UnexpectedValueException('scope not found'); + } + } + + return [ + 'title' => $data['title'] ?? '', + 'scope' => $scope, + ]; + } + /** * @param Person $entity */ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php index 58a6b5863..782ed13db 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php @@ -48,6 +48,10 @@ interface PersonContextInterface extends DocGeneratorContextWithAdminFormInterfa */ public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool; + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array; + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array; + /** * @param Person $entity */ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextWithThirdParty.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextWithThirdParty.php index 23d57c431..b4fa6fb26 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextWithThirdParty.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextWithThirdParty.php @@ -123,6 +123,16 @@ class PersonContextWithThirdParty implements DocGeneratorContextWithAdminFormInt return true; } + public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormTransform() method. + } + + public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array + { + // TODO: Implement publicFormReverseTransform() method. + } + public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void { $this->personContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData);