|
1 | 1 | <?php |
2 | 2 | namespace Payum\Bundle\PayumBundle\Sonata; |
3 | 3 |
|
| 4 | +use Payum\Bundle\PayumBundle\Form\Type\GatewayFactoriesChoiceType; |
| 5 | +use Payum\Core\Registry\GatewayFactoryRegistryInterface; |
4 | 6 | use Payum\Core\Security\CryptedInterface; |
5 | 7 | use Payum\Core\Security\CypherInterface; |
6 | 8 | use Sonata\AdminBundle\Admin\AbstractAdmin; |
7 | 9 | use Sonata\AdminBundle\Datagrid\ListMapper; |
8 | 10 | use Sonata\AdminBundle\Form\FormMapper; |
| 11 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 12 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 13 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
9 | 14 | use Symfony\Component\Form\FormBuilderInterface; |
| 15 | +use Symfony\Component\Form\FormEvent; |
| 16 | +use Symfony\Component\Form\FormEvents; |
10 | 17 | use Symfony\Component\Form\FormFactoryInterface; |
11 | 18 | use Payum\Bundle\PayumBundle\Form\Type\GatewayConfigType; |
| 19 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
12 | 20 |
|
13 | 21 | class GatewayConfigAdmin extends AbstractAdmin |
14 | 22 | { |
15 | | - protected FormFactoryInterface $formFactory; |
16 | 23 | protected ?CypherInterface $cypher; |
17 | 24 |
|
18 | | - public function setFormFactory(FormFactoryInterface $formFactory): void |
19 | | - { |
20 | | - $this->formFactory = $formFactory; |
21 | | - } |
22 | | - |
| 25 | + protected GatewayFactoryRegistryInterface $registry; |
| 26 | + |
23 | 27 | public function setCypher(CypherInterface $cypher): void |
24 | 28 | { |
25 | 29 | $this->cypher = $cypher; |
26 | 30 | } |
| 31 | + |
| 32 | + public function setGatewayFactoryRegistry(GatewayFactoryRegistryInterface $registry) |
| 33 | + { |
| 34 | + $this->registry = $registry; |
| 35 | + } |
27 | 36 |
|
28 | 37 | /** |
29 | 38 | * {@inheritdoc} |
30 | 39 | */ |
31 | 40 | protected function configureFormFields(FormMapper $form): void |
32 | 41 | { |
33 | | - $form->reorder(array()); //hack! |
| 42 | + |
| 43 | + $form |
| 44 | + ->with('General', ['class' => 'col-md-4'])->end() |
| 45 | + ->with('Configuration', ['class' => 'col-md-8'])->end() |
| 46 | + ; |
| 47 | + |
| 48 | + $form |
| 49 | + ->with('General') |
| 50 | + ->add('gatewayName') |
| 51 | + ->add('factoryName', GatewayFactoriesChoiceType::class, [ |
| 52 | + 'disabled' => (bool) $this->getSubject() && null !== $this->getSubject()->getId(), |
| 53 | + ]) |
| 54 | + ->end() |
| 55 | + ; |
| 56 | + |
| 57 | + if ($this->getSubject() && $this->getSubject()->getId()) { |
| 58 | + $this->buildCredentials($form, $this->getSubject()); |
| 59 | + } |
34 | 60 | } |
35 | 61 |
|
36 | 62 | /** |
@@ -78,28 +104,58 @@ public function prePersist($object): void |
78 | 104 | /** |
79 | 105 | * {@inheritdoc} |
80 | 106 | */ |
81 | | - public function getObject($id) |
| 107 | + protected function alterObject(object $object): void |
82 | 108 | { |
83 | | - $object = parent::getObject($id); |
84 | | - |
85 | 109 | if ($this->cypher && $object instanceof CryptedInterface) { |
86 | 110 | $object->decrypt($this->cypher); |
87 | 111 | } |
88 | | - |
89 | | - return $object; |
90 | 112 | } |
91 | 113 |
|
92 | | - /** |
93 | | - * {@inheritDoc} |
94 | | - */ |
95 | | - public function getFormBuilder(): FormBuilderInterface |
| 114 | + public function buildCredentials(FormMapper $form, object $object): void |
96 | 115 | { |
97 | | - $formBuilder = $this->formFactory->createBuilder(GatewayConfigType::class, $this->getSubject(), array( |
98 | | - 'data_class' => get_class($this->getSubject()), |
99 | | - )); |
| 116 | + /** @var array $data */ |
| 117 | + $data = $object; |
| 118 | + |
| 119 | + |
| 120 | + $propertyPath = is_array($data) ? '[factoryName]' : 'factoryName'; |
| 121 | + $factoryName = PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath); |
| 122 | + if (empty($factoryName)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + $gatewayFactory = $this->registry->getGatewayFactory($factoryName); |
| 127 | + $config = $gatewayFactory->createConfig(); |
| 128 | + |
| 129 | + if (isset($config['payum.gateway_config_type'])) { |
| 130 | + $form |
| 131 | + ->with('Configuration') |
| 132 | + ->add('config', $config['payum.gateway_config_type'], [ |
| 133 | + 'label' => false, |
| 134 | + ]) |
| 135 | + ->end() |
| 136 | + ; |
100 | 137 |
|
101 | | - $this->defineFormBuilder($formBuilder); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + $form->add('config', FormType::class); |
| 142 | + $configForm = $form->get('config'); |
| 143 | + |
| 144 | + $propertyPath = is_array($data) ? '[config]' : 'config'; |
| 145 | + $firstTime = ! PropertyAccess::createPropertyAccessor()->getValue($data, $propertyPath); |
| 146 | + |
| 147 | + foreach ($config['payum.default_options'] as $name => $value) { |
| 148 | + $propertyPath = is_array($data) ? "[config][{$name}]" : "config[{$name}]"; |
| 149 | + if ($firstTime) { |
| 150 | + PropertyAccess::createPropertyAccessor()->setValue($data, $propertyPath, $value); |
| 151 | + } |
102 | 152 |
|
103 | | - return $formBuilder; |
| 153 | + $type = is_bool($value) ? CheckboxType::class : TextType::class; |
| 154 | + |
| 155 | + $options = []; |
| 156 | + $options['required'] = in_array($name, $config['payum.required_options']); |
| 157 | + |
| 158 | + $configForm->add($name, $type, $options); |
| 159 | + } |
104 | 160 | } |
105 | 161 | } |
0 commit comments