103 lines
2.7 KiB
PHP
103 lines
2.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Livewire\Templates;
|
||
|
|
|
||
|
|
use App\Models\Template;
|
||
|
|
use Livewire\Attributes\Layout;
|
||
|
|
use Livewire\Component;
|
||
|
|
|
||
|
|
#[Layout('components.layouts.app')]
|
||
|
|
class Index extends Component
|
||
|
|
{
|
||
|
|
public bool $showForm = false;
|
||
|
|
|
||
|
|
public ?int $editingId = null;
|
||
|
|
|
||
|
|
public string $name = '';
|
||
|
|
|
||
|
|
public string $body = '';
|
||
|
|
|
||
|
|
public ?string $type = null;
|
||
|
|
|
||
|
|
public ?string $title = null;
|
||
|
|
|
||
|
|
public ?string $target = null;
|
||
|
|
|
||
|
|
public string $placeholders = '';
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
return view('livewire.templates.index', [
|
||
|
|
'templates' => Template::orderBy('name')->get(),
|
||
|
|
'types' => Template::TYPES,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function create(): void
|
||
|
|
{
|
||
|
|
$this->resetForm();
|
||
|
|
$this->showForm = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function edit(int $id): void
|
||
|
|
{
|
||
|
|
$template = Template::findOrFail($id);
|
||
|
|
$this->editingId = $template->id;
|
||
|
|
$this->name = $template->name;
|
||
|
|
$this->body = $template->body;
|
||
|
|
$this->type = $template->type;
|
||
|
|
$this->title = $template->title;
|
||
|
|
$this->target = $template->target;
|
||
|
|
$this->placeholders = $template->placeholders ? implode(', ', $template->placeholders) : '';
|
||
|
|
$this->showForm = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function save(): void
|
||
|
|
{
|
||
|
|
$validated = $this->validate([
|
||
|
|
'name' => ['required', 'string', 'max:255'],
|
||
|
|
'body' => ['required', 'string'],
|
||
|
|
'type' => ['nullable', 'string', 'in:' . implode(',', Template::TYPES)],
|
||
|
|
'title' => ['nullable', 'string', 'max:255'],
|
||
|
|
'target' => ['nullable', 'string', 'max:255'],
|
||
|
|
'placeholders' => ['nullable', 'string'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Parse placeholders from comma-separated string
|
||
|
|
$placeholderArray = null;
|
||
|
|
if ($validated['placeholders']) {
|
||
|
|
$placeholderArray = array_map('trim', explode(',', $validated['placeholders']));
|
||
|
|
$placeholderArray = array_filter($placeholderArray);
|
||
|
|
}
|
||
|
|
|
||
|
|
Template::updateOrCreate(['id' => $this->editingId], [
|
||
|
|
'name' => $validated['name'],
|
||
|
|
'body' => $validated['body'],
|
||
|
|
'type' => $validated['type'],
|
||
|
|
'title' => $validated['title'],
|
||
|
|
'target' => $validated['target'],
|
||
|
|
'placeholders' => $placeholderArray,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$this->resetForm();
|
||
|
|
$this->showForm = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete(int $id): void
|
||
|
|
{
|
||
|
|
Template::findOrFail($id)->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function resetForm(): void
|
||
|
|
{
|
||
|
|
$this->editingId = null;
|
||
|
|
$this->name = '';
|
||
|
|
$this->body = '';
|
||
|
|
$this->type = null;
|
||
|
|
$this->title = null;
|
||
|
|
$this->target = null;
|
||
|
|
$this->placeholders = '';
|
||
|
|
$this->resetValidation();
|
||
|
|
}
|
||
|
|
}
|