2024-10-23 11:32:51 +02:00
|
|
|
<?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');
|
|
|
|
|
|
2024-10-23 15:49:09 +02:00
|
|
|
$newComment = [
|
2024-10-23 11:32:51 +02:00
|
|
|
'text' => $data->text,
|
2024-10-23 15:45:04 +02:00
|
|
|
'username' => $user->name()->isNotEmpty() ? (string) $user->name() : (string) $user->email(),
|
2024-10-23 11:32:51 +02:00
|
|
|
'date' => (string) $data->date,
|
2024-10-23 16:19:33 +02:00
|
|
|
'id' => $data->id
|
2024-10-23 11:32:51 +02:00
|
|
|
];
|
2024-10-23 15:49:09 +02:00
|
|
|
|
|
|
|
|
$comments[$data->targetPage][$data->id] = $newComment;
|
2024-10-23 11:32:51 +02:00
|
|
|
|
|
|
|
|
$newFile = $file->update([
|
|
|
|
|
'comments' => $comments
|
|
|
|
|
]);
|
|
|
|
|
|
2024-10-23 16:19:33 +02:00
|
|
|
foreach (kirby()->users()->without($user) as $otherUser) {
|
|
|
|
|
try {
|
|
|
|
|
$notifications = $otherUser->notifications()->isNotEmpty() ? $otherUser->notifications()->toArray() : ["comments" => []];
|
|
|
|
|
$notifications['comments'][$data->id] = $newComment;
|
|
|
|
|
$otherUser->update([
|
|
|
|
|
"notifications" => $notifications
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
throw new Exception($th->getMessage(), 1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 15:45:04 +02:00
|
|
|
return getFileData($newFile);
|
2024-10-23 11:32:51 +02:00
|
|
|
}
|
|
|
|
|
];
|