refactor comment system into comments plugin

This commit is contained in:
isUnknown 2024-10-29 11:12:57 +01:00
parent 31844269c0
commit fb36329556
8 changed files with 134 additions and 101 deletions

View file

@ -0,0 +1,9 @@
<?php
Kirby::plugin('adrienpayet/pdc-comments', [
'routes' => [
require(__DIR__ . '/routes/create.php'),
require(__DIR__ . '/routes/update.php'),
require(__DIR__ . '/routes/delete.php'),
]
]);

View file

@ -0,0 +1,47 @@
<?php
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());
$newComment = [
'pageUri' => $data->pageUri,
'fileUuid' => (string) $file->uuid(),
'filePageTarget' => $data->targetPage,
'position' => [
'x' => null,
'y' => null
],
'replies' => [],
'text' => $data->text,
'user' => [
'name' => (string) $user->name(),
'email' => (string) $user->email(),
'uuid' => (string) $user->uuid(),
'role' => (string) $user->role()
],
'date' => (string) $data->date,
'id' => $data->id,
];
$comments[$data->id] = $newComment;
$newFile = $file->update([
'comments' => $comments
]);
$user->sendNotification('comments', $newComment);
return getFileData($newFile);
}
];

View file

@ -0,0 +1,25 @@
<?php
return [
'pattern' => '(:all)delete-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());
unset($comments[$data->id] );
$newFile = $file->update([
'comments' => $comments
]);
return getFileData($newFile);
}
];

View file

@ -0,0 +1,28 @@
<?php
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());
$comments[$data->id]['text'] = $data->text;
$comments[$data->id]['date'] = $data->date;
$newFile = $file->update([
'comments' => $comments
]);
// $user->sendNotification('comments', $comments[$data->id]);
return getFileData($newFile);
}
];