diff --git a/public/site/blueprints/pages/project.yml b/public/site/blueprints/pages/project.yml index af26e15..421a01d 100644 --- a/public/site/blueprints/pages/project.yml +++ b/public/site/blueprints/pages/project.yml @@ -50,20 +50,9 @@ tabs: - virtual-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 + + notificationsTab: + icon: bell + fields: + notifications: fields/notifications diff --git a/public/site/models/project.php b/public/site/models/project.php index ad0ddc9..fa4d8da 100644 --- a/public/site/models/project.php +++ b/public/site/models/project.php @@ -1,6 +1,8 @@ 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; diff --git a/public/site/plugins/comments/routes/delete.php b/public/site/plugins/comments/routes/delete.php index f9dae6e..2e2a522 100644 --- a/public/site/plugins/comments/routes/delete.php +++ b/public/site/plugins/comments/routes/delete.php @@ -39,8 +39,8 @@ return [ echo json_encode(getFileData($newFile)); - kirby()->user()->deleteNotification($project, $data->id); - + $project->deleteNotification($data->id); + exit; } ]; diff --git a/public/site/plugins/notifications/index.php b/public/site/plugins/notifications/index.php index 0e05df4..ee316ec 100644 --- a/public/site/plugins/notifications/index.php +++ b/public/site/plugins/notifications/index.php @@ -1,29 +1,33 @@ '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") ] ]); diff --git a/public/site/plugins/notifications/routes/delete.php b/public/site/plugins/notifications/routes/delete.php new file mode 100644 index 0000000..109bef0 --- /dev/null +++ b/public/site/plugins/notifications/routes/delete.php @@ -0,0 +1,20 @@ + '(: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()); + } + } +]; \ No newline at end of file diff --git a/public/site/plugins/notifications/src/NotificationsPage.php b/public/site/plugins/notifications/src/NotificationsPage.php new file mode 100644 index 0000000..3494d5a --- /dev/null +++ b/public/site/plugins/notifications/src/NotificationsPage.php @@ -0,0 +1,37 @@ +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)) + ]); + } +} \ No newline at end of file diff --git a/public/site/plugins/notifications/user-methods/delete.php b/public/site/plugins/notifications/user-methods/delete.php index 48dcb21..85ad9c0 100644 --- a/public/site/plugins/notifications/user-methods/delete.php +++ b/public/site/plugins/notifications/user-methods/delete.php @@ -1,10 +1,10 @@ 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) { diff --git a/public/site/templates/projects.json.php b/public/site/templates/projects.json.php index 51c6563..c87efaa 100644 --- a/public/site/templates/projects.json.php +++ b/public/site/templates/projects.json.php @@ -1,22 +1,23 @@ $project->title()->value(), - 'url' => $project->url(), - 'uri' => '/' . $project->uri(), - 'modified' => $project->modified('Y-MM-d'), - 'currentStep' => $project->currentStep()->value(), - 'status' => $project->status(), - 'logo' => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : '', - 'steps' => $project->getSteps(), + "title" => $project->title()->value(), + "url" => $project->url(), + "uri" => "/" . $project->uri(), + "modified" => $project->modified("Y-MM-d"), + "currentStep" => $project->currentStep()->value(), + "status" => $project->status(), + "logo" => $project->client()->toPage() ? $project->client()->toPage()->logo()->toFile()->url() : "", + "steps" => $project->getSteps(), + "notifications" => Yaml::decode($project->notifications()->value), "uuid" => (string) $project->uuid() ]; } 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) { - throw new Exception($th->getMessage() . ' line ' . $th->getLine(), 1); + throw new Exception($th->getMessage() . " line " . $th->getLine(), 1); $children = []; }