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

218 lines
11 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Actions\Create\CreateProjektInhaltsverzeichnis;
use App\Models\KontoTextblock;
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\Checkbox;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Components\Tabs\Tab;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Support\Enums\FontWeight;
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([
Tabs::make('Tabs')
->tabs([
Tab::make('Überschrift')
->schema([
Textarea::make('name')
->required()
->label('Kapitel / Absatz')
->columnSpan(2),
Checkbox::make('ueberschrift_nicht_anzeigen')
->label('Überschrift nicht angezeigen')
->columnSpan(2),
TextInput::make('ordnungszahl')
->label('Kapitel Index')
->placeholder('3.21')
->numeric()
->default(0)
->belowContent([
'Bei "0" wird der Index im PDF nicht angezeigt',
])
->columnSpan(1),
TextInput::make('seite')
->label('Seitenzahl')
->numeric()
->default(0)
->belowContent([
'Bei "0" wird die Seitenzahl automatisch generiert',
])
->columnSpan(1),
]),
Tab::make('Textblock')
->schema([
Grid::make()
->columns(6)
->schema([
RichEditor::make('textblock')
->extraInputAttributes(['style' => 'min-height: 20rem; max-height: 50vh; overflow-y: auto;'])
->columnSpan(5)
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript'],
]),
Fieldset::make('')
->contained(false)
->schema([
Action::make('textblock_sichern')
->schema([
Hidden::make('konto_id')->default(auth()->user()->konto_id),
RichEditor::make('textblock')
->extraInputAttributes(['style' => 'min-height: 20rem; max-height: 50vh; overflow-y: auto;'])
->hidden()
->default(function ($record) {
return $record['textblock'];
}),
TextInput::make('name')->required()
->label('Geben sie der Vorlage einen eindeutigen Namen')
->default(function ($record) {
return $record['name'];
}),
])
->modalSubmitActionLabel('Sichern')
->iconButton()
->icon('phosphor-floppy-disk')
->tooltip('Als persönliche Vorlage speichern')
->action(function (array $mountedActions, $data) {
$record = $mountedActions[0]->getRawData();
KontoTextblock::updateOrCreate(
[
'konto_id' => auth()->user()->konto_id,
'name' => $data['name'],
], [
'textblock' => RichContentRenderer::make($record['textblock'])->toHtml(),
]);
}),
Action::make('textblock_laden')
->schema([
Select::make('konto_textblock_id')
->hiddenLabel()
->searchable()
->options(function ($record) {
return KontoTextblock::where('konto_id', auth()->user()->konto_id)->pluck('name', 'id')->toArray();
})->belowLabel([
'Bitte eine Vorlage auswählen',
]),
])
->icon('phosphor-article')
->iconButton()
->modalSubmitActionLabel('Einsetzen')
->tooltip('Persönliche Vorlage laden.')
->action(function (Set $set, array $data) {
$set('textblock', KontoTextblock::query()->pluck('textblock')->first());
}),
])
->columns(1),
]),
]),
])->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('text')
->columns([
TextColumn::make('ordnungszahl')
->width('2rem')
->label('Index')
->getStateUsing(fn ($record): string => $record->ordnungszahl == '0' ? '' : number_format($record->ordnungszahl, 2)),
TextColumn::make('name')
->wrap()
->weight(fn ($record): FontWeight => match ($record->ueberschrift_nicht_anzeigen) {
1 => FontWeight::Thin,
default => FontWeight::SemiBold,
})
->description(fn ($record): string => $record->textblock_kennung->name ?? ''),
TextColumn::make('tabellen.name')->wrap()->label('Tabelle'),
TextColumn::make('textblock.name')->wrap(),
IconColumn::make('textblock')
->alignCenter()
->icon('phosphor-article')
->color(fn (string $state): string => match ($state) {
'0' => 'gray',
default => 'info',
}),
TextColumn::make('seite')
->alignCenter()
->badge()
->color('info')
->getStateUsing(function ($record) {
if ($record->ohne_seitenzahl_anzeige) {
return null;
} elseif ($record->seite > 0) {
return (string) $record->seite;
} else {
return (string) 'auto';
}
}),
])
->defaultSort('sort', direction: 'asc')
->reorderable('sort')
->filters([
//
])
->headerActions([
Action::make('generate')
->action(fn (CreateProjektInhaltsverzeichnis $createInhaltsverzeichnis) => $createInhaltsverzeichnis->create($this->ownerRecord))
->requiresConfirmation()
->modalHeading('Inhaltsverzeichnis neu generieren')
->modalDescription('Bestehende Daten werden gelöscht.')
->modalSubmitActionLabel('Ja, neu generieren')
->label('Generieren')
->tooltip('Inhaltsverzeichnis wird neu erstellt.')
->icon('phosphor-gear'),
CreateAction::make()
->icon('phosphor-file-plus')
->createAnother(false),
])
->recordActions([
EditAction::make()->iconButton(),
DeleteAction::make()->iconButton(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
}