Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-10-19 16:25:15 +02:00
parent 2ed2c0389d
commit a8aa6d9927
50 changed files with 1164 additions and 8 deletions
@@ -0,0 +1,157 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\ModelloResource\Pages;
use App\Models\Modello;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Validation\Rules\Unique;
class ModelloResource extends Resource
{
protected static ?string $model = Modello::class;
protected static ?string $navigationIcon = 'heroicon-o-squares-2x2';
protected static ?string $slug = 'modelli';
protected static ?string $navigationGroup = 'Prodotti';
protected static ?string $navigationLabel = 'Modelli';
protected static ?string $modelLabel = 'modello';
protected static ?string $pluralModelLabel = 'modelli';
protected static ?int $navigationSort = 11;
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Section::make('Anagrafica')
->columns(2)
->schema([
Forms\Components\Select::make('sistema_id')
->label('Sistema')
->relationship('sistema', 'nome')
->required()
->preload()
->live(),
Forms\Components\TextInput::make('codice')
->label('Codice interno')
->helperText('Usato dal configuratore nel parametro ?type=.')
->required()
->maxLength(100)
->alphaDash()
->unique(
ignoreRecord: true,
modifyRuleUsing: fn (Unique $rule, Get $get) => $rule->where('sistema_id', $get('sistema_id')),
),
Forms\Components\TextInput::make('nome')
->label('Nome')
->helperText('Etichetta mostrata nella lista modelli del configuratore.')
->required()
->maxLength(255),
Forms\Components\TextInput::make('descrizione')
->label('Descrizione')
->maxLength(255),
Forms\Components\FileUpload::make('icona')
->label('Icona')
->image()
->disk('public')
->directory('modelli/icone')
->maxSize(2048),
Forms\Components\Grid::make(1)
->columnSpan(1)
->schema([
Forms\Components\TextInput::make('ordine')
->label('Ordine')
->numeric()
->minValue(0)
->default(0),
Forms\Components\Toggle::make('attivo')
->label('Visibile nel configuratore')
->helperText('Richiede la configurazione 3D.')
->default(true)
->live(),
]),
]),
Forms\Components\Section::make('Configurazione 3D')
->description('Asset del configuratore usati per questo modello.')
->columns(2)
->collapsible()
->schema([
Forms\Components\TextInput::make('cartella')
->label('Cartella dati')
->helperText('src/assets/data/modelli/<sistema>/<cartella>/')
->required(fn (Get $get) => (bool) $get('attivo')),
Forms\Components\TextInput::make('file_modello')
->label('File modello 3D')
->helperText('src/assets/models/<sistema>/<file>.glb')
->required(fn (Get $get) => (bool) $get('attivo')),
Forms\Components\Select::make('categoria')
->label('Categoria')
->options(Modello::CATEGORIE)
->required(fn (Get $get) => (bool) $get('attivo')),
Forms\Components\TextInput::make('cartella_texture')
->label('Cartella texture')
->helperText('Se vuota si usa la cartella dati.'),
Forms\Components\CheckboxList::make('moduli')
->label('Moduli')
->options(Modello::MODULI)
->columns(3)
->columnSpanFull(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->defaultSort('ordine')
->reorderable('ordine')
->defaultGroup('sistema.nome')
->columns([
Tables\Columns\ImageColumn::make('icona')->label('Icona')->disk('public')->height(48),
Tables\Columns\TextColumn::make('codice')->label('Codice')->searchable()->copyable(),
Tables\Columns\TextColumn::make('nome')->label('Nome')->searchable(),
Tables\Columns\TextColumn::make('descrizione')->label('Descrizione')->searchable()->wrap()->placeholder('—'),
Tables\Columns\TextColumn::make('categoria')
->label('Categoria')
->badge()
->formatStateUsing(fn (?string $state) => Modello::CATEGORIE[$state] ?? $state)
->placeholder('—'),
Tables\Columns\ToggleColumn::make('attivo')
->label('Attivo')
->disabled(fn (Modello $record) => ! $record->attivo && (! $record->cartella || ! $record->file_modello)),
])
->filters([
Tables\Filters\SelectFilter::make('sistema')->label('Sistema')->relationship('sistema', 'nome'),
Tables\Filters\SelectFilter::make('categoria')->label('Categoria')->options(Modello::CATEGORIE),
Tables\Filters\TernaryFilter::make('attivo')->label('Attivo'),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListModelli::route('/'),
'create' => Pages\CreateModello::route('/create'),
'edit' => Pages\EditModello::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Filament\Resources\ModelloResource\Pages;
use App\Filament\Resources\ModelloResource;
use Filament\Resources\Pages\CreateRecord;
class CreateModello extends CreateRecord
{
protected static string $resource = ModelloResource::class;
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
@@ -0,0 +1,24 @@
<?php
namespace App\Filament\Resources\ModelloResource\Pages;
use App\Filament\Resources\ModelloResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditModello extends EditRecord
{
protected static string $resource = ModelloResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\ModelloResource\Pages;
use App\Filament\Resources\ModelloResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListModelli extends ListRecords
{
protected static string $resource = ModelloResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
@@ -0,0 +1,94 @@
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\SistemaResource\Pages;
use App\Models\Sistema;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
class SistemaResource extends Resource
{
protected static ?string $model = Sistema::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?string $slug = 'sistemi';
protected static ?string $navigationGroup = 'Prodotti';
protected static ?string $navigationLabel = 'Sistemi';
protected static ?string $modelLabel = 'sistema';
protected static ?string $pluralModelLabel = 'sistemi';
protected static ?int $navigationSort = 10;
public static function form(Form $form): Form
{
return $form->schema([
Forms\Components\Section::make()
->columns(2)
->schema([
Forms\Components\TextInput::make('codice')
->label('Codice')
->helperText('Usato dal configuratore nel parametro ?sistema= (es. pvc).')
->required()
->maxLength(50)
->alphaDash()
->unique(ignoreRecord: true),
Forms\Components\TextInput::make('nome')
->label('Nome')
->required()
->maxLength(255),
Forms\Components\TextInput::make('ordine')
->label('Ordine')
->numeric()
->minValue(0)
->default(0),
Forms\Components\Toggle::make('attivo')
->label('Attivo nel configuratore')
->default(true)
->inline(false),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->defaultSort('ordine')
->reorderable('ordine')
->columns([
Tables\Columns\TextColumn::make('codice')->label('Codice')->searchable(),
Tables\Columns\TextColumn::make('nome')->label('Nome')->searchable(),
Tables\Columns\TextColumn::make('modelli_count')->label('Modelli')->counts('modelli'),
Tables\Columns\IconColumn::make('attivo')->label('Attivo')->boolean(),
])
->actions([
Tables\Actions\Action::make('modelli')
->label('Modelli')
->icon('heroicon-o-squares-2x2')
->color('gray')
->url(fn (Sistema $record) => ModelloResource::getUrl('index', [
'tableFilters' => ['sistema' => ['value' => $record->getKey()]],
])),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()
->modalDescription('Verrà eliminato definitivamente il sistema insieme a tutti i suoi modelli.'),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListSistemi::route('/'),
'create' => Pages\CreateSistema::route('/create'),
'edit' => Pages\EditSistema::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\Filament\Resources\SistemaResource\Pages;
use App\Filament\Resources\SistemaResource;
use Filament\Resources\Pages\CreateRecord;
class CreateSistema extends CreateRecord
{
protected static string $resource = SistemaResource::class;
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
@@ -0,0 +1,25 @@
<?php
namespace App\Filament\Resources\SistemaResource\Pages;
use App\Filament\Resources\SistemaResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditSistema extends EditRecord
{
protected static string $resource = SistemaResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make()
->modalDescription('Verrà eliminato definitivamente il sistema insieme a tutti i suoi modelli.'),
];
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\SistemaResource\Pages;
use App\Filament\Resources\SistemaResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListSistemi extends ListRecords
{
protected static string $resource = SistemaResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}