76 lines
2.2 KiB
PHP
76 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\SiteSection;
|
|
use Illuminate\Validation\Rule;
|
|
use Livewire\Component;
|
|
|
|
class Sections extends Component
|
|
{
|
|
public ?int $editingId = null;
|
|
|
|
public string $key = '';
|
|
|
|
public string $eyebrow = '';
|
|
|
|
public string $title = '';
|
|
|
|
public string $body = '';
|
|
|
|
public string $button_label = '';
|
|
|
|
public string $button_url = '';
|
|
|
|
public bool $is_visible = true;
|
|
|
|
public int $sort_order = 0;
|
|
|
|
public function edit(int $id): void
|
|
{
|
|
$s = SiteSection::findOrFail($id);
|
|
$this->editingId = $s->id;
|
|
foreach (['key', 'eyebrow', 'title', 'body', 'button_label', 'button_url', 'is_visible', 'sort_order'] as $field) {
|
|
$this->$field = $s->$field ?? (is_bool($this->$field) ? false : '');
|
|
}
|
|
}
|
|
|
|
public function add(): void
|
|
{
|
|
$this->reset(['editingId', 'key', 'eyebrow', 'title', 'body', 'button_label', 'button_url', 'sort_order']);
|
|
$this->is_visible = true;
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$data = $this->validate([
|
|
'key' => ['required', 'alpha_dash', 'max:80', Rule::unique('site_sections', 'key')->ignore($this->editingId)],
|
|
'eyebrow' => 'nullable|string|max:255', 'title' => 'required|string|max:255', 'body' => 'nullable|string|max:5000',
|
|
'button_label' => 'nullable|string|max:100', 'button_url' => 'nullable|string|max:500',
|
|
'is_visible' => 'boolean', 'sort_order' => 'integer|min:0|max:999',
|
|
]);
|
|
SiteSection::updateOrCreate(['id' => $this->editingId], $data);
|
|
$this->add();
|
|
session()->flash('status', 'Section saved successfully.');
|
|
}
|
|
|
|
public function toggle(int $id): void
|
|
{
|
|
$s = SiteSection::findOrFail($id);
|
|
$s->update(['is_visible' => ! $s->is_visible]);
|
|
}
|
|
|
|
public function delete(int $id): void
|
|
{
|
|
SiteSection::findOrFail($id)->delete();
|
|
if ($this->editingId === $id) {
|
|
$this->add();
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.sections', ['sections' => SiteSection::orderBy('sort_order')->get()])->layout('layouts.admin', ['title' => 'Page sections']);
|
|
}
|
|
}
|