adrema/app/Form/Fields/NamiField.php

128 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2024-02-16 14:18:16 +01:00
<?php
namespace App\Form\Fields;
2024-03-14 23:54:41 +01:00
use App\Form\Data\FieldCollection;
2024-02-16 14:18:16 +01:00
use App\Form\Models\Form;
use App\Form\Models\Participant;
2024-02-20 01:22:08 +01:00
use App\Form\Presenters\NamiPresenter;
use App\Form\Presenters\Presenter;
2024-02-16 14:18:16 +01:00
use App\Member\Member;
use Faker\Generator;
class NamiField extends Field
{
public static function name(): string
{
return 'NaMi-Mitglieder';
}
public static function meta(): array
{
return [];
}
2024-02-16 14:32:16 +01:00
/**
* @return array<string, mixed>
*/
2024-02-16 14:18:16 +01:00
public static function default(): array
{
return [];
}
public static function fake(Generator $faker): array
{
return [];
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationRules(Form $form): array
2024-02-16 14:18:16 +01:00
{
$rules = [$this->key => 'present|array'];
2024-06-19 10:30:07 +02:00
$inputMembers = request($this->key);
2024-02-18 18:48:31 +01:00
2024-06-19 10:30:07 +02:00
foreach ($form->getFields()->noNamiField()->forMembers() as $field) {
foreach ($field->getRegistrationRules($form) as $ruleKey => $rule) {
foreach ($inputMembers as $memberIndex => $inputMember) {
$rules[$this->key . '.' . $memberIndex . '.' . $ruleKey] = !$inputMember['id'] || $field->namiType === null ? $rule : '';
}
2024-02-18 18:48:31 +01:00
}
}
return [
2024-06-18 02:00:36 +02:00
$this->key . '.*.id' => ['nullable', 'numeric', 'exists:members,mitgliedsnr'],
2024-02-18 18:48:31 +01:00
...$rules,
];
2024-02-16 14:18:16 +01:00
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationAttributes(Form $form): array
2024-02-16 14:18:16 +01:00
{
2024-02-18 18:48:31 +01:00
$rules = [];
$inputMembers = request($this->key);
if (!is_array($inputMembers)) {
return [];
}
2024-06-19 10:30:07 +02:00
foreach ($form->getFields()->noNamiField()->forMembers() as $field) {
2024-02-19 02:10:58 +01:00
foreach ($field->getRegistrationRules($form) as $ruleKey => $rule) {
2024-02-18 18:48:31 +01:00
foreach ($inputMembers as $memberIndex => $inputMember) {
2024-02-19 01:46:15 +01:00
$message = $field->name . ' für ein Mitglied';
$rules = array_merge(
$rules,
str($ruleKey)->contains('*')
? collect(request($this->key . '.' . $memberIndex . '.' . $field->key))
->mapWithKeys(fn ($value, $key) => [$this->key . '.' . $memberIndex . '.' . str($ruleKey)->replace('*', $key) => $message])
->toArray()
: [$this->key . '.' . $memberIndex . '.' . $ruleKey => $message]
);
2024-02-18 18:48:31 +01:00
}
}
}
foreach ($inputMembers as $memberIndex => $inputMember) {
$rules[$this->key . '.' . $memberIndex . '.id'] = 'Mitglied Nr ' . $inputMember['id'];
}
return [
$this->key => $this->name,
...$rules,
];
2024-02-16 14:18:16 +01:00
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationMessages(Form $form): array
2024-02-16 14:18:16 +01:00
{
return [];
}
public function afterRegistration(Form $form, Participant $participant, array $input): void
{
2024-02-16 14:23:59 +01:00
foreach ($input[$this->key] as $memberData) {
$member = $memberData['id'] ? Member::firstWhere(['mitgliedsnr' => $memberData['id']]) : null;
2024-02-16 14:18:16 +01:00
$data = [];
2024-03-14 23:54:41 +01:00
foreach (FieldCollection::fromRequest($form, $memberData) as $field) {
$data[$field->key] = $field->namiType === null || $member === null
2024-03-14 23:54:41 +01:00
? $field->value
2024-06-12 00:41:49 +02:00
: $field->namiType->getMemberAttribute($member, $form);
2024-02-16 14:18:16 +01:00
}
$data[$this->key] = [];
2024-07-02 16:04:07 +02:00
$form->participants()->create(['data' => $data, 'member_id' => $member?->id, 'parent_id' => $participant->id]);
2024-02-16 14:18:16 +01:00
}
}
2024-02-20 01:22:08 +01:00
public function getPresenter(): Presenter
{
return app(NamiPresenter::class);
}
2024-02-16 14:18:16 +01:00
}