adrema/app/Form/Fields/NamiField.php

132 lines
3.6 KiB
PHP
Raw 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-02-18 18:48:31 +01:00
2024-03-07 00:58:14 +01:00
$c = $form->getFields()->forMembers()->noNamiType()->noNamiField()
->map(fn ($field) => $field->getRegistrationRules($form))
->toArray();
2024-02-18 18:48:31 +01:00
foreach ($c as $field) {
foreach ($field as $ruleKey => $rule) {
$rules[$this->key . '.*.' . $ruleKey] = $rule;
}
}
return [
$this->key . '.*.id' => ['required', 'numeric', 'exists:members,mitgliedsnr'],
...$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-03-07 00:58:14 +01:00
$c = $form->getFields()->noNamiField()->forMembers();
2024-02-18 18:48:31 +01:00
foreach ($c 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 02:10:58 +01:00
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 = Member::firstWhere(['mitgliedsnr' => $memberData['id']]);
2024-02-16 14:18:16 +01:00
$data = [];
2024-03-14 23:54:41 +01:00
foreach (FieldCollection::fromRequest($form, $memberData) as $field) {
2024-02-16 14:18:16 +01:00
$data[$field->key] = $field->namiType === null
2024-03-14 23:54:41 +01:00
? $field->value
2024-02-16 14:18:16 +01:00
: $field->namiType->getMemberAttribute($member);
}
$data[$this->key] = [];
2024-03-08 01:26:40 +01:00
$form->participants()->create(['data' => $data, 'mitgliedsnr' => $memberData['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
}