diff --git a/src/Entity/User.php b/src/Entity/User.php index 76075f352683acc13f539fcb48745d7e399d34a1..fb166b64e1018fcc6dc9d0ca144b890155ddb107 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -92,6 +92,11 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface */ private $updatedAt; + /** + * @ORM\Column(type="datetime_immutable", nullable=true) + */ + private $loggedAt; + /** * @var boolean * @ORM\Column(type="boolean") @@ -331,8 +336,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface } /** - * @return Lexicon[] - */ + * @return Lexicon[] + */ public function getMyLexicons() { $result = []; @@ -344,6 +349,19 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $result; } + /** + * @return Lexicon[] + */ + public function getMyGroupLexicons() + { + $result = []; + foreach ($this->getMyGroups() as $group) { // Ajout des lexiques de groupes + $result[] = $group->getLexicon(); + } + + return $result; + } + /** * @return Group[] */ @@ -1230,4 +1248,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this; } + public function getLoggedAt(): ?\DateTimeImmutable + { + return $this->loggedAt; + } + + public function setLoggedAt(?\DateTimeImmutable $loggedAt): self + { + $this->loggedAt = $loggedAt; + + return $this; + } + } diff --git a/src/EventListener/SecuritySubscriber.php b/src/EventListener/SecuritySubscriber.php new file mode 100644 index 0000000000000000000000000000000000000000..d8f20c10fdbba9ce603207c0e887a1021ebdf69f --- /dev/null +++ b/src/EventListener/SecuritySubscriber.php @@ -0,0 +1,49 @@ +<?php + +namespace App\EventListener; + +use App\Entity\User; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Security\Http\Event\InteractiveLoginEvent; +use Symfony\Component\Security\Http\SecurityEvents; + +/** + * @package App\EventListener + */ +class SecuritySubscriber implements EventSubscriberInterface +{ + /** + * @var EntityManagerInterface + */ + private $em; + + /** + * @param EntityManagerInterface $em + */ + public function __construct(EntityManagerInterface $em) + { + $this->em = $em; + } + + /** + * @return array + */ + public static function getSubscribedEvents(): array + { + return [ + SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin', + ]; + } + + public function onSecurityInteractiveLogin(InteractiveLoginEvent $event): void + { + $user = $event->getAuthenticationToken()->getUser(); + if ($user instanceof User) { + $user->setLoginDate(new \DateTime()); + + $this->em->persist($user); + $this->em->flush(); + } + } +} \ No newline at end of file diff --git a/src/Manager/LexiconManager.php b/src/Manager/LexiconManager.php index c816da9c7431fe40d831f75dab321cff0f09de12..7a9c5f7324b874bbb39220d52f8fccd1ff6c88d7 100644 --- a/src/Manager/LexiconManager.php +++ b/src/Manager/LexiconManager.php @@ -89,4 +89,24 @@ class LexiconManager return $this->doctrine->getRepository(Entry::class)->getLastEntries($newWordsLexicon); } + + /** + * @param $language + * @return integer + */ + public function getEntriesAddedNbSinceLastLogging(Lexicon $lexicon, User $user) + { + $date = $user->getLoggedAt(); + return count($this->doctrine->getRepository(Entry::class)->getEntriesAddedSince($lexicon, $date)); + } + + /** + * @param $language + * @return integer + */ + public function getEntriesUpdatedNbSinceLastLogging(Lexicon $lexicon, User $user) + { + $date = $user->getLoggedAt(); + return count($this->doctrine->getRepository(Entry::class)->getEntriesUpdatedSince($lexicon, $date)); + } } diff --git a/src/Repository/EntryRepository.php b/src/Repository/EntryRepository.php index 2d2cef69c2917e551e2c86c48c89f52a9f5a69a2..309ca7f71227496e7ce0181aeb1cd2f5cb21cf74 100644 --- a/src/Repository/EntryRepository.php +++ b/src/Repository/EntryRepository.php @@ -106,6 +106,36 @@ class EntryRepository extends ServiceEntityRepository ; } + /** + * @return integer + */ + public function getEntriesAddedSince(Lexicon $lexicon, $date): array + { + return $this->createQueryBuilder('e') + ->andWhere('e.lexicon = :lexicon') + ->setParameter('lexicon', $lexicon) + ->andWhere('e.createdAt >= :date') + ->setParameter('date', $date) + ->getQuery() + ->getResult() + ; + } + + /** + * @return integer + */ + public function getEntriesUpdatedSince(Lexicon $lexicon, $date): array + { + return $this->createQueryBuilder('e') + ->andWhere('e.lexicon = :lexicon') + ->setParameter('lexicon', $lexicon) + ->andWhere('e.updatedAt >= :date') + ->setParameter('date', $date) + ->getQuery() + ->getResult() + ; + } + /** * @return Entry[] Returns an array of Entry objects */ diff --git a/templates/notifications.html.twig b/templates/notifications.html.twig index c3f0d30deb2d20f12e74066d3ab99d8313b06843..691035af4e148ca01345cd3127d0aee46ae0c820 100644 --- a/templates/notifications.html.twig +++ b/templates/notifications.html.twig @@ -28,12 +28,39 @@ {% endif %} <h2>{{ "Notifications permanentes"|trans }}</h2> - <div class="notifications-item"> <img src="https://img.icons8.com/flat_round/64/000000/vote-badge.png" alt="img"> - <div class="text"> - <h4>John Silvester</h4> - <p>+20 vista badge earned</p> + + {% for lexicon in app.user.myGroupLexicons %} + <div class="notifications-item d-flex justify-content-between align-items-center"> + <a href="{{ path('app_lexicon_show', {id: lexicon.id}) }}"> + <div class="d-flex justify-content-start align-items-center"> + {{ lexicon|badge }} + <div class="text ps-3"> + <h4>{{ lexicon }}</h4> + <p> + {{ "Entrées ajoutées:" }} {{ lexicon_manager.entriesAddedNbSinceLastLogging(lexicon, app.user) }} + {{ "Entrées modifiées:" }} {{ lexicon_manager.entriesUpdatedNbSinceLastLogging(lexicon, app.user) }} + </p> + </div> + </div> + </a> </div> + {% endfor %} + + <div class="notifications-item d-flex justify-content-between align-items-center"> + {% set newWordsLexicon = lexicon_manager.newWordsLexicon %} + <a href="{{ path('app_lexicon_show', {id: newWordsLexicon.id}) }}"> + <div class="d-flex justify-content-start align-items-center"> + {{ newWordsLexicon|badge }} + <div class="text ps-3"> + <h4>{{ "Propositions de mots dans l'agora" }}</h4> + <p> + {{ "Nombre:" }} {{ lexicon_manager.entriesAddedNbSinceLastLogging(newWordsLexicon, app.user) }} + </p> + </div> + </div> + </a> </div> + </div> {#{% macro actions(notification) %}#}