1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\DynamicEntityGui\Communication\Controller; |
||
9 | |||
10 | use Generated\Shared\Transfer\DynamicEntityConfigurationCollectionResponseTransfer; |
||
11 | use Generated\Shared\Transfer\ErrorTransfer; |
||
12 | use Spryker\Zed\DynamicEntityGui\DynamicEntityGuiConfig; |
||
13 | use Spryker\Zed\Kernel\Communication\Controller\AbstractController; |
||
14 | use Symfony\Component\Form\FormError; |
||
15 | use Symfony\Component\Form\FormInterface; |
||
16 | use Symfony\Component\HttpFoundation\Request; |
||
17 | |||
18 | /** |
||
19 | * @method \Spryker\Zed\DynamicEntityGui\Communication\DynamicEntityGuiCommunicationFactory getFactory() |
||
20 | */ |
||
21 | class ConfigurationEditController extends AbstractController |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected const REQUEST_TABLE_NAME = 'table-name'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected const MESSAGE_CONFIGURATION_UPDATED = 'Configuration is updated successfully'; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected const MESSAGE_ERROR_TABLE_NOT_EXIST = 'Table with name `%s` does not exist'; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected const MESSAGE_NAME_PLACEHOLDER = '%s'; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected const KEY_FORM = 'form'; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected const TYPE = 'type'; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected const KEY_FIELD_DEFINITIONS = 'field_definitions'; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected const KEY_FIELD_NAME = 'field_name'; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | protected const ERROR_MESSAGE_PLACEHOLDER = '`%s` - %s'; |
||
67 | |||
68 | /** |
||
69 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
70 | * |
||
71 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|array<mixed> |
||
72 | */ |
||
73 | public function indexAction(Request $request) |
||
74 | { |
||
75 | $tableName = $request->get(static::REQUEST_TABLE_NAME); |
||
76 | |||
77 | $dataProvider = $this->getFactory()->createUpdateDynamicDataConfigurationFormDataProvider(); |
||
78 | $dynamicDataConfigurationTransfer = $dataProvider->getData($tableName); |
||
79 | |||
80 | if ($dynamicDataConfigurationTransfer === null) { |
||
81 | $this->addErrorMessage(static::MESSAGE_ERROR_TABLE_NOT_EXIST, [ |
||
82 | static::MESSAGE_NAME_PLACEHOLDER => $tableName, |
||
83 | ]); |
||
84 | |||
85 | return $this->redirectResponse(DynamicEntityGuiConfig::URL_DYNAMIC_DATA_CONFIGURATION_LIST); |
||
86 | } |
||
87 | |||
88 | $updateDynamicDataConfigurationForm = $this->getFactory() |
||
89 | ->getUpdateDynamicDataConfigurationForm( |
||
90 | $dynamicDataConfigurationTransfer, |
||
91 | $dataProvider->getOptions($tableName), |
||
92 | ) |
||
93 | ->handleRequest($request); |
||
94 | |||
95 | if ($updateDynamicDataConfigurationForm->isSubmitted() && $updateDynamicDataConfigurationForm->isValid()) { |
||
96 | return $this->updateDynamicEntityConfiguration($updateDynamicDataConfigurationForm); |
||
97 | } |
||
98 | |||
99 | return $this->viewResponse([ |
||
100 | static::KEY_FORM => $updateDynamicDataConfigurationForm->createView(), |
||
101 | ]); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param \Symfony\Component\Form\FormInterface $dynamicDataConfigurationForm |
||
106 | * |
||
107 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|array<mixed> |
||
108 | */ |
||
109 | protected function updateDynamicEntityConfiguration(FormInterface $dynamicDataConfigurationForm) |
||
110 | { |
||
111 | $dynamicDataConfiguration = $dynamicDataConfigurationForm->getData(); |
||
112 | |||
113 | $dynamicEntityConfigurationCollectionRequestTransfer = $this->getFactory() |
||
114 | ->createDynamicDataConfigurationMapper() |
||
115 | ->mapDynamicDataConfigurationDataToCollectionRequestTransfer($dynamicDataConfiguration); |
||
116 | |||
117 | $dynamicEntityConfigurationCollectionResponseTransfer = $this->getFactory() |
||
118 | ->getDynamicEntityFacade() |
||
119 | ->updateDynamicEntityConfigurationCollection($dynamicEntityConfigurationCollectionRequestTransfer); |
||
120 | |||
121 | if ($dynamicEntityConfigurationCollectionResponseTransfer->getErrors()->count() === 0) { |
||
122 | $this->addSuccessMessage(static::MESSAGE_CONFIGURATION_UPDATED); |
||
123 | |||
124 | return $this->redirectResponse(DynamicEntityGuiConfig::URL_DYNAMIC_DATA_CONFIGURATION_LIST); |
||
125 | } |
||
126 | |||
127 | $dynamicDataConfigurationForm = $this->mapErrorsToForm($dynamicDataConfigurationForm, $dynamicEntityConfigurationCollectionResponseTransfer); |
||
128 | |||
129 | return $this->viewResponse([ |
||
130 | static::KEY_FORM => $dynamicDataConfigurationForm->createView(), |
||
131 | ]); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param \Symfony\Component\Form\FormInterface $dynamicDataConfigurationForm |
||
136 | * @param \Generated\Shared\Transfer\DynamicEntityConfigurationCollectionResponseTransfer $dynamicEntityConfigurationCollectionResponseTransfer |
||
137 | * |
||
138 | * @return \Symfony\Component\Form\FormInterface |
||
139 | */ |
||
140 | protected function mapErrorsToForm( |
||
141 | FormInterface $dynamicDataConfigurationForm, |
||
142 | DynamicEntityConfigurationCollectionResponseTransfer $dynamicEntityConfigurationCollectionResponseTransfer |
||
143 | ): FormInterface { |
||
144 | foreach ($dynamicEntityConfigurationCollectionResponseTransfer->getErrors() as $errorTransfer) { |
||
145 | $errorAddedToDefinitionForm = $this->addFormErrorToFieldDefinitionForm($dynamicDataConfigurationForm, $errorTransfer); |
||
146 | |||
147 | if ($errorAddedToDefinitionForm === true) { |
||
148 | continue; |
||
149 | } |
||
150 | |||
151 | $dynamicDataConfigurationForm->addError($this->mapFormErrorTransferToFormError($errorTransfer)); |
||
152 | } |
||
153 | |||
154 | return $dynamicDataConfigurationForm; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param \Symfony\Component\Form\FormInterface $dynamicDataConfigurationForm |
||
159 | * @param \Generated\Shared\Transfer\ErrorTransfer $errorTransfer |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | protected function addFormErrorToFieldDefinitionForm(FormInterface $dynamicDataConfigurationForm, ErrorTransfer $errorTransfer): bool |
||
164 | { |
||
165 | $fieldDefinitionsForms = $dynamicDataConfigurationForm->get(static::KEY_FIELD_DEFINITIONS)->all(); |
||
166 | /** @var \Symfony\Component\Form\FormInterface $fieldDefinitionsForm */ |
||
167 | foreach ($fieldDefinitionsForms as $fieldDefinitionsForm) { |
||
168 | $data = $fieldDefinitionsForm->getData(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
169 | |||
170 | if ( |
||
171 | isset($fieldDefinitionsForm->getData()[static::KEY_FIELD_NAME]) && |
||
172 | $fieldDefinitionsForm->getData()[static::KEY_FIELD_NAME] === $errorTransfer->getEntityIdentifier() |
||
173 | ) { |
||
174 | $fieldDefinitionsForm->addError($this->mapFormErrorTransferToFormError($errorTransfer)); |
||
175 | |||
176 | return true; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @param \Generated\Shared\Transfer\ErrorTransfer $errorTransfer |
||
185 | * |
||
186 | * @return \Symfony\Component\Form\FormError |
||
187 | */ |
||
188 | protected function mapFormErrorTransferToFormError(ErrorTransfer $errorTransfer): FormError |
||
189 | { |
||
190 | $errorMessage = sprintf( |
||
191 | static::ERROR_MESSAGE_PLACEHOLDER, |
||
192 | $errorTransfer->getEntityIdentifier(), |
||
193 | $errorTransfer->getMessage(), |
||
194 | ); |
||
195 | |||
196 | return new FormError($errorMessage); |
||
197 | } |
||
198 | } |
||
199 |