2026-01-08 13:10:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Http\Controller;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Lucent\Core\Repository\FieldRepo;
|
|
|
|
|
use Lucent\Core\Repository\SchemaRepo;
|
|
|
|
|
use Lucent\Core\Schema\Data\Field;
|
|
|
|
|
use Lucent\Core\Schema\Data\FieldProp\FieldProp;
|
|
|
|
|
use Lucent\Id\Id;
|
|
|
|
|
use Lucent\Svelte\Svelte;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
2026-01-08 18:13:17 +02:00
|
|
|
class FieldController
|
2026-01-08 13:10:18 +02:00
|
|
|
{
|
|
|
|
|
public function __construct(private readonly Svelte $svelte) {}
|
|
|
|
|
|
|
|
|
|
public function create(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schemaId = $request->input("schema");
|
|
|
|
|
$fieldType = $request->input("type");
|
|
|
|
|
|
|
|
|
|
$schemas = SchemaRepo::all();
|
|
|
|
|
$schema = collect($schemas)->firstWhere("id", $schemaId);
|
|
|
|
|
|
|
|
|
|
if (empty($schema)) {
|
|
|
|
|
return response()->json(["error" => "Schema not found"], 404);
|
|
|
|
|
}
|
|
|
|
|
if (empty($fieldType)) {
|
|
|
|
|
return response()->json(["error" => "Field type not found"], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// $fieldProps = FieldProp::fromType($fieldType);
|
|
|
|
|
|
2026-01-08 18:13:17 +02:00
|
|
|
return Svelte::view(
|
2026-01-08 13:10:18 +02:00
|
|
|
view: "fieldCreate",
|
|
|
|
|
title: "Create Field",
|
|
|
|
|
data: [
|
|
|
|
|
"schemas" => $schemas,
|
|
|
|
|
"schema" => $schema,
|
|
|
|
|
// "fieldProps" => $fieldProps,
|
|
|
|
|
"type" => $fieldType,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postCreate(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schemaId = $request->input("schemaId");
|
|
|
|
|
$name = $request->input("name");
|
|
|
|
|
$alias = $request->input("alias");
|
|
|
|
|
$fieldType = $request->input("fieldType");
|
2026-01-08 15:19:08 +02:00
|
|
|
$fields = FieldRepo::all();
|
|
|
|
|
$newRank = collect($fields)->last()->rank + 1;
|
2026-01-08 13:10:18 +02:00
|
|
|
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
"name" => "required|string|max:30|min:2",
|
|
|
|
|
"alias" => "required|alpha_dash:ascii|max:30|min:2",
|
|
|
|
|
"schemaId" => "required",
|
|
|
|
|
"fieldType" => "required",
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json(["errors" => $validator->errors()], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$schemas = SchemaRepo::all();
|
|
|
|
|
$schema = collect($schemas)->firstWhere("id", $schemaId);
|
|
|
|
|
|
|
|
|
|
$fieldProps = FieldProp::fromType($fieldType);
|
|
|
|
|
|
|
|
|
|
$field = new Field(
|
|
|
|
|
id: Id::new(),
|
|
|
|
|
schemaId: $schemaId,
|
|
|
|
|
name: $name,
|
|
|
|
|
alias: $alias,
|
|
|
|
|
type: $fieldType,
|
|
|
|
|
props: $fieldProps,
|
2026-01-08 15:19:08 +02:00
|
|
|
rank: $newRank,
|
2026-01-08 13:10:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
FieldRepo::insert($field);
|
|
|
|
|
return response()->json(["field" => $field], 201);
|
|
|
|
|
}
|
2026-01-08 15:19:08 +02:00
|
|
|
|
|
|
|
|
public function edit(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$fieldId = $request->route("id");
|
|
|
|
|
$fields = FieldRepo::all();
|
|
|
|
|
$field = collect($fields)->firstWhere("id", $fieldId);
|
|
|
|
|
|
|
|
|
|
if (empty($field)) {
|
|
|
|
|
return response()->json(["errors" => ["Field not found"]], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$schemas = SchemaRepo::all();
|
|
|
|
|
$schema = collect($schemas)->firstWhere("id", $field->schemaId);
|
|
|
|
|
|
2026-01-08 18:13:17 +02:00
|
|
|
return Svelte::view(
|
2026-01-08 15:19:08 +02:00
|
|
|
view: "fieldEdit",
|
|
|
|
|
title: "Edit Field",
|
|
|
|
|
data: [
|
|
|
|
|
"schemas" => $schemas,
|
|
|
|
|
"schema" => $schema,
|
|
|
|
|
"field" => $field,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postUpdate(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$fieldData = $request->input("field");
|
|
|
|
|
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
|
"field.name" => "required|string|max:30|min:2",
|
|
|
|
|
"field.alias" => "required|alpha_dash:ascii|max:30|min:2",
|
|
|
|
|
]);
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
|
return response()->json(["errors" => $validator->errors()], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$field = FieldRepo::findOne(data_get($fieldData, "id"));
|
|
|
|
|
|
|
|
|
|
if (empty($field)) {
|
|
|
|
|
return response()->json(["errors" => ["Field not found"]], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fieldProps = FieldProp::fromArray(
|
|
|
|
|
$field->type,
|
|
|
|
|
data_get($fieldData, "props"),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$field = new Field(
|
|
|
|
|
id: $field->id,
|
|
|
|
|
schemaId: $field->schemaId,
|
|
|
|
|
name: data_get($fieldData, "name"),
|
|
|
|
|
alias: data_get($fieldData, "alias"),
|
|
|
|
|
type: $field->type,
|
|
|
|
|
rank: $field->rank,
|
|
|
|
|
props: $fieldProps,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
FieldRepo::update($field);
|
|
|
|
|
return response()->json(["field" => $field], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postDelete(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$fieldId = $request->input("fieldId");
|
|
|
|
|
$field = FieldRepo::findOne($fieldId);
|
|
|
|
|
|
|
|
|
|
if (empty($field)) {
|
|
|
|
|
return response()->json(["errors" => ["Field not found"]], 404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FieldRepo::delete($fieldId);
|
|
|
|
|
return response()->json([], 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function postReorder(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$ids = $request->input("ids");
|
|
|
|
|
|
|
|
|
|
foreach ($ids as $index => $id) {
|
|
|
|
|
$field = FieldRepo::findOne($id);
|
|
|
|
|
if ($field) {
|
|
|
|
|
$field->rank = $index;
|
|
|
|
|
FieldRepo::update($field);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response()->json(["ids" => $ids], 200);
|
|
|
|
|
}
|
2026-01-08 13:10:18 +02:00
|
|
|
}
|