|
22 | 22 | use OCA\Forms\Db\SubmissionMapper; |
23 | 23 | use OCA\Forms\Db\UploadedFile; |
24 | 24 | use OCA\Forms\Db\UploadedFileMapper; |
| 25 | +use OCA\Forms\Events\FormSubmittedEvent; |
25 | 26 | use OCA\Forms\Exception\NoSuchFormException; |
26 | 27 | use OCA\Forms\ResponseDefinitions; |
27 | 28 | use OCA\Forms\Service\ConfigService; |
@@ -546,6 +547,10 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype |
546 | 547 | $questionData = $sourceQuestion->read(); |
547 | 548 | unset($questionData['id']); |
548 | 549 | $questionData['order'] = end($allQuestions)->getOrder() + 1; |
| 550 | + if (is_array($questionData['extraSettings'] ?? null) |
| 551 | + && ($questionData['extraSettings']['confirmationRecipient'] ?? false) === true) { |
| 552 | + $questionData['extraSettings']['confirmationRecipient'] = false; |
| 553 | + } |
549 | 554 |
|
550 | 555 | $newQuestion = Question::fromParams($questionData); |
551 | 556 | $this->questionMapper->insert($newQuestion); |
@@ -648,6 +653,12 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair |
648 | 653 | if (key_exists('extraSettings', $keyValuePairs) && !$this->formsService->areExtraSettingsValid($keyValuePairs['extraSettings'], $question->getType())) { |
649 | 654 | throw new OCSBadRequestException('Invalid extraSettings, will not update.'); |
650 | 655 | } |
| 656 | + $this->assertSingleConfirmationRecipientQuestion( |
| 657 | + $formId, |
| 658 | + $questionId, |
| 659 | + $question->getType(), |
| 660 | + is_array($keyValuePairs['extraSettings'] ?? null) ? $keyValuePairs['extraSettings'] : null, |
| 661 | + ); |
651 | 662 |
|
652 | 663 | // Create QuestionEntity with given Params & Id. |
653 | 664 | $question = Question::fromParams($keyValuePairs); |
@@ -1405,7 +1416,7 @@ public function newSubmission(int $formId, array $answers, string $shareHash = ' |
1405 | 1416 | $this->formMapper->update($form); |
1406 | 1417 |
|
1407 | 1418 | //Create Activity |
1408 | | - $this->formsService->notifyNewSubmission($form, $submission); |
| 1419 | + $this->formsService->notifyNewSubmission($form, $submission, FormSubmittedEvent::TRIGGER_CREATED); |
1409 | 1420 |
|
1410 | 1421 | if ($form->getFileId() !== null) { |
1411 | 1422 | $this->jobList->add(SyncSubmissionsWithLinkedFileJob::class, ['form_id' => $form->getId()]); |
@@ -1487,7 +1498,7 @@ public function updateSubmission(int $formId, int $submissionId, array $answers) |
1487 | 1498 | } |
1488 | 1499 |
|
1489 | 1500 | //Create Activity |
1490 | | - $this->formsService->notifyNewSubmission($form, $submission); |
| 1501 | + $this->formsService->notifyNewSubmission($form, $submission, FormSubmittedEvent::TRIGGER_UPDATED); |
1491 | 1502 |
|
1492 | 1503 | return new DataResponse($submissionId); |
1493 | 1504 | } |
@@ -1827,6 +1838,32 @@ private function checkAccessUpdate(array $keyValuePairs): void { |
1827 | 1838 | } |
1828 | 1839 | } |
1829 | 1840 |
|
| 1841 | + /** |
| 1842 | + * Ensure only one short-email question can be used as confirmation recipient in a form. |
| 1843 | + * |
| 1844 | + * @param array<string, mixed>|null $extraSettings |
| 1845 | + */ |
| 1846 | + private function assertSingleConfirmationRecipientQuestion(int $formId, int $questionId, string $questionType, ?array $extraSettings): void { |
| 1847 | + if ($questionType !== Constants::ANSWER_TYPE_SHORT || !is_array($extraSettings) || ($extraSettings['confirmationRecipient'] ?? false) !== true) { |
| 1848 | + return; |
| 1849 | + } |
| 1850 | + |
| 1851 | + $formQuestions = $this->questionMapper->findByForm($formId); |
| 1852 | + foreach ($formQuestions as $formQuestion) { |
| 1853 | + if ($formQuestion->getId() === $questionId) { |
| 1854 | + continue; |
| 1855 | + } |
| 1856 | + |
| 1857 | + $existingSettings = $formQuestion->getExtraSettings(); |
| 1858 | + $isExistingConfirmationRecipient = $formQuestion->getType() === Constants::ANSWER_TYPE_SHORT |
| 1859 | + && ($existingSettings['validationType'] ?? null) === 'email' |
| 1860 | + && ($existingSettings['confirmationRecipient'] ?? false) === true; |
| 1861 | + if ($isExistingConfirmationRecipient) { |
| 1862 | + throw new OCSBadRequestException('Only one confirmation recipient question is allowed per form'); |
| 1863 | + } |
| 1864 | + } |
| 1865 | + } |
| 1866 | + |
1830 | 1867 | /** |
1831 | 1868 | * Checks if the current user is allowed to archive/unarchive the form |
1832 | 1869 | */ |
|
0 commit comments