101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use App\Models\Datacenter;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.admin')]
|
|
class Datacenters extends Component
|
|
{
|
|
#[Validate('required|string|max:8|alpha_dash|unique:datacenters,code')]
|
|
public string $code = '';
|
|
|
|
#[Validate('required|string|max:255')]
|
|
public string $name = '';
|
|
|
|
#[Validate('nullable|string|max:255')]
|
|
public string $location = '';
|
|
|
|
// Inline edit state (code is immutable once created — it is referenced by hosts).
|
|
public ?string $editingUuid = null;
|
|
|
|
public string $editName = '';
|
|
|
|
public string $editLocation = '';
|
|
|
|
public function edit(string $uuid): void
|
|
{
|
|
$this->authorize('datacenters.manage');
|
|
$dc = Datacenter::query()->where('uuid', $uuid)->first();
|
|
if ($dc === null) {
|
|
return;
|
|
}
|
|
$this->editingUuid = $uuid;
|
|
$this->editName = $dc->name;
|
|
$this->editLocation = (string) $dc->location;
|
|
}
|
|
|
|
public function cancelEdit(): void
|
|
{
|
|
$this->reset('editingUuid', 'editName', 'editLocation');
|
|
}
|
|
|
|
public function update(): void
|
|
{
|
|
$this->authorize('datacenters.manage');
|
|
if ($this->editingUuid === null) {
|
|
return;
|
|
}
|
|
|
|
$data = $this->validate([
|
|
'editName' => 'required|string|max:255',
|
|
'editLocation' => 'nullable|string|max:255',
|
|
]);
|
|
|
|
Datacenter::query()->where('uuid', $this->editingUuid)->update([
|
|
'name' => $data['editName'],
|
|
'location' => $data['editLocation'] ?: null,
|
|
]);
|
|
|
|
$this->cancelEdit();
|
|
$this->dispatch('notify', message: __('datacenters.updated'));
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$this->authorize('datacenters.manage');
|
|
// Normalize before validating so the unique check matches how the row is
|
|
// stored — otherwise `FSN` passes the rule but the lowercased insert collides.
|
|
$this->code = strtolower(trim($this->code));
|
|
|
|
$data = $this->validate();
|
|
|
|
Datacenter::create([
|
|
'code' => $data['code'],
|
|
'name' => $data['name'],
|
|
'location' => $data['location'] ?: null,
|
|
'active' => true,
|
|
]);
|
|
|
|
$this->reset('code', 'name', 'location');
|
|
$this->dispatch('notify', message: __('datacenters.created'));
|
|
}
|
|
|
|
public function toggle(string $uuid): void
|
|
{
|
|
$this->authorize('datacenters.manage');
|
|
$datacenter = Datacenter::query()->where('uuid', $uuid)->first();
|
|
$datacenter?->update(['active' => ! $datacenter->active]);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.datacenters', [
|
|
'datacenters' => Datacenter::query()->withCount('hosts')->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
}
|