Implement Weeztix integration per documentation: database config and subscriber coupon_code, OAuth redirect/callback, admin setup UI with company/coupon selection via AJAX, synchronous coupon creation on public subscribe with duplicate and rate-limit handling, Mailwizz field mapping for coupon codes, subscriber table and CSV export, and connection hint on the pages list. Made-with: Cursor
154 lines
5.4 KiB
PHP
154 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
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;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\View\View;
|
|
|
|
class PageController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly PreregistrationPageBlockWriter $blockWriter
|
|
) {
|
|
$this->authorizeResource(PreregistrationPage::class, 'page');
|
|
}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$query = PreregistrationPage::query()
|
|
->withCount('subscribers')
|
|
->with('weeztixConfig')
|
|
->orderByDesc('start_date');
|
|
|
|
if (! $request->user()?->isSuperadmin()) {
|
|
$query->where('user_id', $request->user()->id);
|
|
} else {
|
|
$query->with('user');
|
|
}
|
|
|
|
$pages = $query->get();
|
|
|
|
return view('admin.pages.index', compact('pages'));
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('admin.pages.create');
|
|
}
|
|
|
|
public function store(StorePreregistrationPageRequest $request): RedirectResponse
|
|
{
|
|
$validated = $request->validated();
|
|
$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, $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(['blocks']);
|
|
});
|
|
|
|
$page->syncLegacyContentColumnsFromBlocks();
|
|
|
|
return redirect()
|
|
->route('admin.pages.edit', $page)
|
|
->with('status', __('Page created. Public URL: :url', ['url' => url('/r/'.$page->slug)]));
|
|
}
|
|
|
|
public function show(PreregistrationPage $page): RedirectResponse
|
|
{
|
|
return redirect()->route('admin.pages.edit', $page);
|
|
}
|
|
|
|
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();
|
|
/** @var array<string|int, array<string, mixed>> $blocks */
|
|
$blocks = $validated['blocks'];
|
|
unset($validated['blocks']);
|
|
|
|
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'] = null;
|
|
}
|
|
$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['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.edit', $page)
|
|
->with('status', __('Page updated.'));
|
|
}
|
|
|
|
public function destroy(PreregistrationPage $page): RedirectResponse
|
|
{
|
|
DB::transaction(function () use ($page): void {
|
|
$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 && $page->logo_image !== '') {
|
|
Storage::disk('public')->delete($page->logo_image);
|
|
}
|
|
$page->delete();
|
|
});
|
|
|
|
return redirect()
|
|
->route('admin.pages.index')
|
|
->with('status', __('Page deleted.'));
|
|
}
|
|
}
|