Files
nutzwerte_fi4/app/Filament/Resources/Projektes/RelationManagers/ProjektInhaltsverzeichnisRelationManager.php
T

234 lines
13 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Actions\Create\CreateProjektInhaltsverzeichnis;
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\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([
TextInput::make('konto_textblock_id')->hidden(),
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('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),
]),
]),
])->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(),
]),
]);
}
}