twig extension for listing notification on a given entity

This commit is contained in:
Julien Fastré 2021-12-30 01:02:16 +01:00
parent 1576507f7e
commit 7bc4ad9779
7 changed files with 148 additions and 3 deletions

View File

@ -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 [];
}
}

View File

@ -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'],
]),
];
}
}

View File

@ -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,
]);
}
}

View File

@ -133,6 +133,29 @@ final class NotificationRepository implements ObjectRepository
return $query->getQuery()->getResult();
}
/**
* @param string $relatedEntityClass
* @param int $relatedEntityId
* @param User $user
* @return array|Notification[]
*/
public function findNotificationAsAddresseeByRelatedEntityAndUser(string $relatedEntityClass, int $relatedEntityId, User $user): array
{
$qb = $this->repository->createQueryBuilder('n');
$qb
->select('n')
->where($qb->expr()->eq('n.relatedEntityClass', ':relatedEntityClass'))
->andWhere($qb->expr()->eq('n.relatedEntityId', ':relatedEntityId'))
->andWhere($qb->expr()->isMemberOf(':user', 'n.addressees'))
->setParameter('relatedEntityClass', $relatedEntityClass)
->setParameter('relatedEntityId', $relatedEntityId)
->setParameter('user', $user)
;
return $qb->getQuery()->getResult();
}
/**
* @param mixed|null $limit
* @param mixed|null $offset

View File

@ -0,0 +1,17 @@
<div class="list_notification_for">
{% for notification in notifications %}
<div class="notification {% if notification.isReadBy(app.user) %}read{% else %}unread{% endif %}">
<div>{{ 'notification.you were notified by %sender%'|trans({'%sender%': notification.sender|chill_entity_render_string }) }}</div>
<div>
<ul class="record_actions_small">
<li>
<span class="notification_toggle_read_status" data-notification-id="{{ notification.id }}" data-notification-current-is-read="{{ notification.isReadBy(app.user) }}"></span>
</li>
<li>
<a href="{{ chill_path_add_return_path('chill_main_notification_show', {'id': notification.id}) }}" class="btn btn-show"></a>
</li>
</ul>
</div>
</div>
{% endfor %}
</div>

View File

@ -16,3 +16,9 @@ services:
Chill\MainBundle\Notification\NotificationHandlerManager:
arguments:
$handlers: !tagged_iterator chill_main.notification_handler
Chill\MainBundle\Notification\NotificationPresence: ~
Chill\MainBundle\Notification\Templating\NotificationTwigExtension: ~
Chill\MainBundle\Notification\Templating\NotificationTwigExtensionRuntime: ~

View File

@ -13,14 +13,23 @@
{% endmacro %}
{% block js %}
{{ parent() }}
{{ encore_entry_script_tags('page_accompanying_course_index_person_locate') }}
{{ encore_entry_script_tags('page_accompanying_course_index_masonry') }}
{{ parent() }}
{{ encore_entry_script_tags('page_accompanying_course_index_person_locate') }}
{{ encore_entry_script_tags('page_accompanying_course_index_masonry') }}
{{ encore_entry_script_tags('mod_notification_toggle_read_status') }}
{% endblock %}
{% block css %}
{{ parent() }}
{{ encore_entry_link_tags('mod_notification_toggle_read_status') }}
{% endblock %}
{% block content %}
<div class="accompanyingcourse-resume">
{{ chill_list_notifications('Chill\\PersonBundle\\Entity\\AccompanyingPeriod', accompanyingCourse.id) }}
<div id="dashboards" class="row" data-masonry='{"percentPosition": true }'>
{% if 'DRAFT' == accompanyingCourse.step %}
<div class="col-4 warnings mb-4">