2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
use App\Exports\FormSubmissionExport;
|
2024-02-23 10:54:12 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2024-02-03 11:50:57 +00:00
|
|
|
use App\Http\Requests\AnswerFormRequest;
|
2024-02-23 10:54:12 +00:00
|
|
|
use App\Http\Resources\FormSubmissionResource;
|
2024-02-03 11:50:57 +00:00
|
|
|
use App\Jobs\Form\StoreFormSubmissionJob;
|
2024-02-23 10:54:12 +00:00
|
|
|
use App\Models\Forms\Form;
|
2024-02-03 11:50:57 +00:00
|
|
|
use App\Models\Forms\FormSubmission;
|
2022-09-20 19:59:52 +00:00
|
|
|
use App\Service\Forms\FormSubmissionFormatter;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
2024-02-23 09:50:35 +00:00
|
|
|
use Vinkla\Hashids\Facades\Hashids;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
class FormSubmissionController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
2024-01-13 18:57:39 +00:00
|
|
|
$this->middleware('auth', ['except' => ['submissionFile']]);
|
|
|
|
$this->middleware('signed', ['only' => ['submissionFile']]);
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function submissions(string $id)
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
$form = Form::findOrFail((int) $id);
|
2022-09-20 19:59:52 +00:00
|
|
|
$this->authorize('view', $form);
|
|
|
|
|
|
|
|
return FormSubmissionResource::collection($form->submissions()->paginate(100));
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:50:35 +00:00
|
|
|
public function update(AnswerFormRequest $request, $id, $submissionId)
|
2024-02-03 11:50:57 +00:00
|
|
|
{
|
2024-02-23 09:50:35 +00:00
|
|
|
$form = $request->form;
|
2024-02-03 11:50:57 +00:00
|
|
|
$this->authorize('update', $form);
|
|
|
|
$job = new StoreFormSubmissionJob($request->form, $request->validated());
|
|
|
|
$job->setSubmissionId($submissionId)->handle();
|
|
|
|
|
2024-02-23 09:50:35 +00:00
|
|
|
$data = new FormSubmissionResource(FormSubmission::findOrFail($submissionId));
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2024-02-03 11:50:57 +00:00
|
|
|
return $this->success([
|
|
|
|
'message' => 'Record successfully updated.',
|
2024-02-23 10:54:12 +00:00
|
|
|
'data' => $data,
|
2024-02-03 11:50:57 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
public function export(string $id)
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
$form = Form::findOrFail((int) $id);
|
2024-01-16 16:41:01 +00:00
|
|
|
$this->authorize('view', $form);
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
$allRows = [];
|
|
|
|
foreach ($form->submissions->toArray() as $row) {
|
|
|
|
$formatter = (new FormSubmissionFormatter($form, $row['data']))
|
|
|
|
->outputStringsOnly()
|
2022-10-17 07:45:28 +00:00
|
|
|
->setEmptyForNoValue()
|
2024-01-31 11:21:28 +00:00
|
|
|
->showRemovedFields()
|
2024-02-23 09:50:35 +00:00
|
|
|
->showHiddenFields()
|
2024-01-31 11:21:28 +00:00
|
|
|
->useSignedUrlForFiles();
|
2024-02-23 09:50:35 +00:00
|
|
|
$allRows[] = [
|
|
|
|
'id' => Hashids::encode($row['id']),
|
2024-02-23 10:54:12 +00:00
|
|
|
'created_at' => date('Y-m-d H:i', strtotime($row['created_at'])),
|
|
|
|
...$formatter->getCleanKeyValue(),
|
2024-02-23 09:50:35 +00:00
|
|
|
];
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
$csvExport = (new FormSubmissionExport($allRows));
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return Excel::download(
|
|
|
|
$csvExport,
|
2024-02-23 10:54:12 +00:00
|
|
|
$form->slug.'-submission-data.csv',
|
2022-09-20 19:59:52 +00:00
|
|
|
\Maatwebsite\Excel\Excel::CSV
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function submissionFile($id, $fileName)
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
$fileName = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $id).'/'
|
|
|
|
.urldecode($fileName);
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2024-02-23 10:54:12 +00:00
|
|
|
if (! Storage::exists($fileName)) {
|
2022-09-20 19:59:52 +00:00
|
|
|
return $this->error([
|
|
|
|
'message' => 'File not found.',
|
|
|
|
], 404);
|
|
|
|
}
|
|
|
|
|
2024-01-13 18:57:39 +00:00
|
|
|
if (config('filesystems.default') !== 's3') {
|
|
|
|
return response()->file(Storage::path($fileName));
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return redirect(
|
2024-02-23 09:50:35 +00:00
|
|
|
Storage::temporaryUrl($fileName, now()->addMinute())
|
2022-09-20 19:59:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|