61 lines
No EOL
1.6 KiB
PHP
61 lines
No EOL
1.6 KiB
PHP
<?php
|
|
|
|
use adrienpayet\comments\Reply;
|
|
|
|
return [
|
|
'pattern' => '(:all)reply-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());
|
|
|
|
$replyData = [
|
|
"location" => [
|
|
"page" => $page,
|
|
"project" => $project,
|
|
"dialogUri" => $data->dialogUri,
|
|
"file" => $file,
|
|
],
|
|
"parentId" => $data->parentId,
|
|
"position" => [
|
|
"pageIndex" => $data->position->pageIndex,
|
|
],
|
|
"date" => (string) $data->date,
|
|
"text" => $data->text,
|
|
"author" => kirby()->user(),
|
|
"id" => Str::uuid(),
|
|
"type" => "comment",
|
|
];
|
|
|
|
$newReply = new Reply($replyData);
|
|
|
|
foreach ($comments as &$comment) {
|
|
if ($comment['id'] === $newReply->parentId()) {
|
|
$comment['replies'][] = $newReply->toArray();
|
|
}
|
|
}
|
|
|
|
$newFile = $file->update([
|
|
'comments' => $comments
|
|
]);
|
|
|
|
$user->sendNotification($project, $replyData);
|
|
|
|
return getFileData($newFile);
|
|
}
|
|
]; |