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

50 lines
1.2 KiB
PHP
Raw Normal View History

2024-10-23 11:32:51 +02:00
<?php
2024-10-30 09:49:16 +01:00
use adrienpayet\comments\Comment;
2024-10-23 11:32:51 +02:00
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-30 09:49:16 +01:00
$data = [
'page' => $page,
'file' => $file,
'filePageIndex' => $data->filePageIndex,
'position' => [
'x' => $data->position->x,
'y' => $data->position->y
],
'replies' => [],
2024-10-23 11:32:51 +02:00
'text' => $data->text,
'author' => $user,
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
2024-10-30 09:49:16 +01:00
$newComment = new Comment($data);
$comments[] = $newComment->toArray();
2024-10-23 11:32:51 +02:00
$newFile = $file->update([
'comments' => $comments
]);
try {
$user->sendNotification($page->parent()->uri(), $newComment->toArray());
} catch (\Throwable $th) {
throw new Exception($th->getMessage(), 1);
}
return getFileData($newFile);
2024-10-23 11:32:51 +02:00
}
];