From 5604c92d613c2f1f6f125862bdfaa4886cf6817e Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Thu, 8 Apr 2021 20:56:57 +0200 Subject: [PATCH] fix(question): make import resilient against missing parameters Signed-off-by: Thierry Bugier --- inc/abstractfield.class.php | 5 +++++ inc/question.class.php | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/inc/abstractfield.class.php b/inc/abstractfield.class.php index 12f1bd2d4..253a3e581 100644 --- a/inc/abstractfield.class.php +++ b/inc/abstractfield.class.php @@ -184,6 +184,11 @@ public function getEmptyParameters(): array { return []; } + /** + * Undocumented function + * + * @return PluginFormcreatorAbstractQuestionParameter[] + */ public final function getParameters(): array { $parameters = $this->getEmptyParameters(); foreach ($parameters as $fieldname => $parameter) { diff --git a/inc/question.class.php b/inc/question.class.php index 634a82d1b..a783f539e 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -961,7 +961,17 @@ public static function import(PluginFormcreatorLinker $linker, array $input = [] if (isset($input['_parameters'])) { $parameters = $field->getParameters(); foreach ($parameters as $fieldName => $parameter) { - $parameter::import($linker, $input['_parameters'][$input['fieldtype']][$fieldName], $itemId); + if (is_array($input['_parameters'][$input['fieldtype']][$fieldName])) { + /** @var PluginFormcreatorExportableInterface $parameter */ + $parameter::import($linker, $input['_parameters'][$input['fieldtype']][$fieldName], $itemId); + } else { + // Import data incomplete, parameter not defined + // Adding an empty parameter (assuming the question is actually added or updated in DB) + $parameterInput = $parameter->fields; + $parameterInput['plugin_formcreator_questions_id'] = $itemId; + unset($parameterInput['id']); + $parameter->add($parameterInput); + } } }