mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-11-04 03:08:25 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			217 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			217 lines
		
	
	
		
			7.4 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\ActivityBundle\Service\DocGenerator;
 | 
						|
 | 
						|
use Chill\ActivityBundle\Entity\Activity;
 | 
						|
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
 | 
						|
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
 | 
						|
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
 | 
						|
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
 | 
						|
use Chill\DocGeneratorBundle\Service\Context\BaseContextData;
 | 
						|
use Chill\DocStoreBundle\Entity\StoredObject;
 | 
						|
use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
 | 
						|
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
 | 
						|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
 | 
						|
use Chill\PersonBundle\Entity\Person;
 | 
						|
use Chill\PersonBundle\Templating\Entity\PersonRender;
 | 
						|
use Doctrine\ORM\EntityManagerInterface;
 | 
						|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
 | 
						|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
 | 
						|
use Symfony\Component\Form\Extension\Core\Type\TextType;
 | 
						|
use Symfony\Component\Form\FormBuilderInterface;
 | 
						|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
 | 
						|
use Symfony\Contracts\Translation\TranslatorInterface;
 | 
						|
 | 
						|
class ActivityContext implements
 | 
						|
    DocGeneratorContextWithAdminFormInterface,
 | 
						|
    DocGeneratorContextWithPublicFormInterface
 | 
						|
{
 | 
						|
    private BaseContextData $baseContextData;
 | 
						|
 | 
						|
    private DocumentCategoryRepository $documentCategoryRepository;
 | 
						|
 | 
						|
    private EntityManagerInterface $em;
 | 
						|
 | 
						|
    private NormalizerInterface $normalizer;
 | 
						|
 | 
						|
    private PersonRender $personRender;
 | 
						|
 | 
						|
    private TranslatableStringHelperInterface $translatableStringHelper;
 | 
						|
 | 
						|
    private TranslatorInterface $translator;
 | 
						|
 | 
						|
    public function __construct(
 | 
						|
        DocumentCategoryRepository $documentCategoryRepository,
 | 
						|
        NormalizerInterface $normalizer,
 | 
						|
        TranslatableStringHelperInterface $translatableStringHelper,
 | 
						|
        EntityManagerInterface $em,
 | 
						|
        PersonRender $personRender,
 | 
						|
        TranslatorInterface $translator,
 | 
						|
        BaseContextData $baseContextData
 | 
						|
    ) {
 | 
						|
        $this->documentCategoryRepository = $documentCategoryRepository;
 | 
						|
        $this->normalizer = $normalizer;
 | 
						|
        $this->translatableStringHelper = $translatableStringHelper;
 | 
						|
        $this->em = $em;
 | 
						|
        $this->personRender = $personRender;
 | 
						|
        $this->translator = $translator;
 | 
						|
        $this->baseContextData = $baseContextData;
 | 
						|
    }
 | 
						|
 | 
						|
    public function adminFormReverseTransform(array $data): array
 | 
						|
    {
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    public function adminFormTransform(array $data): array
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            'mainPerson' => $data['mainPerson'] ?? false,
 | 
						|
            'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'),
 | 
						|
            'person1' => $data['person1'] ?? false,
 | 
						|
            'person1Label' => $data['person1Label'] ?? $this->translator->trans('docgen.person 1'),
 | 
						|
            'person2' => $data['person2'] ?? false,
 | 
						|
            'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'),
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    public function buildAdminForm(FormBuilderInterface $builder): void
 | 
						|
    {
 | 
						|
        $builder
 | 
						|
            ->add('mainPerson', CheckboxType::class, [
 | 
						|
                'required' => false,
 | 
						|
                'label' => 'docgen.Ask for main person',
 | 
						|
            ])
 | 
						|
            ->add('mainPersonLabel', TextType::class, [
 | 
						|
                'label' => 'main person label',
 | 
						|
                'required' => true,
 | 
						|
            ])
 | 
						|
            ->add('person1', CheckboxType::class, [
 | 
						|
                'required' => false,
 | 
						|
                'label' => 'docgen.Ask for person 1',
 | 
						|
            ])
 | 
						|
            ->add('person1Label', TextType::class, [
 | 
						|
                'label' => 'person 1 label',
 | 
						|
                'required' => true,
 | 
						|
            ])
 | 
						|
            ->add('person2', CheckboxType::class, [
 | 
						|
                'required' => false,
 | 
						|
                'label' => 'docgen.Ask for person 2',
 | 
						|
            ])
 | 
						|
            ->add('person2Label', TextType::class, [
 | 
						|
                'label' => 'person 2 label',
 | 
						|
                'required' => true,
 | 
						|
            ]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param Activity $entity
 | 
						|
     */
 | 
						|
    public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
 | 
						|
    {
 | 
						|
        $options = $template->getOptions();
 | 
						|
        $persons = $entity->getPersons();
 | 
						|
 | 
						|
        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,
 | 
						|
                    'label' => $options[$key . 'Label'],
 | 
						|
                ]);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
 | 
						|
    {
 | 
						|
        if (!$entity instanceof Activity) {
 | 
						|
            throw new UnexpectedTypeException($entity, Activity::class);
 | 
						|
        }
 | 
						|
        $options = $template->getOptions();
 | 
						|
 | 
						|
        $data = [];
 | 
						|
        $data = array_merge($data, $this->baseContextData->getData());
 | 
						|
        $data['activity'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Activity::class, 'groups' => 'docgen:read']);
 | 
						|
 | 
						|
        $data['course'] = $this->normalizer->normalize($entity->getAccompanyingPeriod(), 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']);
 | 
						|
        $data['person'] = $this->normalizer->normalize($entity->getPerson(), 'docgen', ['docgen:expects' => Person::class, 'groups' => 'docgen:read']);
 | 
						|
 | 
						|
        foreach (['mainPerson', 'person1', 'person2'] as $k) {
 | 
						|
            if ($options[$k]) {
 | 
						|
                $data[$k] = $this->normalizer->normalize($contextGenerationData[$k], 'docgen', [
 | 
						|
                    'docgen:expects' => Person::class,
 | 
						|
                    'groups' => 'docgen:read',
 | 
						|
                    'docgen:person:with-household' => true,
 | 
						|
                    'docgen:person:with-relations' => true,
 | 
						|
                ]);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getDescription(): string
 | 
						|
    {
 | 
						|
        return 'docgen.A basic context for activity';
 | 
						|
    }
 | 
						|
 | 
						|
    public function getEntityClass(): string
 | 
						|
    {
 | 
						|
        return Activity::class;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getFormData(DocGeneratorTemplate $template, $entity): array
 | 
						|
    {
 | 
						|
        return [
 | 
						|
            'activity' => $entity,
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    public static function getKey(): string
 | 
						|
    {
 | 
						|
        return self::class;
 | 
						|
    }
 | 
						|
 | 
						|
    public function getName(): string
 | 
						|
    {
 | 
						|
        return 'docgen.Activity 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 Activity $entity
 | 
						|
     */
 | 
						|
    public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
 | 
						|
    {
 | 
						|
        $entity->addDocument($storedObject);
 | 
						|
 | 
						|
        $this->em->persist($storedObject);
 | 
						|
    }
 | 
						|
}
 |