|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the B2Bcodext CMS Form Builder. |
| 5 | + * |
| 6 | + * (c) Daniel Nahrebecki <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace B2bCode\Bundle\CmsFormBundle\Builder; |
| 13 | + |
| 14 | +use B2bCode\Bundle\CmsFormBundle\Entity\CmsForm; |
| 15 | +use B2bCode\Bundle\CmsFormBundle\Entity\CmsFormField; |
| 16 | +use B2bCode\Bundle\CmsFormBundle\Exception\CmsFormNotFound; |
| 17 | +use B2bCode\Bundle\CmsFormBundle\Form\Type\CmsFormType; |
| 18 | +use B2bCode\Bundle\CmsFormBundle\Provider\FieldTypeRegistry; |
| 19 | +use B2bCode\Bundle\CmsFormBundle\Validator\Config\FormConstraintCollection; |
| 20 | +use B2bCode\Bundle\CmsFormBundle\Validator\ConstraintProviderInterface; |
| 21 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 22 | +use Symfony\Component\Form\FormBuilderInterface as SymfonyFormBuilderInterface; |
| 23 | +use Symfony\Component\Form\FormFactoryInterface; |
| 24 | +use Symfony\Component\Form\FormInterface; |
| 25 | +use Symfony\Component\Routing\RouterInterface; |
| 26 | + |
| 27 | +class FormBuilder implements FormBuilderInterface |
| 28 | +{ |
| 29 | + /** @var FormFactoryInterface */ |
| 30 | + protected $formFactory; |
| 31 | + |
| 32 | + /** @var ManagerRegistry */ |
| 33 | + protected $managerRegistry; |
| 34 | + |
| 35 | + /** @var FieldTypeRegistry */ |
| 36 | + protected $fieldTypeRegistry; |
| 37 | + |
| 38 | + /** @var RouterInterface */ |
| 39 | + protected $router; |
| 40 | + |
| 41 | + /** @var ConstraintProviderInterface */ |
| 42 | + protected $constraintProvider; |
| 43 | + |
| 44 | + /** |
| 45 | + * @param FormFactoryInterface $formFactory |
| 46 | + * @param ManagerRegistry $managerRegistry |
| 47 | + * @param FieldTypeRegistry $fieldTypeRegistry |
| 48 | + * @param RouterInterface $router |
| 49 | + * @param ConstraintProviderInterface $constraintProvider |
| 50 | + */ |
| 51 | + public function __construct( |
| 52 | + FormFactoryInterface $formFactory, |
| 53 | + ManagerRegistry $managerRegistry, |
| 54 | + FieldTypeRegistry $fieldTypeRegistry, |
| 55 | + RouterInterface $router, |
| 56 | + ConstraintProviderInterface $constraintProvider |
| 57 | + ) { |
| 58 | + $this->formFactory = $formFactory; |
| 59 | + $this->managerRegistry = $managerRegistry; |
| 60 | + $this->fieldTypeRegistry = $fieldTypeRegistry; |
| 61 | + $this->router = $router; |
| 62 | + $this->constraintProvider = $constraintProvider; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function getForm(string $alias, array $options = []): FormInterface |
| 69 | + { |
| 70 | + $repository = $this->managerRegistry->getManagerForClass(CmsForm::class)->getRepository(CmsForm::class); |
| 71 | + /** @var CmsForm $cmsForm */ |
| 72 | + $cmsForm = $repository->findOneBy(['alias' => $alias]); |
| 73 | + |
| 74 | + if ($cmsForm === null) { |
| 75 | + throw new CmsFormNotFound(sprintf('CmsForm with alias %s not found', $alias)); |
| 76 | + } |
| 77 | + |
| 78 | + if (!array_key_exists('action', $options) || $options['action'] === null || $options['action'] === '') { |
| 79 | + $options['action'] = $this->router->generate('b2b_code_cms_frontend_ajax_respond', [ |
| 80 | + 'uuid' => $cmsForm->uuid() |
| 81 | + ]); |
| 82 | + } |
| 83 | + |
| 84 | + return $this->buildForm($cmsForm, $options); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Builds form containing only one field. Useful for the field preview. |
| 89 | + * |
| 90 | + * @param CmsFormField $field |
| 91 | + * @return FormInterface |
| 92 | + */ |
| 93 | + public function buildField(CmsFormField $field): FormInterface |
| 94 | + { |
| 95 | + $formBuilder = $this->formFactory->createBuilder(CmsFormType::class, null, ['csrf_protection' => false]); |
| 96 | + $this->addField($formBuilder, $field); |
| 97 | + |
| 98 | + return $formBuilder->getForm(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param CmsForm $cmsForm |
| 103 | + * @param array $options |
| 104 | + * |
| 105 | + * @return FormInterface |
| 106 | + */ |
| 107 | + protected function buildForm(CmsForm $cmsForm, array $options = []): FormInterface |
| 108 | + { |
| 109 | + $formBuilder = $this->formFactory->createBuilder(CmsFormType::class, null, $options); |
| 110 | + $constraintCollection = $this->constraintProvider->getConstraintsForForm($cmsForm); |
| 111 | + |
| 112 | + // configure fields |
| 113 | + foreach ($cmsForm->getFields() as $field) { |
| 114 | + $this->addField($formBuilder, $field, $constraintCollection); |
| 115 | + } |
| 116 | + |
| 117 | + return $formBuilder->getForm(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @param SymfonyFormBuilderInterface $formBuilder |
| 122 | + * @param CmsFormField $field |
| 123 | + * @param FormConstraintCollection $constraintCollection |
| 124 | + */ |
| 125 | + protected function addField( |
| 126 | + SymfonyFormBuilderInterface $formBuilder, |
| 127 | + CmsFormField $field, |
| 128 | + ?FormConstraintCollection $constraintCollection = null |
| 129 | + ): void { |
| 130 | + if ($constraintCollection === null) { |
| 131 | + $constraintCollection = new FormConstraintCollection(new CmsForm()); |
| 132 | + } |
| 133 | + |
| 134 | + $formType = $this->fieldTypeRegistry->getByKey($field->getType()); |
| 135 | + if ($formType === null) { |
| 136 | + return; |
| 137 | + } |
| 138 | + $fieldOptions = array_merge($formType->getFormOptions(), $field->getOptions()); |
| 139 | + $constraints = $this->buildConstraintsForField($field, $constraintCollection, $fieldOptions); |
| 140 | + if (count($constraints) > 0) { |
| 141 | + $fieldOptions['constraints'] = $constraints; |
| 142 | + } |
| 143 | + |
| 144 | + $formBuilder->add($field->getName(), $formType->getFormType(), $fieldOptions); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @param CmsFormField $field |
| 149 | + * @param FormConstraintCollection $constraintCollection |
| 150 | + * @param array $fieldOptions |
| 151 | + * @return array |
| 152 | + */ |
| 153 | + protected function buildConstraintsForField( |
| 154 | + CmsFormField $field, |
| 155 | + FormConstraintCollection $constraintCollection, |
| 156 | + array $fieldOptions |
| 157 | + ): array { |
| 158 | + $constraints = []; |
| 159 | + |
| 160 | + if (array_key_exists('constraints', $fieldOptions)) { |
| 161 | + $constraints = $fieldOptions['constraints']; |
| 162 | + } |
| 163 | + |
| 164 | + if ($constraintCollection->hasFieldAnyConstraints($field->getName())) { |
| 165 | + return array_merge($constraints, $constraintCollection->getConstraintsForField($field->getName())); |
| 166 | + } |
| 167 | + |
| 168 | + return $constraints; |
| 169 | + } |
| 170 | +} |
0 commit comments