Skip to content

Commit

Permalink
WiP Custom Forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Dec 26, 2023
1 parent 1bc0b34 commit 5442ab3
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 16 deletions.
20 changes: 19 additions & 1 deletion app/Http/Controllers/CustomFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,44 @@

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)
{
// 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.')]]);
}
}
5 changes: 5 additions & 0 deletions app/Models/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 15 additions & 2 deletions app/Models/CustomFormSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,9 @@ public function shouts()
{
return $this->hasMany(Shout::class);
}

public function customFormSubmissions()
{
return $this->hasMany(CustomFormSubmission::class);
}
}
43 changes: 32 additions & 11 deletions app/Policies/CustomFormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function viewAny(User $user): bool
if ($user->can('read custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -29,6 +31,8 @@ public function view(User $user, CustomForm $customForm): bool
if ($user->can('read custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -39,6 +43,8 @@ public function create(User $user): bool
if ($user->can('create custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -49,6 +55,8 @@ public function update(User $user, CustomForm $customForm): bool
if ($user->can('update custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -59,6 +67,8 @@ public function delete(User $user, CustomForm $customForm): bool
if ($user->can('delete custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -69,6 +79,8 @@ public function restore(User $user, CustomForm $customForm): bool
if ($user->can('delete custom_forms')) {
return true;
}

return false;
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
82 changes: 80 additions & 2 deletions resources/js/Pages/CustomForm/ShowCustomForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,42 @@
<div
class="overflow-x-auto md:w-9/12"
>
<AlertCard
v-if="formDisabled"
text-color="text-orange-800 dark:text-orange-500"
border-color="border-orange-500"
>
{{ disabledErrorMessage.title }}
<template #body>
{{
disabledErrorMessage.body
}}
</template>
</AlertCard>
<div class="min-w-full">
<div class="shadow max-w-none bg-white dark:bg-cool-gray-800 px-3 py-2 md:px-10 md:py-5 overflow-hidden rounded">
<div
v-if="customForm.description"
class="prose dark:prose-dark max-w-none mb-4"
class="prose dark:prose-dark max-w-none mb-6 pb-6 border-b dark:border-gray-700"
v-html="customForm.description_html"
/>

<FormKit
:disabled="formDisabled"
type="form"
@submit="submitForm"
>
<FormKitSchema :schema="formSchema" />
</FormKit>

<p
v-if="!formDisabled && customForm.max_submission_per_user"
class="text-xs text-gray-500 dark:text-gray-400 text-right"
>
{{ __("You can submit this form only :count times.", {
count: customForm.max_submission_per_user
}) }}
</p>
</div>
</div>
</div>
Expand All @@ -56,12 +78,20 @@ import ServerStatusBox from '@/Shared/ServerStatusBox.vue';
import ShoutBox from '@/Shared/ShoutBox.vue';
import { FormKitSchema } from '@formkit/vue';
import {useFormKit} from '@/Composables/useFormKit';
import { useForm } from '@inertiajs/vue3';
import { useForm, usePage } from '@inertiajs/vue3';
import { computed } from 'vue';
import AlertCard from '@/Components/AlertCard.vue';
import { useTranslations } from '@/Composables/useTranslations';

const { __ } = useTranslations();

const props = defineProps({
customForm: {
type: Object,
},
currentUserCanSubmit: {
type: Boolean,
},
});

const formSchema = useFormKit().generateSchemaFromFieldsArray(props.customForm.fields);
Expand All @@ -85,4 +115,52 @@ const promisifyForm = (values) => {
});
});
};

const formDisabled = computed(() => {
return props.customForm.status.value !== 'active' || !props.currentUserCanSubmit;
});

const disabledErrorMessage = computed(() => {
if (!formDisabled.value) {
return {
title: null,
body: null,
};
}

if (props.customForm.status.value === 'disabled') {
return {
title: __('Oh Jeez! This form is currently disabled.'),
body: __(
'It seems the form is no longer accepting submissions. Please check back later.'
),
};
}

if (props.customForm.status.value !== 'active') {
return {
title: __('Oh Jeez! This form is not active.'),
body: __(
'It seems the form is not active yet. Please check back later.'
),
};
}

if (['auth', 'staff'].includes(props.customForm.can_create_submission) && !usePage().props.auth.user) {
return {
title: __('Oh Jeez! You need to be logged in to submit this form.'),
body: __(
'Please login and try again.'
),
};
}

return {
title: __('Oh Jeez! You have already submitted this form.'),
body: __(
'You have already submitted this form maximum number of times allowed.'
),
};
});

</script>

0 comments on commit 5442ab3

Please sign in to comment.