Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Filament/Shelter/Resources/BeneficiaryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enums\Gender;
use App\Filament\Shelter\Resources\BeneficiaryResource\Pages;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryDynamicForm;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryDynamicInfolist;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryForm;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryInfolist;
Expand Down Expand Up @@ -57,6 +58,8 @@ public static function form(Form $form): Form
Forms\Components\Section::make()
->columns(2)
->schema(BeneficiaryForm::getSchema()),

...BeneficiaryDynamicForm::getSchema(),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Enums\Form\Type;
use App\Filament\Concerns\UsesBreadcrumbFromTitle;
use App\Filament\Shelter\Resources\BeneficiaryResource;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryDynamicForm;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\BeneficiaryForm;
use App\Filament\Shelter\Resources\BeneficiaryResource\Schemas\StayForm;
use App\Models\Beneficiary;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function getSteps(): array
Grid::make(3)
->schema(BeneficiaryForm::getSchema()),

...Form::render(Type::PERSONAL),
...BeneficiaryDynamicForm::getSchema(),
]),

Step::make(__('app.beneficiary.steps.stay'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,75 @@

namespace App\Filament\Shelter\Resources\BeneficiaryResource\Pages;

use App\Enums\Form\Type;
use App\Filament\Shelter\Resources\BeneficiaryResource;
use App\Models\Form;
use App\Models\Form\FieldResponse;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditBeneficiary extends EditRecord
{
protected static string $resource = BeneficiaryResource::class;

protected ?bool $hasDatabaseTransactions = true;

protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
];
}

protected function mutateFormDataBeforeFill(array $data): array
{
$data['form'] = $this->getRecord()->latestPersonal
?->fields
->pluck('value', 'field_id')
->all();

return $data;
}

protected function mutateFormDataBeforeSave(array $data): array
{
$response = $this->getRecord()
->latestPersonal()
->firstOr(function () {
$form = Form::query()
->latestPublished(Type::PERSONAL)
->first(['id']);

if (blank($form)) {
return;
}

return $this->getRecord()->personal()->create([
'form_id' => $form->id,
]);
});

$this->wrapInDatabaseTransaction(
fn () => collect(data_get($data, 'form'))->each(
fn ($value, int $field_id) => FieldResponse::updateOrCreate(
[
'response_id' => $response->id,
'field_id' => $field_id,
],
[
'value' => $value,
]
)
)
);

return $data;
}

protected function getRedirectUrl(): ?string
{
return static::getResource()::getUrl('view', [
'record' => $this->getRecord(),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Filament\Shelter\Resources\BeneficiaryResource\Schemas;

use App\Enums\Form\Type;
use App\Models\Form;

class BeneficiaryDynamicForm
{
public static function getSchema(): array
{
return Form::render(Type::PERSONAL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Enums\Form\FieldType;
use App\Models\Form\Field;
use App\Models\Form\Response;
use Carbon\Carbon;
use Filament\Infolists\Components\Group;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
Expand Down Expand Up @@ -35,8 +36,8 @@ public static function getSchema(): array
->description($section->description)
->columns(3)
->collapsible()
->schema(function () use ($section, &$fieldIndex) {
return $section->fields->map(function (Field $field) use (&$fieldIndex) {
->schema(
$section->fields->map(function (Field $field) use (&$fieldIndex) {
$component = TextEntry::make("fields.{$fieldIndex}.value")
->label($field->label);

Expand All @@ -54,13 +55,15 @@ public static function getSchema(): array
break;

case FieldType::DATE:
$component->date();
$component->formatStateUsing(function ($state) {
return rescue(fn () => Carbon::parse($state)->toFormattedDate(), $state, false);
});
break;
}

return $component;
})->all();
});
})->all()
);
})->all();
}),
];
Expand Down
1 change: 1 addition & 0 deletions app/Models/Form/FieldResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FieldResponse extends Model
protected $table = 'form_field_responses';

protected $fillable = [
'response_id',
'field_id',
'value',
];
Expand Down
Loading