Skip to content

Instantly share code, notes, and snippets.

@mrleblanc101
Last active August 9, 2024 00:09
Show Gist options
  • Select an option

  • Save mrleblanc101/a88deb517a594ed9adfadc1c3fd59d4e to your computer and use it in GitHub Desktop.

Select an option

Save mrleblanc101/a88deb517a594ed9adfadc1c3fd59d4e to your computer and use it in GitHub Desktop.
<x-dynamic-component
:component="$getFieldWrapperView()"
:field="$field"
>
@php
$id = $getId();
$statePath = $getStatePath();
$practicesData = $getPracticesData();
$stateValues = $getStateValues();
$rowKey = 1;
$groupedPractice = collect($practicesData)->sortBy(['category_practice_name', 'name'])->groupBy('category_practice_name');
@endphp
<div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
<x-filament-tables::container>
<x-filament-tables::table>
@foreach($groupedPractice as $groupLabel => $practices)
@foreach($practices as $i => $practice)
<x-filament-tables::row wire:key="{{ $id }}.{{ $rowKey }}" key="{{ $id }}.{{ $rowKey }}">
@if($i === 0)
<x-filament-tables::cell class="py-4 px-3" rowspan="{{ count($practices) }}">{{ $groupLabel }}</x-filament-tables::cell>
@endif
@php
$supStatPath = $statePath . '.' . $practice['id'];
@endphp
<x-filament-tables::cell class="py-4 px-3">{{ $practice['name'] }}</x-filament-tables::cell>
<x-filament-tables::cell class="py-4 px-3">
<x-filament::input.wrapper>
<x-filament::input.select
wire:key="{{ $id }}.{{ $rowKey }}.state"
wire:model="{{ $supStatPath . '.state' }}"
wire:loading.attr="disabled"
>
@foreach ($stateValues as $value => $label)
<option value="{{ $value }}">{{ $label }}</option>
@endforeach
</x-filament::input.select>
</x-filament::input.wrapper>
</x-filament-tables::cell>
<x-filament-tables::cell class="py-4 px-3">
<x-filament::input.wrapper>
<x-filament::input
type="text"
wire:key="{{ $id }}.{{ $rowKey }}.notes"
wire:model="{{ $supStatPath . '.notes' }}"
wire:loading.attr="disabled"
/>
</x-filament::input.wrapper>
</x-filament-tables::cell>
</x-filament-tables::row>
@php
$rowKey++
@endphp
@endforeach
@endforeach
</x-filament-tables::table>
</x-filament-tables::container>
</div>
</x-dynamic-component>
<?php
namespace App\Forms\Components;
use App\Models\DigitalProduct\Practice;
use App\Models\DigitalProduct\Product;
use Filament\Forms\Components\CheckboxList;
class PracticeMatrix extends CheckboxList
{
protected string $view = 'forms.components.practice-matrix';
protected array $practicesData = [];
public function practicesData(array $data): static
{
$this->practicesData = $data;
return $this;
}
public function getPracticesData(): array
{
return $this->evaluate($this->practicesData);
}
public function getStateValues(): array
{
return ['no' => 'Non', 'yes' => 'Oui', 'n/a' => 'N/A'];
}
protected function setUp(): void
{
parent::setUp();
$this->afterStateHydrated(static function ($record, PracticeMatrix $component, $state) {
if ($record) {
$practiceData = self::getPracticeData($record->id);
$component->practicesData($practiceData);
$component->state($practiceData);
}
});
$this->beforeStateDehydrated(static function ($record, PracticeMatrix $component, $state) {
if ($record) {
$practiceData = self::getPracticeData($record->id);
$component->practicesData($practiceData);
$component->state($practiceData);
}
});
}
public static function getPracticeData($id)
{
$allPractices = Practice::all();
$practicesData = [];
if ($id) {
$product = Product::find($id);
$productPractices = $product->practices()->get()->keyBy('id')->toArray();
foreach ($allPractices as $practice) {
$practiceId = $practice->id;
$practicesData[$practiceId] = ['id' => $practiceId, 'name' => $practice->name, 'category_practice_name' => $practice->category_practice ? $practice->category_practice->name : ''];
if (isset($productPractices[$practiceId])) {
foreach ($productPractices[$practiceId]['pivot'] as $fieldname => $value) {
$practicesData[$practiceId][$fieldname] = $value;
}
} else {
foreach ($product->practices()->getPivotColumns() as $fieldname) {
$practicesData[$practiceId][$fieldname] = '';
}
}
}
}
return $practicesData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment