diff --git a/extension/Classes/Core/Form/FormAsFile.php b/extension/Classes/Core/Form/FormAsFile.php index b75d3bc57ab0e50bd795b652de431994f1dc74fd..a2e334f248d701f05c861475182efcb5a6ead2ec 100644 --- a/extension/Classes/Core/Form/FormAsFile.php +++ b/extension/Classes/Core/Form/FormAsFile.php @@ -61,6 +61,20 @@ class FormAsFile } $formFromFile = json_decode($fileContents, true); + // make sure container names are unique and non-empty + $containerNames = []; + foreach ($formFromFile[F_FILE_FORM_ELEMENT] as $formElementFromFile) { + if ($formElementFromFile[FE_CLASS] === FE_CLASS_CONTAINER) { + if (in_array($formElementFromFile[FE_NAME], $containerNames)) { + Thrower::userFormException('Failed to import form file.', "Multiple formElements of class container with the same name '" . $formElementFromFile[FE_NAME] . "' in form file '$pathFileName'"); + } + if ($formElementFromFile[FE_NAME] == '') { + Thrower::userFormException('Failed to import form file.', "Found formElement of class container with empty name in form file '$pathFileName'"); + } + $containerNames[] = $formElementFromFile[FE_NAME]; + } + } + // Delete old form with that name from DB if it exists. if (array_key_exists(F_ID, $formFromDb)) { $formId = $formFromDb[F_ID]; @@ -391,7 +405,7 @@ class FormAsFile */ private static function insertFormElement(array $values, int $formId, Database $database): int { - // filter allowed formElement columns + // filter allowed formElement columns (remove id, formId, feIdContainer) $formElementSchema = $database->getTableDefinition(TABLE_NAME_FORM_ELEMENT); $formElementColumns = array_column($formElementSchema, 'Field'); $insertValues = array_filter($values, function ($columnName) use ($formElementColumns) { @@ -638,15 +652,19 @@ class FormAsFile list($sql, $parameterArray) = SqlQuery::selectFormElementById($formId); $formElements = $database->sql($sql, ROW_REGULAR, $parameterArray); // array(array(column name => value)) - // Translate container references (id to name) and remove all id columns + // Create id => name dictionary for column names $adjustedContainerNames = 0; $containerNames = array_reduce($formElements, function ($result, $formElement) use ($formName, $formId, &$adjustedContainerNames) { if ($formElement[FE_CLASS] === FE_CLASS_CONTAINER) { $containerName = $formElement[FE_NAME]; + + // container name not unique => adjust make unique if (in_array($containerName, $result)) { $containerName = $containerName . '_auto_adjust_not_unique_' . count($result); $adjustedContainerNames++; } + + // container name empty => adjust make non-empty unique if ($containerName === '') { $containerName = 'auto_adjust_empty_' . count($result); $adjustedContainerNames++; @@ -655,18 +673,26 @@ class FormAsFile } return $result; }, []); // array(id => name) + + // ajdust formElements for export $formElements = array_map(function ($formElement) use ($containerNames) { + + // in case container name was auto adjusted above we set new name if (array_key_exists($formElement[FE_ID], $containerNames)) { - // in case container name was auto adjusted above we set new name $formElement[FE_NAME] = $containerNames[$formElement[FE_ID]]; } + + // Replace container id references with name references $containerId = $formElement[FE_ID_CONTAINER]; if ($containerId !== 0) { $formElement[FE_FILE_CONTAINER_NAME] = $containerNames[$containerId]; } + + // remove columns id, formId, feIdContainer unset($formElement[FE_ID_CONTAINER]); unset($formElement[FE_ID]); unset($formElement[FE_FORM_ID]); + return $formElement; }, $formElements);