diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php new file mode 100644 index 000000000..2b22d607c --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -0,0 +1,214 @@ +documentCategoryRepository = $documentCategoryRepository; + $this->normalizer = $normalizer; + $this->translatableStringHelper = $translatableStringHelper; + $this->em = $em; + $this->baseContextData = $baseContextData; + } + + 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'), + // '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'), + // ]; + + 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('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, + // ]) + ->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', 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); + } + $options = $template->getOptions(); + + $data = []; + $data = array_merge($data, $this->baseContextData->getData()); + $data['person'] = $this->normalizer->normalize($entity, '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 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); + } +} \ No newline at end of file