|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Forms\Listener; |
| 11 | + |
| 12 | +use OCA\Forms\Constants; |
| 13 | +use OCA\Forms\Db\AnswerMapper; |
| 14 | +use OCA\Forms\Db\QuestionMapper; |
| 15 | +use OCA\Forms\Events\FormSubmittedEvent; |
| 16 | +use OCA\Forms\Service\OwnerNotificationMailService; |
| 17 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 18 | +use OCP\EventDispatcher\Event; |
| 19 | +use OCP\EventDispatcher\IEventListener; |
| 20 | +use OCP\IUserManager; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @implements IEventListener<FormSubmittedEvent> |
| 25 | + */ |
| 26 | +class OwnerNotificationListener implements IEventListener { |
| 27 | + public function __construct( |
| 28 | + private OwnerNotificationMailService $ownerNotificationMailService, |
| 29 | + private AnswerMapper $answerMapper, |
| 30 | + private QuestionMapper $questionMapper, |
| 31 | + private IUserManager $userManager, |
| 32 | + private LoggerInterface $logger, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function handle(Event $event): void { |
| 37 | + if (!($event instanceof FormSubmittedEvent)) { |
| 38 | + return; |
| 39 | + } |
| 40 | + if (!$event->isNewSubmission()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $form = $event->getForm(); |
| 45 | + if (!$form->getNotifyOwnerOnSubmission()) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + $submission = $event->getSubmission(); |
| 50 | + $owner = $this->userManager->get($form->getOwnerId()); |
| 51 | + $ownerMail = trim((string)$owner?->getEMailAddress()); |
| 52 | + if ($ownerMail === '') { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $answerSummaries = []; |
| 57 | + try { |
| 58 | + $answers = $this->answerMapper->findBySubmission($submission->getId()); |
| 59 | + } catch (DoesNotExistException $e) { |
| 60 | + return; |
| 61 | + } |
| 62 | + foreach ($answers as $answer) { |
| 63 | + try { |
| 64 | + $question = $this->questionMapper->findById($answer->getQuestionId()); |
| 65 | + } catch (DoesNotExistException $e) { |
| 66 | + $this->logger->warning('Question missing while preparing owner notification mail', [ |
| 67 | + 'formId' => $form->getId(), |
| 68 | + 'submissionId' => $submission->getId(), |
| 69 | + 'questionId' => $answer->getQuestionId(), |
| 70 | + ]); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + $questionType = $question->getType(); |
| 75 | + $answerText = trim($answer->getText() ?? ''); |
| 76 | + if ( |
| 77 | + $answerText !== '' |
| 78 | + && in_array($questionType, [Constants::ANSWER_TYPE_SHORT, Constants::ANSWER_TYPE_LONG], true) |
| 79 | + ) { |
| 80 | + $answerSummaries[] = [ |
| 81 | + 'question' => $question->getText(), |
| 82 | + 'answer' => $answerText, |
| 83 | + ]; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $this->ownerNotificationMailService->send( |
| 88 | + $form, |
| 89 | + $submission, |
| 90 | + [$ownerMail], |
| 91 | + $answerSummaries, |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments