feat: add previous configuration handling for user selections in quotes
- Implemented session storage management for the last configuration choices made by the user. - Added functionality to prompt users when changing models to retain their previous selections. - Introduced methods to apply previous choices to the newly loaded model, including finishes, handles, glass types, and options. - Enhanced user experience by ensuring selections are automatically applied where applicable.
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ModelloResource\RelationManagers;
|
||||
|
||||
use App\Models\Finitura;
|
||||
use Filament\Forms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Finiture del profilo del sistema e, per ciascuna, la scelta del modello: come il
|
||||
* sistema (nessuna riga in finiture_modelli) oppure lato proprio o disattivata.
|
||||
*/
|
||||
class FinitureRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'finitureDelSistema';
|
||||
|
||||
protected static ?string $title = 'Finiture';
|
||||
|
||||
protected static ?string $modelLabel = 'finitura';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'finiture';
|
||||
|
||||
/** Scelte del modello per finitura_id, lette una volta per richiesta. */
|
||||
protected ?\Illuminate\Support\Collection $scelte = null;
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->description('Per ogni finitura del profilo scegli se nel modello vale l\'impostazione del sistema (lato e attivazione in "Finiture e maniglie") o una propria: un lato diverso, oppure disattivata. La scelta del modello può anche attivare una finitura disattiva nel sistema.')
|
||||
->defaultSort('ordine')
|
||||
->defaultPaginationPageOption(50)
|
||||
->columns([
|
||||
Tables\Columns\ImageColumn::make('anteprima')->label('Anteprima')->disk('public')->height(36),
|
||||
Tables\Columns\TextColumn::make('descrizione')
|
||||
->label('Finitura')
|
||||
->description(fn (Finitura $record) => $record->codice)
|
||||
->searchable(['descrizione', 'codice']),
|
||||
Tables\Columns\TextColumn::make('sistema')
|
||||
->label('Nel sistema')
|
||||
->state(fn (Finitura $record) => $record->attivo ? Finitura::LATI[$record->lato] : 'Disattiva')
|
||||
->color('gray'),
|
||||
Tables\Columns\SelectColumn::make('scelta_modello')
|
||||
->label('In questo modello')
|
||||
->options(Finitura::LATI_MODELLO)
|
||||
->placeholder('Come il sistema')
|
||||
->getStateUsing(fn (Finitura $record) => $this->scelte()->get($record->id))
|
||||
->updateStateUsing(function (Finitura $record, ?string $state) {
|
||||
$this->salvaScelta([$record->id], $state);
|
||||
|
||||
return $state;
|
||||
}),
|
||||
Tables\Columns\TextColumn::make('effettiva')
|
||||
->label('Risultato')
|
||||
->state(function (Finitura $record) {
|
||||
$lato = $this->scelte()->get($record->id) ?? ($record->attivo ? $record->lato : 'nessuno');
|
||||
|
||||
return $lato === 'nessuno' ? 'Non disponibile' : Finitura::LATI[$lato];
|
||||
})
|
||||
->badge()
|
||||
->color(fn (string $state) => $state === 'Non disponibile' ? 'danger' : 'success'),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\TernaryFilter::make('personalizzata')
|
||||
->label('Scelta del modello')
|
||||
->placeholder('Tutte')
|
||||
->trueLabel('Personalizzate')
|
||||
->falseLabel('Come il sistema')
|
||||
->queries(
|
||||
true: fn ($query) => $query->whereIn('id', $this->scelte()->keys()),
|
||||
false: fn ($query) => $query->whereNotIn('id', $this->scelte()->keys()),
|
||||
),
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\Action::make('ripristina')
|
||||
->label('Tutte come il sistema')
|
||||
->icon('heroicon-o-arrow-uturn-left')
|
||||
->color('gray')
|
||||
->requiresConfirmation()
|
||||
->modalDescription('Elimina le scelte del modello: per tutte le finiture torna a valere l\'impostazione del sistema.')
|
||||
->visible(fn () => $this->scelte()->isNotEmpty())
|
||||
->action(function () {
|
||||
DB::table('finiture_modelli')->where('modello_id', $this->getOwnerRecord()->getKey())->delete();
|
||||
$this->scelte = null;
|
||||
|
||||
Notification::make()->title('Finiture ripristinate come nel sistema')->success()->send();
|
||||
}),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkAction::make('imposta')
|
||||
->label('Imposta nel modello')
|
||||
->icon('heroicon-o-adjustments-horizontal')
|
||||
->form([
|
||||
Forms\Components\Select::make('lato')
|
||||
->label('In questo modello')
|
||||
->options(['sistema' => 'Come il sistema'] + Finitura::LATI_MODELLO)
|
||||
->required(),
|
||||
])
|
||||
->action(fn (Collection $records, array $data) => $this->salvaScelta(
|
||||
$records->modelKeys(),
|
||||
$data['lato'] === 'sistema' ? null : $data['lato'],
|
||||
))
|
||||
->deselectRecordsAfterCompletion(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function isReadOnly(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
private function scelte(): \Illuminate\Support\Collection
|
||||
{
|
||||
return $this->scelte ??= DB::table('finiture_modelli')
|
||||
->where('modello_id', $this->getOwnerRecord()->getKey())
|
||||
->pluck('lato', 'finitura_id');
|
||||
}
|
||||
|
||||
/** null = come il sistema (rimuove la scelta del modello). */
|
||||
private function salvaScelta(array $finiture, ?string $lato): void
|
||||
{
|
||||
$modello = $this->getOwnerRecord()->getKey();
|
||||
$righe = DB::table('finiture_modelli')->where('modello_id', $modello)->whereIn('finitura_id', $finiture);
|
||||
|
||||
if ($lato === null || $lato === '') {
|
||||
$righe->delete();
|
||||
} else {
|
||||
abort_unless(array_key_exists($lato, Finitura::LATI_MODELLO), 422);
|
||||
DB::table('finiture_modelli')->upsert(
|
||||
array_map(fn ($id) => ['modello_id' => $modello, 'finitura_id' => $id, 'lato' => $lato, 'created_at' => now(), 'updated_at' => now()], $finiture),
|
||||
['modello_id', 'finitura_id'],
|
||||
['lato', 'updated_at'],
|
||||
);
|
||||
}
|
||||
|
||||
$this->scelte = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user