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

583 lines
30 KiB
PHP

<?php
namespace App\Filament\Resources\Projektes\RelationManagers;
use App\Actions\Create\CreateOrientierungZuAbschlaege;
use App\Models\Merkmale;
use App\Models\Orientierung;
use App\Models\OrientierungZuAbschlaege;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Support\Enums\FontWeight;
use Filament\Support\Enums\Width;
use Filament\Tables\Columns\CheckboxColumn;
use Filament\Tables\Columns\SelectColumn;
use Filament\Tables\Columns\Summarizers\Sum;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;
use Livewire\Component;
class OrientierungZuAbschlaegesRelationManager extends RelationManager
{
protected static string $relationship = 'orientierung_zu_abschlaeges';
protected static ?string $title = 'Orientierung Zu/Abschläge';
protected static ?string $navigationLabel = 'Orientierung Zu/Abschläge';
protected static ?string $modelLabel = 'Orientierung Zu/Abschläge';
protected static ?string $pluralModelLabel = 'Orientierung Zu/Abschläge';
public function table(Table $table): Table
{
$spalten = Merkmale::map($this->ownerRecord->id);
return $table
// ->striped()
->recordTitleAttribute('top_nummer')
->description('Objekte "NiO" (Nicht in Orientierung) sind auch ohne "Type"')
->columns([
TextColumn::make('top_nummer')
->alignCenter()
->label('Top Nummer')
->searchable()
->extraHeaderAttributes([
'style' => 'position: sticky; left: 0; z-index: 1;',
'class' => 'bg-gray-50 dark:bg-gray-900',
])
->extraCellAttributes([
'style' => 'position: sticky; left: 0; z-index: 1;',
'class' => 'bg-gray-50 dark:bg-gray-900',
]),
CheckboxColumn::make('nicht_in_orientierung')
->label('NiO')
->toggleable()
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextColumn::make('raumart')
->toggleable()
->width('2rem')
->searchable(),
TextInputColumn::make('type')
->toggleable()
->disabled(fn ($record): bool => $record->nicht_in_orientierung)
->width('2rem')
->searchable(),
SelectColumn::make('orientierung_id')
->toggleable()
->width('2rem')
->label('Orientierung')
->selectablePlaceholder(true)
->options(Orientierung::pluck('name', 'id'))
->disableOptionWhen(fn (string $value): bool => in_array($value, [2, 11, 24, 33, 36]))
->preloadOptions()->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('a')->label($spalten['a'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['a']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('a', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('b')->label($spalten['b'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['b']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('b', $state, $record);
})->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('c')->label($spalten['c'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['c']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('c', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('d')->label($spalten['d'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['d']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('d', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('e')->label($spalten['e'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['e']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('e', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('f')->label($spalten['f'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['f']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('f', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('g')->label($spalten['g'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['g']) ? false : true)
->toggleable()
->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('g', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('h')->label($spalten['h'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['h']) ? false : true)
->toggleable()->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('h', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('i')->label($spalten['i'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['i']) ? false : true)
->toggleable()->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('i', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextInputColumn::make('j')->label($spalten['j'] ?? '')
->extraInputAttributes(['onClick' => 'this.select()'])
->hidden(isset($spalten['j']) ? false : true)
->toggleable()->rules(['numeric'])
->alignCenter()
->beforeStateUpdated(function ($record, $state) {
$record['nutzwert'] = $this->sum_nutzwerte('j', $state, $record);
})
->afterStateUpdated(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
TextColumn::make('nutzwert')
->extraHeaderAttributes([
'style' => 'position: sticky; right: 0; z-index: 1;',
'class' => 'bg-gray-50 dark:bg-gray-900',
])
->extraCellAttributes([
'style' => 'position: sticky; right: 0; z-index: 1;',
'class' => 'bg-gray-50 dark:bg-gray-900',
])
->summarize(Sum::make()->label('Gesammt'))
->alignCenter()
->weight(FontWeight::ExtraBold),
])
->filters([
TernaryFilter::make('nicht_in_orientierung')
->label('Objekte "Nicht in Orientierung" anzeigen'),
])
->headerActions([
Action::make('merkmale')
->icon('phosphor-pen')
->modalCancelAction(fn (Action $action) => $action->label('Schließen'))
->modalSubmitAction(false)
->modalHeading('Merkmale')
->slideOver()
// ->modalWidth(Width::Medium)
->modalContent(view('filament.pages.actions.merkmale'))
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
Action::make('erstellen')
->action(fn (CreateOrientierungZuAbschlaege $createOrientierungZuAbschlaege) => $createOrientierungZuAbschlaege->create($this->ownerRecord))
->requiresConfirmation()
->modalHeading('Orientierung Zu/Abschläge erstellen')
->modalDescription('Alle bestehenden Daten in dieser Tabelle werden vorher gelöscht.')
->modalSubmitActionLabel('Erstellen')
->label('Erstellen')
->tooltip('Daten werden aus "Orientierung Zu/Abschläge" Tabelle gefiltert.')
->icon('heroicon-o-cog-8-tooth')
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
])
->toolbarActions([
BulkActionGroup::make([
BulkAction::make('raumart')
->color('primary')
->schema([
TextInput::make('raumart')
->label('Name eingeben')
->prefixIcon('phosphor-house-simple'),
])
->icon('phosphor-house-simple')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
['raumart' => $data['raumart']]
);
}
})
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('type')
->color('primary')
->schema([
TextInput::make('type')
->label('Name eingeben')
->prefixIcon('phosphor-align-center-horizontal-simple'),
])
->icon('phosphor-align-center-horizontal-simple')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
OrientierungZuAbschlaege::where('id', '=', $record->id)
->where('nicht_in_orientierung', '=', false)
->update(
['type' => $data['type']]
);
}
})
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('methode')
->color('primary')
->label('Orientierung')
->schema([
Select::make('methode_id')
->label('Orientierung wählen')
->prefixIcon('phosphor-compass')
->options(Orientierung::pluck('name', 'id'))
->disableOptionWhen(fn (string $value): bool => in_array($value, [2, 11, 24, 33, 36]))
->required(),
])
->icon('phosphor-compass')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
['orientierung_id' => $data['methode_id']]
);
}
})
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('nicht_in_orientierung')
->color('primary')
->label('Nicht In Orientierung')
->schema([
Radio::make('nicht_in_orientierung')
->hiddenLabel()
->default(1)
->options([
'1' => 'Ja',
'0' => 'Nein',
]),
])
->icon('phosphor-check-square')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
['nicht_in_orientierung' => 1]
);
}
})
->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('a')
->label($spalten['a'] ?? '')
->color('info')
->hidden(isset($spalten['a']) ? false : true)
->schema([
TextInput::make('a')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('a', $data['a'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'a' => $data['a'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('b')
->label($spalten['b'] ?? '')
->color('info')
->hidden(isset($spalten['b']) ? false : true)
->schema([
TextInput::make('b')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('b', $data['b'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'b' => $data['b'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('c')
->label($spalten['c'] ?? '')
->color('info')
->hidden(isset($spalten['c']) ? false : true)
->schema([
TextInput::make('c')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('c', $data['c'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'c' => $data['c'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('d')
->label($spalten['d'] ?? '')
->color('info')
->hidden(isset($spalten['d']) ? false : true)
->schema([
TextInput::make('d')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('d', $data['d'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'd' => $data['d'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('e')
->label($spalten['e'] ?? '')
->color('info')
->hidden(isset($spalten['e']) ? false : true)
->schema([
TextInput::make('e')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('e', $data['e'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'e' => $data['e'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('f')
->label($spalten['f'] ?? '')
->color('info')
->hidden(isset($spalten['f']) ? false : true)
->schema([
TextInput::make('f')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('f', $data['f'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'f' => $data['f'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('g')
->label($spalten['g'] ?? '')
->color('info')
->hidden(isset($spalten['g']) ? false : true)
->schema([
TextInput::make('g')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('g', $data['g'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'g' => $data['g'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('h')
->label($spalten['h'] ?? '')
->color('info')
->hidden(isset($spalten['h']) ? false : true)
->schema([
TextInput::make('h')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('h', $data['h'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'h' => $data['h'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('i')
->label($spalten['i'] ?? '')
->color('info')
->hidden(isset($spalten['i']) ? false : true)
->schema([
TextInput::make('i')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('i', $data['i'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'i' => $data['i'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
BulkAction::make('j')
->label($spalten['j'] ?? '')
->color('info')
->hidden(isset($spalten['j']) ? false : true)
->schema([
TextInput::make('j')
->prefixIcon('phosphor-math-operations'),
])
->icon('phosphor-math-operations')
->action(function (Collection $records, array $data): void {
foreach ($records as $record) {
$nutzwert = $this->sum_nutzwerte('j', $data['j'], $record);
OrientierungZuAbschlaege::where('id', '=', $record->id)
->update(
[
'j' => $data['j'],
'nutzwert' => $nutzwert,
]
);
}
})->after(function (Component $livewire) {
$livewire->dispatch('refreshOrientierung');
}),
]),
]);
}
public function sum_nutzwerte($char, $state, $record)
{
$range = range('a', 'j');
$sum = 0;
foreach ($range as $val) {
if ($val == $char) {
$sum += $state;
} else {
$sum += $record[$val];
}
}
return round($sum);
}
}