Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Form Submission Export Functionality #657

Merged
merged 6 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions api/app/Http/Controllers/Forms/FormSubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Str;
use Maatwebsite\Excel\Facades\Excel;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Http\Request;

class FormSubmissionController extends Controller
{
Expand Down Expand Up @@ -46,38 +47,46 @@ public function update(AnswerFormRequest $request, $id, $submissionId)
]);
}

public function export(string $id)
public function export(Request $request, string $id)
{
$form = Form::findOrFail((int) $id);
$this->authorize('view', $form);

$allRows = [];
$displayColumns = collect($request->columns)->filter(fn ($value, $key) => $value === true)->toArray();
foreach ($form->submissions->toArray() as $row) {
$formatter = (new FormSubmissionFormatter($form, $row['data']))
->outputStringsOnly()
->setEmptyForNoValue()
->showRemovedFields()
->showHiddenFields()
->useSignedUrlForFiles();
$allRows[] = [
'id' => Hashids::encode($row['id']),
'created_at' => date('Y-m-d H:i', strtotime($row['created_at'])),
...$formatter->getCleanKeyValue(),
];
$formattedData = $formatter->getCleanKeyValue();
$filteredData = ['id' => Hashids::encode($row['id'])];
foreach ($displayColumns as $column => $value) {
$key = collect($formattedData)->keys()->first(fn ($key) => str_contains($key, $column));
if ($key) {
$filteredData[$key] = $formattedData[$key];
}
}
if (isset($displayColumns['created_at'])) {
$filteredData['created_at'] = date('Y-m-d H:i', strtotime($row['created_at']));
}
$allRows[] = $filteredData;
}
$csvExport = (new FormSubmissionExport($allRows));

return Excel::download(
$csvExport,
$form->slug.'-submission-data.csv',
$form->slug . '-submission-data.csv',
\Maatwebsite\Excel\Excel::CSV
);
}

public function submissionFile($id, $fileName)
{
$fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id).'/'
.urldecode($fileName);
$fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id) . '/'
. urldecode($fileName);

if (! Storage::exists($fileName)) {
return $this->error([
Expand Down
2 changes: 1 addition & 1 deletion api/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@

Route::get('/{id}/submissions', [FormSubmissionController::class, 'submissions'])->name('submissions');
Route::put('/{id}/submissions/{submission_id}', [FormSubmissionController::class, 'update'])->name('submissions.update')->middleware([ResolveFormMiddleware::class]);
Route::get('/{id}/submissions/export', [FormSubmissionController::class, 'export'])->name('submissions.export');
Route::post('/{id}/submissions/export', [FormSubmissionController::class, 'export'])->name('submissions.export');
Route::get('/{id}/submissions/file/{filename}', [FormSubmissionController::class, 'submissionFile'])
->middleware('signed')
->withoutMiddleware(['auth:api'])
Expand Down
11 changes: 8 additions & 3 deletions client/components/open/forms/components/FormSubmissions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default {
if (!this.form) {
return ''
}
return this.runtimeConfig.public.apiBase + '/open/forms/' + this.form.id + '/submissions/export'
return this.runtimeConfig.public.apiBase + 'open/forms/' + this.form.id + '/submissions/export'
},
isLoading() {
return this.recordStore.loading
Expand Down Expand Up @@ -333,8 +333,13 @@ export default {
return
}
this.exportLoading = true
opnFetch(this.exportUrl, {responseType: "blob"})
.then(blob => {
opnFetch(this.exportUrl, {
responseType: "blob",
method: "POST",
body: {
columns: this.displayColumns
}
}).then(blob => {
const filename = `${this.form.slug}-${Date.now()}-submissions.csv`
const a = document.createElement("a")
document.body.appendChild(a)
Expand Down
Loading