Files
crewli/api/database/factories/FormBuilder/FormFieldFactory.php
bert.hausmans 85815ccb16 feat(forms): add Eloquent models, observer, events, activity-log helpers
Phase 4 of S1.

Models (app/Models/FormBuilder/): FormSchema, FormSchemaSection, FormField,
FormSubmission, FormValue, FormValueOption, FormTemplate, FormFieldLibrary,
FormSchemaWebhook, FormWebhookDelivery, FormSubmissionSectionStatus,
FormSubmissionDelegation. Plus UserProfile at app/Models/ (user-universal).

OrganisationScope applied on: FormSchema, FormTemplate, FormFieldLibrary.
FormSchemaWebhook documents inherited-scope discipline (OrganisationScope's
strategies — organisation_id/event_id/festival_section_id — don't cover
form_schema_id; direct queries would leak across orgs, so must go via
$schema->webhooks()).

User::profile()/getOrCreateProfile(), Event::formSchemas() (morphMany),
Person::formSubmissions() (morphMany).

Morph map enforced in AppServiceProvider with 28 keys covering every model
that appears as activitylog subject/causer. Also updated
OrganisationDashboardService (and its test) to query activitylog via
getMorphClass() instead of FQCN.

Activity log strategy: nuanced explicit calls (logSchemaChange on FormSchema,
logFieldChange on FormField) — no LogsActivity trait. Suppression for bulk
fixtures via App\Support\ActivityLog::suppressed(fn() => ...) which flips
config('activitylog.enabled') around a callback. Both our explicit calls
and spatie's trait on Organisation respect the flag via ActivityLogger::log().

FormValueObserver (app/Observers/FormBuilder/) populates value_indexed/
value_number/value_date/value_bool on save per field.value_storage_hint,
rebuilds form_value_options pivot on multi-value filterable fields, cleans
up on delete. Memoised field cache avoids N+1. Registered in AppServiceProvider.

9 lightweight event classes (app/Events/FormBuilder/) as SerializesModels
containers — submission lifecycle signatures lock in for S2 services, no
listeners yet.

Factories for all models with Dutch fake data (fake('nl_NL')). FormSchema
factory uses defaultSubmissionMode(); FormField factory uses
recommendedValueStorageHint().

Tests: 9 new observer tests (all pass); full suite 910/910 (up from 901).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 12:35:41 +02:00

77 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Database\Factories\FormBuilder;
use App\Enums\FormBuilder\FormFieldDisplayWidth;
use App\Enums\FormBuilder\FormFieldType;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
/** @extends Factory<FormField> */
final class FormFieldFactory extends Factory
{
protected $model = FormField::class;
/** @return array<string, mixed> */
public function definition(): array
{
$fieldType = fake()->randomElement([
FormFieldType::TEXT,
FormFieldType::TEXTAREA,
FormFieldType::EMAIL,
FormFieldType::NUMBER,
FormFieldType::BOOLEAN,
FormFieldType::SELECT,
]);
$label = fake('nl_NL')->randomElement([
'Voornaam', 'Achternaam', 'E-mail', 'Telefoon', 'Opmerkingen',
'Shirtmaat', 'Allergieën', 'Motivatie', 'Geboortedatum',
]);
return [
'form_schema_id' => FormSchema::factory(),
'form_schema_section_id' => null,
'library_field_id' => null,
'field_type' => $fieldType->value,
'slug' => Str::slug($label).'-'.Str::lower(Str::random(4)),
'label' => $label,
'help_text' => fake()->boolean(30) ? fake('nl_NL')->sentence() : null,
'options' => $fieldType === FormFieldType::SELECT
? ['Optie A', 'Optie B', 'Optie C']
: null,
'validation_rules' => null,
'is_required' => fake()->boolean(40),
'is_filterable' => false,
'is_portal_visible' => true,
'is_admin_only' => false,
'is_unique' => false,
'is_pii' => false,
'display_width' => FormFieldDisplayWidth::FULL,
'binding' => null,
'conditional_logic' => null,
'role_restrictions' => null,
'translations' => null,
'value_storage_hint' => $fieldType->recommendedValueStorageHint(),
'review_required' => false,
'sort_order' => 0,
];
}
public function ofType(FormFieldType $type): static
{
return $this->state(fn () => [
'field_type' => $type->value,
'value_storage_hint' => $type->recommendedValueStorageHint(),
]);
}
public function filterable(): static
{
return $this->state(fn () => ['is_filterable' => true]);
}
}