netgescon-day0/app/Filament/Pages/SuperAdmin/CassettoFiscaleService.php

147 lines
4.9 KiB
PHP
Executable File

<?php
namespace App\Filament\Pages\SuperAdmin;
use App\Models\FeCassettoServiceConfig;
use App\Models\User;
use BackedEnum;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Illuminate\Support\Facades\Auth;
use UnitEnum;
class CassettoFiscaleService extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationLabel = 'Servizio Cassetto Fiscale';
protected static ?string $title = 'Servizio Cassetto Fiscale';
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-key';
protected static UnitEnum|string|null $navigationGroup = 'Super Admin';
protected static ?int $navigationSort = 3;
protected static ?string $slug = 'superadmin/servizio-cassetto-fiscale';
protected string $view = 'filament.pages.super-admin.cassetto-fiscale-service';
public ?array $data = [];
public static function canAccess(): bool
{
$user = Auth::user();
return $user instanceof User && $user->hasRole('super-admin');
}
public function mount(): void
{
$cfg = FeCassettoServiceConfig::query()->first();
$this->getSchema('form')?->fill([
'base_url' => (string) ($cfg?->base_url ?? ''),
// secrets intentionally not prefilled
'password_script' => null,
'username' => null,
'password' => null,
'pin' => null,
]);
}
public function form(Schema $schema): Schema
{
return $schema
->statePath('data')
->components([
Section::make('Parametri servizio (globali)')
->columns(2)
->schema([
TextInput::make('base_url')
->label('URL base (opzionale)')
->helperText('Se vuoto, usa il default di sistema.')
->maxLength(512)
->columnSpanFull(),
TextInput::make('password_script')
->label('passwordScript (licenza)')
->password()
->revealable()
->helperText('Lascia vuoto per non modificare.')
->dehydrated(fn ($state): bool => filled($state))
->maxLength(255)
->columnSpanFull(),
TextInput::make('username')
->label('Username Entratel')
->helperText('Lascia vuoto per non modificare.')
->dehydrated(fn ($state): bool => filled($state))
->maxLength(255),
TextInput::make('password')
->label('Password Entratel')
->password()
->revealable()
->helperText('Lascia vuoto per non modificare.')
->dehydrated(fn ($state): bool => filled($state))
->maxLength(255),
TextInput::make('pin')
->label('PIN Entratel')
->password()
->revealable()
->helperText('Lascia vuoto per non modificare.')
->dehydrated(fn ($state): bool => filled($state))
->maxLength(255),
]),
]);
}
public function submit(): void
{
$state = $this->getSchema('form')?->getState() ?? [];
if (is_array($state['data'] ?? null)) {
$state = $state['data'];
} elseif (is_array($this->data ?? null) && ! empty($this->data)) {
$state = $this->data;
}
/** @var FeCassettoServiceConfig $cfg */
$cfg = FeCassettoServiceConfig::query()->firstOrCreate(['id' => 1], []);
$update = [
'base_url' => trim((string) ($state['base_url'] ?? '')) ?: null,
];
foreach (['password_script', 'username', 'password', 'pin'] as $k) {
if (isset($state[$k]) && filled($state[$k])) {
$update[$k] = (string) $state[$k];
}
}
$cfg->fill($update);
$cfg->save();
Notification::make()
->title('Configurazione salvata')
->success()
->send();
// Clear secrets from local form state
$this->getSchema('form')?->fill([
'base_url' => (string) ($cfg->base_url ?? ''),
'password_script' => null,
'username' => null,
'password' => null,
'pin' => null,
]);
}
}