Files
nutzwerte_fi4/app/Filament/Resources/Projektes/RelationManagers/ProjektInhaltsverzeichnisRelationManager.php
T
2026-05-11 14:21:27 +02:00

388 lines
24 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Actions\Create\CreateProjektInhaltsverzeichnis;
use App\Actions\Helper\ShowHelpInhalt;
use App\Models\Inhaltselement;
use App\Models\KontoTextblock;
use App\Models\ProjektInhaltsverzeichnis;
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\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\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\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\SelectFilter;
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([
Checkbox::make('neue_seite')
->label('Start neuer Seite'),
Checkbox::make('ueberschrift_verbergen')
->label('Überschrift im Gutachten verbergen'),
TextInput::make('name')
->prefixIcon('phosphor-article')
->afterLabel([ShowHelpInhalt::show()])
->required()
->label('Überschrift')
->columnSpan(2),
TextInput::make('ordnungszahl')
->label('Kapitel Index')
->prefixIcon('phosphor-list-numbers')
->placeholder('3.21')
->numeric()
->default(0)
->belowContent([
'Bei "0" wird der Index nicht angezeigt',
])
->columnSpan(2),
// TextInput::make('seite')
// ->label('Seitenzahl')
// ->numeric()
// ->default(0)
// ->belowContent([
// 'Bei "0" wird die Seitenzahl automatisch generiert',
// ])
// ->columnSpan(1),
]),
Tab::make('Textblock A')
->schema([
Grid::make()
->columns(6)
->schema([
// TextInput::make('textblock_name')
// ->columnSpan(5)
// ->label('Textblock Name'),
TextInput::make('konto_textblock_id')->hidden(),
RichEditor::make('textblock')
->hiddenLabel()
->aboveLabel(fn ($record): string => $record?->inhaltselement_id != 0 ? 'Ausgabe vor Inhaltselement' : '')
->columnSpan(5)
->extraInputAttributes(['style' => 'min-height: 20rem; max-height: 50vh; overflow-y: auto;'])
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript', 'horizontalRule', 'table'],
])->floatingToolbars([
'paragraph' => [
'bold', 'italic', 'underline', 'strike', 'subscript', 'superscript',
],
'heading' => [
'h1', 'h2', 'h3',
],
'table' => [
'tableAddColumnBefore', 'tableAddColumnAfter', 'tableDeleteColumn',
'tableAddRowBefore', 'tableAddRowAfter', 'tableDeleteRow',
'tableMergeCells', 'tableSplitCell',
'tableToggleHeaderRow',
'tableDelete',
],
]),
Fieldset::make('')
->contained(false)
->schema([
Action::make('sichern')
->iconButton()
->tooltip('Änderungen in der Vorlage speichern')
->icon('phosphor-floppy-disk')
->schema([
TextInput::make('name')->required()
->label('Ändern sie gegebeben Falls den Namen')
->default(function ($record) {
return KontoTextblock::where('id', $record?->konto_textblock_id)->pluck('name')->first();
}),
])
->hidden(function ($record) {
return (bool) ! $record?->konto_textblock_id;
})
->action(function (array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
KontoTextblock::where('id', $record['konto_textblock_id'])
->update(['name' => $data['name'], 'textblock' => RichContentRenderer::make($record['textblock'])->toHtml()]);
}),
Action::make('neu')
->iconButton()
->icon('phosphor-file-plus')
->schema([
TextInput::make('name')->required()
->label('Geben sie der Vorlage einen eindeutigen Namen')
->default(function ($record) {
return $record?->name;
}),
])
->modalSubmitActionLabel('Sichern')
->tooltip('Neue Vorlage erstellen')
->action(function (array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
$block = new KontoTextblock;
$block->konto_id = auth()->user()->konto_id;
$block->name = $data['name'];
$block->textblock = RichContentRenderer::make($record['textblock'])->toHtml();
$block->save();
ProjektInhaltsverzeichnis::where('id', $record['id'])->update(['konto_textblock_id' => $block->id]);
}),
Action::make('laden')
->iconButton()
->icon('phosphor-article')
->schema([
Select::make('konto_textblock_id')
->hiddenLabel()
->searchable()
->options(KontoTextblock::where('konto_id', auth()->user()->konto_id)->pluck('name', 'id'))
->belowLabel([
'Bitte eine Vorlage auswählen',
]),
])
->modalSubmitActionLabel('Einsetzen')
->tooltip('Vorlage laden.')
->action(function (Set $set, array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
ProjektInhaltsverzeichnis::where('id', $record['id'])->update(['konto_textblock_id' => $data['konto_textblock_id']]);
$set('konto_textblock_id', $data['konto_textblock_id']);
$set('textblock', KontoTextblock::where('id', $data['konto_textblock_id'])->pluck('textblock')->first());
}),
])->columns(1),
]),
]),
Tab::make('Inhaltselement')
->schema([
Select::make('inhaltselement_id')->label('Inhaltselement')
->prefixIcon('phosphor-table')
->default(0)
->options(function () {
$res = Inhaltselement::all()->pluck('name', 'id')->toArray();
array_push($res, 'Ohne');
return $res;
}),
]),
Tab::make('Textblock B')
->schema([
Grid::make()
->columns(6)
->schema([
TextInput::make('konto_textblock_id')->hidden(),
RichEditor::make('textblock2')
->hiddenLabel()
->aboveLabel(fn ($record): string => $record?->inhaltselement_id != 0 ? 'Ausgabe nach Inhaltselement' : '')
->columnSpan(5)
->extraInputAttributes(['style' => 'min-height: 20rem; max-height: 50vh; overflow-y: auto;'])
->toolbarButtons([
['bold', 'italic', 'underline', 'strike', 'subscript', 'superscript', 'horizontalRule', 'table'],
])->floatingToolbars([
'paragraph' => [
'bold', 'italic', 'underline', 'strike', 'subscript', 'superscript',
],
'heading' => [
'h1', 'h2', 'h3',
],
'table' => [
'tableAddColumnBefore', 'tableAddColumnAfter', 'tableDeleteColumn',
'tableAddRowBefore', 'tableAddRowAfter', 'tableDeleteRow',
'tableMergeCells', 'tableSplitCell',
'tableToggleHeaderRow',
'tableDelete',
],
]),
Fieldset::make('')
->contained(false)
->schema([
Action::make('sichern')
->iconButton()
->tooltip('Änderungen in der Vorlage speichern')
->icon('phosphor-floppy-disk')
->schema([
TextInput::make('name')->required()
->label('Ändern sie gegebeben Falls den Namen')
->default(function ($record) {
return KontoTextblock::where('id', $record?->konto_textblock_id)->pluck('name')->first();
}),
])
->hidden(function ($record) {
return (bool) ! $record?->konto_textblock_id;
})
->action(function (array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
KontoTextblock::where('id', $record['konto_textblock_id'])
->update(['name' => $data['name'], 'textblock' => RichContentRenderer::make($record['textblock'])->toHtml()]);
}),
Action::make('neu')
->iconButton()
->icon('phosphor-file-plus')
->schema([
TextInput::make('name')->required()
->label('Geben sie der Vorlage einen eindeutigen Namen')
->default(function ($record) {
return $record?->name;
}),
])
->modalSubmitActionLabel('Sichern')
->tooltip('Neue Vorlage erstellen')
->action(function (array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
$block = new KontoTextblock;
$block->konto_id = auth()->user()->konto_id;
$block->name = $data['name'];
$block->textblock = RichContentRenderer::make($record['textblock'])->toHtml();
$block->save();
ProjektInhaltsverzeichnis::where('id', $record['id'])->update(['konto_textblock_id' => $block->id]);
}),
Action::make('laden')
->iconButton()
->icon('phosphor-article')
->schema([
Select::make('konto_textblock_id')
->hiddenLabel()
->searchable()
->options(KontoTextblock::where('konto_id', auth()->user()->konto_id)->pluck('name', 'id'))
->belowLabel([
'Bitte eine Vorlage auswählen',
]),
])
->modalSubmitActionLabel('Einsetzen')
->tooltip('Vorlage laden.')
->action(function (Set $set, array $mountedActions, array $data) {
$record = $mountedActions[0]->getRawData();
ProjektInhaltsverzeichnis::where('id', $record['id'])->update(['konto_textblock_id' => $data['konto_textblock_id']]);
$set('konto_textblock_id', $data['konto_textblock_id']);
$set('textblock', KontoTextblock::where('id', $data['konto_textblock_id'])->pluck('textblock')->first());
}),
])->columns(1),
]),
]),
])->vertical()->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->emptyStateHeading('Kein Inhaltsverzeichnis')
->emptyStateDescription('Generiere ein Inhaltsverzeichnis um zu beginnen.')
->recordTitleAttribute('text')
// ->recordClasses(fn ($record) => $record->part == 'gutachten' ? 'border-l border-primary-500' : 'border-l border-success-500')
->columns([
TextColumn::make('ordnungszahl')
->label('Index')
->alignRight()
->width('2rem')
->label('Index')
->getStateUsing(fn ($record): string => $record->ordnungszahl == '0' ? '' : number_format($record->ordnungszahl, 2)),
IconColumn::make('neue_seite')
->tooltip('Start neue Seite')
->alignCenter()
->label('Start')
->color('primary')
->icon(fn (string $state): string => match ($state) {
'1' => 'phosphor-file-plus',
default => ''
}),
TextColumn::make('name')
->color('primary')
->wrap()
->searchable(),
TextColumn::make('textblock_name')
->label('Textblock')
->wrap(),
TextColumn::make('inhaltselement.name')
->label('Inhalts Element'),
TextInputColumn::make('seite')
->extraInputAttributes(['onClick' => 'this.select()'])
->width('2rem')
->alignCenter()
->tooltip('Bei 0 wird keine Seitenzahl ausgegeben.')
->getStateUsing(fn ($record): string => $record->seite == 0 ? '' : $record->seite),
])
// ->defaultPaginationPageOption(5)
->persistFiltersInSession()
->deferFilters(false)
->filtersFormColumns(3)
->filters([
SelectFilter::make('part')
->selectablePlaceholder(false)
->default('standard')
->label('Gutachten Komponenten')
->options([
'standard' => 'Gutachten Standard Texte',
'nutzwerte' => 'Nutzwerberechnung',
'paragraph' => 'Auszug Gutachten gem. §6',
]),
], FiltersLayout::AboveContent)
->defaultSort('sort', direction: 'asc')
->reorderable('sort')
->headerActions([
Action::make('generate')
->action(fn (CreateProjektInhaltsverzeichnis $createInhaltsverzeichnis) => $createInhaltsverzeichnis->create($this->ownerRecord))
->hidden(fn (): bool => $this->ownerRecord->bezahlt_am ? false : true)
->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()
->hidden(fn (): bool => $this->ownerRecord->bezahlt_am ? false : true)
->icon('phosphor-file-plus')
->createAnother(false),
])
->recordActions([
EditAction::make()->iconButton()->hidden(fn ($record) => (bool) $record->no_edit),
DeleteAction::make()->iconButton(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
}