Implemented a block editor for changing the layout of the page

This commit is contained in:
2026-04-04 01:17:05 +02:00
parent 0800f7664f
commit ff58e82497
41 changed files with 2706 additions and 298 deletions

View File

@@ -8,6 +8,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StorePreregistrationPageRequest;
use App\Http\Requests\Admin\UpdatePreregistrationPageRequest;
use App\Models\PreregistrationPage;
use App\Services\PreregistrationPageBlockWriter;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -17,8 +18,9 @@ use Illuminate\View\View;
class PageController extends Controller
{
public function __construct()
{
public function __construct(
private readonly PreregistrationPageBlockWriter $blockWriter
) {
$this->authorizeResource(PreregistrationPage::class, 'page');
}
@@ -47,31 +49,32 @@ class PageController extends Controller
public function store(StorePreregistrationPageRequest $request): RedirectResponse
{
$validated = $request->validated();
$background = $request->file('background_image');
$logo = $request->file('logo_image');
unset($validated['background_image'], $validated['logo_image']);
$validated['slug'] = (string) Str::uuid();
$validated['user_id'] = $request->user()->id;
$validated['heading'] = $validated['title'];
$validated['intro_text'] = null;
$validated['phone_enabled'] = false;
$validated['background_image'] = null;
$validated['logo_image'] = null;
$page = DB::transaction(function () use ($validated, $background, $logo): PreregistrationPage {
$page = PreregistrationPage::create($validated);
$paths = [];
if ($background !== null) {
$paths['background_image'] = $background->store("preregister/pages/{$page->id}", 'public');
}
if ($logo !== null) {
$paths['logo_image'] = $logo->store("preregister/pages/{$page->id}", 'public');
}
if ($paths !== []) {
$page->update($paths);
$page = DB::transaction(function () use ($validated, $request): PreregistrationPage {
$page = PreregistrationPage::query()->create($validated);
$this->blockWriter->seedDefaultBlocks($page);
$page = $page->fresh(['blocks']);
$bgFile = $request->file('page_background');
if ($bgFile !== null && $bgFile->isValid()) {
$path = $bgFile->store("preregister/pages/{$page->id}", 'public');
$page->update(['background_image' => $path]);
}
return $page->fresh();
return $page->fresh(['blocks']);
});
$page->syncLegacyContentColumnsFromBlocks();
return redirect()
->route('admin.pages.index')
->route('admin.pages.edit', $page)
->with('status', __('Page created. Public URL: :url', ['url' => url('/r/'.$page->slug)]));
}
@@ -82,44 +85,61 @@ class PageController extends Controller
public function edit(PreregistrationPage $page): View
{
$page->load(['blocks' => fn ($q) => $q->orderBy('sort_order')]);
return view('admin.pages.edit', compact('page'));
}
public function update(UpdatePreregistrationPageRequest $request, PreregistrationPage $page): RedirectResponse
{
$validated = $request->validated();
$background = $request->file('background_image');
$logo = $request->file('logo_image');
unset($validated['background_image'], $validated['logo_image']);
/** @var array<string|int, array<string, mixed>> $blocks */
$blocks = $validated['blocks'];
unset($validated['blocks']);
DB::transaction(function () use ($validated, $background, $logo, $page): void {
if ($background !== null) {
if ($page->background_image !== null) {
Storage::disk('public')->delete($page->background_image);
DB::transaction(function () use ($validated, $blocks, $request, $page): void {
$disk = Storage::disk('public');
if ($request->boolean('remove_page_background')) {
if (is_string($page->background_image) && $page->background_image !== '') {
$disk->delete($page->background_image);
}
$validated['background_image'] = $background->store("preregister/pages/{$page->id}", 'public');
$validated['background_image'] = null;
}
if ($logo !== null) {
if ($page->logo_image !== null) {
Storage::disk('public')->delete($page->logo_image);
$bgFile = $request->file('page_background');
if ($bgFile !== null && $bgFile->isValid()) {
if (is_string($page->background_image) && $page->background_image !== '') {
$disk->delete($page->background_image);
}
$validated['logo_image'] = $logo->store("preregister/pages/{$page->id}", 'public');
$validated['background_image'] = $bgFile->store("preregister/pages/{$page->id}", 'public');
}
$page->update($validated);
$this->blockWriter->replaceBlocks($page, $blocks, $request);
});
$page->fresh(['blocks'])->syncLegacyContentColumnsFromBlocks();
return redirect()
->route('admin.pages.index')
->route('admin.pages.edit', $page)
->with('status', __('Page updated.'));
}
public function destroy(PreregistrationPage $page): RedirectResponse
{
DB::transaction(function () use ($page): void {
if ($page->background_image !== null) {
$page->load('blocks');
foreach ($page->blocks as $block) {
if ($block->type !== 'image') {
continue;
}
$path = data_get($block->content, 'image');
if (is_string($path) && $path !== '') {
Storage::disk('public')->delete($path);
}
}
if ($page->background_image !== null && $page->background_image !== '') {
Storage::disk('public')->delete($page->background_image);
}
if ($page->logo_image !== null) {
if ($page->logo_image !== null && $page->logo_image !== '') {
Storage::disk('public')->delete($page->logo_image);
}
$page->delete();

View File

@@ -70,7 +70,7 @@ class SubscriberController extends Controller
->orderBy('created_at')
->get();
$phoneEnabled = $page->phone_enabled;
$phoneEnabled = $page->isPhoneFieldEnabledForSubscribers();
return response()->streamDownload(function () use ($subscribers, $phoneEnabled): void {
$handle = fopen('php://output', 'w');