2022-09-20 19:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\AnswerFormRequest;
|
|
|
|
use App\Http\Resources\FormResource;
|
|
|
|
use App\Jobs\Form\StoreFormSubmissionJob;
|
|
|
|
use App\Models\Forms\Form;
|
2023-01-10 13:52:14 +00:00
|
|
|
use App\Models\Forms\FormSubmission;
|
2022-09-20 19:59:52 +00:00
|
|
|
use App\Service\Forms\FormCleaner;
|
|
|
|
use App\Service\WorkspaceHelper;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-02-23 10:54:12 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-01-10 13:52:14 +00:00
|
|
|
use Vinkla\Hashids\Facades\Hashids;
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
class PublicFormController extends Controller
|
|
|
|
{
|
2024-02-23 10:54:12 +00:00
|
|
|
public const FILE_UPLOAD_PATH = 'forms/?/submissions';
|
2022-09-20 19:59:52 +00:00
|
|
|
|
2024-02-23 10:54:12 +00:00
|
|
|
public const TMP_FILE_UPLOAD_PATH = 'tmp/';
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
public function show(Request $request, string $slug)
|
|
|
|
{
|
2023-02-19 12:11:50 +00:00
|
|
|
$form = Form::whereSlug($slug)->whereIn('visibility', ['public', 'closed'])->firstOrFail();
|
2022-09-20 19:59:52 +00:00
|
|
|
if ($form->workspace == null) {
|
|
|
|
// Workspace deleted
|
|
|
|
return $this->error([
|
2024-02-23 10:54:12 +00:00
|
|
|
'message' => 'Form not found.',
|
2022-09-20 19:59:52 +00:00
|
|
|
], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$formCleaner = new FormCleaner();
|
|
|
|
|
|
|
|
// Disable pro features if needed
|
2024-02-23 10:54:12 +00:00
|
|
|
$form->fill(
|
|
|
|
$formCleaner
|
2022-09-20 19:59:52 +00:00
|
|
|
->processForm($request, $form)
|
|
|
|
->performCleaning($form->workspace)
|
|
|
|
->getData()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Increase form view counter if not login
|
2024-02-23 10:54:12 +00:00
|
|
|
if (! Auth::check()) {
|
2022-09-20 19:59:52 +00:00
|
|
|
$form->views()->create();
|
|
|
|
}
|
|
|
|
|
2023-08-30 07:58:29 +00:00
|
|
|
return (new FormResource($form))
|
|
|
|
->setCleanings($formCleaner->getPerformedCleanings());
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function listUsers(Request $request)
|
|
|
|
{
|
|
|
|
// Check that form has user field
|
|
|
|
$form = $request->form;
|
2024-02-23 10:54:12 +00:00
|
|
|
if (! $form->has_user_field) {
|
2022-09-20 19:59:52 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use serializer
|
|
|
|
$workspace = $form->workspace;
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
return (new WorkspaceHelper($workspace))->getAllUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function showAsset($assetFileName)
|
|
|
|
{
|
|
|
|
$path = FormController::ASSETS_UPLOAD_PATH.'/'.$assetFileName;
|
2024-02-23 10:54:12 +00:00
|
|
|
if (! Storage::exists($path)) {
|
2022-09-20 19:59:52 +00:00
|
|
|
return $this->error([
|
|
|
|
'message' => 'File not found.',
|
2024-02-23 10:54:12 +00:00
|
|
|
'file_name' => $assetFileName,
|
2022-09-20 19:59:52 +00:00
|
|
|
]);
|
|
|
|
}
|
2024-02-23 10:54:12 +00:00
|
|
|
|
2023-08-16 08:59:07 +00:00
|
|
|
return redirect()->to(Storage::temporaryUrl($path, now()->addMinutes(5)));
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function answer(AnswerFormRequest $request)
|
|
|
|
{
|
|
|
|
$form = $request->form;
|
2023-01-10 13:52:14 +00:00
|
|
|
$submissionId = false;
|
|
|
|
|
|
|
|
if ($form->editable_submissions) {
|
|
|
|
$job = new StoreFormSubmissionJob($form, $request->validated());
|
|
|
|
$job->handle();
|
|
|
|
$submissionId = Hashids::encode($job->getSubmissionId());
|
2024-02-23 10:54:12 +00:00
|
|
|
} else {
|
2023-01-10 13:52:14 +00:00
|
|
|
StoreFormSubmissionJob::dispatch($form, $request->validated());
|
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
return $this->success(array_merge([
|
|
|
|
'message' => 'Form submission saved.',
|
2024-02-23 10:54:12 +00:00
|
|
|
'submission_id' => $submissionId,
|
2022-09-20 19:59:52 +00:00
|
|
|
], $request->form->is_pro && $request->form->redirect_url ? [
|
|
|
|
'redirect' => true,
|
2024-02-23 10:54:12 +00:00
|
|
|
'redirect_url' => $request->form->redirect_url,
|
2022-09-20 19:59:52 +00:00
|
|
|
] : [
|
2024-02-23 10:54:12 +00:00
|
|
|
'redirect' => false,
|
2022-09-20 19:59:52 +00:00
|
|
|
]));
|
|
|
|
}
|
2023-01-10 13:52:14 +00:00
|
|
|
|
|
|
|
public function fetchSubmission(Request $request, string $slug, string $submissionId)
|
|
|
|
{
|
|
|
|
$submissionId = ($submissionId) ? Hashids::decode($submissionId) : false;
|
|
|
|
$submissionId = isset($submissionId[0]) ? $submissionId[0] : false;
|
|
|
|
$form = Form::whereSlug($slug)->whereVisibility('public')->firstOrFail();
|
2024-02-23 10:54:12 +00:00
|
|
|
if ($form->workspace == null || ! $form->editable_submissions || ! $submissionId) {
|
2023-01-10 13:52:14 +00:00
|
|
|
return $this->error([
|
|
|
|
'message' => 'Not allowed.',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$submission = FormSubmission::findOrFail($submissionId);
|
2023-01-13 13:56:37 +00:00
|
|
|
|
2023-01-10 13:52:14 +00:00
|
|
|
if ($submission->form_id != $form->id) {
|
|
|
|
return $this->error([
|
|
|
|
'message' => 'Not allowed.',
|
|
|
|
], 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success(['data' => ($submission) ? $submission->data : []]);
|
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|