boboko lulnar

This commit is contained in:
2026-04-20 21:07:35 +03:00
parent 57b0727788
commit 4a7eb217a1
32 changed files with 1350 additions and 858 deletions
+14 -21
View File
@@ -2,7 +2,6 @@
namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Session\Session;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
@@ -13,44 +12,37 @@ use Lucent\Account\AuthService;
use Lucent\Channel\ChannelService;
use Lucent\LucentException;
use Lucent\Svelte\Svelte;
use Lucent\Util\Form\FormException;
use Lucent\Util\Form\ResponseFormError;
use function Lucent\Response\fail;
use function Lucent\Response\ok;
class AuthController
{
public function __construct(
private readonly AuthService $authService,
private readonly AuthService $authService,
private readonly AccountService $accountService,
private readonly ChannelService $channelService,
private readonly Session $session,
private readonly Svelte $svelte,
)
{
}
private readonly Session $session,
private readonly Svelte $svelte,
) {}
public function register(Request $request): View|RedirectResponse
{
if ($this->accountService->countUsers() > 0) {
return redirect($this->channelService->channel->lucentUrl . "/login");
return redirect(
$this->channelService->channel->lucentUrl . "/login",
);
}
return $this->svelte->render(
layout: "account",
view: "register",
title: "Create an account",
);
}
public function postRegister(Request $request): Response
{
if ($this->accountService->countUsers() > 0) {
abort(400);
}
@@ -70,7 +62,9 @@ class AuthController
public function login()
{
if ($this->accountService->countUsers() == 0) {
return redirect($this->channelService->channel->lucentUrl . "/register");
return redirect(
$this->channelService->channel->lucentUrl . "/register",
);
}
return view("lucent::auth.login");
@@ -89,25 +83,24 @@ class AuthController
"email" => $request->input("email"),
"token" => $request->input("token"),
]);
}
public function postVerify(Request $request)
{
try {
$this->authService->login($request->input("email"), $request->input("token"));
$this->authService->login(
$request->input("email"),
$request->input("token"),
);
} catch (LucentException $th) {
return ResponseFormError::fromException($th);
}
return [];
}
public function logout(): RedirectResponse
{
$this->session->flush();
return redirect($this->channelService->channel->lucentUrl . "/login");
}
}
+10 -10
View File
@@ -14,16 +14,13 @@ use function Lucent\Response\ok;
class HomeController extends Controller
{
public function __construct(
private readonly Svelte $svelte,
private readonly Svelte $svelte,
private readonly AccountService $accountService,
private readonly Query $query,
)
{
}
private readonly Query $query,
) {}
public function home(): View
{
return $this->svelte->render(
layout: "channel",
view: "homeIndex",
@@ -38,10 +35,13 @@ class HomeController extends Controller
$sort = data_get($urlParams, "sort") ?? "-_sys.updatedAt";
$filter = data_get($urlParams, "filter") ?? [];
$arguments = array_merge([
"schema_in" => $this->accountService->currentReadableSchemas(),
"status_in" => ["draft", "published"]
], $filter);
$arguments = array_merge(
[
"schema_in" => $this->accountService->currentReadableSchemas(),
"status_in" => ["draft", "published"],
],
$filter,
);
$limit = 20;
+165 -90
View File
@@ -27,23 +27,26 @@ use function Lucent\Response\ok;
class RecordController extends Controller
{
public function __construct(
private readonly RecordService $recordService,
private readonly AccountService $accountService,
private readonly ChannelService $channelService,
private readonly Svelte $svelte,
private readonly Query $query,
private readonly Manager $recordManager,
private readonly RecordService $recordService,
private readonly AccountService $accountService,
private readonly ChannelService $channelService,
private readonly Svelte $svelte,
private readonly Query $query,
private readonly Manager $recordManager,
private readonly OperatorRegistry $operatorRegistry,
private readonly ViewModel $viewModel,
)
{
}
) {}
public function index(Request $request)
{
$schemaName = $request->route("schemaName");
if (!in_array($schemaName, $this->accountService->currentReadableSchemas())) {
if (
!in_array(
$schemaName,
$this->accountService->currentReadableSchemas(),
)
) {
return $this->svelte->render(
layout: "channel",
view: "recordNotFound",
@@ -53,16 +56,18 @@ class RecordController extends Controller
$users = $this->accountService->all();
$schema = $this->channelService->getSchema($schemaName)->get();
$urlParams = $request->all();
$sort = data_get($urlParams, "sort") ?? $schema->sortBy;
$filter = data_get($urlParams, "filter") ?? [];
$arguments = array_merge([
"schema" => $schema->name,
"status_in" => "draft,published",
], $filter);
$arguments = array_merge(
[
"schema" => $schema->name,
"status_in" => "draft,published",
],
$filter,
);
$skip = data_get($urlParams, "skip") ?? 0;
$limit = 30;
@@ -81,7 +86,6 @@ class RecordController extends Controller
->parentsDepth(0)
->runWithCount();
$records = $graph->getRootRecords()->toArray();
$data = [
@@ -93,45 +97,64 @@ class RecordController extends Controller
"systemFields" => array_values(System::list()),
"operators" => $this->operatorRegistry->all(),
"sortParam" => $sort,
"sortField" => $schema->fields->merge(array_values(System::list()))->firstWhere(fn($field) => $field->name === $sort || "-" . $field->name === $sort || "data." . $field->name === $sort || "-data." . $field->name === $sort),
"sortField" => $schema->fields
->merge(array_values(System::list()))
->firstWhere(
fn($field) => $field->name === $sort ||
"-" . $field->name === $sort ||
"data." . $field->name === $sort ||
"-data." . $field->name === $sort,
),
"limit" => $limit,
"skip" => $skip,
"total" => $graph->total ?? 0,
"filter" => $request->input("filter") ?? [],
"inModal" => true,
"isWritable" => in_array($schemaName, $this->accountService->currentWritableSchemas())
"isWritable" => in_array(
$schemaName,
$this->accountService->currentWritableSchemas(),
),
];
if ($request->ajax()) {
$data["modalUrl"] = $request->fullUrl();
if (str_starts_with(config("lucent.url"), "https")) {
$data["modalUrl"] = str_replace("http://", "https://", $request->fullUrl());
$data["modalUrl"] = str_replace(
"http://",
"https://",
$request->fullUrl(),
);
}
return $data;
}
$data["inModal"] = false;
return $this->svelte->render(
layout: "channel",
view: "contentIndex",
title: "Records",
data: $data
data: $data,
);
}
public function exportCSV(Request $request)
{
$schemaName = $request->route("schemaName");
$schema = $this->channelService->channel->schemas->where("name", $schemaName)->first();
$schema = $this->channelService->channel->schemas
->where("name", $schemaName)
->first();
$urlParams = $request->all();
$sort = data_get($urlParams, "sort") ?? "-_sys.updatedAt";
$filter = data_get($urlParams, "filter") ?? [];
$arguments = array_merge([
"schema" => $schema->name,
"status_in" => "draft,published",
], $filter);
$arguments = array_merge(
[
"schema" => $schema->name,
"status_in" => "draft,published",
],
$filter,
);
$records = $this->query
->filter($arguments)
@@ -143,53 +166,75 @@ class RecordController extends Controller
->run()
->tree();
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="' . $schemaName . '.csv";');
$handle = fopen('php://output', 'w');
header("Content-Type: application/csv");
header(
'Content-Disposition: attachment; filename="' .
$schemaName .
'.csv";',
);
$handle = fopen("php://output", "w");
$relationColumns = $this->makeCsvRelationColumns($schema);
$csvRow = ["id", ...array_keys($records[0]->data->toArray()),...$relationColumns];
fputcsv($handle, $csvRow, ',');
$csvRow = [
"id",
...array_keys($records[0]->data->toArray()),
...$relationColumns,
];
fputcsv($handle, $csvRow, ",");
foreach ($records as $record) {
$csvRow = [$record->id, ...$record->data->toArray()];
$csvRow = array_merge($csvRow,$this->makeCsvRelationColumnValues($schema,$record->_children));
$csvRow = array_merge(
$csvRow,
$this->makeCsvRelationColumnValues($schema, $record->_children),
);
$csvRow = array_values($csvRow);
fputcsv($handle, $csvRow, ',');
fputcsv($handle, $csvRow, ",");
}
fclose($handle);
echo $handle;
exit;
exit();
}
private function makeCsvRelationColumns($schema):array{
return $schema->fields->filter(fn($f) => get_class($f) === Reference::class)->reduce(function($c,$f){
$c[] = $f->name." id";
$c[] = $f->name." name";
return $c;
},[]);
private function makeCsvRelationColumns($schema): array
{
return $schema->fields
->filter(fn($f) => get_class($f) === Reference::class)
->reduce(function ($c, $f) {
$c[] = $f->name . " id";
$c[] = $f->name . " name";
return $c;
}, []);
}
private function makeCsvRelationColumnValues($schema, $children):array{
return $schema->fields->filter(fn($f) => get_class($f) === Reference::class)->reduce(function($c,$f) use($children){
$fieldRecords = data_get($children,$f->name);
if(empty($fieldRecords)){
$c[] = "";
$c[] = "";
}elseif (count($fieldRecords) === 1){
$c[] = data_get($fieldRecords,"0.id");
$c[] = $this->viewModel->getRecordName($fieldRecords[0]);
}else{
$c[] = collect($fieldRecords)->pluck("id")->join("::");
$c[] = collect($fieldRecords)->pluck("data.name")->join("::");
}
return $c;
},[]);
private function makeCsvRelationColumnValues($schema, $children): array
{
return $schema->fields
->filter(fn($f) => get_class($f) === Reference::class)
->reduce(function ($c, $f) use ($children) {
$fieldRecords = data_get($children, $f->name);
if (empty($fieldRecords)) {
$c[] = "";
$c[] = "";
} elseif (count($fieldRecords) === 1) {
$c[] = data_get($fieldRecords, "0.id");
$c[] = $this->viewModel->getRecordName($fieldRecords[0]);
} else {
$c[] = collect($fieldRecords)->pluck("id")->join("::");
$c[] = collect($fieldRecords)
->pluck("data.name")
->join("::");
}
return $c;
}, []);
}
public function new(Request $request)
{
if (!in_array($request->input("schema"), $this->accountService->currentWritableSchemas())) {
if (
!in_array(
$request->input("schema"),
$this->accountService->currentWritableSchemas(),
)
) {
return $this->svelte->render(
layout: "channel",
view: "recordNotFound",
@@ -197,8 +242,12 @@ class RecordController extends Controller
);
}
$schema = $this->channelService->channel->schemas->where("name", $request->input("schema"))->first();
$recordHistory = $this->recordManager->fromSession($request->session())->getRecords();
$schema = $this->channelService->channel->schemas
->where("name", $request->input("schema"))
->first();
$recordHistory = $this->recordManager
->fromSession($request->session())
->getRecords();
$record = $this->recordService->createEmpty($schema);
$queryRecord = QueryRecord::fromRecord($record);
return $this->svelte->render(
@@ -210,15 +259,22 @@ class RecordController extends Controller
"record" => $queryRecord,
"recordHistory" => $recordHistory,
"isCreateMode" => true,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
]
"isWritable" => in_array(
$record->schema,
$this->accountService->currentWritableSchemas(),
),
],
);
}
public function newInline(Request $request)
{
if (!in_array($request->input("schema"), $this->accountService->currentWritableSchemas())) {
if (
!in_array(
$request->input("schema"),
$this->accountService->currentWritableSchemas(),
)
) {
return $this->svelte->render(
layout: "channel",
view: "recordNotFound",
@@ -226,7 +282,9 @@ class RecordController extends Controller
);
}
$schema = $this->channelService->getSchema($request->input("schema"))->get();
$schema = $this->channelService
->getSchema($request->input("schema"))
->get();
$record = $this->recordService->createEmpty($schema);
$queryRecord = QueryRecord::fromRecord($record);
@@ -234,7 +292,10 @@ class RecordController extends Controller
"schema" => $schema,
"record" => $queryRecord,
"isCreateMode" => true,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
"isWritable" => in_array(
$record->schema,
$this->accountService->currentWritableSchemas(),
),
];
}
@@ -262,7 +323,12 @@ class RecordController extends Controller
$record = $graph->records->first();
if (!in_array($record->schema, $this->accountService->currentReadableSchemas())) {
if (
!in_array(
$record->schema,
$this->accountService->currentReadableSchemas(),
)
) {
return $this->svelte->render(
layout: "channel",
view: "recordNotFound",
@@ -271,7 +337,10 @@ class RecordController extends Controller
}
$schema = $this->channelService->getSchema($record->schema)->get();
$recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid);
$recordHistory = $this->recordManager
->fromSession($request->session())
->push($rid)
->getRecords($rid);
return $this->svelte->render(
layout: "channel",
view: "recordEdit",
@@ -282,8 +351,11 @@ class RecordController extends Controller
"record" => toArray($record),
"users" => $this->accountService->all(),
"recordHistory" => $recordHistory,
"isWritable" => in_array($record->schema, $this->accountService->currentWritableSchemas())
]
"isWritable" => in_array(
$record->schema,
$this->accountService->currentWritableSchemas(),
),
],
);
}
@@ -295,19 +367,20 @@ class RecordController extends Controller
if ($request->input("value")) {
if (in_array($request->input("ui"), ["text", "date"])) {
$arguments["data." . $request->input("field") . "_regex"] = $request->input("value");
$arguments[
"data." . $request->input("field") . "_regex"
] = $request->input("value");
} elseif ($request->input("ui") == "number") {
$arguments["data." . $request->input("field") . "_eqnum"] = floatval($request->input("value"));
$arguments[
"data." . $request->input("field") . "_eqnum"
] = floatval($request->input("value"));
} elseif ($request->input("ui") == "date") {
} elseif ($request->input("ui") == "search") {
$arguments["search_regex"] = $request->input("value");
}
}
$records = $this->query
->filter($arguments)
->limit(10)
->tree();
$records = $this->query->filter($arguments)->limit(10)->tree();
if ($records->isEmpty()) {
return ok([]);
@@ -320,7 +393,6 @@ class RecordController extends Controller
{
$recordId = $request->input("record.id");
try {
if ($request->input("isCreateMode")) {
$recordId = $this->recordService->create(
data: new RecordInputData(
@@ -329,15 +401,20 @@ class RecordController extends Controller
$request->input("record.data"),
Status::from($request->input("record.status")),
),
edges: array_map(EdgeInputData::fromArray(...), $request->input("edges") ?? [])
edges: array_map(
EdgeInputData::fromArray(...),
$request->input("edges") ?? [],
),
);
} else {
$this->recordService->updateWithEdges(
id: $request->input("record.id"),
data: $request->input("record.data"),
status: Status::from($request->input("record.status")),
edges: array_map(EdgeInputData::fromArray(...), $request->input("edges") ?? []),
edges: array_map(
EdgeInputData::fromArray(...),
$request->input("edges") ?? [],
),
);
}
@@ -347,7 +424,6 @@ class RecordController extends Controller
->childrenDepth(1)
->parentsDepth(1)
->run();
} catch (ValidatorException $th) {
return fail($th->getValidatorErrors());
} catch (LucentException $th) {
@@ -356,11 +432,9 @@ class RecordController extends Controller
return ok(toArray($newGraph));
}
public function clone(Request $request)
{
try {
$newRecordId = $this->recordService->clone(
recordId: $request->route("rid"),
);
@@ -374,10 +448,8 @@ class RecordController extends Controller
public function status(Request $request)
{
$ids = array_map(fn($rec) => $rec["id"], $request->input("records"));
$this->recordService->changeStatusBulk(
status: $request->route("status"),
recordsIds: $ids,
@@ -387,9 +459,12 @@ class RecordController extends Controller
public function emptyTrash(Request $request)
{
$this->recordService->emptyTrash($request->route("schemaName"));
return redirect($this->channelService->channel->lucentUrl . "/content/" . $request->route("schemaName"));
return redirect(
$this->channelService->channel->lucentUrl .
"/content/" .
$request->route("schemaName"),
);
}
public function delete(Request $request)
@@ -409,11 +484,11 @@ class RecordController extends Controller
try {
$this->recordService->rollback(
recordId: $request->route("rid"),
version: (int)$request->route("version")
version: (int) $request->route("version"),
);
} catch (ValidatorException $th) {
return fail($th->getFirstValidatorError());
} catch (LucentException|Throwable $th) {
} catch (LucentException | Throwable $th) {
return fail($th);
}
return ok();
+17 -9
View File
@@ -16,21 +16,29 @@ readonly class AuthMiddleware
private AuthService $authService,
private AccountService $accountService,
private ChannelService $channelService,
private ViewModel $viewModel
)
{
}
private ViewModel $viewModel,
) {}
public function handle(Request $request, Closure $next)
{
if (!$this->authService->isLoggedIn()) {
return redirect($this->channelService->channel->lucentUrl . "/login");
return redirect(
$this->channelService->channel->lucentUrl . "/login",
);
}
$this->authService->refreshSession();
View::share("channel",$this->channelService->channel);
View::share("user",session("user"));
View::share("schemas",$this->channelService->channel->schemas->whereIn("name",$this->accountService->currentReadableSchemas())->values());
View::share("viewModel",$this->viewModel);
View::share("channel", $this->channelService->channel);
View::share("user", toArray($this->authService->getCurrentUser()));
View::share(
"schemas",
$this->channelService->channel->schemas
->whereIn(
"name",
$this->accountService->currentReadableSchemas(),
)
->values(),
);
View::share("viewModel", $this->viewModel);
return $next($request);
}