From 5e9f13be5ea289ac21b13b6a3b70d02896ccf715 Mon Sep 17 00:00:00 2001 From: Ingolf Steinardt Date: Fri, 19 Jul 2024 20:34:58 +0200 Subject: [PATCH 1/2] Fix encoding and decoding for widgets --- .../ContaoWidgetManager.php | 14 +++++++---- .../View/Contao2BackendView/EditMask.php | 24 +++++++++++++++++-- src/Controller/ControllerInterface.php | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php b/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php index ff33085c..db512551 100644 --- a/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php +++ b/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php @@ -271,7 +271,7 @@ protected function getUniqueId($propertyName) * Retrieve the instance of a widget for the given property. * * @param string $property Name of the property for which the widget shall be retrieved. - * @param PropertyValueBagInterface $inputValues The input values to use (optional). + * @param PropertyValueBagInterface $inputValues The input values to use (optional) (RAW widget value format). * * @return Widget|null * @@ -398,7 +398,7 @@ protected function generateHelpText($property, Widget $widget) * @param string $property The name of the property for which the widget shall be rendered. * @param bool $ignoreErrors Flag if the error property of the widget shall get * cleared prior rendering. - * @param PropertyValueBagInterface $inputValues The input values to use (optional). + * @param PropertyValueBagInterface $inputValues The input values to use (optional) (RAW widget value format). * * @return string * @@ -468,7 +468,9 @@ public function buildHiddenFields($value, string $propertyName): array } /** - * {@inheritDoc} + * Process RAW input values. + * + * @var PropertyValueBag $propertyValues The RAW property values from the input provider. * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) @@ -487,6 +489,7 @@ public function processInput(PropertyValueBag $propertyValues): void } // Now get and validate the widgets. + $encodedValues = new PropertyValueBag(); foreach (\array_keys($propertyValues->getArrayCopy()) as $property) { // NOTE: the passed input values are RAW DATA from the input provider - aka widget known values and not // native data as in the model. @@ -502,7 +505,7 @@ public function processInput(PropertyValueBag $propertyValues): void } } elseif ($widget->submitInput()) { try { - $propertyValues->setPropertyValue( + $encodedValues->setPropertyValue( $property, $this->encodeValue($property, $widget->value, $propertyValues) ); @@ -514,6 +517,9 @@ public function processInput(PropertyValueBag $propertyValues): void } } } + foreach ($encodedValues->getArrayCopy() as $propertyName => $propertyValue) { + $propertyValues->setPropertyValue($propertyName, $propertyValue); + } $_POST = $post; Input::resetCache(); diff --git a/src/Contao/View/Contao2BackendView/EditMask.php b/src/Contao/View/Contao2BackendView/EditMask.php index da3ae1b5..a0ca718b 100644 --- a/src/Contao/View/Contao2BackendView/EditMask.php +++ b/src/Contao/View/Contao2BackendView/EditMask.php @@ -522,7 +522,7 @@ protected function getEditButtons() * * @param ContaoWidgetManager $widgetManager The widget manager in use. * @param PaletteInterface $palette The palette to use. - * @param PropertyValueBag|null $propertyValues The property values. + * @param PropertyValueBag|null $propertyValues The property values (model values). * * @return array * @@ -547,6 +547,26 @@ protected function buildFieldSet($widgetManager, $palette, $propertyValues) $editInformation = System::getContainer()->get('cca.dc-general.edit-information'); assert($editInformation instanceof EditInformationInterface); + $rawValues = new PropertyValueBag(); + if (null !== $propertyValues) { + foreach ($palette->getLegends() as $legend) { + $properties = $legend->getProperties($this->model, $propertyValues); + if (!$properties) { + continue; + } + foreach ($properties as $property) { + $propName = $property->getName(); + $this->ensurePropertyExists($propName, $propertyDefinitions); + if ($propertyValues->hasPropertyValue($propName)) { + $rawValues->setPropertyValue( + $propName, + $widgetManager->decodeValue($propName, $propertyValues->getPropertyValue($propName)) + ); + } + } + } + } + $fieldSets = []; $first = true; foreach ($palette->getLegends() as $legend) { @@ -593,7 +613,7 @@ protected function buildFieldSet($widgetManager, $palette, $propertyValues) ); } - $fields[] = $widgetManager->renderWidget($property->getName(), $isAutoSubmit, $propertyValues); + $fields[] = $widgetManager->renderWidget($property->getName(), $isAutoSubmit, $rawValues); } $fieldSet['label'] = $legendName; diff --git a/src/Controller/ControllerInterface.php b/src/Controller/ControllerInterface.php index 8b309bf9..a37d036a 100644 --- a/src/Controller/ControllerInterface.php +++ b/src/Controller/ControllerInterface.php @@ -110,7 +110,7 @@ public function assembleAllChildrenFrom($model, $providerName = ''); * Update the current model from a post request. Additionally, trigger meta palettes, if installed. * * @param ModelInterface $model The model to update. - * @param PropertyValueBagInterface $propertyValues The value bag to retrieve the values from. + * @param PropertyValueBagInterface $propertyValues The value bag to retrieve the values from (model values). * * @return ControllerInterface */ From e824f65ec39acbb7d243c08971e3d978a3d85343 Mon Sep 17 00:00:00 2001 From: Ingolf Steinardt Date: Fri, 19 Jul 2024 20:40:43 +0200 Subject: [PATCH 2/2] Fix PHPCQ --- src/Contao/View/Contao2BackendView/ContaoWidgetManager.php | 2 +- src/Contao/View/Contao2BackendView/EditMask.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php b/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php index db512551..ce5baaf2 100644 --- a/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php +++ b/src/Contao/View/Contao2BackendView/ContaoWidgetManager.php @@ -470,7 +470,7 @@ public function buildHiddenFields($value, string $propertyName): array /** * Process RAW input values. * - * @var PropertyValueBag $propertyValues The RAW property values from the input provider. + * @param PropertyValueBag $propertyValues The RAW property values from the input provider. * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) diff --git a/src/Contao/View/Contao2BackendView/EditMask.php b/src/Contao/View/Contao2BackendView/EditMask.php index a0ca718b..04800251 100644 --- a/src/Contao/View/Contao2BackendView/EditMask.php +++ b/src/Contao/View/Contao2BackendView/EditMask.php @@ -72,6 +72,8 @@ * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.ExcessiveClassLength) + * @SuppressWarnings(PHPMD.NPathComplexity) + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ class EditMask {