Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Modello;
|
||||
use App\Models\Sistema;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class CatalogoController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/sistemi
|
||||
* Sistemi attivi con i rispettivi modelli attivi, nell'ordine del menu.
|
||||
*/
|
||||
public function sistemi(): JsonResponse
|
||||
{
|
||||
$sistemi = Sistema::attivi()
|
||||
->with(['modelli' => fn ($q) => $q->attivi()])
|
||||
->get();
|
||||
|
||||
return response()->json($sistemi->map(fn (Sistema $s) => $this->sistema($s))->values());
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/sistemi/{codice}
|
||||
*/
|
||||
public function sistemaPerCodice(string $codice): JsonResponse
|
||||
{
|
||||
$sistema = Sistema::attivi()
|
||||
->where('codice', $codice)
|
||||
->with(['modelli' => fn ($q) => $q->attivi()])
|
||||
->firstOrFail();
|
||||
|
||||
return response()->json($this->sistema($sistema));
|
||||
}
|
||||
|
||||
private function sistema(Sistema $sistema): array
|
||||
{
|
||||
return [
|
||||
'codice' => $sistema->codice,
|
||||
'nome' => $sistema->nome,
|
||||
'modelli' => $sistema->modelli->map(fn (Modello $m) => $this->modello($m))->values(),
|
||||
];
|
||||
}
|
||||
|
||||
// Stessa forma del vecchio modelli.json (id, nome, cartella, modello, ...) più descrizione e icona.
|
||||
private function modello(Modello $modello): array
|
||||
{
|
||||
return [
|
||||
'id' => $modello->codice,
|
||||
'nome' => $modello->nome,
|
||||
'descrizione' => $modello->descrizione,
|
||||
'icona' => $modello->icona_url,
|
||||
'cartella' => $modello->cartella,
|
||||
'modello' => $modello->file_modello,
|
||||
'categoria' => $modello->categoria,
|
||||
'cartella_texture' => $modello->cartella_texture,
|
||||
'moduli' => $modello->moduli ?? [],
|
||||
'enabled' => true,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class Modello extends Model
|
||||
{
|
||||
protected $table = 'modelli';
|
||||
|
||||
// Categorie usate dal configuratore (es. per scegliere altezza_maniglia_<categoria>.json).
|
||||
public const CATEGORIE = [
|
||||
'finestre' => 'Finestre',
|
||||
'porte' => 'Porte',
|
||||
'scorrevoli' => 'Scorrevoli',
|
||||
'scuretti' => 'Scuretti',
|
||||
'accessori' => 'Accessori',
|
||||
];
|
||||
|
||||
// Moduli dati caricati dal configuratore per il modello.
|
||||
public const MODULI = [
|
||||
'listino' => 'Listino',
|
||||
'modello' => 'Finiture',
|
||||
'maniglia' => 'Maniglia',
|
||||
'altezza_maniglia' => 'Altezza maniglia',
|
||||
'vetro' => 'Vetro',
|
||||
'cerniere' => 'Cerniere',
|
||||
'telaio' => 'Telaio',
|
||||
'avvolgibile' => 'Avvolgibile',
|
||||
'select_finiture' => 'Select finiture',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'sistema_id', 'codice', 'nome', 'descrizione', 'icona', 'ordine', 'attivo',
|
||||
'cartella', 'file_modello', 'categoria', 'cartella_texture', 'moduli',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'ordine' => 'integer',
|
||||
'attivo' => 'boolean',
|
||||
'moduli' => 'array',
|
||||
];
|
||||
|
||||
public function sistema(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Sistema::class);
|
||||
}
|
||||
|
||||
public function scopeAttivi(Builder $query): Builder
|
||||
{
|
||||
return $query->where('attivo', true)->orderBy('ordine')->orderBy('id');
|
||||
}
|
||||
|
||||
public function getIconaUrlAttribute(): ?string
|
||||
{
|
||||
return $this->icona ? Storage::disk('public')->url($this->icona) : null;
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
// Rimuove il file dell'icona sostituita o del modello eliminato.
|
||||
static::updated(function (Modello $modello): void {
|
||||
$vecchia = $modello->getOriginal('icona');
|
||||
if ($modello->wasChanged('icona') && $vecchia) {
|
||||
Storage::disk('public')->delete($vecchia);
|
||||
}
|
||||
});
|
||||
|
||||
static::deleted(function (Modello $modello): void {
|
||||
if ($modello->icona) {
|
||||
Storage::disk('public')->delete($modello->icona);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Sistema extends Model
|
||||
{
|
||||
protected $table = 'sistemi';
|
||||
|
||||
protected $fillable = [
|
||||
'codice', 'nome', 'ordine', 'attivo',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'ordine' => 'integer',
|
||||
'attivo' => 'boolean',
|
||||
];
|
||||
|
||||
public function modelli(): HasMany
|
||||
{
|
||||
return $this->hasMany(Modello::class);
|
||||
}
|
||||
|
||||
public function scopeAttivi(Builder $query): Builder
|
||||
{
|
||||
return $query->where('attivo', true)->orderBy('ordine')->orderBy('id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user