adrema/app/Form/Fields/GroupField.php

103 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2023-12-31 14:29:50 +01:00
<?php
namespace App\Form\Fields;
2024-02-16 14:18:16 +01:00
use App\Form\Models\Form;
use App\Form\Models\Participant;
2024-02-09 23:22:49 +01:00
use App\Form\Presenters\GroupPresenter;
use App\Form\Presenters\Presenter;
2023-12-31 14:29:50 +01:00
use App\Group;
use App\Group\Enums\Level;
use Faker\Generator;
use Illuminate\Validation\Rule;
class GroupField extends Field
{
2024-02-06 01:45:25 +01:00
public bool $required;
public ?string $parentField = null;
public ?int $parentGroup = null;
2024-04-18 01:03:30 +02:00
public bool $hasEmptyOption;
2024-04-25 22:01:55 +02:00
public ?string $emptyOptionValue;
2024-02-06 01:45:25 +01:00
2023-12-31 14:29:50 +01:00
public static function name(): string
{
return 'Gruppierungs-Auswahl';
}
public static function meta(): array
{
return [
2024-04-12 15:23:18 +02:00
['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
2023-12-31 14:29:50 +01:00
['key' => 'parent_field', 'default' => null, 'rules' => ['parent_field' => 'present|nullable|string'], 'label' => 'Übergeordnetes Feld'],
['key' => 'parent_group', 'default' => null, 'rules' => ['parent_group' => ['present', 'nullable', Rule::in(Group::pluck('id')->toArray())]], 'label' => 'Übergeordnete Gruppierung'],
2024-04-18 01:03:30 +02:00
['key' => 'has_empty_option', 'default' => false, 'rules' => ['has_empty_option' => ['present', 'boolean']], 'label' => 'Leere Option erlauben'],
2024-04-25 22:01:55 +02:00
['key' => 'empty_option_value', 'default' => '', 'rules' => ['empty_option_value' => ['present', 'nullable', 'string']], 'label' => 'Leere Option'],
2023-12-31 14:29:50 +01:00
];
}
public static function default(): string
{
return '';
}
public static function fake(Generator $faker): array
{
return [
'required' => $faker->boolean(),
'parent_field' => null,
'parent_group' => null,
2024-04-18 01:03:30 +02:00
'has_empty_option' => $faker->boolean(),
'empty_option_value' => '',
2023-12-31 14:29:50 +01:00
];
}
2024-02-06 01:45:25 +01:00
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationRules(Form $form): array
2024-02-06 01:45:25 +01:00
{
$rules = [$this->required ? 'required' : 'nullable'];
$rules[] = 'integer';
if ($this->parentGroup) {
2024-04-18 01:03:30 +02:00
$rules[] = Rule::in(Group::find($this->parentGroup)->children()->pluck('id')->push(-1));
2024-02-06 01:45:25 +01:00
}
if ($this->parentField && request()->input($this->parentField) && request()->input($this->parentField) !== -1) {
2024-04-18 01:03:30 +02:00
$rules[] = Rule::in(Group::find(request()->input($this->parentField))->children()->pluck('id')->push(-1));
2024-02-06 01:45:25 +01:00
}
return [$this->key => $rules];
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationAttributes(Form $form): array
2024-02-06 01:45:25 +01:00
{
return [$this->key => $this->name];
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationMessages(Form $form): array
2024-02-06 01:45:25 +01:00
{
return [];
}
2024-02-08 23:09:51 +01:00
2024-02-09 23:22:49 +01:00
public function getPresenter(): Presenter
2024-02-08 23:09:51 +01:00
{
2024-04-18 01:03:30 +02:00
return app(GroupPresenter::class)->field($this);
2024-02-08 23:09:51 +01:00
}
2024-02-16 14:18:16 +01:00
2024-02-16 14:32:16 +01:00
/**
* @inheritdoc
*/
2024-02-16 14:18:16 +01:00
public function afterRegistration(Form $form, Participant $participant, array $input): void
{
}
2023-12-31 14:29:50 +01:00
}