2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Http\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Lucent\Account\AccountService;
|
|
|
|
|
use Lucent\Account\AuthService;
|
|
|
|
|
use Lucent\Account\UserRepo;
|
|
|
|
|
use Lucent\Channel\ChannelService;
|
|
|
|
|
use Lucent\LucentException;
|
|
|
|
|
use Lucent\Query\Operator;
|
|
|
|
|
use Lucent\Query\Query;
|
|
|
|
|
use Lucent\Record\Manager;
|
|
|
|
|
use Lucent\Record\QueryRecord;
|
|
|
|
|
use Lucent\Record\RecordService;
|
2023-10-04 13:32:30 +03:00
|
|
|
use Lucent\Schema\System;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Lucent\Schema\Validator\ValidatorException;
|
|
|
|
|
use Lucent\Svelte\Svelte;
|
|
|
|
|
use function Lucent\Response\fail;
|
|
|
|
|
use function Lucent\Response\ok;
|
|
|
|
|
|
|
|
|
|
class RecordController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly RecordService $recordService,
|
|
|
|
|
private readonly AccountService $accountService,
|
|
|
|
|
private readonly AuthService $authService,
|
|
|
|
|
private readonly ChannelService $channelService,
|
|
|
|
|
private readonly Svelte $svelte,
|
|
|
|
|
private readonly UserRepo $userRepo,
|
|
|
|
|
private readonly Query $query,
|
|
|
|
|
private readonly Manager $recordManager
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schemaName = $request->route("schemaName");
|
|
|
|
|
$users = $this->accountService->all();
|
2023-10-04 13:32:30 +03:00
|
|
|
$schema = $this->channelService->getSchema($schemaName)->get();
|
2023-10-02 23:10:49 +03:00
|
|
|
$urlParams = $request->all();
|
|
|
|
|
$sort = data_get($urlParams, "sort") ?? "-_sys.updatedAt";
|
|
|
|
|
$filter = data_get($urlParams, "filter") ?? [];
|
|
|
|
|
$arguments = array_merge([
|
2023-10-04 13:32:30 +03:00
|
|
|
"schema" => $schema->name,
|
|
|
|
|
"status_in" => "draft,published",
|
2023-10-02 23:10:49 +03:00
|
|
|
], $filter);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$skip = data_get($urlParams, "skip") ?? 0;
|
|
|
|
|
$limit = 15;
|
|
|
|
|
$records = [];
|
|
|
|
|
$graphArray = null;
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
$graph = $this->query
|
|
|
|
|
->filter($arguments)
|
|
|
|
|
->limit($limit)
|
|
|
|
|
->status(explode(",", $arguments["status_in"]))
|
|
|
|
|
->skip($skip)
|
|
|
|
|
->sort($sort)
|
|
|
|
|
->childrenDepth(1)
|
|
|
|
|
->parentsDepth(0)
|
|
|
|
|
->runWithCount();
|
|
|
|
|
|
|
|
|
|
$records = $graph->getRootRecords()->toArray();
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
"schemas" => $this->channelService->channel->schemas,
|
|
|
|
|
"schema" => $schema,
|
|
|
|
|
"users" => $users,
|
|
|
|
|
"records" => $records,
|
2023-10-04 13:32:30 +03:00
|
|
|
"graph" => toArray($graph),
|
2023-10-02 23:10:49 +03:00
|
|
|
"systemFields" => array_values(System::list()),
|
|
|
|
|
"operators" => array_values(Operator::list()),
|
|
|
|
|
"sort" => $sort,
|
|
|
|
|
"limit" => $limit,
|
|
|
|
|
"skip" => $skip,
|
2023-10-04 13:32:30 +03:00
|
|
|
"total" => $graph->total ?? 0,
|
2023-10-02 23:10:49 +03:00
|
|
|
"filter" => $request->input("filter") ?? [],
|
|
|
|
|
"inModal" => true,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if ($request->ajax()) {
|
|
|
|
|
$data["modalUrl"] = $request->fullUrl();
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data["inModal"] = false;
|
|
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "channel",
|
|
|
|
|
view: "contentIndex",
|
|
|
|
|
title: "Records",
|
|
|
|
|
data: $data
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportCSV(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schemaName = $request->route("schemaName");
|
|
|
|
|
$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([
|
|
|
|
|
"_sys.schema" => $schema->name,
|
|
|
|
|
"_sys.status_in" => "draft,published",
|
|
|
|
|
], $filter);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$records = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$queryResult = $this->query
|
|
|
|
|
->filter($arguments)
|
|
|
|
|
// ->limit($limit)
|
|
|
|
|
->status(explode(",", $arguments["_sys.status_in"]))
|
|
|
|
|
// ->skip($skip)
|
|
|
|
|
->sort($sort)
|
|
|
|
|
->childrenDepth(0)
|
|
|
|
|
->parentsDepth(0)
|
|
|
|
|
->run();
|
|
|
|
|
|
|
|
|
|
$graph = $queryResult->getQueryRecords();
|
|
|
|
|
$records = $graph->getRootRecords()->toArray();
|
|
|
|
|
} catch (SubqueryNoResultException) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header('Content-Type: application/csv');
|
|
|
|
|
header('Content-Disposition: attachment; filename="' . $schemaName . '.csv";');
|
|
|
|
|
$handle = fopen('php://output', 'w');
|
|
|
|
|
$csvRow = ["id", ...array_keys($records[0]->data->toArray())];
|
|
|
|
|
fputcsv($handle, $csvRow, ',');
|
|
|
|
|
foreach ($records as $record) {
|
|
|
|
|
$csvRow = [$record->id, ...$record->data->toArray()];
|
|
|
|
|
$csvRow = array_values($csvRow);
|
|
|
|
|
fputcsv($handle, $csvRow, ',');
|
|
|
|
|
}
|
|
|
|
|
fclose($handle);
|
|
|
|
|
echo $handle;
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function new(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$schema = $this->channelService->channel->schemas->where("name", $request->input("schema"))->first();
|
|
|
|
|
$recordHistory = $this->recordManager->fromSession($request->session())->getRecords();
|
|
|
|
|
$record = $this->recordService->createEmpty($schema, $this->authService->currentUserId());
|
|
|
|
|
$queryRecord = QueryRecord::fromRecord($record);
|
|
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "channel",
|
|
|
|
|
view: "recordEdit",
|
|
|
|
|
title: "New Record",
|
|
|
|
|
data: [
|
|
|
|
|
"schema" => $schema,
|
|
|
|
|
"record" => $queryRecord,
|
|
|
|
|
"recordHistory" => $recordHistory,
|
|
|
|
|
"isCreateMode" => true,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// public function newInline(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
//
|
|
|
|
|
// $channel = ChannelRepo::current();
|
|
|
|
|
// $schema = $channel->schemas->where("name.value", $request->input("schema"))->first();
|
|
|
|
|
// $record = Record::createEmpty($schema, AuthService::currentUserId($request));
|
|
|
|
|
// $queryRecord = QueryRecord::fromRecord($record);
|
|
|
|
|
//
|
|
|
|
|
// return [
|
|
|
|
|
// "schemas" => $channel->schemas,
|
|
|
|
|
// "schema" => $schema,
|
|
|
|
|
// "record" => $queryRecord,
|
|
|
|
|
// "isCreateMode" => true,
|
|
|
|
|
// ];
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public function edit(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$rid = $request->route("rid");
|
|
|
|
|
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
$graph = $this->query
|
2023-10-02 23:10:49 +03:00
|
|
|
->filter(["id" => $rid])
|
|
|
|
|
->limit(1)
|
|
|
|
|
->skip(0)
|
|
|
|
|
->childrenDepth(2)
|
|
|
|
|
->childrenLimit(100)
|
|
|
|
|
->parentsDepth(1)
|
|
|
|
|
->parentsLimit(100)
|
|
|
|
|
->run();
|
|
|
|
|
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
if ($graph->records->isEmpty()) {
|
2023-10-02 23:10:49 +03:00
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "channel",
|
|
|
|
|
view: "recordNotFound",
|
|
|
|
|
title: "Record Not Found",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
$record = $graph->records->first();
|
|
|
|
|
$schema = $this->channelService->getSchema($record->schema)->get();
|
2023-10-02 23:10:49 +03:00
|
|
|
$recordHistory = $this->recordManager->fromSession($request->session())->push($rid)->getRecords($rid);
|
|
|
|
|
$users = $this->userRepo->all();
|
|
|
|
|
return $this->svelte->render(
|
|
|
|
|
layout: "channel",
|
|
|
|
|
view: "recordEdit",
|
|
|
|
|
title: "Edit Record",
|
|
|
|
|
data: [
|
|
|
|
|
"schema" => $schema,
|
2023-10-04 13:32:30 +03:00
|
|
|
"graph" => toArray($graph),
|
|
|
|
|
"record" => toArray($record),
|
2023-10-02 23:10:49 +03:00
|
|
|
"users" => $users,
|
|
|
|
|
"recordHistory" => $recordHistory,
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// public function editInline(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
// $channel = ChannelRepo::current();
|
|
|
|
|
// $rid = $request->route("rid");
|
|
|
|
|
//
|
|
|
|
|
// $queryResult = $this->query
|
|
|
|
|
// ->filter(["id" => $rid])
|
|
|
|
|
// ->limit(1)
|
|
|
|
|
// ->childrenDepth(2)
|
|
|
|
|
// ->parentsDepth(1)
|
|
|
|
|
// ->run();
|
|
|
|
|
//
|
|
|
|
|
// $graph = $queryResult->getQueryRecords($channel->schemas);
|
|
|
|
|
// $record = $graph->records[0];
|
|
|
|
|
// return ok(
|
|
|
|
|
// [
|
|
|
|
|
// "graph" => $graph->toArray(),
|
|
|
|
|
// "record" => $record->toArray(),
|
|
|
|
|
// ]
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// public function suggestions(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
// $arguments = [
|
|
|
|
|
// "_sys.schema" => $request->input("schema"),
|
|
|
|
|
// ];
|
|
|
|
|
//
|
|
|
|
|
// if ($request->input("value")) {
|
|
|
|
|
// if (in_array($request->input("ui"), ["text", "date"])) {
|
|
|
|
|
// $arguments[$request->input("field") . "_regex"] = $request->input("value");
|
|
|
|
|
// } elseif ($request->input("ui") == "number") {
|
|
|
|
|
// $arguments[$request->input("field") . "_eqnum"] = floatval($request->input("value"));
|
|
|
|
|
// } elseif ($request->input("ui") == "date") {
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// $queryResult = $this->query
|
|
|
|
|
// ->filter($arguments)
|
|
|
|
|
// ->limit(10)
|
|
|
|
|
// ->run();
|
|
|
|
|
//
|
|
|
|
|
// if (!$queryResult->hasResults()) {
|
|
|
|
|
// return ok([]);
|
|
|
|
|
// }
|
|
|
|
|
// $schemas = $this->schemaRepo->all();
|
|
|
|
|
// $graph = $queryResult->getQueryRecords($schemas);
|
|
|
|
|
//
|
|
|
|
|
// return ok($graph->records->toArray());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
public function save(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if ($request->input("isCreateMode")) {
|
|
|
|
|
$this->recordService->create(
|
2023-10-04 13:32:30 +03:00
|
|
|
schemaName: $request->input("record.schema"),
|
2023-10-02 23:10:49 +03:00
|
|
|
data: $request->input("record.data"),
|
|
|
|
|
id: $request->input("record.id"),
|
|
|
|
|
file: $request->input("record._file") ?? [],
|
|
|
|
|
edges: $request->input("edges"),
|
2023-10-04 13:32:30 +03:00
|
|
|
status: $request->input("record.status"),
|
2023-10-02 23:10:49 +03:00
|
|
|
uploadFromUrl: ""
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->recordService->update(
|
|
|
|
|
id: $request->input("record.id"),
|
|
|
|
|
data: $request->input("record.data"),
|
2023-10-04 13:32:30 +03:00
|
|
|
status: $request->input("record.status"),
|
2023-10-02 23:10:49 +03:00
|
|
|
edges: $request->input("edges"),
|
|
|
|
|
updateEdges: true,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 13:32:30 +03:00
|
|
|
$newGraph = $this->query
|
2023-10-02 23:10:49 +03:00
|
|
|
->filter(["id" => $request->input("record.id")])
|
|
|
|
|
->limit(10)
|
|
|
|
|
->childrenDepth(2)
|
|
|
|
|
->parentsDepth(1)
|
|
|
|
|
->run();
|
2023-10-04 13:32:30 +03:00
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
} catch (ValidatorException $th) {
|
|
|
|
|
return fail($th->getValidatorErrors());
|
|
|
|
|
} catch (LucentException $th) {
|
|
|
|
|
return fail($th);
|
|
|
|
|
}
|
2023-10-04 13:32:30 +03:00
|
|
|
return ok(toArray($newGraph));
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function clone(Request $request)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
$newRecordId = $this->recordService->clone(
|
|
|
|
|
recordId: $request->route("rid"),
|
|
|
|
|
);
|
|
|
|
|
} catch (LucentException $th) {
|
|
|
|
|
return fail($th);
|
|
|
|
|
} catch (ValidatorException $e) {
|
|
|
|
|
return fail($e);
|
|
|
|
|
}
|
|
|
|
|
return ok(["id" => $newRecordId]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function status(Request $request)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$ids = array_map(fn($rec) => $rec["id"], $request->input("records"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->recordService->changeStatusBulk(
|
|
|
|
|
status: $request->route("status"),
|
|
|
|
|
recordsIds: $ids,
|
|
|
|
|
);
|
|
|
|
|
return ok();
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// public function delete(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
// $ids = $request->input("ids");
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
// $this->recordService->deleteMany($ids);
|
|
|
|
|
// } catch (Throwable $th) {
|
|
|
|
|
// return fail($th);
|
|
|
|
|
// }
|
|
|
|
|
// return ok();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// public function rollback(Request $request)
|
|
|
|
|
// {
|
|
|
|
|
// try {
|
|
|
|
|
// $this->recordService->rollback(
|
|
|
|
|
// userId: AuthService::currentUserId($request),
|
|
|
|
|
// recordId: $request->route("rid"),
|
|
|
|
|
// version: (int)$request->route("version")
|
|
|
|
|
// );
|
|
|
|
|
// } catch (ValidatorException $th) {
|
|
|
|
|
// return fail($th->getFirstValidatorError());
|
|
|
|
|
// } catch (LucentException|Throwable $th) {
|
|
|
|
|
// return fail($th);
|
|
|
|
|
// }
|
|
|
|
|
// return ok();
|
|
|
|
|
// }
|
|
|
|
|
}
|