designtopack/public/site/config/routes/add-comment.php

36 lines
944 B
PHP
Raw Normal View History

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);
$page = page($data->pageUri);
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
$comments = $file->comments()->isEmpty() == true ? [] : [];
2024-10-23 11:32:51 +02:00
2024-10-23 15:49:09 +02:00
$newComment = [
'fileUuid' => (string )$file->uuid(),
'page' => $data->targetPage,
2024-10-23 11:32:51 +02:00
'text' => $data->text,
'username' => $user->name()->isNotEmpty() ? (string) $user->name() : (string) $user->email(),
2024-10-23 11:32:51 +02:00
'date' => (string) $data->date,
'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
]);
$user->sendNotification('comments', $newComment);
return getFileData($newFile);
2024-10-23 11:32:51 +02:00
}
];