mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 02:23:51 +00:00
twig extension for listing notification on a given entity
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Notification;
|
||||
|
||||
use Chill\MainBundle\Entity\Notification;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Repository\NotificationRepository;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* Helps to find if a notification exist for a given entity
|
||||
*/
|
||||
class NotificationPresence
|
||||
{
|
||||
private Security $security;
|
||||
|
||||
private NotificationRepository $notificationRepository;
|
||||
|
||||
public function __construct(Security $security, NotificationRepository $notificationRepository)
|
||||
{
|
||||
$this->security = $security;
|
||||
$this->notificationRepository = $notificationRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|Notification[]
|
||||
*/
|
||||
public function getNotificationsForClassAndEntity(string $relatedEntityClass, int $relatedEntityId): array
|
||||
{
|
||||
if ($this->security->getUser() instanceof User) {
|
||||
return $this->notificationRepository->findNotificationAsAddresseeByRelatedEntityAndUser(
|
||||
$relatedEntityClass,
|
||||
$relatedEntityId,
|
||||
$this->security->getUser()
|
||||
);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Notification\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class NotificationTwigExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('chill_list_notifications', [NotificationTwigExtensionRuntime::class, 'listNotificationsFor'], [
|
||||
'needs_environment' => true,
|
||||
'is_safe' => ['html'],
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\MainBundle\Notification\Templating;
|
||||
|
||||
use Chill\MainBundle\Notification\NotificationPresence;
|
||||
use Twig\Environment;
|
||||
use Twig\Extension\RuntimeExtensionInterface;
|
||||
|
||||
class NotificationTwigExtensionRuntime implements RuntimeExtensionInterface
|
||||
{
|
||||
private NotificationPresence $notificationPresence;
|
||||
|
||||
public function __construct(NotificationPresence $notificationPresence)
|
||||
{
|
||||
$this->notificationPresence = $notificationPresence;
|
||||
}
|
||||
|
||||
public function listNotificationsFor(Environment $environment, string $relatedEntityClass, int $relatedEntityId, array $options = []): string
|
||||
{
|
||||
$notifications = $this->notificationPresence->getNotificationsForClassAndEntity($relatedEntityClass, $relatedEntityId);
|
||||
|
||||
if ([] === $notifications) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $environment->render('@ChillMain/Notification/extension_list_notifications_for.html.twig', [
|
||||
'notifications' => $notifications,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user