161 lines
6.0 KiB
PHP
161 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages\Gescon;
|
|
|
|
use App\Models\Titolo;
|
|
use App\Models\User;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Pages\Page;
|
|
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 UnitEnum;
|
|
|
|
class TitoliArchivio extends Page implements HasTable
|
|
{
|
|
use InteractsWithTable;
|
|
|
|
protected static ?string $navigationLabel = 'Titoli (anagrafica)';
|
|
|
|
protected static ?string $title = 'Titoli anagrafica';
|
|
|
|
protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-tag';
|
|
|
|
protected static UnitEnum|string|null $navigationGroup = 'Impostazioni';
|
|
|
|
protected static ?int $navigationSort = 9;
|
|
|
|
protected static ?string $slug = 'gescon/titoli';
|
|
|
|
protected string $view = 'filament.pages.gescon.table';
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
$user = Auth::user();
|
|
|
|
return $user instanceof User
|
|
&& $user->hasAnyRole(['super-admin', 'admin', 'amministratore', 'collaboratore'])
|
|
&& Schema::hasTable('titoli');
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->mountInteractsWithTable();
|
|
}
|
|
|
|
protected function getTableQuery(): Builder
|
|
{
|
|
return Titolo::query();
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->striped()
|
|
->defaultSort('ordine')
|
|
->columns([
|
|
TextColumn::make('nome')
|
|
->label('Titolo')
|
|
->searchable()
|
|
->sortable(),
|
|
TextColumn::make('sigla')
|
|
->label('Sigla')
|
|
->searchable()
|
|
->sortable(),
|
|
TextColumn::make('ordine')
|
|
->label('Ordine')
|
|
->sortable(),
|
|
IconColumn::make('attivo')
|
|
->label('Attivo')
|
|
->boolean()
|
|
->sortable(),
|
|
TextColumn::make('updated_at')
|
|
->label('Aggiornato')
|
|
->dateTime('d/m/Y H:i')
|
|
->toggleable(isToggledHiddenByDefault: true),
|
|
])
|
|
->headerActions([
|
|
Action::make('create')
|
|
->label('Nuovo titolo')
|
|
->icon('heroicon-o-plus')
|
|
->form([
|
|
TextInput::make('nome')
|
|
->label('Titolo')
|
|
->required()
|
|
->maxLength(50),
|
|
TextInput::make('sigla')
|
|
->label('Sigla')
|
|
->maxLength(20),
|
|
TextInput::make('ordine')
|
|
->label('Ordine')
|
|
->numeric()
|
|
->default(0),
|
|
Toggle::make('attivo')
|
|
->label('Attivo')
|
|
->default(true),
|
|
])
|
|
->action(function (array $data): void {
|
|
Titolo::query()->create([
|
|
'nome' => trim((string) ($data['nome'] ?? '')),
|
|
'sigla' => isset($data['sigla']) && is_string($data['sigla']) && trim($data['sigla']) !== ''
|
|
? trim((string) $data['sigla'])
|
|
: null,
|
|
'ordine' => isset($data['ordine']) && is_numeric($data['ordine']) ? (int) $data['ordine'] : 0,
|
|
'attivo' => (bool) ($data['attivo'] ?? true),
|
|
]);
|
|
}),
|
|
])
|
|
->actions([
|
|
Action::make('edit')
|
|
->label('Modifica')
|
|
->icon('heroicon-o-pencil-square')
|
|
->form([
|
|
TextInput::make('nome')
|
|
->label('Titolo')
|
|
->required()
|
|
->maxLength(50),
|
|
TextInput::make('sigla')
|
|
->label('Sigla')
|
|
->maxLength(20),
|
|
TextInput::make('ordine')
|
|
->label('Ordine')
|
|
->numeric(),
|
|
Toggle::make('attivo')
|
|
->label('Attivo')
|
|
->default(true),
|
|
])
|
|
->fillForm(fn(Titolo $record): array => [
|
|
'nome' => $record->nome,
|
|
'sigla' => $record->sigla,
|
|
'ordine' => $record->ordine,
|
|
'attivo' => (bool) $record->attivo,
|
|
])
|
|
->action(function (Titolo $record, array $data): void {
|
|
$record->fill([
|
|
'nome' => trim((string) ($data['nome'] ?? $record->nome)),
|
|
'sigla' => isset($data['sigla']) && is_string($data['sigla']) && trim($data['sigla']) !== ''
|
|
? trim((string) $data['sigla'])
|
|
: null,
|
|
'ordine' => isset($data['ordine']) && is_numeric($data['ordine']) ? (int) $data['ordine'] : 0,
|
|
'attivo' => (bool) ($data['attivo'] ?? true),
|
|
]);
|
|
$record->save();
|
|
}),
|
|
Action::make('delete')
|
|
->label('Elimina')
|
|
->icon('heroicon-o-trash')
|
|
->color('danger')
|
|
->requiresConfirmation()
|
|
->action(fn(Titolo $record) => $record->delete()),
|
|
]);
|
|
}
|
|
}
|