Editable Submissions (#49)
* Editable Submissions * refactor some code * Update composer.lock Co-authored-by: JhumanJ <julien@nahum.net>
This commit is contained in:
@@ -7,11 +7,13 @@ use App\Http\Requests\AnswerFormRequest;
|
||||
use App\Http\Resources\FormResource;
|
||||
use App\Jobs\Form\StoreFormSubmissionJob;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Forms\FormSubmission;
|
||||
use App\Service\Forms\FormCleaner;
|
||||
use App\Service\WorkspaceHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class PublicFormController extends Controller
|
||||
{
|
||||
@@ -77,10 +79,19 @@ class PublicFormController extends Controller
|
||||
public function answer(AnswerFormRequest $request)
|
||||
{
|
||||
$form = $request->form;
|
||||
$submissionId = false;
|
||||
|
||||
if ($form->editable_submissions) {
|
||||
$job = new StoreFormSubmissionJob($form, $request->validated());
|
||||
$job->handle();
|
||||
$submissionId = Hashids::encode($job->getSubmissionId());
|
||||
}else{
|
||||
StoreFormSubmissionJob::dispatch($form, $request->validated());
|
||||
}
|
||||
|
||||
StoreFormSubmissionJob::dispatch($form, $request->validated());
|
||||
return $this->success(array_merge([
|
||||
'message' => 'Form submission saved.',
|
||||
'submission_id' => $submissionId
|
||||
], $request->form->is_pro && $request->form->redirect_url ? [
|
||||
'redirect' => true,
|
||||
'redirect_url' => $request->form->redirect_url
|
||||
@@ -88,4 +99,26 @@ class PublicFormController extends Controller
|
||||
'redirect' => false
|
||||
]));
|
||||
}
|
||||
|
||||
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();
|
||||
if ($form->workspace == null || !$form->editable_submissions || !$submissionId) {
|
||||
return $this->error([
|
||||
'message' => 'Not allowed.',
|
||||
]);
|
||||
}
|
||||
|
||||
$submission = FormSubmission::findOrFail($submissionId);
|
||||
if ($submission->form_id != $form->id) {
|
||||
return $this->error([
|
||||
'message' => 'Not allowed.',
|
||||
], 403);
|
||||
}
|
||||
|
||||
return $this->success(['data' => ($submission) ? $submission->data : []]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -99,6 +99,12 @@ class AnswerFormRequest extends FormRequest
|
||||
if ($this->form->is_pro && $this->form->use_captcha) {
|
||||
$this->requestRules['h-captcha-response'] = [new ValidHCaptcha()];
|
||||
}
|
||||
|
||||
// Validate submission_id for edit mode
|
||||
if ($this->form->editable_submissions) {
|
||||
$this->requestRules['submission_id'] = 'string';
|
||||
}
|
||||
|
||||
return $this->requestRules;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
|
||||
'database_fields_update' => 'nullable|array',
|
||||
'max_submissions_count' => 'integer|nullable|min:1',
|
||||
'max_submissions_reached_text' => 'string|nullable',
|
||||
'editable_submissions' => 'boolean|nullable',
|
||||
|
||||
// Properties
|
||||
'properties' => 'required|array',
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Events\Forms\FormSubmitted;
|
||||
use App\Http\Controllers\Forms\PublicFormController;
|
||||
use App\Http\Requests\AnswerFormRequest;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Forms\FormSubmission;
|
||||
use App\Service\Storage\StorageFileNameParser;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||
@@ -15,11 +16,14 @@ use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class StoreFormSubmissionJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public ?string $submissionId = null;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
@@ -39,13 +43,46 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
$formData = $this->getFormData();
|
||||
$this->addHiddenPrefills($formData);
|
||||
|
||||
$this->form->submissions()->create([
|
||||
'data' => $formData,
|
||||
]);
|
||||
$this->storeSubmission($formData);
|
||||
|
||||
$formData["submission_id"] = $this->submissionData['submission_id'] ?? '';
|
||||
FormSubmitted::dispatch($this->form, $formData);
|
||||
}
|
||||
|
||||
public function getSubmissionId()
|
||||
{
|
||||
return $this->submissionId;
|
||||
}
|
||||
|
||||
private function storeSubmission(array $formData)
|
||||
{
|
||||
// Create or update record
|
||||
if ($previousSubmission = $this->submissionToUpdate()) {
|
||||
$previousSubmission->data = $formData;
|
||||
$previousSubmission->save();
|
||||
$this->submissionId = $previousSubmission->id;
|
||||
} else {
|
||||
$response = $this->form->submissions()->create([
|
||||
'data' => $formData,
|
||||
]);
|
||||
$this->submissionId = $response->id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for Submission record to update and returns it
|
||||
*/
|
||||
private function submissionToUpdate(): ?FormSubmission
|
||||
{
|
||||
if ($this->form->editable_submissions && isset($this->submissionData['submission_id']) && $this->submissionData['submission_id']) {
|
||||
$submissionId = $this->submissionData['submission_id'] ? Hashids::decode($this->submissionData['submission_id']) : false;
|
||||
$submissionId = $submissionId[0] ?? null;
|
||||
return $this->form->submissions()->findOrFail($submissionId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve data from request object, and pre-format it if needed.
|
||||
* - Replace notionforms id with notion field ids
|
||||
@@ -146,7 +183,7 @@ class StoreFormSubmissionJob implements ShouldQueue
|
||||
$completeNewFilename = $newPath.'/'.$fileName;
|
||||
|
||||
Storage::disk('s3')->put($completeNewFilename, base64_decode(explode(',', $value)[1]));
|
||||
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,8 @@ class SubmissionConfirmationMail extends OpenFormMail implements ShouldQueue
|
||||
->markdown('mail.form.confirmation-submission-notification',[
|
||||
'fields' => $formatter->getFieldsWithValue(),
|
||||
'form' => $form,
|
||||
'noBranding' => $form->no_branding
|
||||
'noBranding' => $form->no_branding,
|
||||
'submission_id' => isset($this->event->data['submission_id']) ? $this->event->data['submission_id'] : ''
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ class Form extends Model
|
||||
'closed_text',
|
||||
'max_submissions_count',
|
||||
'max_submissions_reached_text',
|
||||
'editable_submissions',
|
||||
|
||||
// Security & Privacy
|
||||
'can_be_indexed',
|
||||
|
||||
Reference in New Issue
Block a user