64 lines
2.1 KiB
PHP
64 lines
2.1 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 = '';
|
|
|
|
// Country picked from config/countries.php (no manual code typos).
|
|
#[Validate('nullable|string|max:2')]
|
|
public string $location = '';
|
|
|
|
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));
|
|
|
|
// Country must be one of the curated list — a forged request can bypass
|
|
// the <select>, so enforce membership server-side (mirrors EditDatacenter).
|
|
$data = $this->validate([
|
|
'code' => 'required|string|max:8|alpha_dash|unique:datacenters,code',
|
|
'name' => 'required|string|max:255',
|
|
'location' => 'nullable|in:'.implode(',', array_keys((array) config('countries'))),
|
|
]);
|
|
|
|
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(),
|
|
'countries' => config('countries'),
|
|
]);
|
|
}
|
|
}
|