designtopack/public/site/config/routes/add-comment.php
2024-10-28 17:50:49 +01:00

36 lines
No EOL
982 B
PHP

<?php
return [
'pattern' => '(:all)add-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());
$newComment = [
'fileUuid' => (string) $file->uuid(),
'page' => $data->targetPage,
'text' => $data->text,
'username' => $user->name()->isNotEmpty() ? (string) $user->name() : (string) $user->email(),
'date' => (string) $data->date,
'id' => $data->id,
];
$comments[$data->targetPage][$data->id] = $newComment;
$newFile = $file->update([
'comments' => $comments
]);
$user->sendNotification('comments', $newComment);
return getFileData($newFile);
}
];