diff --git a/app/Http/Controllers/CustomFormController.php b/app/Http/Controllers/CustomFormController.php index 55cc4eb03..3276306d2 100644 --- a/app/Http/Controllers/CustomFormController.php +++ b/app/Http/Controllers/CustomFormController.php @@ -4,13 +4,18 @@ use App\Models\CustomForm; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Gate; use Inertia\Inertia; class CustomFormController extends Controller { public function index() { + $forms = []; + return Inertia::render('CustomForm/IndexCustomForm', [ + 'forms' => $forms, + ]); } public function show(CustomForm $customForm) @@ -18,12 +23,25 @@ public function show(CustomForm $customForm) // Check if form can be viewed by given user/visitor. $this->authorize('viewPublic', $customForm); + $canSubmitForm = Gate::allows('submit', $customForm); + return Inertia::render('CustomForm/ShowCustomForm', [ 'customForm' => $customForm->append('description_html'), + 'currentUserCanSubmit' => $canSubmitForm, ]); } - public function submit(Request $request) + public function submit(Request $request, CustomForm $customForm) { + $this->authorize('submit', $customForm); + + $customForm->submissions()->create([ + 'user_id' => $request->user()?->id, + 'ip_address' => $request->ip(), + 'data' => $request->all(), + ]); + + return redirect()->route('custom-form.index') + ->with(['toast' => ['type' => 'success', 'title' => __('Submit Successful!'), 'body' => __('Form has been submitted successfully.')]]); } } diff --git a/app/Models/CustomForm.php b/app/Models/CustomForm.php index 61c2edad6..18341dfed 100644 --- a/app/Models/CustomForm.php +++ b/app/Models/CustomForm.php @@ -22,4 +22,9 @@ public function getDescriptionHtmlAttribute(): string|null return $this->description ? $converter->convertToHtml($this->description) : null; } + + public function submissions() + { + return $this->hasMany(CustomFormSubmission::class); + } } diff --git a/app/Models/CustomFormSubmission.php b/app/Models/CustomFormSubmission.php index 49c406338..c9682efec 100644 --- a/app/Models/CustomFormSubmission.php +++ b/app/Models/CustomFormSubmission.php @@ -3,9 +3,22 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; -class CustomFormSubmission extends Model +class CustomFormSubmission extends BaseModel { use HasFactory; + + protected $casts = [ + 'data' => 'array', + ]; + + public function customForm() + { + return $this->belongsTo(CustomForm::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 3da0d876a..53a6fdabc 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -360,4 +360,9 @@ public function shouts() { return $this->hasMany(Shout::class); } + + public function customFormSubmissions() + { + return $this->hasMany(CustomFormSubmission::class); + } } diff --git a/app/Policies/CustomFormPolicy.php b/app/Policies/CustomFormPolicy.php index 40056d325..786404d92 100644 --- a/app/Policies/CustomFormPolicy.php +++ b/app/Policies/CustomFormPolicy.php @@ -19,6 +19,8 @@ public function viewAny(User $user): bool if ($user->can('read custom_forms')) { return true; } + + return false; } /** @@ -29,6 +31,8 @@ public function view(User $user, CustomForm $customForm): bool if ($user->can('read custom_forms')) { return true; } + + return false; } /** @@ -39,6 +43,8 @@ public function create(User $user): bool if ($user->can('create custom_forms')) { return true; } + + return false; } /** @@ -49,6 +55,8 @@ public function update(User $user, CustomForm $customForm): bool if ($user->can('update custom_forms')) { return true; } + + return false; } /** @@ -59,6 +67,8 @@ public function delete(User $user, CustomForm $customForm): bool if ($user->can('delete custom_forms')) { return true; } + + return false; } /** @@ -69,6 +79,8 @@ public function restore(User $user, CustomForm $customForm): bool if ($user->can('delete custom_forms')) { return true; } + + return false; } /** @@ -79,11 +91,13 @@ public function forceDelete(User $user, CustomForm $customForm): bool if ($user->can('delete custom_forms')) { return true; } + + return false; } public function viewPublic(?User $user, CustomForm $customForm) { - $invalidStatus = in_array($customForm->status, [CustomFormStatus::ARCHIVED, CustomFormStatus::DRAFT]); + $invalidStatus = in_array($customForm->status->value, [CustomFormStatus::ARCHIVED, CustomFormStatus::DRAFT]); if ($invalidStatus) { return false; } @@ -100,26 +114,33 @@ public function viewPublic(?User $user, CustomForm $customForm) return false; } - public function submit(User $user, CustomForm $customForm) + public function submit(?User $user, CustomForm $customForm) { - $isActive = $customForm->status === CustomFormStatus::ACTIVE; + // Bail if form is not active + $isActive = $customForm->status->value === CustomFormStatus::ACTIVE; if (! $isActive) { return false; } - $canCreateSubmission = $customForm->can_create_submission; - if ($canCreateSubmission === 'anyone') { - return true; + // Bail if user is not logged in and can_create_submission is auth or staff + if (in_array($customForm->can_create_submission, ['auth', 'staff']) && ! $user) { + return false; } - if ($canCreateSubmission === 'auth' && $user) { - return true; + // Bail if max_submission_per_user is reached + $maxSubmissionPerUser = $customForm->max_submission_per_user; + if (in_array($customForm->can_create_submission, ['auth', 'staff']) && $maxSubmissionPerUser) { + $currentUserSubmissionCount = $customForm->submissions()->where('user_id', $user->id)->count(); + if ($currentUserSubmissionCount >= $maxSubmissionPerUser) { + return false; + } } - if ($canCreateSubmission === 'staff' && $user && $user->isStaffMember()) { - return true; + // Bail if user is not staff and can_create_submission is staff + if ($customForm->can_create_submission === 'staff' && ! $user->isStaffMember()) { + return false; } - return false; + return true; } } diff --git a/database/migrations/2023_12_23_062852_create_custom_form_submissions_table.php b/database/migrations/2023_12_23_062852_create_custom_form_submissions_table.php index 2f98bafd4..c5a90c48d 100644 --- a/database/migrations/2023_12_23_062852_create_custom_form_submissions_table.php +++ b/database/migrations/2023_12_23_062852_create_custom_form_submissions_table.php @@ -15,6 +15,7 @@ public function up(): void $table->id(); $table->foreignId('custom_form_id')->constrained('custom_forms')->cascadeOnDelete(); $table->foreignId('user_id')->nullable()->constrained('users')->cascadeOnDelete(); + $table->string('ip_address')->nullable(); $table->json('data'); $table->timestamps(); diff --git a/resources/js/Pages/CustomForm/ShowCustomForm.vue b/resources/js/Pages/CustomForm/ShowCustomForm.vue index 34641db5c..721585415 100644 --- a/resources/js/Pages/CustomForm/ShowCustomForm.vue +++ b/resources/js/Pages/CustomForm/ShowCustomForm.vue @@ -21,20 +21,42 @@
+ {{ __("You can submit this form only :count times.", { + count: customForm.max_submission_per_user + }) }} +