diff --git a/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationHandler.php b/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationHandler.php index 01d6d537d..f391de1ff 100644 --- a/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationHandler.php +++ b/src/Bundle/ChillActivityBundle/Notification/ActivityNotificationHandler.php @@ -25,7 +25,7 @@ final class ActivityNotificationHandler implements NotificationHandlerInterface $this->activityRepository = $activityRepository; } - public function getTemplate(array $options = []): string + public function getTemplate(Notification $notification, array $options = []): string { return '@ChillActivity/Activity/showInNotification.html.twig'; } diff --git a/src/Bundle/ChillMainBundle/Entity/NotificationComment.php b/src/Bundle/ChillMainBundle/Entity/NotificationComment.php index 792e4abcc..a26c41916 100644 --- a/src/Bundle/ChillMainBundle/Entity/NotificationComment.php +++ b/src/Bundle/ChillMainBundle/Entity/NotificationComment.php @@ -15,11 +15,14 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationInterface; use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface; use DateTimeImmutable; use DateTimeInterface; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Doctrine\ORM\Event\PreFlushEventArgs; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table("chill_main_notification_comment") + * @ORM\HasLifecycleCallbacks */ class NotificationComment implements TrackCreationInterface, TrackUpdateInterface { @@ -52,6 +55,13 @@ class NotificationComment implements TrackCreationInterface, TrackUpdateInterfac */ private ?Notification $notification = null; + /** + * Internal variable which detect if the comment is just persisted. + * + * @internal + */ + private bool $recentlyPersisted = false; + /** * @ORM\Column(type="datetime_immutable", nullable=true) */ @@ -98,6 +108,32 @@ class NotificationComment implements TrackCreationInterface, TrackUpdateInterfac return $this->updatedBy; } + /** + * @ORM\PreFlush + */ + public function onFlushMarkNotificationAsUnread(PreFlushEventArgs $eventArgs): void + { + if ($this->recentlyPersisted) { + foreach ($this->getNotification()->getAddressees() as $addressee) { + if ($this->getCreatedBy() !== $addressee) { + $this->getNotification()->markAsUnreadBy($addressee); + } + } + + if ($this->getNotification()->getSender() !== $this->getCreatedBy()) { + $this->getNotification()->markAsUnreadBy($this->getNotification()->getSender()); + } + } + } + + /** + * @ORM\PrePersist + */ + public function onPrePersist(LifecycleEventArgs $eventArgs): void + { + $this->recentlyPersisted = true; + } + public function setContent(string $content): self { $this->content = $content; diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationHandlerInterface.php b/src/Bundle/ChillMainBundle/Notification/NotificationHandlerInterface.php index 9ad462d9b..774609512 100644 --- a/src/Bundle/ChillMainBundle/Notification/NotificationHandlerInterface.php +++ b/src/Bundle/ChillMainBundle/Notification/NotificationHandlerInterface.php @@ -18,7 +18,7 @@ interface NotificationHandlerInterface /** * Return the template path (twig file). */ - public function getTemplate(array $options = []): string; + public function getTemplate(Notification $notification, array $options = []): string; /** * Return an array which will be passed as data for the template. diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationHandlerManager.php b/src/Bundle/ChillMainBundle/Notification/NotificationHandlerManager.php index 724c9a479..45ebcd30c 100644 --- a/src/Bundle/ChillMainBundle/Notification/NotificationHandlerManager.php +++ b/src/Bundle/ChillMainBundle/Notification/NotificationHandlerManager.php @@ -45,7 +45,7 @@ final class NotificationHandlerManager public function getTemplate(Notification $notification, array $options = []): string { - return $this->getHandler($notification, $options)->getTemplate($options); + return $this->getHandler($notification, $options)->getTemplate($notification, $options); } public function getTemplateData(Notification $notification, array $options = []): array diff --git a/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationHandler.php b/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationHandler.php index 0370fc00e..384daf07b 100644 --- a/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationHandler.php +++ b/src/Bundle/ChillPersonBundle/Notification/AccompanyingPeriodNotificationHandler.php @@ -25,7 +25,7 @@ final class AccompanyingPeriodNotificationHandler implements NotificationHandler $this->accompanyingPeriodRepository = $accompanyingPeriodRepository; } - public function getTemplate(array $options = []): string + public function getTemplate(Notification $notification, array $options = []): string { return 'ChillPersonBundle:AccompanyingPeriod:showInNotification.html.twig'; }