Files
crewli/api/app/Services/RegistrationFieldTemplateService.php
bert.hausmans 6a8d21a5b6 feat: registration field polish, multi-category tags, file uploads, Partner icon
- Restructure field editor dialog: move Options section to bottom with
  divider and subheader, fix delete button with flex layout
- Change tag_category (single string) to tag_categories (JSON array)
  supporting multiple category selection in tag picker fields
- Portal tag picker now groups tags by category with subheaders
- Add generic file upload endpoint (FileUploadService + UploadController)
- Replace email branding logo URL text field with ImageUploadField
- Update Partner crowd type default icon to tabler-affiliate
- Apply changes consistently to both field and template dialogs

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 18:03:49 +02:00

216 lines
9.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\FieldDisplayWidth;
use App\Enums\RegistrationFieldType;
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()
->ordered()
->get();
}
public function createTemplate(Organisation $organisation, array $data): RegistrationFieldTemplate
{
$data['slug'] = $this->generateUniqueSlug($organisation, $data['label']);
if (!isset($data['display_width'])) {
$fieldType = RegistrationFieldType::from($data['field_type']);
$data['display_width'] = FieldDisplayWidth::defaultForFieldType($fieldType)->value;
}
$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_categories' => $template->tag_categories,
'is_required' => $template->is_required,
'is_portal_visible' => $template->is_portal_visible,
'is_admin_only' => $template->is_admin_only,
'is_filterable' => $template->is_filterable,
'help_text' => $template->help_text,
'sort_order' => $maxOrder + 1,
'display_width' => $template->display_width,
]);
$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' => 'Persoonlijke voorkeuren', 'field_type' => 'heading', 'help_text' => 'Vertel ons wat we over jou moeten weten', 'display_width' => 'full', 'sort_order' => 1],
['label' => 'Shirtmaat', 'field_type' => 'select', 'options' => ['XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'], 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 2],
['label' => 'Dieetwensen', 'field_type' => 'multiselect', 'options' => ['Vegetarisch', 'Veganistisch', 'Halal', 'Glutenvrij', 'Lactosevrij', 'Geen pinda\'s', 'Geen noten'], 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 3],
['label' => 'Vergoeding', 'field_type' => 'heading', 'help_text' => 'Kies hoe je wilt worden bedankt voor je inzet', 'display_width' => 'full', 'sort_order' => 4],
['label' => 'Vergoedingstype', 'field_type' => 'radio', 'options' => [
['label' => 'Pro Deo', 'description' => 'Je werkt als vrijwilliger zonder financiële vergoeding'],
['label' => 'Entreeticket', 'description' => 'Je ontvangt een gratis festivalticket als dank voor je inzet'],
['label' => 'Vrijwilligersvergoeding', 'description' => 'Je ontvangt een vergoeding conform de vrijwilligersregeling'],
], 'is_required' => true, 'display_width' => 'full', 'sort_order' => 5],
['label' => 'Noodcontact', 'field_type' => 'heading', 'help_text' => 'Wie kunnen we bereiken bij calamiteiten?', 'display_width' => 'full', 'sort_order' => 6],
['label' => 'Noodcontact naam', 'field_type' => 'text', 'display_width' => 'half', 'sort_order' => 7],
['label' => 'Noodcontact telefoon', 'field_type' => 'text', 'display_width' => 'half', 'sort_order' => 8],
['label' => 'Ervaring & vaardigheden', 'field_type' => 'heading', 'help_text' => 'Welke diploma\'s en skills heb je?', 'display_width' => 'full', 'sort_order' => 9],
['label' => 'EHBO / BHV diploma', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 10],
['label' => 'Rijbewijs', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 11],
['label' => 'Eerder vrijwilliger geweest', 'field_type' => 'boolean', 'is_filterable' => true, 'display_width' => 'half', 'sort_order' => 12],
['label' => 'Certificaten & vaardigheden', 'field_type' => 'tag_picker', 'tag_categories' => null, 'is_filterable' => true, 'display_width' => 'full', 'sort_order' => 13],
['label' => 'Aanvullende informatie', 'field_type' => 'heading', 'display_width' => 'full', 'sort_order' => 14],
['label' => 'Toestemming gegevensverwerking', 'field_type' => 'boolean', 'is_required' => true, '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).', 'display_width' => 'full', 'sort_order' => 15],
['label' => 'Opmerkingen', 'field_type' => 'textarea', 'display_width' => 'full', 'sort_order' => 16],
];
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,
'display_width' => $data['display_width'] ?? 'full',
]);
}
}
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;
}
}