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

53 lines
1.4 KiB
PHP
Raw Normal View History

2024-10-23 11:32:51 +02:00
<?php
return [
'pattern' => '(:all)create-comment.json',
2024-10-23 11:32:51 +02:00
'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);
2024-10-28 16:52:59 +01:00
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
2024-10-23 11:32:51 +02:00
2024-10-23 15:49:09 +02:00
$newComment = [
2024-10-29 17:26:23 +01:00
'page' => [
'uri' => (string) $page->parent()->uri(),
'title' => (string) $page->parent()->title(),
],
'file' => [
'uuid' => (string) $file->uuid(),
'pageIndex' => $data->filePageIndex,
],
'position' => [
'x' => null,
'y' => null
],
'replies' => [],
2024-10-23 11:32:51 +02:00
'text' => $data->text,
'user' => [
'name' => (string) $user->name(),
'email' => (string) $user->email(),
'uuid' => (string) $user->uuid(),
'role' => (string) $user->role()
],
2024-10-23 11:32:51 +02:00
'date' => (string) $data->date,
'id' => $data->id,
2024-10-29 17:26:23 +01:00
'type' => 'comment'
2024-10-23 11:32:51 +02:00
];
2024-10-23 15:49:09 +02:00
$comments[$data->id] = $newComment;
2024-10-23 11:32:51 +02:00
$newFile = $file->update([
'comments' => $comments
]);
2024-10-29 17:26:23 +01:00
$user->sendNotification('comments', $newComment);
return getFileData($newFile);
2024-10-23 11:32:51 +02:00
}
];