add / show comments working

This commit is contained in:
isUnknown 2024-10-23 11:32:51 +02:00
parent a9992b0ff5
commit 6bffbc1707
10 changed files with 163 additions and 40 deletions

View file

@ -0,0 +1,32 @@
<?php
return [
'pattern' => '(:all)add-comment.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
// return json_encode($data->pageUri);
$page = page($data->pageUri);
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : Data::decode($file->comments()->toArray()['comments'], 'yaml');
$comments[$data->targetPage][$data->id] = [
'text' => $data->text,
'username' => (string) $user->name(),
'date' => (string) $data->date,
];
$newFile = $file->update([
'comments' => $comments
]);
return $comments;
}
];