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

50 lines
No EOL
1.2 KiB
PHP

<?php
use adrienpayet\comments\Comment;
return [
'pattern' => '(:all)create-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 ? [] : Yaml::decode($file->comments()->value());
$data = [
'page' => $page,
'file' => $file,
'position' => [
'pageIndex' => $data->position->pageIndex,
'x' => $data->position->x,
'y' => $data->position->y
],
'replies' => [],
'text' => $data->text,
'author' => $user,
'date' => (string) $data->date,
'id' => $data->id,
'type' => 'comment'
];
$newComment = new Comment($data);
$comments[] = $newComment->toArray();
$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);
}
];