adrema/app/Form/Resources/ParticipantResource.php

82 lines
2.9 KiB
PHP
Raw Normal View History

2024-02-08 21:04:00 +01:00
<?php
namespace App\Form\Resources;
use App\Form\Models\Form;
2024-02-09 00:21:33 +01:00
use App\Form\Models\Participant;
2024-04-26 23:20:03 +02:00
use App\Form\Scopes\ParticipantFilterScope;
2024-07-22 20:27:55 +02:00
use App\Prevention\Enums\Prevention;
2024-02-08 21:04:00 +01:00
use Illuminate\Http\Resources\Json\JsonResource;
2024-04-15 16:37:36 +02:00
use Illuminate\Support\Collection;
2024-02-08 21:04:00 +01:00
2024-02-09 00:21:33 +01:00
/**
* @mixin Participant
*/
2024-02-08 21:04:00 +01:00
class ParticipantResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
2024-02-09 00:21:33 +01:00
* @return array<string, mixed>
2024-02-08 21:04:00 +01:00
*/
public function toArray($request)
{
return [
...$this->getModel()->getFields()->present(),
2024-06-21 00:24:27 +02:00
'id' => $this->id,
'created_at' => $this->created_at->format('Y-m-d H:i:s'),
'created_at_display' => $this->created_at->format('d.m.Y'),
'children_count' => $this->children_count,
2024-07-02 18:04:55 +02:00
'member_id' => $this->member_id,
2024-07-22 20:27:55 +02:00
'prevention_items' => $this->member ? Prevention::items($this->member->preventions()) : [],
2024-04-25 21:49:31 +02:00
'links' => [
2024-07-02 18:04:55 +02:00
'assign' => route('participant.assign', ['participant' => $this->getModel()]),
2024-04-25 21:49:31 +02:00
'destroy' => route('participant.destroy', ['participant' => $this->getModel()]),
2024-07-12 19:49:47 +02:00
'children' => route('form.participant.index', ['form' => $this->form, 'parent' => $this->id]),
'fields' => route('participant.fields', ['participant' => $this->getModel()]),
2024-07-12 20:15:24 +02:00
'update' => route('participant.update', ['participant' => $this->getModel()]),
2024-04-25 21:49:31 +02:00
]
];
2024-02-08 21:04:00 +01:00
}
2024-02-09 00:21:33 +01:00
/**
* @return array<string, mixed>
*/
2024-02-08 21:04:00 +01:00
public static function meta(Form $form): array
{
2024-04-15 16:37:36 +02:00
/** @var Collection<int, array<string, mixed>> */
$fieldData = $form->getFields()
->map(fn ($field) => [
'name' => $field->name,
'base_type' => class_basename($field),
'id' => $field->key,
'display_attribute' => $field->getDisplayAttribute(),
]);
2024-04-26 23:20:03 +02:00
$filterData = $form->getFields()
->map(fn ($field) => [
...$field->toArray(),
'base_type' => class_basename($field),
]);
2024-02-08 21:04:00 +01:00
return [
2024-04-26 23:20:03 +02:00
'filter' => ParticipantFilterScope::fromRequest(request()->input('filter', ''))->setForm($form),
2024-07-13 18:24:43 +02:00
'form_config' => $form->config,
2024-04-26 23:20:03 +02:00
'default_filter_value' => ParticipantFilterScope::$nan,
'filters' => $filterData,
'form_meta' => $form->meta,
2024-06-20 23:25:14 +02:00
'has_nami_field' => $form->getFields()->hasNamiField(),
'links' => [
'update_form_meta' => route('form.update-meta', ['form' => $form]),
2024-07-13 18:24:43 +02:00
'store_participant' => route('form.participant.store', ['form' => $form]),
],
2024-04-15 16:37:36 +02:00
'columns' => $fieldData->push([
'name' => 'Registriert am',
'id' => 'created_at',
'display_attribute' => 'created_at_display'
])
2024-02-08 21:04:00 +01:00
];
}
}