designtopack/public/site/plugins/comments/routes/reply.php

47 lines
1.1 KiB
PHP
Raw Normal View History

<?php
2024-10-30 09:56:16 +01:00
use adrienpayet\comments\Reply;
return [
2024-10-29 16:51:31 +01:00
'pattern' => '(:all)reply-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());
2024-10-30 09:56:16 +01:00
$data = [
'page' => $page,
'parentId' => $data->parentId,
'file' => $file,
'filePageIndex' => $data->filePageIndex,
'text' => $data->text,
2024-10-30 13:29:26 +01:00
'author' => $user,
'date' => (string) $data->date,
'id' => $data->id,
2024-10-29 17:26:23 +01:00
'type' => 'comment'
];
2024-10-30 09:56:16 +01:00
$newReply = new Reply($data);
2024-10-30 13:29:26 +01:00
foreach ($comments as &$comment) {
if ($comment['id'] === $newReply->parentId()) {
$comment['replies'][] = $newReply->toArray();
}
}
$newFile = $file->update([
'comments' => $comments
]);
2024-10-30 13:29:26 +01:00
$user->sendNotification($newReply);
return getFileData($newFile);
}
];