adrema/app/Form/Resources/ParticipantResource.php

74 lines
2.3 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-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-04-25 21:49:31 +02:00
'links' => [
'destroy' => route('participant.destroy', ['participant' => $this->getModel()]),
2024-06-21 00:25:14 +02:00
'children' => route('form.participant.index', ['form' => $this->form, 'parent' => $this->id])
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),
'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-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
];
}
}