Files
crewli/api/app/Models/RegistrationFormField.php
bert.hausmans d57dcdb616 feat: HEADING field type for registration forms — replace section property with structural field
Replace the per-field `section` text property with a dedicated HEADING field type that
organizers add as a separate block for visual grouping. Also fixes duplicate heading bug
on portal radio fields, replaces cramped VBtnToggle with VSelect for field width, and
adds grouped field type dropdown with structure/input categories.

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

104 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\FieldDisplayWidth;
use App\Enums\RegistrationFieldType;
use App\Models\Scopes\OrganisationScope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Concerns\HasUlids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
final class RegistrationFormField extends Model
{
use HasFactory;
use HasUlids;
/** @var string Used by OrganisationScope to determine filtering strategy */
public string $organisationScopeColumn = 'event_id';
protected static function booted(): void
{
static::addGlobalScope(new OrganisationScope());
}
protected $fillable = [
'event_id',
'label',
'slug',
'field_type',
'options',
'tag_category',
'is_required',
'is_portal_visible',
'is_admin_only',
'is_filterable',
'help_text',
'sort_order',
'display_width',
];
protected function casts(): array
{
return [
'field_type' => RegistrationFieldType::class,
'options' => 'array',
'is_required' => 'boolean',
'is_portal_visible' => 'boolean',
'is_admin_only' => 'boolean',
'is_filterable' => 'boolean',
'sort_order' => 'integer',
'display_width' => FieldDisplayWidth::class,
];
}
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
public function personFieldValues(): HasMany
{
return $this->hasMany(PersonFieldValue::class, 'registration_form_field_id');
}
/** @return array<int, array{label: string, description: string|null}>|null */
public function getNormalizedOptionsAttribute(): ?array
{
if ($this->options === null) {
return null;
}
return collect($this->options)->map(function (mixed $option): array {
if (is_string($option)) {
return ['label' => $option, 'description' => null];
}
return [
'label' => $option['label'] ?? (string) $option,
'description' => $option['description'] ?? null,
];
})->toArray();
}
public function isMultiValue(): bool
{
return $this->field_type->isMultiValue();
}
public function scopeOrdered(Builder $query): Builder
{
return $query->orderBy('sort_order');
}
public function scopePortalVisible(Builder $query): Builder
{
return $query->where('is_portal_visible', true)->where('is_admin_only', false);
}
}