schema index
This commit is contained in:
@@ -5,7 +5,6 @@ namespace Lucent\Http\Controller;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Lucent\Account\AccountService;
|
||||
use Lucent\Account\AuthService;
|
||||
use Lucent\Channel\ChannelService;
|
||||
use Lucent\LucentException;
|
||||
use Lucent\Query\Operator\OperatorRegistry;
|
||||
@@ -24,31 +23,30 @@ use Lucent\ViewModel\ViewModel;
|
||||
use function Lucent\Response\fail;
|
||||
use function Lucent\Response\ok;
|
||||
|
||||
class RecordController extends Controller
|
||||
class RecordController
|
||||
{
|
||||
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())) {
|
||||
return $this->svelte->render(
|
||||
layout: "channel",
|
||||
view: "recordNotFound",
|
||||
title: "Schema Not Found",
|
||||
);
|
||||
if (
|
||||
!in_array(
|
||||
$schemaName,
|
||||
$this->accountService->currentReadableSchemas(),
|
||||
)
|
||||
) {
|
||||
return Svelte::view("recordNotFound", "Schema Not Found", []);
|
||||
}
|
||||
|
||||
$users = $this->accountService->all();
|
||||
@@ -57,12 +55,13 @@ class RecordController extends Controller
|
||||
$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 +80,6 @@ class RecordController extends Controller
|
||||
->parentsDepth(0)
|
||||
->runWithCount();
|
||||
|
||||
|
||||
$records = $graph->getRootRecords()->toArray();
|
||||
|
||||
$data = [
|
||||
@@ -93,45 +91,62 @@ 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",
|
||||
return Svelte::view(
|
||||
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 +158,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 +234,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 +251,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 +274,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 +284,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 +315,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 +329,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 +343,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 +359,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 +385,6 @@ class RecordController extends Controller
|
||||
{
|
||||
$recordId = $request->input("record.id");
|
||||
try {
|
||||
|
||||
if ($request->input("isCreateMode")) {
|
||||
$recordId = $this->recordService->create(
|
||||
data: new RecordInputData(
|
||||
@@ -329,15 +393,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 +416,6 @@ class RecordController extends Controller
|
||||
->childrenDepth(1)
|
||||
->parentsDepth(1)
|
||||
->run();
|
||||
|
||||
} catch (ValidatorException $th) {
|
||||
return fail($th->getValidatorErrors());
|
||||
} catch (LucentException $th) {
|
||||
@@ -356,11 +424,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 +440,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 +451,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 +476,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();
|
||||
|
||||
Reference in New Issue
Block a user