2022-09-20 21:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Forms;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Http\Requests\AnswerFormRequest;
|
|
|
|
|
use App\Http\Resources\FormResource;
|
2024-04-15 15:12:36 +02:00
|
|
|
use App\Http\Resources\FormSubmissionResource;
|
2022-09-20 21:59:52 +02:00
|
|
|
use App\Jobs\Form\StoreFormSubmissionJob;
|
|
|
|
|
use App\Models\Forms\Form;
|
2023-01-10 14:52:14 +01:00
|
|
|
use App\Models\Forms\FormSubmission;
|
2025-02-01 22:52:06 +01:00
|
|
|
use App\Service\Forms\FormSubmissionProcessor;
|
2022-09-20 21:59:52 +02:00
|
|
|
use App\Service\Forms\FormCleaner;
|
|
|
|
|
use App\Service\WorkspaceHelper;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2024-02-23 11:54:12 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-01-10 14:52:14 +01:00
|
|
|
use Vinkla\Hashids\Facades\Hashids;
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
class PublicFormController extends Controller
|
|
|
|
|
{
|
2024-02-23 11:54:12 +01:00
|
|
|
public const FILE_UPLOAD_PATH = 'forms/?/submissions';
|
2022-09-20 21:59:52 +02:00
|
|
|
|
2024-02-23 11:54:12 +01:00
|
|
|
public const TMP_FILE_UPLOAD_PATH = 'tmp/';
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
public function show(Request $request, string $slug)
|
|
|
|
|
{
|
2023-02-19 13:11:50 +01:00
|
|
|
$form = Form::whereSlug($slug)->whereIn('visibility', ['public', 'closed'])->firstOrFail();
|
2022-09-20 21:59:52 +02:00
|
|
|
if ($form->workspace == null) {
|
|
|
|
|
// Workspace deleted
|
|
|
|
|
return $this->error([
|
2024-02-23 11:54:12 +01:00
|
|
|
'message' => 'Form not found.',
|
2022-09-20 21:59:52 +02:00
|
|
|
], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$formCleaner = new FormCleaner();
|
|
|
|
|
|
|
|
|
|
// Disable pro features if needed
|
2024-02-23 11:54:12 +01:00
|
|
|
$form->fill(
|
|
|
|
|
$formCleaner
|
2024-04-15 15:12:36 +02:00
|
|
|
->processForm($request, $form)
|
|
|
|
|
->performCleaning($form->workspace)
|
|
|
|
|
->getData()
|
2022-09-20 21:59:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Increase form view counter if not login
|
2024-04-15 15:12:36 +02:00
|
|
|
if (!Auth::check()) {
|
2022-09-20 21:59:52 +02:00
|
|
|
$form->views()->create();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 09:58:29 +02:00
|
|
|
return (new FormResource($form))
|
|
|
|
|
->setCleanings($formCleaner->getPerformedCleanings());
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function listUsers(Request $request)
|
|
|
|
|
{
|
|
|
|
|
// Check that form has user field
|
|
|
|
|
$form = $request->form;
|
2024-04-15 15:12:36 +02:00
|
|
|
if (!$form->has_user_field) {
|
2022-09-20 21:59:52 +02:00
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use serializer
|
|
|
|
|
$workspace = $form->workspace;
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2022-09-20 21:59:52 +02:00
|
|
|
return (new WorkspaceHelper($workspace))->getAllUsers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function showAsset($assetFileName)
|
|
|
|
|
{
|
2024-04-15 15:12:36 +02:00
|
|
|
$path = FormController::ASSETS_UPLOAD_PATH . '/' . $assetFileName;
|
|
|
|
|
if (!Storage::exists($path)) {
|
2022-09-20 21:59:52 +02:00
|
|
|
return $this->error([
|
|
|
|
|
'message' => 'File not found.',
|
2024-02-23 11:54:12 +01:00
|
|
|
'file_name' => $assetFileName,
|
2022-09-20 21:59:52 +02:00
|
|
|
]);
|
|
|
|
|
}
|
2024-02-23 11:54:12 +01:00
|
|
|
|
2024-08-05 12:06:20 +02:00
|
|
|
$internal_url = Storage::temporaryUrl($path, now()->addMinutes(5));
|
|
|
|
|
|
2024-09-18 19:20:52 +02:00
|
|
|
foreach (config('filesystems.disks.s3.temporary_url_rewrites') as $from => $to) {
|
2024-08-05 12:06:20 +02:00
|
|
|
$internal_url = str_replace($from, $to, $internal_url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->to($internal_url);
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-01 22:52:06 +01:00
|
|
|
public function answer(AnswerFormRequest $request, FormSubmissionProcessor $formSubmissionProcessor)
|
2022-09-20 21:59:52 +02:00
|
|
|
{
|
|
|
|
|
$form = $request->form;
|
2024-10-21 17:41:20 +02:00
|
|
|
$isFirstSubmission = ($form->submissions_count === 0);
|
2023-01-10 14:52:14 +01:00
|
|
|
$submissionId = false;
|
|
|
|
|
|
2024-09-18 19:20:52 +02:00
|
|
|
$submissionData = $request->validated();
|
|
|
|
|
$completionTime = $request->get('completion_time') ?? null;
|
|
|
|
|
unset($submissionData['completion_time']); // Remove completion_time from the main data array
|
|
|
|
|
|
2025-02-01 22:52:06 +01:00
|
|
|
$job = new StoreFormSubmissionJob($form, $submissionData, $completionTime);
|
|
|
|
|
|
|
|
|
|
if ($formSubmissionProcessor->shouldProcessSynchronously($form)) {
|
2023-01-10 14:52:14 +01:00
|
|
|
$job->handle();
|
|
|
|
|
$submissionId = Hashids::encode($job->getSubmissionId());
|
2025-02-01 22:52:06 +01:00
|
|
|
// Update submission data with generated values for redirect URL
|
|
|
|
|
$submissionData = $job->getProcessedData();
|
2024-02-23 11:54:12 +01:00
|
|
|
} else {
|
2024-09-18 19:20:52 +02:00
|
|
|
StoreFormSubmissionJob::dispatch($form, $submissionData, $completionTime);
|
2023-01-10 14:52:14 +01:00
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
|
|
|
|
|
return $this->success(array_merge([
|
|
|
|
|
'message' => 'Form submission saved.',
|
2024-02-23 11:54:12 +01:00
|
|
|
'submission_id' => $submissionId,
|
2024-10-22 10:34:29 +02:00
|
|
|
'is_first_submission' => $isFirstSubmission,
|
2025-02-01 22:52:06 +01:00
|
|
|
], $formSubmissionProcessor->getRedirectData($form, $submissionData)));
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|
2023-01-10 14:52:14 +01: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-04-15 15:12:36 +02:00
|
|
|
if ($form->workspace == null || !$form->editable_submissions || !$submissionId) {
|
2023-01-10 14:52:14 +01:00
|
|
|
return $this->error([
|
|
|
|
|
'message' => 'Not allowed.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:48:28 +01:00
|
|
|
$submission = FormSubmission::find($submissionId);
|
|
|
|
|
if (!$submission) {
|
|
|
|
|
return $this->error([
|
|
|
|
|
'message' => 'Submission not found.',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$submission = new FormSubmissionResource($submission);
|
2024-04-15 15:12:36 +02:00
|
|
|
$submission->publiclyAccessed();
|
2023-01-13 14:56:37 +01:00
|
|
|
|
2023-01-10 14:52:14 +01:00
|
|
|
if ($submission->form_id != $form->id) {
|
|
|
|
|
return $this->error([
|
|
|
|
|
'message' => 'Not allowed.',
|
|
|
|
|
], 403);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 15:12:36 +02:00
|
|
|
return $this->success($submission->toArray($request));
|
2023-01-10 14:52:14 +01:00
|
|
|
}
|
2022-09-20 21:59:52 +02:00
|
|
|
}
|