diff --git a/src/Controller/ChatController.php b/src/Controller/ChatController.php index 88448f84..08b824de 100644 --- a/src/Controller/ChatController.php +++ b/src/Controller/ChatController.php @@ -238,23 +238,22 @@ public function showChat(Chat $chat, Request $request, ChatSeenByHelper $chatSee ]); } - $participantsForm = $this->createForm(ChatUserRecipientType::class, null, ['multiple' => false, 'required' => false]); + $participantsForm = $this->createForm(ChatUserRecipientType::class, null, ['multiple' => true, 'required' => false]); $participantsForm->handleRequest($request); if($this->isGranted(ChatVoter::Edit, $chat) && $participantsForm->isSubmitted() && $participantsForm->isValid()) { - $newParticipant = $participantsForm->getData(); + $newParticipants = $participantsForm->getData(); - if(!$chat->getParticipants()->contains($newParticipant)) { - $chat->getParticipants()->add($newParticipant); - $this->chatRepository->persist($chat); - - $dispatcher->dispatch(new ChatMessageCreatedEvent($chat->getMessages()->first())); - - $this->addFlash('success', 'chat.participants.add.success'); - } else { - $this->addFlash('error', 'chat.participants.add.error'); + foreach ($newParticipants as $newParticipant) { + if (!$chat->getParticipants()->contains($newParticipant)) { + $chat->getParticipants()->add($newParticipant); + } } + $this->chatRepository->persist($chat); + //$dispatcher->dispatch(new ChatMessageCreatedEvent($chat->getMessages()->first())); + $this->addFlash('success', 'chat.participants.add.success'); + return $this->redirectToRoute('show_chat', [ 'uuid' => $chat->getUuid() ]); diff --git a/src/Doctrine/ChatMessageRecipientsSubscriber.php b/src/Doctrine/ChatMessageRecipientsSubscriber.php new file mode 100644 index 00000000..ffea830f --- /dev/null +++ b/src/Doctrine/ChatMessageRecipientsSubscriber.php @@ -0,0 +1,77 @@ +tokenStorage->getToken()?->getUser(); + + if(!$currentUser instanceof User) { + return; + } + + $uow = $args->getObjectManager()->getUnitOfWork(); + + foreach($uow->getScheduledCollectionUpdates() as $collectionUpdate) { + if(!$collectionUpdate->getOwner() instanceof Chat) { + continue; + } + + $chat = $collectionUpdate->getOwner(); + assert($chat instanceof Chat); + + $addedParticipants = [ ]; + $removedParticipants = [ ]; + + foreach($collectionUpdate->getInsertDiff() as $user) { + $addedParticipants[] = $user; + } + + foreach($collectionUpdate->getDeleteDiff() as $user) { + $removedParticipants[] = $user; + } + + $content = ''; + + if(count($addedParticipants) > 0) { + $content = $this->translator->trans('chat.participants.success.added', [ + '%users%' => implode(', ', array_map(fn(User $user) => $this->userStringConverter->convert($user, includeUsername: false), $addedParticipants)), + ]); + } + + if(count($removedParticipants) > 0) { + $content .= "\n\n" . $this->translator->trans('chat.participants.success.remove', [ + '%users%' => implode(', ', array_map(fn(User $user) => $this->userStringConverter->convert($user, includeUsername: false), $removedParticipants)), + ]); + } + + $chatMessage = (new ChatMessage()) + ->setChat($chat) + ->setContent(trim($content)) + ->setCreatedBy($currentUser); + + $this->entityCollector->collectForPersist($chatMessage); + } + } +} \ No newline at end of file diff --git a/src/EventSubscriber/DoctrineEntityCollector.php b/src/EventSubscriber/DoctrineEntityCollector.php new file mode 100644 index 00000000..c3c26950 --- /dev/null +++ b/src/EventSubscriber/DoctrineEntityCollector.php @@ -0,0 +1,63 @@ +collectedForPersist[] = $entity; + } + + public function collectForRemoval(object $entity): void { + $this->collectedForRemoval[] = $entity; + } + + private function persistRemoveAndFlush(): void { + foreach($this->collectedForPersist as $entity) { + $this->em->persist($entity); + } + + foreach($this->collectedForRemoval as $entity) { + $this->em->remove($entity); + } + + $this->em->flush(); + } + + public function onKernelTerminate(TerminateEvent $event): void { + $this->persistRemoveAndFlush(); + } + + public function onConsoleTerminate(ConsoleTerminateEvent $event): void { + $this->persistRemoveAndFlush(); + } + + public static function getSubscribedEvents(): array { + return [ + TerminateEvent::class => ['onKernelTerminate', 10], + ConsoleTerminateEvent::class => ['onConsoleTerminate', 10] + ]; + } +} \ No newline at end of file diff --git a/src/EventSubscriber/DoctrineEventsCollector.php b/src/EventSubscriber/DoctrineEventsCollector.php index 2d6fbe55..418f3a7b 100644 --- a/src/EventSubscriber/DoctrineEventsCollector.php +++ b/src/EventSubscriber/DoctrineEventsCollector.php @@ -41,14 +41,14 @@ public function onKernelTerminate(TerminateEvent $event): void { $this->dispatchAllEvents(); } - public function onConsoleTermine(ConsoleTerminateEvent $event): void { + public function onConsoleTerminate(ConsoleTerminateEvent $event): void { $this->dispatchAllEvents(); } public static function getSubscribedEvents(): array { return [ - TerminateEvent::class => 'onKernelTerminate', - ConsoleTerminateEvent::class => 'onConsoleTermine' + TerminateEvent::class => ['onKernelTerminate', 9 ], + ConsoleTerminateEvent::class => ['onConsoleTerminate', 9] ]; } } \ No newline at end of file diff --git a/templates/chat/show.html.twig b/templates/chat/show.html.twig index 0200f074..248559ca 100644 --- a/templates/chat/show.html.twig +++ b/templates/chat/show.html.twig @@ -206,12 +206,11 @@ {{ form_start(participantsForm) }} -