#68 - finish notification class. Content notification on validate brief working

This commit is contained in:
isUnknown 2024-12-18 13:56:21 +01:00
parent 3fb043210e
commit fa36c9ef4a
12 changed files with 246 additions and 24 deletions

View file

@ -4,4 +4,18 @@ home: /panel/pages/projects
fields:
notifications:
type: hidden
label: Notifications
type: structure
fields:
location:
type: object
fields:
page:
type: object
fields:
uri:
type: text
href:
type: url
type:
type: text

View file

@ -7,12 +7,12 @@ return [
$json = file_get_contents('php://input');
$data = json_decode($json);
$brief = page($data->briefUri);
$project = $brief->parent();
$href = $data->dialogUri ? $data->dialogUri : $brief->url();
$page = page($data->briefUri);
$project = $page->parent();
$dialogUri = $data->dialogUri ? $data->dialogUri : null;
try {
$newPage = $brief->update([
$newPage = $page->update([
'isValidated' => 'true'
]);
@ -21,20 +21,13 @@ return [
$notification = [
'location' => [
'href' => (string) $href,
'project' => [
'title' => (string) $project->title(),
'uri' => (string) $project->url()
]
'page' => $page,
'project' => $project,
'dialogUri' => (string) $dialogUri,
],
'date' => $dateTime->format('Y-m-d\TH:i:sP'),
'text' => "Brief (" . $project->title()->value() . ")",
'author' => [
'name' => (string) kirby()->user()->name(),
'email' => (string) kirby()->user()->email(),
'uuid' => (string) kirby()->user()->uuid(),
'role' => (string) kirby()->user()->role(),
],
'author' => kirby()->user(),
'id' => Str::uuid(),
'type' => 'content'
];
@ -46,7 +39,7 @@ return [
];
} catch (\Throwable $th) {
return [
"error" => "Can't validate '" . $brief->title()->value() . "' brief.",
"error" => "Can't validate '" . $page->title()->value() . "' brief.",
'details' => $th->getMessage()
];
}

View file

@ -0,0 +1,27 @@
<?php
namespace adrienpayet;
class Author
{
protected string $name;
protected string $email;
protected string $uuid;
protected string $role;
public function __construct($author) {
$this->name = (string) $author->name();
$this->email = (string) $author->email();
$this->uuid = (string) $author->uuid();
$this->role = (string) $author->role();
}
public function toArray() {
return [
"name" => $this->name,
"email" => $this->email,
"uuid" => $this->uuid,
"role" => $this->role,
];
}
}

View file

@ -4,6 +4,17 @@ load([
'ProjectPage' => 'models/ProjectPage.php',
], __DIR__);
F::loadClasses([
'adrienpayet\\notifications\\Notification' => __DIR__ . '/src/Notification.php',
'adrienpayet\\notifications\\location\\Location' => __DIR__ . '/src/location/Location.php',
'adrienpayet\\notifications\\location\\PageDetails' => __DIR__ . '/src/location/PageDetails.php',
'adrienpayet\\notifications\\location\\ProjectDetails' => __DIR__ . '/src/location/ProjectDetails.php',
'adrienpayet\\notifications\\location\\FileDetails' => __DIR__ . '/src/location/FileDetails.php',
'adrienpayet\\notifications\\Position' => __DIR__ . '/src/Position.php',
'adrienpayet\\Author' => __DIR__ . '/../classes/Author.php'
]);
Kirby::plugin('adrienpayet/pdc-notifications', [
'routes' => [
require(__DIR__ . '/routes/readAll.php'),

View file

@ -0,0 +1,50 @@
<?php
namespace adrienpayet\notifications;
use adrienpayet\notifications\location\Location;
use adrienpayet\Author;
class Notification
{
protected string $type;
protected Location $location;
protected string $text;
protected Author $author;
protected string $date;
protected string $id;
protected string $isRead;
protected ?Position $position = null;
public function __construct($data) {
$this->type = $data["type"];
$this->location = new Location($data["location"]);
$this->text = $data["text"];
$this->author = new Author($data["author"]);
$this->date = $data["date"];
$this->id = $data["id"];
$this->isRead = $data["isRead"];
if ($data["type"] === "comment") {
$this->position = new Position($data["position"]);
}
}
public function toArray() {
$array = [
"type" => $this->type,
"location" => $this->location->toArray(),
"text" => $this->text,
"author" => $this->author->toArray(),
"date" => $this->date,
"id" => $this->id,
"isRead" => $this->isRead,
];
if ($this->type === "comment") {
$array["position"] = $this->position->toArray();
}
return $array;
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace adrienpayet\notifications;
class Position
{
public int $pageIndex;
public float $x;
public float $y;
public function __construct(array $data)
{
$this->pageIndex = $data['pageIndex'];
$this->x = (float) $data['x'];
$this->y = (float) $data['y'];
}
public function toArray() {
return [
"pageIndex" => $this->pageIndex,
"x" => $this->x,
"y" => $this->y,
];
}
}

View file

@ -0,0 +1,22 @@
<?php
namespace adrienpayet\notifications\location;
class FileDetails
{
public string $uuid;
public string $url;
public function __construct(array $data)
{
$this->uuid = (string) $data->uuid();
$this->url = (string) $data->url();
}
public function toArray() {
return [
"uuid" => $this->uuid,
"url" => $this->url
];
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace adrienpayet\notifications\location;
class Location
{
protected PageDetails $page;
protected ?string $dialogUri = null;
protected ProjectDetails $project;
protected ?FileDetails $file = null;
public function __construct(array $data)
{
$this->page = new PageDetails($data["page"]);
$this->dialogUri = $data["dialogUri"];
$this->project = new ProjectDetails($data["project"]);
if (isset($data['file'])) {
$this->file = new FileDetails($data["file"]);
}
}
public function toArray() {
$array = [
"page" => $this->page->toArray(),
"project" => $this->project->toArray(),
];
if ($this->dialogUri) {
$array["dialogUri"] = $this->dialogUri;
$array["file"] = $this->file;
}
return $array;
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace adrienpayet\notifications\location;
use Kirby\Cms\Page;
class PageDetails
{
protected Page $page;
public function __construct(Page $page)
{
$this->page = $page;
}
public function toArray() {
return [
"uri" => (string) $this->page->uri(),
"title" => (string) $this->page->title(),
];
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace adrienpayet\notifications\location;
use Kirby;
class ProjectDetails
{
public string $title;
public string $uri;
public function __construct(Kirby\Cms\Page $page)
{
$this->title = (string) $page->title();
$this->uri = (string) $page->uri();
}
public function toArray() {
return [
"title" => $this->title,
"uri" => $this->uri
];
}
}

View file

@ -1,5 +1,7 @@
<?php
use adrienpayet\notifications\Notification;
/**
* Send a notification to all users who manage a specific project, excluding the user sending the notification.
*
@ -26,7 +28,8 @@
? Yaml::decode($otherUser->notifications()->value())
: [];
$notifications[] = $notificationData;
$newNotification = new Notification($notificationData);
$notifications[] = $newNotification->toArray();
$otherUser->update([
'notifications' => $notifications