Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Dec 25, 2023
1 parent 8c5ec09 commit 22b5f46
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 4 deletions.
29 changes: 29 additions & 0 deletions app/Http/Controllers/CustomFormController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Controllers;

use App\Models\CustomForm;
use Illuminate\Http\Request;
use Inertia\Inertia;

class CustomFormController extends Controller
{
public function index()
{

}

public function show(CustomForm $customForm)
{
// Check if form can be viewed by given user/visitor.
$this->authorize('viewPublic', $customForm);

return Inertia::render('CustomForm/ShowCustomForm', [
'customForm' => $customForm->append('description_html'),
]);
}

public function submit(Request $request)
{
}
}
8 changes: 8 additions & 0 deletions app/Models/CustomForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\CustomFormStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use League\CommonMark\GithubFlavoredMarkdownConverter;

class CustomForm extends BaseModel
{
Expand All @@ -15,4 +16,11 @@ class CustomForm extends BaseModel
'require_restricted_permission_to_view_submission' => 'boolean',
'is_notify_staff_on_submission' => 'boolean',
];

public function getDescriptionHtmlAttribute(): string|null
{
$converter = new GithubFlavoredMarkdownConverter();

return $this->description ? $converter->convertToHtml($this->description) : null;
}
}
43 changes: 43 additions & 0 deletions app/Policies/CustomFormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Policies;

use App\Enums\CustomFormStatus;
use App\Models\CustomForm;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down Expand Up @@ -79,4 +80,46 @@ public function forceDelete(User $user, CustomForm $customForm): bool
return true;
}
}

public function viewPublic(?User $user, CustomForm $customForm)
{
$invalidStatus = in_array($customForm->status, [CustomFormStatus::ARCHIVED, CustomFormStatus::DRAFT]);
if ($invalidStatus) {
return false;
}

$canCreateSubmission = $customForm->can_create_submission;
if ($canCreateSubmission === 'anyone' || $canCreateSubmission === 'auth') {
return true;
}

if ($canCreateSubmission === 'staff' && $user && $user->isStaffMember()) {
return true;
}

return false;
}

public function submit(User $user, CustomForm $customForm)
{
$isActive = $customForm->status === CustomFormStatus::ACTIVE;
if (! $isActive) {
return false;
}

$canCreateSubmission = $customForm->can_create_submission;
if ($canCreateSubmission === 'anyone') {
return true;
}

if ($canCreateSubmission === 'auth' && $user) {
return true;
}

if ($canCreateSubmission === 'staff' && $user && $user->isStaffMember()) {
return true;
}

return false;
}
}
6 changes: 3 additions & 3 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
// Super Admin can do anything even if that permission is missing for him.
Gate::before(function ($user, $ability) {
return $user->hasRole(Role::SUPER_ADMIN_ROLE_NAME) ? true : null;
});
// Gate::before(function ($user, $ability) {
// return $user->hasRole(Role::SUPER_ADMIN_ROLE_NAME) ? true : null;
// });

Gate::define('viewPulse', function (User $user) {
return $user->can('view pulse_admin_dashboard');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function up(): void
$table->string('status')->default('active'); // draft, active, disabled, archived

$table->string('can_create_submission')->default('anyone'); // anyone -> anyone, "auth" -> only authenticated users, "staff" -> only staff
$table->integer('max_submission_per_user')->nullable(); // null -> unlimited
$table->boolean('require_restricted_permission_to_view_submission')->default(false); // Only staff with view restricted_custom_form_submission permission can view submission for this form.
$table->boolean('is_notify_staff_on_submission')->default(false); // notify staff (with view access) when new submission is made.

Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Admin/CustomForm/IndexCustomForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const headerRow = [
>
<InertiaLink
as="a"
:href="route('custom-page.show', item.id)"
:href="route('custom-form.show', item.slug)"
class="inline-flex items-center justify-center text-blue-500 hover:text-blue-800"
>
<EyeIcon class="inline-block w-5 h-5" />
Expand Down
88 changes: 88 additions & 0 deletions resources/js/Pages/CustomForm/ShowCustomForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!-- eslint-disable vue/no-v-html -->
<template>
<app-layout>
<app-head :title="customForm.title" />

<div class="py-4 px-2 md:py-12 md:px-10 max-w-7xl mx-auto">
<div class="flex justify-between md:mb-4">
<h1 class="text-center font-bold text-2xl text-gray-900 dark:text-gray-200 mb-5">
{{ customForm.title }}
</h1>
<div class="">
<inertia-link
:href="route('home')"
class="inline-flex items-center px-4 py-2 bg-gray-400 dark:bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-500 active:bg-gray-600 focus:outline-none focus:border-gray-500 focus:shadow-outline-gray transition ease-in-out duration-150"
>
<span>{{ __("Homepage") }}</span>
</inertia-link>
</div>
</div>
<div class="flex flex-col md:flex-row md:space-x-4">
<div
class="overflow-x-auto md:w-9/12"
>
<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"
v-html="customForm.description_html"
/>

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

<div
class="md:w-3/12 flex-1 space-y-4 mt-4 md:mt-0"
>
<server-status-box />
<shout-box />
</div>
</div>
</div>
</app-layout>
</template>

<script setup>
import AppLayout from '@/Layouts/AppLayout.vue';
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';

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

const formSchema = useFormKit().generateSchemaFromFieldsArray(props.customForm.fields);

const submitForm = async (values) => {
await promisifyForm(values);
};

const promisifyForm = (values) => {
return new Promise((resolve, reject) => {
const form = useForm({
...values
});
form.post(route('custom-form.submit', props.customForm.slug), {
onSuccess: visit => {
resolve(visit);
},
onError: err => {
reject(err);
}
});
});
};
</script>
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
Route::get('download', [\App\Http\Controllers\DownloadController::class, 'index'])->name('download.index');
Route::get('download/{download:slug}', [\App\Http\Controllers\DownloadController::class, 'show'])->name('download.show');
Route::get('download/{download:slug}/download', [\App\Http\Controllers\DownloadController::class, 'download'])->name('download.download');

// Custom Form
Route::get('form', [\App\Http\Controllers\CustomFormController::class, 'index'])->name('custom-form.index');
Route::get('form/{customForm:slug}', [\App\Http\Controllers\CustomFormController::class, 'show'])->name('custom-form.show');
Route::post('form/{customForm:slug}', [\App\Http\Controllers\CustomFormController::class, 'submit'])->name('custom-form.submit');
});

/**
Expand Down

0 comments on commit 22b5f46

Please sign in to comment.