65 lines
No EOL
1.8 KiB
PHP
65 lines
No EOL
1.8 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);
|
|
|
|
$parsedUrl = parse_url($data->dialogUri);
|
|
$query = $parsedUrl['query'] ?? null;
|
|
parse_str($query, $queryParams);
|
|
$stepSlug = $queryParams['dialog'] ?? null;
|
|
|
|
$targetPageUri = $stepSlug ? $parsedUrl['path'] . '/' . $stepSlug : $parsedUrl['path'];
|
|
|
|
$project = page($parsedUrl['path']);
|
|
$page = page($targetPageUri);
|
|
$file = $page->file($data->fileName);
|
|
$user = kirby()->user($data->userUuid);
|
|
|
|
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
|
|
|
|
$commentData = [
|
|
"location" => [
|
|
"page" => $page,
|
|
"project" => $project,
|
|
"dialogUri" => $data->dialogUri,
|
|
"file" => $file
|
|
],
|
|
"position" => [
|
|
"pageIndex" => $data->position->pageIndex,
|
|
"x" => $data->position->x,
|
|
"y" => $data->position->y
|
|
],
|
|
"date" => (string) $data->date,
|
|
"text" => $data->text,
|
|
"author" => kirby()->user(),
|
|
"id" => Str::uuid(),
|
|
"type" => "comment",
|
|
];
|
|
|
|
$newComment = new Comment($commentData);
|
|
|
|
$comments[] = $newComment->toArray();
|
|
|
|
$newFile = $file->update([
|
|
'comments' => $comments
|
|
]);
|
|
|
|
echo json_encode(getFileData($newFile));
|
|
|
|
try {
|
|
$user->sendNotification($project, $commentData);
|
|
} catch (\Throwable $th) {
|
|
echo json_encode([
|
|
"error" => $th->getMessage() . " in " . $th->getFile() . " line " . $th->getLine(),
|
|
]);
|
|
}
|
|
|
|
exit;
|
|
}
|
|
]; |