create and delete notification for a project working

This commit is contained in:
isUnknown 2025-01-10 15:38:57 +01:00
parent 8c6e21c707
commit 9222069ef5
9 changed files with 105 additions and 52 deletions

View file

@ -50,9 +50,9 @@ return [
echo json_encode(getFileData($newFile));
try {
$user->sendNotification($project, $commentData);
$project->createNotification($commentData);
} catch (\Throwable $th) {
throw new Exception($th->getMessage(), 1);
throw new Exception($th->getMessage() . ". Line " . $th->getLine() . " in file " . $th->getFile(), 1);
}
exit;

View file

@ -39,8 +39,8 @@ return [
echo json_encode(getFileData($newFile));
kirby()->user()->deleteNotification($project, $data->id);
$project->deleteNotification($data->id);
exit;
}
];

View file

@ -1,29 +1,33 @@
<?php
load([
'ProjectPage' => 'models/ProjectPage.php',
"ProjectPage" => "models/ProjectPage.php",
], __DIR__);
F::loadClasses([
'adrienpayet\\notifications\\Notification' => __DIR__ . '/src/Notification.php',
'adrienpayet\\D2P\\data\\Position' => __DIR__ . '/../classes/Position.php',
'adrienpayet\\D2P\data\Author' => __DIR__ . '/../classes/Author.php',
'adrienpayet\\D2P\\data\\location\\Location' => __DIR__ . '/../classes/location/Location.php',
'adrienpayet\\D2P\\data\\location\\PageDetails' => __DIR__ . '/../classes/location/PageDetails.php',
'adrienpayet\\D2P\\data\\location\\ProjectDetails' => __DIR__ . '/../classes/location/ProjectDetails.php',
'adrienpayet\\D2P\\data\\location\\FileDetails' => __DIR__ . '/../classes/location/FileDetails.php',
// Own classes
"adrienpayet\\notifications\\Notification" => __DIR__ . "/src/Notification.php",
"adrienpayet\\notifications\\NotificationsPage" => __DIR__ . "/src/NotificationsPage.php",
// Shared classes
"adrienpayet\\D2P\\data\\Position" => __DIR__ . "/../classes/Position.php",
"adrienpayet\\D2P\data\Author" => __DIR__ . "/../classes/Author.php",
"adrienpayet\\D2P\\data\\location\\Location" => __DIR__ . "/../classes/location/Location.php",
"adrienpayet\\D2P\\data\\location\\PageDetails" => __DIR__ . "/../classes/location/PageDetails.php",
"adrienpayet\\D2P\\data\\location\\ProjectDetails" => __DIR__ . "/../classes/location/ProjectDetails.php",
"adrienpayet\\D2P\\data\\location\\FileDetails" => __DIR__ . "/../classes/location/FileDetails.php",
]);
Kirby::plugin('adrienpayet/pdc-notifications', [
'routes' => [
require(__DIR__ . '/routes/readAll.php'),
require(__DIR__ . '/routes/read.php')
Kirby::plugin("adrienpayet/pdc-notifications", [
"routes" => [
require(__DIR__ . "/routes/readAll.php"),
require(__DIR__ . "/routes/read.php")
],
'userMethods' => [
'sendNotification' => require(__DIR__ . '/user-methods/send.php'),
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php'),
'readNotification' => require(__DIR__ . '/user-methods/read.php'),
'readAllNotifications' => require(__DIR__ . '/user-methods/readAll.php')
"userMethods" => [
// "sendNotification" => require(__DIR__ . "/user-methods/send.php"),
"deleteNotification" => require(__DIR__ . "/user-methods/delete.php"),
"readNotification" => require(__DIR__ . "/user-methods/read.php"),
"readAllNotifications" => require(__DIR__ . "/user-methods/readAll.php")
]
]);

View file

@ -0,0 +1,20 @@
<?php
return [
'pattern' => '(:all)delete-user-notification.json',
'method' => 'POST',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
try {
kirby()->user()->deleteNotification($data->notificationId);
return json_encode([
"status" => "success",
"message" => "Notification n°{$data->notificationId} correctly deleted for all \"{$$project->title()}\" managers."
]);
} catch (\Throwable $th) {
return json_encode($th->getMessage() . ' line ' . $th->getLine());
}
}
];

View file

@ -0,0 +1,37 @@
<?php
namespace adrienpayet\notifications;
use Kirby\Cms\Page;
use Kirby\Data\Yaml;
use adrienpayet\notifications\Notification;
class NotificationsPage extends Page {
public function createNotification($notificationData) {
$newNotification = new Notification($notificationData);
$notifications = $this->notifications()->isNotEmpty()
? Yaml::decode($this->notifications()->value())
: [];
$notifications[] = $newNotification->toArray();
$this->update([
'notifications' => $notifications
]);
}
public function deleteNotification($notificationId) {
$notifications = $this->notifications()->isNotEmpty()
? Yaml::decode($this->notifications()->value())
: [];
foreach ($notifications as $key => $notification) {
if ($notification['id'] === $notificationId) {
unset($notifications[$key]);
}
}
$this->update([
'notifications' => Yaml::encode(array_values($notifications))
]);
}
}

View file

@ -1,10 +1,10 @@
<?php
return function($project, $notificationId) {
foreach ($project->managers() as $projectManager) {
foreach (kirby()->users() as $user) {
try {
$notifications = $projectManager->notifications()->isNotEmpty()
? Yaml::decode($projectManager->notifications()->value())
$notifications = $user->notifications()->isNotEmpty()
? Yaml::decode($user->notifications()->value())
: [];
foreach ($notifications as $key => $notification) {
@ -13,7 +13,7 @@ return function($project, $notificationId) {
}
}
$projectManager->update([
$user->update([
'notifications' => Yaml::encode(array_values($notifications))
]);
} catch (\Throwable $th) {