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

72 lines
2 KiB
PHP
Raw Normal View History

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 [
'pattern' => '(:all)create-comment.json',
'method' => 'POST',
2024-10-23 11:32:51 +02:00
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
// kirby()->impersonate('kirby');
2024-12-19 17:11:46 +01:00
$page = page($data->fileParentUri);
$project = $page->parent()->template() == 'project' ? $page->parent() : $page->parent()->parent();
2024-10-23 11:32:51 +02:00
$file = $page->file($data->fileName);
$user = kirby()->user($data->userUuid);
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
foreach ($comments as $existingComment) {
2025-02-19 09:38:01 +01:00
if
(
$existingComment['author']['email'] == $user->email() &&
$existingComment['text'] == $data->text &&
$existingComment['position']['x'] == $data->position->x &&
$existingComment['position']['y'] == $data->position->y
) {
return json_encode(null);
}
}
$commentData = [
'location' => [
'page' => $page,
'project' => $project,
'file' => $file,
],
'position' => [
'x' => $data->position->x,
'y' => $data->position->y,
],
'date' => (string) $data->date,
'text' => $data->text,
'author' => kirby()->user(),
'id' => Str::uuid(),
'type' => 'comment',
'readby' => [], // Pour le système de notifications dérivées
2024-10-23 11:32:51 +02:00
];
2024-10-23 15:49:09 +02:00
2024-12-19 17:11:46 +01:00
if (isset($data->position->pageIndex)) {
$commentData['position']['pageIndex'] = $data->position->pageIndex;
2024-12-19 16:03:20 +01:00
}
$newComment = new Comment($commentData);
2024-10-30 09:49:16 +01:00
$comments[] = $newComment->toArray();
2024-10-23 11:32:51 +02:00
$newFile = $file->update([
'comments' => $comments,
]);
2024-10-23 11:32:51 +02:00
2024-11-17 12:01:20 +01:00
echo json_encode(getFileData($newFile));
// Note: Les notifications sont maintenant dérivées des commentaires.
// Plus besoin d'appeler createNotification().
2024-11-17 12:01:20 +01:00
exit;
},
];