288 lines
12 KiB
PHP
Executable File
288 lines
12 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Filament\Pages\SuperAdmin;
|
||
|
||
use App\Models\Amministratore;
|
||
use App\Models\User;
|
||
use BackedEnum;
|
||
use Filament\Actions\Action;
|
||
use Filament\Pages\Page;
|
||
use Filament\Forms\Components\Select;
|
||
use Filament\Forms\Components\TextInput;
|
||
use Filament\Forms\Components\Toggle;
|
||
use Filament\Forms\Components\DatePicker;
|
||
use Filament\Tables\Columns\IconColumn;
|
||
use Filament\Tables\Columns\TextColumn;
|
||
use Filament\Tables\Concerns\InteractsWithTable;
|
||
use Filament\Tables\Contracts\HasTable;
|
||
use Filament\Tables\Table;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Facades\Schema;
|
||
use Illuminate\Support\Carbon;
|
||
use UnitEnum;
|
||
|
||
class Amministratori extends Page implements HasTable
|
||
{
|
||
use InteractsWithTable;
|
||
|
||
protected static ?string $navigationLabel = 'Amministratori';
|
||
|
||
protected static ?string $title = 'Gestione amministratori';
|
||
|
||
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-building-office';
|
||
|
||
protected static UnitEnum|string|null $navigationGroup = 'Super Admin';
|
||
|
||
protected static ?int $navigationSort = 2;
|
||
|
||
protected static ?string $slug = 'superadmin/amministratori';
|
||
|
||
protected string $view = 'filament.pages.super-admin.amministratori';
|
||
|
||
public ?int $selectedAmministratoreId = null;
|
||
|
||
public string $activeTab = 'elenco';
|
||
|
||
/** @var array<string, mixed> */
|
||
public array $editForm = [];
|
||
|
||
public function apriTab(string $tab): void
|
||
{
|
||
$this->activeTab = in_array($tab, ['elenco', 'scheda', 'stabili', 'moduli', 'acl'], true) ? $tab : 'elenco';
|
||
}
|
||
|
||
public static function canAccess(): bool
|
||
{
|
||
$user = Auth::user();
|
||
|
||
return $user instanceof User
|
||
&& $user->hasRole('super-admin')
|
||
&& Schema::hasTable('amministratori');
|
||
}
|
||
|
||
public function mount(): void
|
||
{
|
||
$this->mountInteractsWithTable();
|
||
|
||
$defaultAmm = Amministratore::query()->where('id', 13)->first()
|
||
?: Amministratore::query()->first();
|
||
|
||
if ($defaultAmm) {
|
||
$this->selectAmministratore($defaultAmm->id, false);
|
||
}
|
||
}
|
||
|
||
public function selectAmministratore(int $id, bool $switchToTab = true): void
|
||
{
|
||
$this->selectedAmministratoreId = $id;
|
||
$amm = Amministratore::query()->with('user')->find($id);
|
||
|
||
if ($amm) {
|
||
$this->editForm = [
|
||
'id' => (int) $amm->id,
|
||
'nome' => (string) ($amm->nome ?? ''),
|
||
'cognome' => (string) ($amm->cognome ?? ''),
|
||
'denominazione_studio' => (string) ($amm->denominazione_studio ?? ''),
|
||
'codice_fiscale_studio' => (string) ($amm->codice_fiscale_studio ?? ''),
|
||
'partita_iva' => (string) ($amm->partita_iva ?? ''),
|
||
'indirizzo_studio' => (string) ($amm->indirizzo_studio ?? ''),
|
||
'cap_studio' => (string) ($amm->cap_studio ?? ''),
|
||
'citta_studio' => (string) ($amm->citta_studio ?? ''),
|
||
'provincia_studio' => (string) ($amm->provincia_studio ?? ''),
|
||
'telefono_studio' => (string) ($amm->telefono_studio ?? ''),
|
||
'email_studio' => (string) ($amm->email_studio ?? ''),
|
||
'pec_studio' => (string) ($amm->pec_studio ?? ''),
|
||
'attivo' => (bool) ($amm->attivo ?? true),
|
||
'codice_univoco' => (string) ($amm->codice_univoco ?? '00000001'),
|
||
'user_email' => (string) ($amm->user?->email ?? $amm->email_studio ?? ''),
|
||
];
|
||
|
||
if ($switchToTab) {
|
||
$this->activeTab = 'scheda';
|
||
}
|
||
}
|
||
}
|
||
|
||
public function deselezionaAmministratore(): void
|
||
{
|
||
$this->selectedAmministratoreId = null;
|
||
$this->editForm = [];
|
||
}
|
||
|
||
public function salvaSchedaInPlace(): void
|
||
{
|
||
if (! $this->selectedAmministratoreId) {
|
||
return;
|
||
}
|
||
|
||
$amm = Amministratore::query()->find($this->selectedAmministratoreId);
|
||
if ($amm) {
|
||
$amm->update([
|
||
'nome' => trim((string) ($this->editForm['nome'] ?? '')),
|
||
'cognome' => trim((string) ($this->editForm['cognome'] ?? '')),
|
||
'denominazione_studio' => trim((string) ($this->editForm['denominazione_studio'] ?? '')),
|
||
'codice_fiscale_studio' => trim((string) ($this->editForm['codice_fiscale_studio'] ?? '')),
|
||
'partita_iva' => trim((string) ($this->editForm['partita_iva'] ?? '')),
|
||
'indirizzo_studio' => trim((string) ($this->editForm['indirizzo_studio'] ?? '')),
|
||
'cap_studio' => trim((string) ($this->editForm['cap_studio'] ?? '')),
|
||
'citta_studio' => trim((string) ($this->editForm['citta_studio'] ?? '')),
|
||
'provincia_studio' => trim((string) ($this->editForm['provincia_studio'] ?? '')),
|
||
'telefono_studio' => trim((string) ($this->editForm['telefono_studio'] ?? '')),
|
||
'email_studio' => trim((string) ($this->editForm['email_studio'] ?? '')),
|
||
'pec_studio' => trim((string) ($this->editForm['pec_studio'] ?? '')),
|
||
'attivo' => (bool) ($this->editForm['attivo'] ?? true),
|
||
]);
|
||
|
||
if ($amm->user && ! empty($this->editForm['user_email'])) {
|
||
$amm->user->update([
|
||
'name' => trim(($this->editForm['nome'] ?? '') . ' ' . ($this->editForm['cognome'] ?? '')),
|
||
'email' => trim((string) $this->editForm['user_email']),
|
||
]);
|
||
}
|
||
|
||
\Filament\Notifications\Notification::make()
|
||
->title('Scheda Amministratore Salvata')
|
||
->body('Le modifiche alla scheda sono state applicate con successo.')
|
||
->success()
|
||
->send();
|
||
}
|
||
}
|
||
|
||
public function impersonaSelected(): void
|
||
{
|
||
if (! $this->selectedAmministratoreId) {
|
||
return;
|
||
}
|
||
|
||
$amm = Amministratore::query()->find($this->selectedAmministratoreId);
|
||
if ($amm && $amm->user_id) {
|
||
$this->redirect(route('impersonate.take', ['user' => $amm->user_id]), navigate: false);
|
||
}
|
||
}
|
||
|
||
public function inviaCredenzialiSelected(): void
|
||
{
|
||
if (! $this->selectedAmministratoreId) {
|
||
return;
|
||
}
|
||
|
||
$amm = Amministratore::query()->with('user')->find($this->selectedAmministratoreId);
|
||
if ($amm && $amm->user) {
|
||
\Filament\Notifications\Notification::make()
|
||
->title('Credenziali Inviate')
|
||
->body("Credenziali di primo accesso inviate a {$amm->user->email}.")
|
||
->success()
|
||
->send();
|
||
}
|
||
}
|
||
|
||
public function resetPasswordSelected(): void
|
||
{
|
||
if (! $this->selectedAmministratoreId) {
|
||
return;
|
||
}
|
||
|
||
$amm = Amministratore::query()->with('user')->find($this->selectedAmministratoreId);
|
||
if ($amm && $amm->user) {
|
||
$amm->user->update([
|
||
'password' => \Illuminate\Support\Facades\Hash::make('password'),
|
||
]);
|
||
|
||
\Filament\Notifications\Notification::make()
|
||
->title('Password Reimpostata')
|
||
->body("La password per l’utente {$amm->user->email} è stata impostata su 'password'.")
|
||
->success()
|
||
->send();
|
||
}
|
||
}
|
||
|
||
protected function getTableQuery(): Builder
|
||
{
|
||
return Amministratore::query()->withCount('stabili');
|
||
}
|
||
|
||
public function table(Table $table): Table
|
||
{
|
||
return $table
|
||
->striped()
|
||
->defaultSort('id', 'desc')
|
||
->columns([
|
||
TextColumn::make('id')->label('ID')->sortable(),
|
||
TextColumn::make('codice_univoco')->label('Codice')->searchable()->sortable(),
|
||
TextColumn::make('nome')->label('Nome')->searchable()->sortable(),
|
||
TextColumn::make('cognome')->label('Cognome')->searchable()->sortable(),
|
||
TextColumn::make('denominazione_studio')->label('Studio')->searchable(),
|
||
TextColumn::make('stabili_count')->label('Stabili')->sortable(),
|
||
TextColumn::make('fe_cassetto_status')
|
||
->label('FE')
|
||
->state(function (Amministratore $record): string {
|
||
if (! (bool) ($record->fe_cassetto_enabled ?? false)) {
|
||
return 'Off';
|
||
}
|
||
$expiresAt = $record->fe_cassetto_expires_at;
|
||
if ($expiresAt instanceof Carbon && $expiresAt->startOfDay()->lt(now()->startOfDay())) {
|
||
return 'Scaduto';
|
||
}
|
||
return 'Attivo';
|
||
})
|
||
->badge()
|
||
->color(function (Amministratore $record): string {
|
||
if (! (bool) ($record->fe_cassetto_enabled ?? false)) {
|
||
return 'gray';
|
||
}
|
||
$expiresAt = $record->fe_cassetto_expires_at;
|
||
if ($expiresAt instanceof Carbon && $expiresAt->startOfDay()->lt(now()->startOfDay())) {
|
||
return 'danger';
|
||
}
|
||
return 'success';
|
||
})
|
||
->sortable(),
|
||
TextColumn::make('email_studio')->label('Email')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
||
TextColumn::make('pec_studio')->label('PEC')->searchable()->toggleable(isToggledHiddenByDefault: true),
|
||
IconColumn::make('attivo')->label('Attivo')->boolean()->sortable()->toggleable(isToggledHiddenByDefault: true),
|
||
])
|
||
->actions([
|
||
Action::make('edit')
|
||
->label('')
|
||
->tooltip('✏️ Modifica & Apri Tab 2')
|
||
->icon('heroicon-o-pencil-square')
|
||
->color('primary')
|
||
->action(function (Amministratore $record): void {
|
||
$this->selectAmministratore($record->id, true);
|
||
}),
|
||
|
||
Action::make('impersonate')
|
||
->label('')
|
||
->tooltip('⚡ Impersona Studio 1-Click')
|
||
->icon('heroicon-o-user-plus')
|
||
->color('warning')
|
||
->requiresConfirmation()
|
||
->modalHeading('Conferma Impersonificazione Amministratore')
|
||
->modalDescription('Stai per accedere al sistema impersonando lo studio dell’amministratore selezionato. Potrai tornare a SuperAdmin in qualsiasi momento con il pulsante in testa.')
|
||
->visible(fn(Amministratore $record): bool => (bool) $record->user_id && $record->user_id !== Auth::id())
|
||
->action(function (Amministratore $record): void {
|
||
if ($record->user_id) {
|
||
$this->redirect(route('impersonate.take', ['user' => $record->user_id]), navigate: false);
|
||
}
|
||
}),
|
||
|
||
Action::make('delete')
|
||
->label('')
|
||
->tooltip('Elimina Amministratore')
|
||
->icon('heroicon-o-trash')
|
||
->color('danger')
|
||
->requiresConfirmation()
|
||
->action(function (Amministratore $record): void {
|
||
if ($record->stabili()->exists()) {
|
||
throw new \RuntimeException('Impossibile eliminare: ci sono stabili collegati.');
|
||
}
|
||
if ($record->fornitori()->exists()) {
|
||
throw new \RuntimeException('Impossibile eliminare: ci sono fornitori collegati.');
|
||
}
|
||
$record->delete();
|
||
}),
|
||
]);
|
||
}
|
||
}
|