designtopack/public/site/plugins/comments/routes/create.php
2024-10-30 10:56:11 +01:00

47 lines
No EOL
1.1 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->pageUri);
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
$data = [
'page' => $page,
'file' => $file,
'filePageIndex' => $data->filePageIndex,
'position' => [
'x' => null,
'y' => null
],
'replies' => [],
'text' => $data->text,
'user' => $user,
'date' => (string) $data->date,
'id' => $data->id,
'type' => 'comment'
];
$newComment = new Comment($data);
$comments[$newComment->id()] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments
]);
$user->sendNotification('comments', $newComment);
return getFileData($newFile);
}
];