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
|
|
|
{
|
2024-02-19 23:59:23 +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 [
|
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);
|
|
|
|
|
2024-02-19 23:59:23 +01:00
|
|
|
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-06-12 00:41:49 +02:00
|
|
|
: $field->namiType->getMemberAttribute($member, $form);
|
2024-02-16 14:18:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
}
|