designtopack/public/site/plugins/comments/routes/create.php
2025-02-09 10:07:15 +01:00

59 lines
No EOL
1.6 KiB
PHP

<?php
use adrienpayet\comments\Comment;
return [
'pattern' => '(:all)create-comment.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
$page = page($data->fileParentUri);
$project = $page->parent()->template() == "project" ? $page->parent() : $page->parent()->parent();
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
$commentData = [
"location" => [
"page" => $page,
"project" => $project,
"file" => $file
],
"position" => [
"x" => $data->position->x,
"y" => $data->position->y
],
"date" => (string) $data->date,
"text" => $data->text,
"author" => kirby()->user(),
"id" => Str::uuid(),
"type" => "comment",
];
if (isset($data->position->pageIndex)) {
$commentData["position"]["pageIndex"] = $data->position->pageIndex;
}
$newComment = new Comment($commentData);
$comments[] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments
]);
echo json_encode(getFileData($newFile));
try {
$project->createNotification($commentData);
} catch (\Throwable $th) {
throw new Exception($th->getMessage() . ". line " . $th->getLine() . " in file " . $th->getFile(), 1);
}
exit;
}
];