feat: registration form fields, section preferences, tag sync & schema updates

Implement EAV system for dynamic event-specific registration fields
with organisation-level templates, person section preferences with
priority ranking, and TagSyncService for deferred tag_picker sync.

New tables: registration_field_templates, registration_form_fields,
person_field_values, person_section_preferences.
New columns: persons.remarks, events.registration_show_section_preferences,
events.registration_show_availability.

58 tests, 126 assertions — all 432 tests pass (zero regressions).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 22:10:16 +02:00
parent fcff3b0344
commit f6e3568011
51 changed files with 3774 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Person;
use App\Models\PersonSectionPreference;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
final class PersonSectionPreferenceService
{
public function getPreferences(Person $person): Collection
{
return PersonSectionPreference::where('person_id', $person->id)
->with('festivalSection')
->orderBy('priority')
->get();
}
public function replacePreferences(Person $person, array $preferences): void
{
$old = PersonSectionPreference::where('person_id', $person->id)->get()->toArray();
DB::transaction(function () use ($person, $preferences): void {
PersonSectionPreference::where('person_id', $person->id)->delete();
foreach ($preferences as $pref) {
PersonSectionPreference::create([
'person_id' => $person->id,
'festival_section_id' => $pref['festival_section_id'],
'priority' => $pref['priority'],
]);
}
});
$activityLogger = activity('section_preferences')
->performedOn($person)
->withProperties([
'old' => $old,
'attributes' => $preferences,
]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('person.section_preferences.replaced');
}
}

View File

@@ -0,0 +1,199 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\Event;
use App\Models\Organisation;
use App\Models\RegistrationFieldTemplate;
use App\Models\RegistrationFormField;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
final class RegistrationFieldTemplateService
{
public function listForOrganisation(Organisation $organisation): Collection
{
return $organisation->registrationFieldTemplates()
->active()
->ordered()
->get();
}
public function createTemplate(Organisation $organisation, array $data): RegistrationFieldTemplate
{
$data['slug'] = $this->generateUniqueSlug($organisation, $data['label']);
$template = $organisation->registrationFieldTemplates()->create($data);
$activityLogger = activity('registration_templates')
->performedOn($template)
->withProperties(['attributes' => $data]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_template.created');
return $template;
}
public function updateTemplate(RegistrationFieldTemplate $template, array $data): RegistrationFieldTemplate
{
$old = $template->toArray();
if (isset($data['label']) && $data['label'] !== $template->label) {
$data['slug'] = $this->generateUniqueSlug($template->organisation, $data['label'], $template->id);
}
$template->update($data);
$activityLogger = activity('registration_templates')
->performedOn($template)
->withProperties(['old' => $old, 'attributes' => $data]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_template.updated');
return $template->fresh();
}
public function deleteTemplate(RegistrationFieldTemplate $template): void
{
if ($template->is_system) {
$template->update(['is_active' => false]);
$activityLogger = activity('registration_templates')
->performedOn($template);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_template.deactivated');
return;
}
$activityLogger = activity('registration_templates')
->withProperties(['deleted_template' => $template->toArray()]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_template.deleted');
$template->delete();
}
public function createFieldFromTemplate(Event $event, RegistrationFieldTemplate $template): RegistrationFormField
{
$slug = $this->generateUniqueFieldSlug($event, $template->label);
$maxOrder = RegistrationFormField::where('event_id', $event->id)->max('sort_order') ?? -1;
$field = RegistrationFormField::create([
'event_id' => $event->id,
'label' => $template->label,
'slug' => $slug,
'field_type' => $template->field_type,
'options' => $template->options,
'tag_category' => $template->tag_category,
'is_required' => $template->is_required,
'is_portal_visible' => $template->is_portal_visible,
'is_admin_only' => $template->is_admin_only,
'is_filterable' => $template->is_filterable,
'section' => $template->section,
'help_text' => $template->help_text,
'sort_order' => $maxOrder + 1,
]);
$activityLogger = activity('registration_fields')
->performedOn($field)
->withProperties(['from_template_id' => $template->id]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_field.created_from_template');
return $field;
}
/**
* Seed system templates for a newly created organisation.
*/
public static function seedSystemTemplates(Organisation $organisation): void
{
$templates = [
['label' => 'Shirtmaat', 'field_type' => 'select', 'options' => ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'], 'is_filterable' => true, 'sort_order' => 1],
['label' => 'Dieetwensen', 'field_type' => 'multiselect', 'options' => ['Vegetarisch', 'Veganistisch', 'Halal', 'Glutenvrij', 'Lactosevrij', 'Geen pinda\'s', 'Geen noten'], 'is_filterable' => true, 'sort_order' => 2],
['label' => 'Vergoeding', 'field_type' => 'radio', 'options' => ['Pro Deo', 'Entreeticket', 'Vrijwilligersvergoeding'], 'section' => 'Vergoeding', 'sort_order' => 3],
['label' => 'Toestemming gegevensverwerking', 'field_type' => 'boolean', 'is_required' => true, 'section' => 'Toestemming', 'help_text' => 'Ik geef toestemming voor de verwerking van mijn persoonsgegevens ten behoeve van de organisatie van dit evenement, conform de Algemene Verordening Gegevensbescherming (AVG).', 'sort_order' => 4],
['label' => 'Noodcontact naam', 'field_type' => 'text', 'section' => 'Noodcontact', 'sort_order' => 5],
['label' => 'Noodcontact telefoon', 'field_type' => 'text', 'section' => 'Noodcontact', 'sort_order' => 6],
['label' => 'EHBO / BHV diploma', 'field_type' => 'boolean', 'is_filterable' => true, 'sort_order' => 7],
['label' => 'Rijbewijs', 'field_type' => 'boolean', 'is_filterable' => true, 'sort_order' => 8],
['label' => 'Eerder vrijwilliger geweest', 'field_type' => 'boolean', 'is_filterable' => true, 'sort_order' => 9],
['label' => 'Certificaten & vaardigheden', 'field_type' => 'tag_picker', 'tag_category' => null, 'is_filterable' => true, 'sort_order' => 10],
['label' => 'Opmerkingen', 'field_type' => 'textarea', 'sort_order' => 11],
];
foreach ($templates as $data) {
$organisation->registrationFieldTemplates()->create([
...$data,
'slug' => Str::slug($data['label']),
'is_system' => true,
'is_active' => true,
'is_required' => $data['is_required'] ?? false,
'is_filterable' => $data['is_filterable'] ?? false,
'is_portal_visible' => true,
'is_admin_only' => false,
]);
}
}
private function generateUniqueSlug(Organisation $organisation, string $label, ?string $excludeId = null): string
{
$base = Str::slug($label);
$slug = $base;
$counter = 1;
while (true) {
$query = RegistrationFieldTemplate::where('organisation_id', $organisation->id)
->where('slug', $slug);
if ($excludeId) {
$query->where('id', '!=', $excludeId);
}
if (!$query->exists()) {
return $slug;
}
$counter++;
$slug = "{$base}-{$counter}";
}
}
private function generateUniqueFieldSlug(Event $event, string $label): string
{
$base = Str::slug($label);
$slug = $base;
$counter = 1;
while (RegistrationFormField::where('event_id', $event->id)->where('slug', $slug)->exists()) {
$counter++;
$slug = "{$base}-{$counter}";
}
return $slug;
}
}

View File

@@ -0,0 +1,219 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\RegistrationFieldType;
use App\Models\Event;
use App\Models\Person;
use App\Models\PersonFieldValue;
use App\Models\RegistrationFormField;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
final class RegistrationFormFieldService
{
public function __construct(
private readonly TagSyncService $tagSyncService,
) {}
public function listForEvent(Event $event): Collection
{
return RegistrationFormField::where('event_id', $event->id)
->ordered()
->get();
}
public function createField(Event $event, array $data): RegistrationFormField
{
$data['slug'] = $this->generateUniqueSlug($event, $data['label']);
$field = RegistrationFormField::create([
'event_id' => $event->id,
...$data,
]);
$activityLogger = activity('registration_fields')
->performedOn($field)
->withProperties(['attributes' => $data]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_field.created');
return $field;
}
public function updateField(RegistrationFormField $field, array $data): RegistrationFormField
{
$old = $field->toArray();
if (isset($data['label']) && $data['label'] !== $field->label) {
$data['slug'] = $this->generateUniqueSlug($field->event, $data['label'], $field->id);
}
$field->update($data);
$activityLogger = activity('registration_fields')
->performedOn($field)
->withProperties(['old' => $old, 'attributes' => $data]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_field.updated');
return $field->fresh();
}
public function deleteField(RegistrationFormField $field): void
{
$activityLogger = activity('registration_fields')
->withProperties(['deleted_field' => $field->toArray()]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_field.deleted');
$field->delete();
}
public function reorderFields(Event $event, array $orderedIds): void
{
DB::transaction(function () use ($event, $orderedIds): void {
foreach ($orderedIds as $index => $id) {
RegistrationFormField::where('id', $id)
->where('event_id', $event->id)
->update(['sort_order' => $index]);
}
});
}
public function upsertPersonValues(Person $person, array $values): void
{
$fields = RegistrationFormField::where('event_id', $person->event_id)
->get()
->keyBy('slug');
DB::transaction(function () use ($person, $values, $fields): void {
foreach ($values as $slug => $rawValue) {
$field = $fields->get($slug);
if ($field === null) {
continue;
}
$data = ['person_id' => $person->id, 'registration_form_field_id' => $field->id];
if ($field->isMultiValue()) {
$data['value'] = null;
$data['selected_options'] = is_array($rawValue) ? $rawValue : [$rawValue];
} else {
$data['value'] = $rawValue === null ? null : (string) $rawValue;
$data['selected_options'] = null;
}
PersonFieldValue::updateOrCreate(
['person_id' => $person->id, 'registration_form_field_id' => $field->id],
$data,
);
}
});
$activityLogger = activity('registration_values')
->performedOn($person)
->withProperties(['slugs' => array_keys($values)]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('person.field_values.upserted');
$this->tagSyncService->syncFromRegistration($person);
}
public function getPersonValues(Person $person): Collection
{
return PersonFieldValue::where('person_id', $person->id)
->with('registrationFormField')
->get();
}
public function importFromEvent(Event $targetEvent, Event $sourceEvent): Collection
{
$sourceFields = RegistrationFormField::where('event_id', $sourceEvent->id)
->ordered()
->get();
$maxOrder = RegistrationFormField::where('event_id', $targetEvent->id)->max('sort_order') ?? -1;
$created = collect();
foreach ($sourceFields as $sourceField) {
$slug = $this->generateUniqueSlug($targetEvent, $sourceField->label);
$field = RegistrationFormField::create([
'event_id' => $targetEvent->id,
'label' => $sourceField->label,
'slug' => $slug,
'field_type' => $sourceField->field_type,
'options' => $sourceField->options,
'tag_category' => $sourceField->tag_category,
'is_required' => $sourceField->is_required,
'is_portal_visible' => $sourceField->is_portal_visible,
'is_admin_only' => $sourceField->is_admin_only,
'is_filterable' => $sourceField->is_filterable,
'section' => $sourceField->section,
'help_text' => $sourceField->help_text,
'sort_order' => ++$maxOrder,
]);
$created->push($field);
}
$activityLogger = activity('registration_fields')
->withProperties([
'source_event_id' => $sourceEvent->id,
'target_event_id' => $targetEvent->id,
'fields_copied' => $created->count(),
]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('registration_field.imported_from_event');
return $created;
}
private function generateUniqueSlug(Event $event, string $label, ?string $excludeId = null): string
{
$base = Str::slug($label);
$slug = $base;
$counter = 1;
while (true) {
$query = RegistrationFormField::where('event_id', $event->id)
->where('slug', $slug);
if ($excludeId) {
$query->where('id', '!=', $excludeId);
}
if (!$query->exists()) {
return $slug;
}
$counter++;
$slug = "{$base}-{$counter}";
}
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\RegistrationFieldType;
use App\Models\Person;
use App\Models\PersonFieldValue;
use App\Models\RegistrationFormField;
use App\Models\UserOrganisationTag;
use Illuminate\Support\Facades\DB;
/**
* Syncs tag_picker registration field values to user_organisation_tags.
*
* TODO: Additional trigger points to wire when those services are built:
* - PersonService::approve() when account is created and user_id is set
* - PersonIdentityService::confirmMatch() when user_id is linked via identity matching
*/
final class TagSyncService
{
public function syncFromRegistration(Person $person): void
{
if ($person->user_id === null) {
return;
}
$organisationId = $person->event?->organisation_id;
if ($organisationId === null) {
return;
}
$tagPickerFields = RegistrationFormField::where('event_id', $person->event_id)
->where('field_type', RegistrationFieldType::TAG_PICKER)
->pluck('id');
if ($tagPickerFields->isEmpty()) {
return;
}
$selectedTagIds = PersonFieldValue::where('person_id', $person->id)
->whereIn('registration_form_field_id', $tagPickerFields)
->whereNotNull('selected_options')
->get()
->flatMap(fn (PersonFieldValue $v) => $v->selected_options ?? [])
->unique()
->values()
->all();
DB::transaction(function () use ($person, $organisationId, $selectedTagIds): void {
$existingSelfReported = UserOrganisationTag::where('user_id', $person->user_id)
->where('organisation_id', $organisationId)
->where('source', 'self_reported')
->pluck('person_tag_id')
->all();
$toAdd = array_diff($selectedTagIds, $existingSelfReported);
$toRemove = array_diff($existingSelfReported, $selectedTagIds);
if (!empty($toRemove)) {
UserOrganisationTag::where('user_id', $person->user_id)
->where('organisation_id', $organisationId)
->where('source', 'self_reported')
->whereIn('person_tag_id', $toRemove)
->delete();
}
foreach ($toAdd as $tagId) {
UserOrganisationTag::create([
'user_id' => $person->user_id,
'organisation_id' => $organisationId,
'person_tag_id' => $tagId,
'source' => 'self_reported',
'assigned_at' => now(),
]);
}
});
$activityLogger = activity('tag_sync')
->performedOn($person)
->withProperties([
'synced_tag_ids' => $selectedTagIds,
'organisation_id' => $organisationId,
]);
if (auth()->user()) {
$activityLogger->causedBy(auth()->user());
}
$activityLogger->log('person.tags.synced_from_registration');
}
}