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

47 lines
1.3 KiB
PHP
Raw Normal View History

2024-10-23 11:32:51 +02:00
<?php
return [
'pattern' => '(:all)delete-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->location->page->uri);
$project = page($data->location->project->uri);
$file = $page->file($data->location->file->uuid);
2024-12-18 18:39:45 +01:00
$isReply = $data->location->parentId ?? false;
2024-10-23 11:32:51 +02:00
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 16:32:13 +01:00
foreach ($comments as $key => &$comment) {
if ($isReply) {
2024-12-18 18:39:45 +01:00
if ($comment['id'] === $data->location->parentId) {
2024-10-30 16:32:13 +01:00
foreach ($comment['replies'] as $replyKey => $reply) {
if ($reply['id'] === $data->id) {
unset($comment['replies'][$replyKey]);
$comment['replies'] = array_values($comment['replies']);
}
}
}
} else {
if ($comment['id'] === $data->id) {
unset($comments[$key]);
}
}
}
2024-11-17 12:01:20 +01:00
$comments = array_values($comments);
2024-10-30 16:32:13 +01:00
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-22 07:53:43 +01:00
kirby()->user()->deleteNotification($project, $data->id);
2024-10-23 11:32:51 +02:00
2024-11-17 12:01:20 +01:00
exit;
2024-10-23 11:32:51 +02:00
}
2024-10-30 16:32:13 +01:00
];