mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
184 lines
5.9 KiB
PHP
184 lines
5.9 KiB
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\PersonBundle\Service\DocGenerator;
|
|
|
|
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
|
|
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
|
|
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
|
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
|
|
use Chill\DocStoreBundle\Entity\DocumentCategory;
|
|
use Chill\DocStoreBundle\Entity\PersonDocument;
|
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
|
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
|
|
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use DateTime;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
use function array_key_exists;
|
|
|
|
class PersonContext implements DocGeneratorContextWithAdminFormInterface
|
|
{
|
|
private BaseContextData $baseContextData;
|
|
|
|
private DocumentCategoryRepository $documentCategoryRepository;
|
|
|
|
private EntityManagerInterface $em;
|
|
|
|
private NormalizerInterface $normalizer;
|
|
|
|
private TranslatableStringHelperInterface $translatableStringHelper;
|
|
|
|
private TranslatorInterface $translator;
|
|
|
|
public function __construct(
|
|
DocumentCategoryRepository $documentCategoryRepository,
|
|
NormalizerInterface $normalizer,
|
|
TranslatableStringHelperInterface $translatableStringHelper,
|
|
EntityManagerInterface $em,
|
|
TranslatorInterface $translator,
|
|
BaseContextData $baseContextData
|
|
) {
|
|
$this->documentCategoryRepository = $documentCategoryRepository;
|
|
$this->normalizer = $normalizer;
|
|
$this->translatableStringHelper = $translatableStringHelper;
|
|
$this->em = $em;
|
|
$this->baseContextData = $baseContextData;
|
|
$this->translator = $translator;
|
|
}
|
|
|
|
public function adminFormReverseTransform(array $data): array
|
|
{
|
|
if (array_key_exists('category', $data)) {
|
|
$data['category'] = [
|
|
'idInsideBundle' => $data['category']->getIdInsideBundle(),
|
|
'bundleId' => $data['category']->getBundleId(),
|
|
];
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function adminFormTransform(array $data): array
|
|
{
|
|
$r = [
|
|
'mainPerson' => $data['mainPerson'] ?? false,
|
|
'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'),
|
|
];
|
|
|
|
if (array_key_exists('category', $data)) {
|
|
$r['category'] = array_key_exists('category', $data) ?
|
|
$this->documentCategoryRepository->find($data['category']) : null;
|
|
}
|
|
|
|
return $r;
|
|
}
|
|
|
|
public function buildAdminForm(FormBuilderInterface $builder): void
|
|
{
|
|
$builder
|
|
->add('category', EntityType::class, [
|
|
'placeholder' => 'Choose a document category',
|
|
'class' => DocumentCategory::class,
|
|
'query_builder' => static function (EntityRepository $er) {
|
|
return $er->createQueryBuilder('c')
|
|
->where('c.documentClass = :docClass')
|
|
->setParameter('docClass', PersonDocument::class);
|
|
},
|
|
'choice_label' => function ($entity = null) {
|
|
return $entity ? $this->translatableStringHelper->localize($entity->getName()) : '';
|
|
},
|
|
]);
|
|
}
|
|
|
|
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
|
|
{
|
|
if (!$entity instanceof Person) {
|
|
throw new UnexpectedTypeException($entity, Person::class);
|
|
}
|
|
|
|
$data = [];
|
|
$data = array_merge($data, $this->baseContextData->getData());
|
|
$data['person'] = $this->normalizer->normalize($entity, 'docgen', [
|
|
'docgen:expects' => Person::class,
|
|
'groups' => ['docgen:read'],
|
|
'docgen:person:with-household' => true,
|
|
'docgen:person:with-relations' => true,
|
|
'docgen:person:with-budget' => true,
|
|
]);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return 'docgen.A basic context for person';
|
|
}
|
|
|
|
public function getEntityClass(): string
|
|
{
|
|
return Person::class;
|
|
}
|
|
|
|
public function getFormData(DocGeneratorTemplate $template, $entity): array
|
|
{
|
|
return [
|
|
'person' => $entity,
|
|
];
|
|
}
|
|
|
|
public static function getKey(): string
|
|
{
|
|
return self::class;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return 'docgen.Person basic';
|
|
}
|
|
|
|
public function hasAdminForm(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param Person $entity
|
|
*/
|
|
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
|
|
{
|
|
$doc = new PersonDocument();
|
|
$doc->setTemplate($template)
|
|
->setTitle($this->translatableStringHelper->localize($template->getName()))
|
|
->setDate(new DateTime())
|
|
->setDescription($this->translatableStringHelper->localize($template->getName()))
|
|
->setPerson($entity)
|
|
->setObject($storedObject);
|
|
|
|
if (array_key_exists('category', $template->getOptions())) {
|
|
$doc
|
|
->setCategory(
|
|
$this->documentCategoryRepository->find(
|
|
$template->getOptions()['category']
|
|
)
|
|
);
|
|
}
|
|
|
|
$this->em->persist($doc);
|
|
}
|
|
}
|