diff --git a/config/services.yaml b/config/services.yaml index fd1400ec32d..4547030e614 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,6 +91,11 @@ services: $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor' + Chamilo\CoreBundle\State\MessageProcessor: + bind: + $persistProcessor: '@api_platform.doctrine.orm.state.persist_processor' + $removeProcessor: '@api_platform.doctrine.orm.state.remove_processor' + Chamilo\CoreBundle\EventSubscriber\AnonymousUserSubscriber: tags: - name: kernel.event_subscriber diff --git a/src/CoreBundle/Entity/Message.php b/src/CoreBundle/Entity/Message.php index 241be2e640d..efbbc4bd91b 100644 --- a/src/CoreBundle/Entity/Message.php +++ b/src/CoreBundle/Entity/Message.php @@ -18,6 +18,7 @@ use ApiPlatform\Metadata\Put; use Chamilo\CoreBundle\Repository\MessageRepository; use Chamilo\CoreBundle\State\MessageByGroupStateProvider; +use Chamilo\CoreBundle\State\MessageProcessor; use DateTime; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; @@ -56,7 +57,8 @@ denormalizationContext: [ 'groups' => ['message:write'], ], - security: "is_granted('ROLE_USER')" + security: "is_granted('ROLE_USER')", + processor: MessageProcessor::class, )] #[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])] #[ApiFilter( diff --git a/src/CoreBundle/State/MessageProcessor.php b/src/CoreBundle/State/MessageProcessor.php new file mode 100644 index 00000000000..717900a8e65 --- /dev/null +++ b/src/CoreBundle/State/MessageProcessor.php @@ -0,0 +1,66 @@ +removeProcessor->process($data, $operation, $uriVariables, $context); + } + + $message = $this->persistProcessor->process($data, $operation, $uriVariables, $context); + + assert($message instanceof Message); + + if ($operation instanceof Post) { + if (Message::MESSAGE_TYPE_INBOX === $message->getMsgType()) { + $this->sendMailForInboxMessage($message); + } + } + + return $message; + } + + private function sendMailForInboxMessage(Message $message): void + { + foreach ($message->getReceivers() as $receiver) { + $user = $receiver->getReceiver(); + + MessageManager::send_message( + $user->getId(), + $message->getTitle(), + $message->getContent(), + [], + [], + 0, + 0, + 0, + 0, + 0, + false, + 0, + true, + false, + Message::MESSAGE_TYPE_INBOX + ); + } + } +}