2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Resources\FormSubmissionResource;
|
|
|
|
use App\Models\Forms\Form;
|
|
|
|
use App\Exports\FormSubmissionExport;
|
|
|
|
use App\Service\Forms\FormSubmissionFormatter;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$form = Form::findOrFail((int) $id);
|
|
|
|
$this->authorize('view', $form);
|
|
|
|
|
|
|
|
return FormSubmissionResource::collection($form->submissions()->paginate(100));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function export(string $id)
|
|
|
|
{
|
|
|
|
$form = Form::findOrFail((int) $id);
|
|
|
|
$this->authorize('view', $form);
|
|
|
|
|
|
|
|
$allRows = [];
|
|
|
|
foreach ($form->submissions->toArray() as $row) {
|
|
|
|
$formatter = (new FormSubmissionFormatter($form, $row['data']))
|
|
|
|
->outputStringsOnly()
|
2022-10-17 07:45:28 +00:00
|
|
|
->setEmptyForNoValue()
|
|
|
|
->showRemovedFields();
|
2022-09-20 19:59:52 +00:00
|
|
|
$tmp = $formatter->getCleanKeyValue();
|
|
|
|
$tmp['Create Date'] = date("Y-m-d H:i", strtotime($row['created_at']));
|
|
|
|
$allRows[] = $tmp;
|
|
|
|
}
|
|
|
|
$csvExport = (new FormSubmissionExport($allRows));
|
|
|
|
return Excel::download(
|
|
|
|
$csvExport,
|
|
|
|
$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);
|
|
|
|
|
2023-08-16 08:59:07 +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-01-13 18:57:39 +00:00
|
|
|
Storage::temporaryUrl($fileName, now()->addMinute())
|
2022-09-20 19:59:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|