diff --git a/Engine/Modules/Notifications/.meta/.phpstorm.meta.php b/Engine/Modules/Notifications/.meta/.phpstorm.meta.php new file mode 100644 index 000000000..9b878dd86 --- /dev/null +++ b/Engine/Modules/Notifications/.meta/.phpstorm.meta.php @@ -0,0 +1,11 @@ + \Oforge\Engine\Modules\Notifications\BackendNotificationService::class, + ])); + } + +} diff --git a/Engine/Modules/Notifications/Abstracts/AbstractNotificationService.php b/Engine/Modules/Notifications/Abstracts/AbstractNotificationService.php index 980d30582..617931b14 100644 --- a/Engine/Modules/Notifications/Abstracts/AbstractNotificationService.php +++ b/Engine/Modules/Notifications/Abstracts/AbstractNotificationService.php @@ -4,18 +4,21 @@ use Oforge\Engine\Modules\Core\Abstracts\AbstractDatabaseAccess; -abstract class AbstractNotificationService extends AbstractDatabaseAccess { - const ALL = "all"; - const UNSEEN = "unseen"; - const SEEN = "seen"; +abstract class AbstractNotificationService extends AbstractDatabaseAccess +{ + public const ALL = "all"; + public const UNSEEN = "unseen"; + public const SEEN = "seen"; - public function __construct($models) { + public function __construct($models) + { parent::__construct($models); } - abstract function getNotifications($userId, $selector = AbstractNotificationService::ALL); + abstract public function addNotification(int $userId, string $type, string $message, ?string $link = null); - abstract function addNotification($userId, $type, $message, $link); + abstract public function getUserNotifications(int $userId, string $selector = AbstractNotificationService::ALL) : array; + + abstract public function markAsSeen(int $id) : bool; - abstract function markAsSeen($id); } diff --git a/Engine/Modules/Notifications/Bootstrap.php b/Engine/Modules/Notifications/Bootstrap.php index e0a6ce770..412a707d6 100644 --- a/Engine/Modules/Notifications/Bootstrap.php +++ b/Engine/Modules/Notifications/Bootstrap.php @@ -2,7 +2,6 @@ namespace Oforge\Engine\Modules\Notifications; -use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\ORMException; use Oforge\Engine\Modules\AdminBackend\Core\Services\BackendNavigationService; use Oforge\Engine\Modules\Core\Abstracts\AbstractBootstrap; @@ -19,11 +18,13 @@ * * @package Oforge\Engine\Modules\Notifications */ -class Bootstrap extends AbstractBootstrap { +class Bootstrap extends AbstractBootstrap +{ /** * Bootstrap constructor. */ - public function __construct() { + public function __construct() + { $this->endpoints = [ BackendNotificationController::class, ]; @@ -40,19 +41,21 @@ public function __construct() { /** * @throws ServiceNotFoundException * @throws ORMException - * @throws OptimisticLockException * @throws ConfigElementAlreadyExistException * @throws ConfigOptionKeyNotExistException * @throws ParentNotFoundException */ - public function activate() { + public function activate() + { /** @var BackendNavigationService $backendNavigationService */ $backendNavigationService = Oforge()->Services()->get('backend.navigation'); - $backendNavigationService->add([ - 'name' => 'notifications', - 'order' => 1, - 'position' => 'topbar', - ]); + $backendNavigationService->add( + [ + 'name' => 'notifications', + 'order' => 1, + 'position' => 'topbar', + ] + ); } } diff --git a/Engine/Modules/Notifications/Controller/BackendNotificationController.php b/Engine/Modules/Notifications/Controller/BackendNotificationController.php index 712366376..57efbd1f5 100644 --- a/Engine/Modules/Notifications/Controller/BackendNotificationController.php +++ b/Engine/Modules/Notifications/Controller/BackendNotificationController.php @@ -2,7 +2,6 @@ namespace Oforge\Engine\Modules\Notifications\Controller; -use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\ORMException; use Oforge\Engine\Modules\AdminBackend\Core\Abstracts\SecureBackendController; use Oforge\Engine\Modules\Auth\Models\User\BackendUser; @@ -20,9 +19,11 @@ * @package Oforge\Engine\Modules\Notifications\Controller * @EndpointClass(path="/backend/notifications", name="backend_notifications", assetScope="Backend") */ -class BackendNotificationController extends SecureBackendController { +class BackendNotificationController extends SecureBackendController +{ - public function initPermissions() { + public function initPermissions() + { $this->ensurePermission('indexAction', BackendUser::ROLE_LOGGED_IN); } @@ -33,11 +34,11 @@ public function initPermissions() { * * @return Response * @throws ORMException - * @throws OptimisticLockException * @throws ServiceNotFoundException * @EndpointAction(path="/{id}") */ - public function indexAction(Request $request, Response $response, array $args) { + public function indexAction(Request $request, Response $response, array $args) + { if (isset($args['id'])) { /** @var BackendNotificationService $backendNotificationService */ $backendNotificationService = Oforge()->Services()->get('backend.notifications'); diff --git a/Engine/Modules/Notifications/Models/BackendNotification.php b/Engine/Modules/Notifications/Models/BackendNotification.php index a75e9b3f8..5dd162e22 100644 --- a/Engine/Modules/Notifications/Models/BackendNotification.php +++ b/Engine/Modules/Notifications/Models/BackendNotification.php @@ -2,6 +2,7 @@ namespace Oforge\Engine\Modules\Notifications\Models; +use DateTime; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\ManyToOne; use Oforge\Engine\Modules\Core\Abstracts\AbstractModel; @@ -11,7 +12,8 @@ * @ORM\Entity * @ORM\HasLifecycleCallbacks */ -class BackendNotification extends AbstractModel { +class BackendNotification extends AbstractModel +{ /** * @var int * @ORM\Column(name="id", type="integer", nullable=false) @@ -19,113 +21,114 @@ class BackendNotification extends AbstractModel { * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; - /** * @var string * @ORM\Column(name="notification_type", type="string", nullable=false) */ private $type; - /** * @var string * @ORM\Column(name="notification_message", type="string", nullable=false) */ private $message; - /** - * @var string + * @var string|null * @ORM\Column(name="link", type="string", nullable=true) */ - private $link; - + private $link = null; /** - * @var int + * @var int|null * @ORM\Column(name="user_id", type="integer", nullable=true) * @ManyToOne(targetEntity="\Oforge\Engine\Modules\Auth\Models\BackendUser", fetch="EXTRA_LAZY") */ private $userId = null; - + /** + * @var int|null + * @ORM\Column(name="role", type="integer", nullable=true) + */ + private $role = null; /** * @var bool * @ORM\Column(name="seen", type="boolean") */ private $seen = false; - /** - * @var \DateTime + * @var DateTime * @ORM\Column(name="timestamp", type="datetime", nullable=true); */ private $timestamp; - /** - * @var int - * @ORM\Column(name="role", type="integer", nullable=true) - */ - private $role = null; - /** * Triggered on insert * * @ORM\PrePersist - * @throws \Exception */ - public function onPrePersist() { - $this->timestamp = new \DateTime("now"); + public function onPrePersist() + { + $this->timestamp = new DateTime("now"); } /** * @return int */ - public function getId() : int { + public function getId() : int + { return $this->id; } /** * @return string */ - public function getType() : string { + public function getType() : string + { return $this->type; } /** * @return string | null */ - public function getLink() : ?string { + public function getLink() : ?string + { return $this->link; } /** - * @return int + * @return int|null */ - public function getUserId() : int { + public function getUserId() : ?int + { return $this->userId; } /** * @return bool */ - public function isSeen() : bool { + public function isSeen() : bool + { return $this->seen; } /** - * @return \DateTime|null + * @return DateTime */ - public function getTimestamp() : ?\DateTime { + public function getTimestamp() : DateTime + { return $this->timestamp; } /** - * @return int + * @return int|null */ - public function getRole() : int { + public function getRole() : ?int + { return $this->role; } /** * @return string */ - public function getMessage() : string { + public function getMessage() : string + { return $this->message; } @@ -134,29 +137,32 @@ public function getMessage() : string { * * @return BackendNotification */ - public function setType(string $type) : BackendNotification { + public function setType(string $type) : BackendNotification + { $this->type = $type; return $this; } /** - * @param string $link + * @param string|null $link * * @return BackendNotification */ - public function setLink(string $link) : BackendNotification { + public function setLink(?string $link) : BackendNotification + { $this->link = $link; return $this; } /** - * @param int $userId + * @param int|null $userId * * @return BackendNotification */ - public function setUserId(int $userId) : BackendNotification { + public function setUserId(?int $userId) : BackendNotification + { $this->userId = $userId; return $this; @@ -167,18 +173,20 @@ public function setUserId(int $userId) : BackendNotification { * * @return BackendNotification */ - public function setSeen(bool $seen) : BackendNotification { + public function setSeen(bool $seen) : BackendNotification + { $this->seen = $seen; return $this; } /** - * @param int $role + * @param int|null $role * * @return BackendNotification */ - public function setRole(int $role) : BackendNotification { + public function setRole(?int $role) : BackendNotification + { $this->role = $role; return $this; @@ -189,9 +197,11 @@ public function setRole(int $role) : BackendNotification { * * @return BackendNotification */ - public function setMessage(string $message) : BackendNotification { + public function setMessage(string $message) : BackendNotification + { $this->message = $message; return $this; } + } diff --git a/Engine/Modules/Notifications/Services/BackendNotificationService.php b/Engine/Modules/Notifications/Services/BackendNotificationService.php index 6d867bd19..4be4821a3 100644 --- a/Engine/Modules/Notifications/Services/BackendNotificationService.php +++ b/Engine/Modules/Notifications/Services/BackendNotificationService.php @@ -1,112 +1,138 @@ BackendNotification::class]); + public function __construct() + { + parent::__construct(BackendNotification::class); } - /** - * @param $id + * @param int $id * - * @return object|null + * @return BackendNotification|null */ - public function getNotificationById($id) { - return $this->repository()->find($id); + public function getNotificationById(int $id) : ?BackendNotification + { + /** @var BackendNotification|null $entity */ + $entity = $this->repository()->find($id); + + return $entity; } /** - * Returns array of user - * - * @param $userId - * @param string $selector + * @param int $userId + * @param string $type + * @param string $message + * @param string|null $link * - * @return array|object[] + * @throws ORMException */ - public function getNotifications($userId, $selector = AbstractNotificationService::ALL) { - switch ($selector) { - case AbstractNotificationService::ALL: - return $this->repository()->findBy(['userId' => $userId]); - break; - case AbstractNotificationService::SEEN; - return $this->repository()->findBy(['userId' => $userId, 'seen' => 1]); - break; - case AbstractNotificationService::UNSEEN: - return $this->repository()->findBy(['userId' => $userId, 'seen' => 0]); - break; - default: - return null; - } + public function addNotification(int $userId, string $type, string $message, ?string $link = null) + { + $link = empty($link) ? null : $link; + $this->entityManager()->create( + BackendNotification::create( + [ + 'userId' => $userId, + 'type' => $type, + 'message' => $message, + 'link' => $link, + ] + ) + ); } /** - * @param $userId - * @param $type - * @param $message - * @param $link + * @param int $role + * @param string $type + * @param string $message + * @param string|null $link * - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException + * @throws ORMException */ - public function addNotification($userId, $type, $message, $link = "") { - $notification = new BackendNotification(); - - $notification->setUserId($userId); - $notification->setType($type); - $notification->setMessage($message); - - if (is_string($link) && !empty($link)) { - $notification->setLink($link); - } - - $this->entityManager()->create($notification); + public function addRoleNotification(int $role, string $type, string $message, ?string $link = null) + { + $link = empty($link) ? null : $link; + $this->entityManager()->create( + BackendNotification::create( + [ + 'role' => $role, + 'type' => $type, + 'message' => $message, + 'link' => $link, + ] + ) + ); } /** - * @param $id + * @param $role * - * @throws \Doctrine\ORM\ORMException + * @return BackendNotification[] */ - public function markAsSeen($id) { - $notification = $this->repository()->find($id); + public function getRoleNotifications($role) : array + { + /** @var BackendNotification[] $entities */ + $entities = $this->repository()->findBy(['role' => $role]); - $notification->setSeen(true); - - $this->entityManager()->update($notification); + return $entities; } /** - * @param $role + * Returns array of user + * + * @param int $userId + * @param string $selector * - * @return array|object[] + * @return BackendNotification[] */ - public function getRoleNotifications($role) { - return $this->repository()->findBy(['role' => $role]); + public function getUserNotifications(int $userId, string $selector = AbstractNotificationService::ALL) : array + { + switch ($selector) { + case AbstractNotificationService::ALL: + $criteria = ['userId' => $userId]; + break; + case AbstractNotificationService::SEEN; + $criteria = ['userId' => $userId, 'seen' => true]; + break; + case AbstractNotificationService::UNSEEN: + default: + $criteria = ['userId' => $userId, 'seen' => false]; + } + /** @var BackendNotification[] $entities */ + $entities = $this->repository()->findBy($criteria); + + return $entities; } /** - * @param $role - * @param $type - * @param $message - * @param $link + * @param int $id * - * @throws \Doctrine\ORM\ORMException - * @throws \Doctrine\ORM\OptimisticLockException + * @return bool */ - public function addRoleNotification($role, $type, $message, $link) { - $notification = new BackendNotification(); - - $notification->setRole($role); - $notification->setType($type); - $notification->setMessage($message); - $notification->setLink($link); + public function markAsSeen(int $id) : bool + { + try { + $notification = $this->repository()->find($id); + if (isset($notification)) { + $notification->setSeen(true); + $this->entityManager()->update($notification); + + return true; + } + } catch (ORMException $exception) { + Oforge()->Logger()->logException($exception); + } - $this->entityManager()->create($notification); + return false; } } diff --git a/Engine/Modules/TemplateEngine/Extensions/Twig/BackendExtension.php b/Engine/Modules/TemplateEngine/Extensions/Twig/BackendExtension.php index ee7a6f424..db7dc9969 100644 --- a/Engine/Modules/TemplateEngine/Extensions/Twig/BackendExtension.php +++ b/Engine/Modules/TemplateEngine/Extensions/Twig/BackendExtension.php @@ -39,16 +39,11 @@ public function getFunctions() { * @throws ServiceNotFoundException */ public function getBackendNotifications() { - if (isset($_SESSION['auth'])) { - /** @var AuthService $authService */ - $authService = Oforge()->Services()->get('auth'); - $user = $authService->decode($_SESSION['auth']); - if (isset($user) && isset($user['id'])) { - /** @var BackendNotificationService $notificationService */ - $notificationService = Oforge()->Services()->get('backend.notifications'); + if (($userId = Oforge()->View()->get('user.id')) !== null) { + /** @var BackendNotificationService $notificationService */ + $notificationService = Oforge()->Services()->get('backend.notifications'); - return $notificationService->getNotifications($user['id'], AbstractNotificationService::UNSEEN); - } + return $notificationService->getUserNotifications($userId, AbstractNotificationService::UNSEEN); } return []; diff --git a/Themes/Base/Backend/Master/Components/PageHeader/Messages.twig b/Themes/Base/Backend/Master/Components/PageHeader/Messages.twig index f4fe8550b..0f0eb991b 100644 --- a/Themes/Base/Backend/Master/Components/PageHeader/Messages.twig +++ b/Themes/Base/Backend/Master/Components/PageHeader/Messages.twig @@ -6,4 +6,4 @@ {% endfor %} -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/Themes/Base/Backend/Master/Components/PageHeader/Topbar/Notifications.twig b/Themes/Base/Backend/Master/Components/PageHeader/Topbar/Notifications.twig index 6ad107580..3214676c9 100644 --- a/Themes/Base/Backend/Master/Components/PageHeader/Topbar/Notifications.twig +++ b/Themes/Base/Backend/Master/Components/PageHeader/Topbar/Notifications.twig @@ -1,27 +1,27 @@ - +{% set backend_notifications = backend_notifications() %} - {% if backend_notifications()|length > 0 %} - {{ backend_notifications()|length }} + {% if backend_notifications|length > 0 %} + {{ backend_notifications|length }} {% endif %}