diff --git a/EMS/core-bundle/src/Controller/Form/SubmissionController.php b/EMS/core-bundle/src/Controller/Form/SubmissionController.php index 455fa5658..eb4ea01ce 100644 --- a/EMS/core-bundle/src/Controller/Form/SubmissionController.php +++ b/EMS/core-bundle/src/Controller/Form/SubmissionController.php @@ -53,7 +53,7 @@ public function indexAction(Request $request, UserInterface $user): Response case TableAbstract::DOWNLOAD_ACTION: return $this->downloadMultiple($table->getSelected()); case TableAbstract::EXPORT_ACTION: - $config = $config = $this->formSubmissionService->generateExportConfig($table->getSelected()); + $config = $this->formSubmissionService->generateExportConfig($table->getSelected()); return $this->spreadsheetGeneratorService->generateSpreadsheet($config); default: diff --git a/EMS/core-bundle/src/Service/Form/Submission/FormSubmissionService.php b/EMS/core-bundle/src/Service/Form/Submission/FormSubmissionService.php index e49efedeb..44591f0a3 100644 --- a/EMS/core-bundle/src/Service/Form/Submission/FormSubmissionService.php +++ b/EMS/core-bundle/src/Service/Form/Submission/FormSubmissionService.php @@ -129,7 +129,6 @@ public function generateExportConfig(array $formSubmissionIds): array $formSubmission = $this->getById($formSubmissionId); /** @var array $data */ $data = $formSubmission->getData(); - $data = \array_filter($data, fn ($value) => \is_string($value)); $data['id'] = $formSubmission->getId(); $data['form'] = $formSubmission->getName(); $data['instance'] = $formSubmission->getInstance(); @@ -139,13 +138,9 @@ public function generateExportConfig(array $formSubmissionIds): array $data['deadline'] = null === $expireDate ? '' : $expireDate->format('Y-m-d'); $sheetName = $formSubmission->getName(); - if (!\key_exists($sheetName, $sheets)) { - $titles = []; - foreach ($data as $key => $value) { - $titles[] = $key; - } - $sheets[$sheetName] = [$titles]; - } + $titles = $sheets[$sheetName][0] ?? []; + $titles = \array_unique(\array_merge($titles, \array_keys($data))); + $sheets[$sheetName][0] = $titles; $sheets[$sheetName] = \array_merge($sheets[$sheetName], [$data]); } @@ -153,7 +148,7 @@ public function generateExportConfig(array $formSubmissionIds): array foreach ($sheets as $key => $value) { $config['sheets'][] = [ 'name' => $key, - 'rows' => $value, + 'rows' => $this->normalizeRows($value), ]; } @@ -295,4 +290,21 @@ public function deleteByItemName(string $name): string { throw new \RuntimeException('deleteByItemName method not yet implemented'); } + + /** + * @param mixed[][] $rows + * @return mixed[][] + */ + private function normalizeRows(array $rows): array + { + if (\count($rows) < 2) { + return $rows; + } + $titles = $rows[0]; + for ($i = 1; $i < \count($rows); ++$i) { + $rows[$i] = \array_map(fn (string $key) => $rows[$i][$key] ?? null, $titles); + } + + return $rows; + } }