Skip to content

Commit

Permalink
Editable Submissions (JhumanJ#49)
Browse files Browse the repository at this point in the history
* Editable Submissions

* refactor some code

* Update composer.lock

Co-authored-by: JhumanJ <[email protected]>
  • Loading branch information
chiragnotionforms and JhumanJ authored Jan 10, 2023
1 parent 997a86f commit 0a33581
Show file tree
Hide file tree
Showing 390 changed files with 2,623 additions and 1,160 deletions.
35 changes: 34 additions & 1 deletion app/Http/Controllers/Forms/PublicFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
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
{
Expand Down Expand Up @@ -77,15 +79,46 @@ public function showAsset($assetFileName)
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
] : [
'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 : []]);
}

}
6 changes: 6 additions & 0 deletions app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public function rules()
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;
}

Expand Down
1 change: 1 addition & 0 deletions app/Http/Requests/UserFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function rules()
'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',
Expand Down
45 changes: 41 additions & 4 deletions app/Jobs/Form/StoreFormSubmissionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand All @@ -15,11 +16,14 @@
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.
*
Expand All @@ -39,13 +43,46 @@ public function handle()
$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
Expand Down Expand Up @@ -146,7 +183,7 @@ private function storeSignature(?string $value)
$completeNewFilename = $newPath.'/'.$fileName;

Storage::disk('s3')->put($completeNewFilename, base64_decode(explode(',', $value)[1]));

return $fileName;
}

Expand Down
3 changes: 2 additions & 1 deletion app/Mail/Forms/SubmissionConfirmationMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public function build()
->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'] : ''
]);
}

Expand Down
1 change: 1 addition & 0 deletions app/Models/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Form extends Model
'closed_text',
'max_submissions_count',
'max_submissions_reached_text',
'editable_submissions',

// Security & Privacy
'can_be_indexed',
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"doctrine/dbal": "^3.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"itsgoingd/clockwork": "^5.1",
"jhumanj/laravel-model-stats": "^0.4.0",
"laravel/cashier": "^13.4",
"laravel/framework": "^9.0",
Expand All @@ -42,7 +41,8 @@
"spatie/laravel-sluggable": "^3.0",
"spatie/laravel-webhook-server": "^3.1.2",
"stevebauman/purify": "^4.0",
"tymon/jwt-auth": "^1.0.2"
"tymon/jwt-auth": "^1.0.2",
"vinkla/hashids": "^10.0"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.12",
Expand Down
Loading

0 comments on commit 0a33581

Please sign in to comment.