create comment classes

This commit is contained in:
isUnknown 2024-10-30 09:49:16 +01:00
parent cd1f065962
commit f5ffe312e2
8 changed files with 246 additions and 63 deletions

View file

@ -22,7 +22,23 @@ m2unqxu0:
position:
x: null
y: null
replies: [ ]
replies:
m2vmx97a:
page:
uri: projects/miss-dior-blooming-bouquet
title: Miss Dior Blooming Bouquet
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
text: Test de réponse
user:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T09:48:49+01:00
id: m2vmx97a
type: comment
text: >
Un commentaire écrit par Adrien pour
tester.
@ -69,4 +85,50 @@ m2unytm0:
role: client
date: 2024-10-29T17:30:15+01:00
id: m2unytm0
type: comment
type: comment
m2vmw7ct:
page:
uri: >
projects/miss-dior-blooming-bouquet/client-brief
title: Brief client
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
replies: [ ]
text: >
encore un test après créaton classes
php comment
user:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T09:48:00+01:00
id: m2vmw7ct
type: comment
position:
x: null
y: null
m2vmwvmt:
page:
uri: >
projects/miss-dior-blooming-bouquet/client-brief
title: Brief client
file:
uuid: file://s0lNtRA0Z7ybTCWG
pageIndex: 1
replies: [ ]
text: >
encore un test après créaton classes
php comment
user:
name: Adrien Payet
email: adrien.payet@outlook.com
uuid: user://WWjXgPWk
role: admin
date: 2024-10-30T09:48:31+01:00
id: m2vmwvmt
type: comment
position:
x: null
y: null

View file

@ -0,0 +1,24 @@
{
"name": "adrienpayet/kirby4-comments",
"description": "Enable comment.",
"license": "MIT",
"type": "kirby-plugin",
"version": "0.0.0",
"authors": [
{
"name": "Adrien Payet",
"email": "contact@adrien-payet.fr"
}
],
"require": {
"getkirby/composer-installer": "^1.1"
},
"autoload": {
"psr-4": {
"Adrienpayet\\Comments\\": "src/"
}
},
"config": {
"optimize-autoloader": true
}
}

View file

@ -1,6 +1,12 @@
<?php
Kirby::plugin('adrienpayet/pdc-comments', [
F::loadClasses([
'adrienpayet\\comments\\BaseComment' => __DIR__ . '/src/BaseComment.php',
'adrienpayet\\comments\\Comment' => __DIR__ . '/src/Comment.php',
'adrienpayet\\comments\\Reply' => __DIR__ . '/src/Reply.php'
]);
Kirby::plugin('adrienpayet/kirby4-comments', [
'routes' => [
require(__DIR__ . '/routes/create.php'),
require(__DIR__ . '/routes/update.php'),

View file

@ -1,5 +1,7 @@
<?php
use adrienpayet\comments\Comment;
return [
'pattern' => '(:all)create-comment.json',
'method' => 'POST',
@ -14,33 +16,25 @@ return [
$comments = $file->comments()->isEmpty() == true ? [] : Yaml::decode($file->comments()->value());
$newComment = [
'page' => [
'uri' => (string) $page->parent()->uri(),
'title' => (string) $page->parent()->title(),
],
'file' => [
'uuid' => (string) $file->uuid(),
'pageIndex' => $data->filePageIndex,
],
$data = [
'page' => $page,
'file' => $file,
'filePageIndex' => $data->filePageIndex,
'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()
],
'user' => $user,
'date' => (string) $data->date,
'id' => $data->id,
'type' => 'comment'
];
$comments[$data->id] = $newComment;
$newComment = new Comment($data);
$comments[$newComment->id()] = $newComment->toArray();
$newFile = $file->update([
'comments' => $comments

View file

@ -0,0 +1,93 @@
<?php
namespace adrienpayet\comments;
class BaseComment {
protected $page;
protected $file;
protected $filePageIndex;
protected $position;
protected $replies;
protected $text;
protected $user;
protected $date;
protected $id;
protected $type;
public function __construct($data) {
$page = $data['page'];
$file = $data['file'];
$filePageIndex = $data['filePageIndex'];
$replies = $data['replies'] ?? [];
$text = $data['text'];
$user = $data['user'];
$date = $data['date'];
$id = $data['id'];
$type = $data['type'] ?? 'comment';
$this->page = [
'uri' => (string) $page->uri(),
'title' => (string) $page->title(),
];
$this->file = [
'uuid' => (string) $file->uuid(),
'pageIndex' => $filePageIndex,
];
$this->replies = $replies ?? [];
$this->text = $text;
$this->user = [
'name' => (string) $user->name(),
'email' => (string) $user->email(),
'uuid' => (string) $user->uuid(),
'role' => (string) $user->role(),
];
$this->date = $date;
$this->id = $id;
$this->type = $type;
}
public function page() {
return $this->page;
}
public function file() {
return $this->file;
}
public function replies() {
return $this->replies;
}
public function text() {
return $this->text;
}
public function user() {
return $this->user;
}
public function date() {
return $this->date;
}
public function id() {
return $this->id;
}
public function type() {
return $this->type;
}
public function toArray() {
return [
'page' => $this->page,
'file' => $this->file,
'replies' => $this->replies,
'text' => $this->text,
'user' => $this->user,
'date' => $this->date,
'id' => $this->id,
'type' => $this->type,
];
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace adrienpayet\comments;
class Comment extends BaseComment
{
protected $position;
public function __construct($data) {
parent::__construct($data);
$this->position = $data['position'] ?? null;
}
public function position() {
return $this->position;
}
public function toArray() {
$array = parent::toArray();
$array['position'] = $this->position;
return $array;
}
}

View file

@ -0,0 +1,24 @@
<?php
namespace adrienpayet\comments;
class Reply extends BaseComment {
protected $parentId;
public function __construct($data) {
parent::__construct($data);
$this->parentId = $data['parentId'];
}
public function parentId() {
return $this->parentId;
}
public function toArray() {
$array = parent::toArray();
$array['parentId'] = $this->parentId;
return $array;
}
}

View file

@ -1,44 +0,0 @@
<?php
Kirby::plugin('adrienpayet/pdc-user-methods', [
'userMethods' => [
'sendNotification' => function ($group, $data) {
foreach (kirby()->users()->not($this) as $otherUser) {
try {
$notifications = $otherUser->notifications()->isNotEmpty()
? Yaml::decode($otherUser->notifications()->value())
: [];
if (!isset($notifications[$group])) {
$notifications[$group] = [];
}
$data['isRead'] = false;
$notifications[$group][$data['id']] = $data;
$otherUser->update([
'notifications' => $notifications
]);
} catch (\Throwable $th) {
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
}
},
'readNotification' => function ($group, $notificationId) {
$notifications = Yaml::decode($this->notifications()->value());
try {
$notifications[$group][$notificationId]['isRead'] = true;
} catch (\Throwable $th) {
//throw $th;
}
$this->update([
"notifications" => $notifications
]);
return $notifications;
}
]
]);