Add value for Participant fields
This commit is contained in:
parent
80def4abde
commit
2a979d932e
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace App\Form\Actions;
|
||||
|
||||
use App\Form\Fields\Field;
|
||||
use App\Form\Data\FieldCollection;
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use App\Member\Member;
|
||||
|
@ -19,7 +19,9 @@ class RegisterAction
|
|||
*/
|
||||
public function handle(Form $form, array $input): Participant
|
||||
{
|
||||
$memberQuery = $form->getFields()->withNamiType()->reduce(fn ($query, $field) => $field->namiType->performQuery($query, data_get($input, $field->key)), (new Member())->newQuery());
|
||||
$memberQuery = FieldCollection::fromRequest($form, $input)
|
||||
->withNamiType()
|
||||
->reduce(fn ($query, $field) => $field->namiType->performQuery($query, $field->value), (new Member())->newQuery());
|
||||
$mglnr = $form->getFields()->withNamiType()->count() && $memberQuery->count() === 1 ? $memberQuery->first()->mitgliedsnr : null;
|
||||
|
||||
$participant = $form->participants()->create([
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace App\Form\Data;
|
|||
|
||||
use App\Form\Fields\Field;
|
||||
use App\Form\Fields\NamiField;
|
||||
use App\Form\Models\Form;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
|
@ -31,4 +32,26 @@ class FieldCollection extends Collection
|
|||
{
|
||||
return $this->filter(fn ($field) => !is_a($field, NamiField::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
*/
|
||||
public static function fromRequest(Form $form, array $input): self
|
||||
{
|
||||
return $form->getFields()->each(fn ($field) => $field->value = array_key_exists($field->key, $input) ? $input[$field->key] : $field->default());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function present(): array
|
||||
{
|
||||
$attributes = collect([]);
|
||||
|
||||
foreach ($this as $field) {
|
||||
$attributes = $attributes->merge($field->present());
|
||||
}
|
||||
|
||||
return $attributes->toArray();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
namespace App\Form\Enums;
|
||||
|
||||
use App\Group\Enums\Level;
|
||||
use App\Member\Member;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
enum SpecialType: string
|
||||
{
|
||||
case FIRSTNAME = 'Vorname';
|
||||
|
|
|
@ -87,14 +87,13 @@ abstract class Field extends Data
|
|||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function presentValue($value)
|
||||
public function present()
|
||||
{
|
||||
return [
|
||||
$this->key => $value,
|
||||
$this->getDisplayAttribute() => $this->getPresenter()->present($value),
|
||||
$this->key => $this->value,
|
||||
$this->getDisplayAttribute() => $this->getPresenter()->present($this->value),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Form\Fields;
|
||||
|
||||
use App\Form\Data\FieldCollection;
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use App\Form\Presenters\NamiPresenter;
|
||||
|
@ -112,9 +113,9 @@ class NamiField extends Field
|
|||
foreach ($input[$this->key] as $memberData) {
|
||||
$member = Member::firstWhere(['mitgliedsnr' => $memberData['id']]);
|
||||
$data = [];
|
||||
foreach ($form->getFields() as $field) {
|
||||
foreach (FieldCollection::fromRequest($form, $memberData) as $field) {
|
||||
$data[$field->key] = $field->namiType === null
|
||||
? data_get($memberData, $field->key, $field->default())
|
||||
? $field->value
|
||||
: $field->namiType->getMemberAttribute($member);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Form\Models;
|
||||
|
||||
use App\Form\Data\FieldCollection;
|
||||
use App\Form\Scopes\ParticipantFilterScope;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
@ -43,4 +44,9 @@ class Participant extends Model
|
|||
{
|
||||
return $filter->apply($query);
|
||||
}
|
||||
|
||||
public function getFields(): FieldCollection
|
||||
{
|
||||
return FieldCollection::fromRequest($this->form, $this->data);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace App\Form\Resources;
|
||||
|
||||
use App\Form\Fields\Field;
|
||||
use App\Form\Models\Form;
|
||||
use App\Form\Models\Participant;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
@ -20,13 +19,7 @@ class ParticipantResource extends JsonResource
|
|||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$attributes = collect([]);
|
||||
|
||||
foreach ($this->form->getFields() as $field) {
|
||||
$attributes = $attributes->merge($field->presentValue($this->data[$field->key]));
|
||||
}
|
||||
|
||||
return $attributes->toArray();
|
||||
return $this->getModel()->getFields()->present();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Tests\Feature\Form;
|
|||
use App\Form\Fields\Field;
|
||||
use App\Form\Enums\NamiType;
|
||||
use Worksome\RequestFactories\RequestFactory;
|
||||
use App\Form\Enums\SpecialType;
|
||||
|
||||
/**
|
||||
* @method self name(string $name)
|
||||
|
@ -19,6 +20,7 @@ use Worksome\RequestFactories\RequestFactory;
|
|||
* @method self parentField(string $fieldKey)
|
||||
* @method self namiType(?NamiType $type)
|
||||
* @method self forMembers(bool $forMembers)
|
||||
* @method self specialType(SpecialType $specialType)
|
||||
*/
|
||||
class FormtemplateFieldRequest extends RequestFactory
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue