feat(form-builder): form_field_configs relational table + non-validation key split + drop validation_rules JSON columns
This commit is contained in:
102
api/app/Models/Scopes/FormFieldConfigScope.php
Normal file
102
api/app/Models/Scopes/FormFieldConfigScope.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models\Scopes;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
|
||||
/**
|
||||
* Third sibling in the form-field-child-table scope family, after
|
||||
* `FormFieldBindingScope` (WS-5a) and `FormFieldValidationRuleScope`
|
||||
* (WS-5b commit 1). Identical UNION-over-two-owner-chains shape:
|
||||
*
|
||||
* owner_id ∈ (
|
||||
* SELECT id FROM form_fields
|
||||
* WHERE form_schema_id ∈ (SELECT id FROM form_schemas WHERE organisation_id = ?)
|
||||
* UNION
|
||||
* SELECT id FROM form_field_library
|
||||
* WHERE organisation_id = ?
|
||||
* )
|
||||
*
|
||||
* Base-class extraction between the three siblings is deliberately
|
||||
* deferred to WS-5d (where `form_field_options` lands and the fourth
|
||||
* concrete implementation may clarify what truly varies). Premature
|
||||
* abstraction from three is still premature.
|
||||
*/
|
||||
final class FormFieldConfigScope implements Scope
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?string $organisationId = null,
|
||||
) {}
|
||||
|
||||
public function apply(Builder $builder, Model $model): void
|
||||
{
|
||||
$orgId = $this->resolveOrganisationId();
|
||||
if ($orgId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fieldIds = FormField::query()
|
||||
->withoutGlobalScope(OrganisationScope::class)
|
||||
->whereIn(
|
||||
'form_schema_id',
|
||||
FormSchema::query()
|
||||
->withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $orgId)
|
||||
->select('id'),
|
||||
)
|
||||
->select('id');
|
||||
|
||||
$libraryIds = FormFieldLibrary::query()
|
||||
->withoutGlobalScope(OrganisationScope::class)
|
||||
->where('organisation_id', $orgId)
|
||||
->select('id');
|
||||
|
||||
$table = $model->getTable();
|
||||
|
||||
$builder->where(function (Builder $outer) use ($table, $fieldIds, $libraryIds): void {
|
||||
$outer->where(function (Builder $q) use ($table, $fieldIds): void {
|
||||
$q->where("$table.owner_type", 'form_field')
|
||||
->whereIn("$table.owner_id", $fieldIds);
|
||||
})->orWhere(function (Builder $q) use ($table, $libraryIds): void {
|
||||
$q->where("$table.owner_type", 'form_field_library')
|
||||
->whereIn("$table.owner_id", $libraryIds);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private function resolveOrganisationId(): ?string
|
||||
{
|
||||
if ($this->organisationId !== null) {
|
||||
return $this->organisationId;
|
||||
}
|
||||
|
||||
$route = request()->route();
|
||||
if ($route === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$org = $route->parameter('organisation');
|
||||
|
||||
if ($org instanceof \App\Models\Organisation) {
|
||||
return $org->id;
|
||||
}
|
||||
|
||||
if (is_string($org) && $org !== '') {
|
||||
return $org;
|
||||
}
|
||||
|
||||
$event = $route->parameter('event');
|
||||
if ($event instanceof \App\Models\Event) {
|
||||
return $event->organisation_id;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user