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,20 +50,9 @@ tabs:
- virtual-sample - virtual-sample
- physical-sample - physical-sample
# client:
# label: Client
# icon: account
# columns:
# - width: 1/1
# sections:
# client:
# type: fields
# fields:
# client:
# type: pages
# multiple: false
# query: site.find("clients").childrenAndDrafts
# subpages: false
# layout: cards
# size: medium
files: tabs/files files: tabs/files
notificationsTab:
icon: bell
fields:
notifications: fields/notifications

View file

@ -1,6 +1,8 @@
<?php <?php
class ProjectPage extends Page { use adrienpayet\notifications\NotificationsPage;
class ProjectPage extends NotificationsPage {
public function getSteps() { public function getSteps() {
$steps = []; $steps = [];

View file

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

View file

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

View file

@ -1,29 +1,33 @@
<?php <?php
load([ load([
'ProjectPage' => 'models/ProjectPage.php', "ProjectPage" => "models/ProjectPage.php",
], __DIR__); ], __DIR__);
F::loadClasses([ F::loadClasses([
'adrienpayet\\notifications\\Notification' => __DIR__ . '/src/Notification.php', // Own classes
'adrienpayet\\D2P\\data\\Position' => __DIR__ . '/../classes/Position.php', "adrienpayet\\notifications\\Notification" => __DIR__ . "/src/Notification.php",
'adrienpayet\\D2P\data\Author' => __DIR__ . '/../classes/Author.php', "adrienpayet\\notifications\\NotificationsPage" => __DIR__ . "/src/NotificationsPage.php",
'adrienpayet\\D2P\\data\\location\\Location' => __DIR__ . '/../classes/location/Location.php',
'adrienpayet\\D2P\\data\\location\\PageDetails' => __DIR__ . '/../classes/location/PageDetails.php', // Shared classes
'adrienpayet\\D2P\\data\\location\\ProjectDetails' => __DIR__ . '/../classes/location/ProjectDetails.php', "adrienpayet\\D2P\\data\\Position" => __DIR__ . "/../classes/Position.php",
'adrienpayet\\D2P\\data\\location\\FileDetails' => __DIR__ . '/../classes/location/FileDetails.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', [ Kirby::plugin("adrienpayet/pdc-notifications", [
'routes' => [ "routes" => [
require(__DIR__ . '/routes/readAll.php'), require(__DIR__ . "/routes/readAll.php"),
require(__DIR__ . '/routes/read.php') require(__DIR__ . "/routes/read.php")
], ],
'userMethods' => [ "userMethods" => [
'sendNotification' => require(__DIR__ . '/user-methods/send.php'), // "sendNotification" => require(__DIR__ . "/user-methods/send.php"),
'deleteNotification' => require(__DIR__ . '/user-methods/delete.php'), "deleteNotification" => require(__DIR__ . "/user-methods/delete.php"),
'readNotification' => require(__DIR__ . '/user-methods/read.php'), "readNotification" => require(__DIR__ . "/user-methods/read.php"),
'readAllNotifications' => require(__DIR__ . '/user-methods/readAll.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 <?php
return function($project, $notificationId) { return function($project, $notificationId) {
foreach ($project->managers() as $projectManager) { foreach (kirby()->users() as $user) {
try { try {
$notifications = $projectManager->notifications()->isNotEmpty() $notifications = $user->notifications()->isNotEmpty()
? Yaml::decode($projectManager->notifications()->value()) ? Yaml::decode($user->notifications()->value())
: []; : [];
foreach ($notifications as $key => $notification) { foreach ($notifications as $key => $notification) {
@ -13,7 +13,7 @@ return function($project, $notificationId) {
} }
} }
$projectManager->update([ $user->update([
'notifications' => Yaml::encode(array_values($notifications)) 'notifications' => Yaml::encode(array_values($notifications))
]); ]);
} catch (\Throwable $th) { } catch (\Throwable $th) {

View file

@ -1,22 +1,23 @@
<?php <?php
function getProjectData($project) { function getProjectData($project) {
return [ return [
'title' => $project->title()->value(), "title" => $project->title()->value(),
'url' => $project->url(), "url" => $project->url(),
'uri' => '/' . $project->uri(), "uri" => "/" . $project->uri(),
'modified' => $project->modified('Y-MM-d'), "modified" => $project->modified("Y-MM-d"),
'currentStep' => $project->currentStep()->value(), "currentStep" => $project->currentStep()->value(),
'status' => $project->status(), "status" => $project->status(),
'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '', "logo" => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : "",
'steps' => $project->getSteps(), "steps" => $project->getSteps(),
"notifications" => Yaml::decode($project->notifications()->value),
"uuid" => (string) $project->uuid() "uuid" => (string) $project->uuid()
]; ];
} }
try { try {
$children = $kirby->user()->role() == 'admin' ? $page->children()->map(fn($project) => getProjectData($project))->values() : $kirby->user()->projects()->toPages()->map(fn($project) => getProjectData($project))->values(); $children = $kirby->user()->role() == "admin" ? $page->children()->map(fn($project) => getProjectData($project))->values() : $kirby->user()->projects()->toPages()->map(fn($project) => getProjectData($project))->values();
} catch (\Throwable $th) { } catch (\Throwable $th) {
throw new Exception($th->getMessage() . ' line ' . $th->getLine(), 1); throw new Exception($th->getMessage() . " line " . $th->getLine(), 1);
$children = []; $children = [];
} }