designtopack/public/site/plugins/comments/routes/reply.php
2024-12-20 12:42:46 +01:00

52 lines
No EOL
1.6 KiB
PHP

<?php
use adrienpayet\comments\Reply;
return [
'pattern' => '(:all)reply-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());
foreach ($comments as &$comment) {
$isParentComment = $comment['id'] === $data->parentId;
if ($isParentComment) {
$replyData = [
"location" => [
"page" => $page,
"project" => $project,
"dialogUri" => $data->dialogUri,
"file" => $file,
"parent" => $comment
],
"position" => [
"pageIndex" => $data->position->pageIndex ?? null,
],
"date" => (string) $data->date,
"text" => $data->text,
"author" => kirby()->user(),
"id" => Str::uuid(),
"type" => "comment-reply",
];
$newReply = new Reply($replyData);
$comment['replies'][] = $newReply->toArray();
}
}
$newFile = $file->update([
'comments' => $comments
]);
$user->sendNotification($project, $replyData);
return getFileData($newFile);
}
];