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 [
|
2024-10-29 11:12:57 +01:00
|
|
|
'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);
|
|
|
|
|
|
2024-11-18 12:00:19 +01:00
|
|
|
$parsedUrl = parse_url($data->path);
|
|
|
|
|
$query = $parsedUrl['query'] ?? null;
|
|
|
|
|
parse_str($query, $queryParams);
|
|
|
|
|
$stepSlug = $queryParams['dialog'] ?? null;
|
|
|
|
|
|
|
|
|
|
$targetPageUri = $stepSlug ? $parsedUrl['path'] . '/' . $stepSlug : $parsedUrl['path'];
|
|
|
|
|
|
|
|
|
|
$page = page($targetPageUri);
|
2024-10-23 11:32:51 +02:00
|
|
|
$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 = [
|
2024-11-18 12:00:19 +01:00
|
|
|
'href' => $data->path,
|
2024-10-30 09:49:16 +01:00
|
|
|
'page' => $page,
|
|
|
|
|
'file' => $file,
|
2024-10-29 11:12:57 +01:00
|
|
|
'position' => [
|
2024-11-11 15:06:56 +01:00
|
|
|
'pageIndex' => $data->position->pageIndex,
|
2024-11-06 16:38:46 +01:00
|
|
|
'x' => $data->position->x,
|
|
|
|
|
'y' => $data->position->y
|
2024-10-29 11:12:57 +01:00
|
|
|
],
|
|
|
|
|
'replies' => [],
|
2024-11-27 17:51:49 +01:00
|
|
|
'text' => $data->text,
|
2024-10-30 12:15:28 +01:00
|
|
|
'author' => $user,
|
2024-10-23 11:32:51 +02:00
|
|
|
'date' => (string) $data->date,
|
2024-10-28 15:55:14 +01:00
|
|
|
'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);
|
|
|
|
|
|
2024-10-30 12:15:28 +01:00
|
|
|
$comments[] = $newComment->toArray();
|
2024-10-23 11:32:51 +02:00
|
|
|
|
|
|
|
|
$newFile = $file->update([
|
|
|
|
|
'comments' => $comments
|
|
|
|
|
]);
|
|
|
|
|
|
2024-11-17 12:01:20 +01:00
|
|
|
echo json_encode(getFileData($newFile));
|
|
|
|
|
|
2024-11-11 13:54:54 +01:00
|
|
|
try {
|
2024-11-21 19:43:49 +01:00
|
|
|
$user->sendNotification($page->parent(), $newComment->toArray());
|
2024-11-11 13:54:54 +01:00
|
|
|
} catch (\Throwable $th) {
|
2024-11-22 07:53:43 +01:00
|
|
|
echo json_encode([
|
|
|
|
|
"error" => $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine(),
|
|
|
|
|
]);
|
2024-11-11 13:54:54 +01:00
|
|
|
}
|
2024-11-17 12:01:20 +01:00
|
|
|
|
|
|
|
|
exit;
|
2024-10-23 11:32:51 +02:00
|
|
|
}
|
|
|
|
|
];
|