mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
add form to doc generation and custom form to admin template configuration
This commit is contained in:
@@ -15,11 +15,21 @@ 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;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\PersonBundle\Templating\Entity\PersonRender;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
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;
|
||||
|
||||
class AccompanyingPeriodContext implements DocGeneratorContextInterface
|
||||
@@ -30,28 +40,62 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface
|
||||
|
||||
private TranslatableStringHelperInterface $translatableStringHelper;
|
||||
|
||||
private DocumentCategoryRepository $documentCategoryRepository;
|
||||
|
||||
private PersonRender $personRender;
|
||||
|
||||
public function __construct(
|
||||
DocumentCategoryRepository $documentCategoryRepository,
|
||||
NormalizerInterface $normalizer,
|
||||
TranslatableStringHelperInterface $translatableStringHelper,
|
||||
EntityManagerInterface $em
|
||||
EntityManagerInterface $em,
|
||||
PersonRender $personRender
|
||||
) {
|
||||
$this->documentCategoryRepository = $documentCategoryRepository;
|
||||
$this->normalizer = $normalizer;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->em = $em;
|
||||
$this->personRender = $personRender;
|
||||
}
|
||||
|
||||
public function getData($entity): array
|
||||
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
|
||||
{
|
||||
if (!$entity instanceof AccompanyingPeriod) {
|
||||
throw new UnexpectedTypeException($entity, AccompanyingPeriod::class);
|
||||
}
|
||||
$options = $template->getOptions();
|
||||
|
||||
return $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]);
|
||||
$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 getForm($entity)
|
||||
/**
|
||||
* @param AccompanyingPeriod $entity
|
||||
*/
|
||||
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
|
||||
{
|
||||
// TODO: Implement getForm() method.
|
||||
$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
|
||||
@@ -61,30 +105,104 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Accompanying Period';
|
||||
return 'Accompanying Period basic';
|
||||
}
|
||||
|
||||
public function hasForm(): bool
|
||||
public function getDescription(): string
|
||||
{
|
||||
return false;
|
||||
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): void
|
||||
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);
|
||||
->setObject($storedObject)
|
||||
;
|
||||
|
||||
if (array_key_exists('category', $template->getOptions()['category'])) {
|
||||
$doc
|
||||
->setCategory($this->documentCategoryRepository->find(
|
||||
$template->getOptions()['category'])
|
||||
);
|
||||
}
|
||||
|
||||
$this->em->persist($doc);
|
||||
}
|
||||
|
||||
public function supports(string $entityClass): bool
|
||||
public function hasAdminForm(): bool
|
||||
{
|
||||
return AccompanyingPeriod::class === $entityClass;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function buildAdminForm(FormBuilderInterface $builder): void
|
||||
{
|
||||
$builder
|
||||
->add('mainPerson', CheckboxType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
->add('person1', CheckboxType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
->add('person2', CheckboxType::class, [
|
||||
'required' => false,
|
||||
])
|
||||
->add('category', EntityType::class, [
|
||||
'placeholder' => 'Choose a document category',
|
||||
'class' => 'ChillDocStoreBundle:DocumentCategory',
|
||||
'query_builder' => static function (EntityRepository $er) {
|
||||
return $er->createQueryBuilder('c')
|
||||
->where('c.documentClass = :docClass')
|
||||
->setParameter('docClass', AccompanyingCourseDocument::class);
|
||||
},
|
||||
'choice_label' => function ($entity = null) {
|
||||
return $entity ? $this->translatableStringHelper->localize($entity->getName()) : '';
|
||||
},
|
||||
]);
|
||||
;
|
||||
}
|
||||
|
||||
public function adminFormTransform(array $data): array
|
||||
{
|
||||
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 adminFormReverseTransform(array $data): array
|
||||
{
|
||||
return [
|
||||
'mainPerson' => $data['mainPerson'],
|
||||
'person1' => $data['person1'],
|
||||
'person2' => $data['person2'],
|
||||
'category' => [
|
||||
'idInsideBundle' => $data['category']->getIdInsideBundle(),
|
||||
'bundleId' => $data['category']->getBundleId()
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function getEntityClass(): string
|
||||
{
|
||||
return AccompanyingPeriod::class;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user