Files
nutzwerte_fi4/app/Filament/Resources/Projektes/RelationManagers/ProjektInhaltsverzeichnisRelationManager.php
T
2025-10-12 11:51:33 +02:00

173 lines
7.8 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Models\KontoTextblock;
use App\Models\TextblockKennung;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Actions;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
class ProjektInhaltsverzeichnisRelationManager extends RelationManager
{
protected static string $relationship = 'projekt_inhaltsverzeichnis';
protected static ?string $title = 'Inhaltsverzeichnis';
protected static ?string $navigationLabel = 'Inhaltsverzeichnis';
protected static ?string $modelLabel = 'Inhaltsverzeichnis';
protected static ?string $pluralModelLabel = 'Inhaltsverzeichnis';
public function form(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('ordnungszahl')->numeric(),
TextInput::make('name'),
Select::make('textblock_kennung_id')
->label('Textblock Art')
->options(function () {
$relatedOptions = TextblockKennung::all()->pluck('name', 'id')->toArray();
return ['0' => 'ohne Textblock'] + $relatedOptions;
})
->live(),
RichEditor::make('textblock')
->hidden(fn (Get $get): bool => ! $get('textblock_kennung_id'))
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript'],
]),
Fieldset::make('Vorlagen')
->hiddenLabel()
->hidden(fn ($record): bool => ! isset($record) || ! $record['textblock_kennung_id'])
->schema([
Action::make('textblock_sichern')
->schema([
Hidden::make('konto_id')->default(auth()->user()->konto_id),
Hidden::make('namecheck')->default(function ($record) {
return $record['name'];
}),
Hidden::make('textblock_kennung_id')->default(function ($record) {
return $record['textblock_kennung_id'];
}),
RichEditor::make('textblock')
->hidden()
->default(function ($record) {
return $record['textblock'];
}),
TextInput::make('name')->required()
->label('Geben sie dem Textblock einen eindeutigen Namen')
->validationMessages([
'different' => 'Name ist schon in Verwendung bitte verwenden Sie einen eindeutig Namen.',
])
->different('namecheck')
->default(function ($record) {
return $record['name'];
}),
])
->modalSubmitActionLabel('Sichern')
->icon('phosphor-floppy-disk')
->tooltip('Ein veränderter Textblock kann als persönliche Vorlage gespeichert werden.')
->color('info')
->action(function (array $mountedActions, $data) {
$record = $mountedActions[0]->getRawData();
KontoTextblock::updateOrCreate(
[
'konto_id' => auth()->user()->konto_id,
'name' => $data['name'],
'textblock_kennung_id' => $record['textblock_kennung_id'],
], [
'textblock' => RichContentRenderer::make($record['textblock'])->toHtml(),
]);
}),
Actions::make([
Action::make('textblock_laden')
->schema([
Radio::make('konto_textblock_id')
->hiddenLabel()
->options(function ($record) {
return KontoTextblock::where('textblock_kennung_id', $record['textblock_kennung_id'])->where('konto_id', auth()->user()->konto_id)->pluck('name', 'id')->toArray();
})->belowLabel([
'Bitte eine Vorlage auswählen',
]),
])
->icon('phosphor-article')
->modalSubmitActionLabel('Einsetzen')
->color('info')
->tooltip('Personaliserten Texblock laden.')
->action(function (Set $set, array $data) {
$set('textblock', KontoTextblock::query()->where('id', $data['konto_textblock_id'])->pluck('textblock')->first());
}),
]),
])->columns(4),
])->columns(1);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('text')
->columns([
// TextColumn::make('sort'),
TextColumn::make('ordnungszahl')
->getStateUsing(fn ($record): string => $record->ordnungszahl == '0' ? '' : number_format($record->ordnungszahl, 2)),
IconColumn::make('textblock_kennung_id')
->label('Text')
->alignCenter()
->icon('phosphor-article')
->color(fn (string $state): string => match ($state) {
'0' => 'gray',
default => 'info',
}),
TextColumn::make('name')
->description(fn ($record): string => $record->textblock_kennung->name ?? ''),
TextColumn::make('seite')
->alignCenter()
->badge()
->color('info')
->getStateUsing(fn ($record): string => $record->seite == '0' ? '...' : $record->seite),
])
->reorderable('sort')
->filters([
//
])
->headerActions([
CreateAction::make(),
])
->recordActions([
EditAction::make('edit')->iconButton()->color('gray'),
DeleteAction::make()->iconButton(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
}