2024-02-16 14:18:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Fields;
|
|
|
|
|
|
|
|
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-02-19 02:10:58 +01:00
|
|
|
$c = collect($form->getFields())
|
2024-02-18 18:58:28 +01:00
|
|
|
->filter(fn ($field) => $field['for_members'] === true)
|
|
|
|
->filter(fn ($field) => $field['nami_type'] === null)
|
|
|
|
->filter(fn ($field) => $field['type'] !== class_basename(static::class))
|
2024-02-19 02:10:58 +01:00
|
|
|
->map(fn ($field) => Field::fromConfig($field)->getRegistrationRules($form));
|
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);
|
|
|
|
|
2024-02-19 23:59:23 +01:00
|
|
|
if (!is_array($inputMembers)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-02-19 02:10:58 +01:00
|
|
|
$c = collect($form->getFields())
|
2024-02-18 18:58:28 +01:00
|
|
|
->filter(fn ($field) => $field['type'] !== class_basename(static::class))
|
|
|
|
->filter(fn ($field) => $field['for_members'] === true)
|
|
|
|
->map(fn ($field) => Field::fromConfig($field));
|
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 = [];
|
|
|
|
foreach ($form->getFields() as $field) {
|
|
|
|
$field = Field::fromConfig($field);
|
|
|
|
|
|
|
|
$data[$field->key] = $field->namiType === null
|
2024-02-16 14:23:59 +01:00
|
|
|
? data_get($memberData, $field->key, $field->default())
|
2024-02-16 14:18:16 +01:00
|
|
|
: $field->namiType->getMemberAttribute($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data[$this->key] = [];
|
2024-02-20 01:14:26 +01:00
|
|
|
$form->participants()->create(['data' => $data, 'mitgliedsnr' => $memberData['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
|
|
|
}
|