Beautiful Open-Source Form Builder
refactor: improve FormSubmissionExport and FormExportService for CSV handling
Chirag Chhatrala393f18d99ac1
diff --git a/api/app/Exports/FormSubmissionExport.php b/api/app/Exports/FormSubmissionExport.phpindex ced1be0a3..6fc195892 100644--- a/api/app/Exports/FormSubmissionExport.php+++ b/api/app/Exports/FormSubmissionExport.php@@ -3,27 +3,22 @@namespace App\Exports;use Maatwebsite\Excel\Concerns\FromArray;-use Maatwebsite\Excel\Concerns\WithHeadingRow;+use Maatwebsite\Excel\Concerns\WithHeadings;-class FormSubmissionExport implements FromArray, WithHeadingRow+class FormSubmissionExport implements FromArray, WithHeadings{protected array $submissionData;+ protected array $headings;public function __construct(array $submissionData){- $headingRow = [];- $contentRow = [];- foreach ($submissionData as $i => $row) {- if ($i == 0) {- $headingRow[] = $this->cleanColumnNames(array_keys($row));- }- $contentRow[] = array_values($row);- }-- $this->submissionData = [- $headingRow,- $contentRow,- ];+ $this->headings = empty($submissionData)+ ? []+ : $this->cleanColumnNames(array_keys($submissionData[0]));++ $this->submissionData = array_map(function ($row) {+ return array_values($row);+ }, $submissionData);}private function cleanColumnNames(array $columnNames): array@@ -37,4 +32,9 @@ class FormSubmissionExport implements FromArray, WithHeadingRow{return $this->submissionData;}++ public function headings(): array+ {+ return $this->headings;+ }}diff --git a/api/app/Service/Forms/FormExportService.php b/api/app/Service/Forms/FormExportService.phpindex a2d2ec2e5..a5b56335e 100644--- a/api/app/Service/Forms/FormExportService.php+++ b/api/app/Service/Forms/FormExportService.php@@ -100,12 +100,11 @@ class FormExportService// Write header row (clean column names)$headers = $this->cleanColumnNames(array_keys($rows[0]));- fputcsv($output, $headers, ',', '"', '');+ fputcsv($output, $headers);- // Write data rows (sanitize values to prevent newlines breaking Excel/parsers)+ // Write data rowsforeach ($rows as $row) {- $sanitizedRow = $this->sanitizeRowForCsv($row);- fputcsv($output, array_values($sanitizedRow), ',', '"', '');+ fputcsv($output, array_values($row));}rewind($output);@@ -115,22 +114,6 @@ class FormExportServicereturn $csvContent;}- /**- * Sanitize row values for CSV export.- * Replaces newlines with spaces to prevent multi-line fields that break- * Excel, Google Sheets, and other parsers that don't handle RFC 4180 multi-line fields.- */- private function sanitizeRowForCsv(array $row): array- {- return collect($row)->map(function ($value) {- if (! is_string($value)) {- return $value;- }-- return preg_replace('/\r\n|\r|\n/', ' ', $value);- })->toArray();- }-/*** Clean column names by removing parenthetical content*/diff --git a/api/tests/Feature/Forms/FormSubmissionExportTest.php b/api/tests/Feature/Forms/FormSubmissionExportTest.phpindex e2bd56458..1f50bc8aa 100644--- a/api/tests/Feature/Forms/FormSubmissionExportTest.php+++ b/api/tests/Feature/Forms/FormSubmissionExportTest.php@@ -1,6 +1,23 @@<?phpuse App\Models\User;+use App\Models\Forms\FormSubmission;++function parseCsvRows(string $content): array+{+ $handle = fopen('php://temp', 'r+');+ fwrite($handle, $content);+ rewind($handle);++ $rows = [];+ while (($row = fgetcsv($handle)) !== false) {+ $rows[] = $row;+ }++ fclose($handle);++ return $rows;+}it('can export form submissions with selected columns', function () {$user = $this->actingAsProUser();@@ -219,6 +236,60 @@ it('includes status column when partial submissions are enabled', function () {expect(str_contains($content, 'Completed'))->toBeTrue();});+it('preserves multiline values without corrupting CSV rows', function () {+ $user = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($user);+ $form = $this->createForm($user, $workspace);+ $textFieldId = collect($form->properties)->firstWhere('type', 'text')['id'];+ $emailFieldId = collect($form->properties)->firstWhere('type', 'email')['id'];++ $form->submissions()->create([+ 'form_id' => $form->id,+ 'data' => [+ $textFieldId => 'Single line content',+ $emailFieldId => '[email protected]',+ ],+ 'status' => FormSubmission::STATUS_COMPLETED,+ ]);++ $multilineValue = "First paragraph.\nSecond paragraph, with comma and \"quoted\" text.";+ $form->submissions()->create([+ 'form_id' => $form->id,+ 'data' => [+ $textFieldId => $multilineValue,+ $emailFieldId => '[email protected]',+ ],+ 'status' => FormSubmission::STATUS_COMPLETED,+ ]);++ $response = $this->postJson(route('open.forms.submissions.export', [+ 'form' => $form,+ ]), [+ 'columns' => [+ $textFieldId => true,+ $emailFieldId => true,+ ],+ ]);++ $response->assertSuccessful();++ ob_start();+ $response->sendContent();+ $content = ob_get_clean();+ $rows = parseCsvRows($content);++ expect(count($rows))->toBe(3); // Header + 2 data rows++ $headerColumnCount = count($rows[0]);+ foreach ($rows as $row) {+ expect(count($row))->toBe($headerColumnCount);+ }++ expect($content)->toContain('First paragraph.');+ expect($content)->toContain('Second paragraph, with comma and ""quoted"" text.');+ expect($content)->toMatch('/First paragraph\.(\r\n|\n)Second paragraph, with comma and ""quoted"" text\./');+});+it('does not include status column when partial submissions are disabled', function () {$user = $this->actingAsProUser();$workspace = $this->createUserWorkspace($user);