|
5 | 5 | use App\Exports\FormSubmissionExport;
|
6 | 6 | use App\Http\Controllers\Controller;
|
7 | 7 | use App\Http\Requests\AnswerFormRequest;
|
| 8 | +use App\Http\Requests\FormSubmissionExportRequest; |
8 | 9 | use App\Http\Resources\FormSubmissionResource;
|
9 | 10 | use App\Jobs\Form\StoreFormSubmissionJob;
|
10 | 11 | use App\Models\Forms\Form;
|
@@ -46,38 +47,46 @@ public function update(AnswerFormRequest $request, $id, $submissionId)
|
46 | 47 | ]);
|
47 | 48 | }
|
48 | 49 |
|
49 |
| - public function export(string $id) |
| 50 | + public function export(FormSubmissionExportRequest $request, string $id) |
50 | 51 | {
|
51 |
| - $form = Form::findOrFail((int) $id); |
| 52 | + $form = $request->form; |
52 | 53 | $this->authorize('view', $form);
|
53 | 54 |
|
54 | 55 | $allRows = [];
|
| 56 | + $displayColumns = collect($request->columns)->filter(fn ($value, $key) => $value === true)->toArray(); |
55 | 57 | foreach ($form->submissions->toArray() as $row) {
|
56 | 58 | $formatter = (new FormSubmissionFormatter($form, $row['data']))
|
57 | 59 | ->outputStringsOnly()
|
58 | 60 | ->setEmptyForNoValue()
|
59 | 61 | ->showRemovedFields()
|
60 | 62 | ->showHiddenFields()
|
61 | 63 | ->useSignedUrlForFiles();
|
62 |
| - $allRows[] = [ |
63 |
| - 'id' => Hashids::encode($row['id']), |
64 |
| - 'created_at' => date('Y-m-d H:i', strtotime($row['created_at'])), |
65 |
| - ...$formatter->getCleanKeyValue(), |
66 |
| - ]; |
| 64 | + $formattedData = $formatter->getCleanKeyValue(); |
| 65 | + $filteredData = ['id' => Hashids::encode($row['id'])]; |
| 66 | + foreach ($displayColumns as $column => $value) { |
| 67 | + $key = collect($formattedData)->keys()->first(fn ($key) => str_contains($key, $column)); |
| 68 | + if ($key) { |
| 69 | + $filteredData[$key] = $formattedData[$key]; |
| 70 | + } |
| 71 | + } |
| 72 | + if (isset($displayColumns['created_at'])) { |
| 73 | + $filteredData['created_at'] = date('Y-m-d H:i', strtotime($row['created_at'])); |
| 74 | + } |
| 75 | + $allRows[] = $filteredData; |
67 | 76 | }
|
68 | 77 | $csvExport = (new FormSubmissionExport($allRows));
|
69 | 78 |
|
70 | 79 | return Excel::download(
|
71 | 80 | $csvExport,
|
72 |
| - $form->slug.'-submission-data.csv', |
| 81 | + $form->slug . '-submission-data.csv', |
73 | 82 | \Maatwebsite\Excel\Excel::CSV
|
74 | 83 | );
|
75 | 84 | }
|
76 | 85 |
|
77 | 86 | public function submissionFile($id, $fileName)
|
78 | 87 | {
|
79 |
| - $fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id).'/' |
80 |
| - .urldecode($fileName); |
| 88 | + $fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id) . '/' |
| 89 | + . urldecode($fileName); |
81 | 90 |
|
82 | 91 | if (! Storage::exists($fileName)) {
|
83 | 92 | return $this->error([
|
|
0 commit comments