-
Notifications
You must be signed in to change notification settings - Fork 120
Add export/import functionality for forms #1425 #3380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,6 +148,8 @@ | |
| * Return a copy of the form if the parameter $fromId is set | ||
| * | ||
| * @param ?int $fromId (optional) Id of the form that should be cloned | ||
| * @param ?bool $import (optional) If it should import the form from post body | ||
| * @param ?array<string, mixed> $formData (optional) The formdata to import | ||
| * @return DataResponse<Http::STATUS_CREATED, FormsForm, array{}> | ||
| * @throws OCSForbiddenException The user is not allowed to create forms | ||
| * | ||
|
|
@@ -157,14 +159,19 @@ | |
| #[NoAdminRequired()] | ||
| #[BruteForceProtection(action: 'form')] | ||
| #[ApiRoute(verb: 'POST', url: '/api/v3/forms')] | ||
| public function newForm(?int $fromId = null): DataResponse { | ||
| public function newForm(?int $fromId = null, ?bool $import = false, ?array $formData = []): DataResponse { | ||
| // Check if user is allowed | ||
| if (!$this->configService->canCreateForms()) { | ||
| $this->logger->debug('This user is not allowed to create Forms.'); | ||
| throw new OCSForbiddenException('This user is not allowed to create Forms.'); | ||
| } | ||
|
|
||
| if ($fromId === null) { | ||
| // Validate mutually exclusive parameters | ||
| if ($fromId !== null && $import === true) { | ||
| throw new OCSBadRequestException('Cannot use both fromId and import parameters'); | ||
| } | ||
|
|
||
| if ($fromId === null && $import === false) { | ||
| // Create Form | ||
| $form = new Form(); | ||
| $form->setOwnerId($this->currentUser->getUID()); | ||
|
|
@@ -183,10 +190,24 @@ | |
|
|
||
| $this->formMapper->insert($form); | ||
| } else { | ||
| $oldForm = $this->formsService->getFormIfAllowed($fromId, Constants::PERMISSION_EDIT); | ||
| // Fill variables from json or database | ||
| if ($import) { | ||
| if (!isset($formData['questions']) || !\is_array($formData['questions'])) { | ||
| throw new OCSBadRequestException('Invalid form data: missing questions'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| } | ||
| $questions = $formData['questions']; | ||
| $oldConfirmationEmailQuestionId = $formData['confirmationEmailQuestionId'] ?? null; | ||
| unset($formData['questions']); | ||
| } else { | ||
| $oldForm = $this->formsService->getFormIfAllowed($fromId, Constants::PERMISSION_EDIT); | ||
|
|
||
| // Read old form, (un)set new form specific data, extend title | ||
| $formData = $oldForm->read(); | ||
| // Read old form, (un)set new form specific data, extend title | ||
| $formData = $oldForm->read(); | ||
| // Get Questions, set new formId, reinsert | ||
| $questions = $this->questionMapper->findByForm($oldForm->getId()); | ||
| $oldConfirmationEmailQuestionId = $oldForm->getConfirmationEmailQuestionId(); | ||
| } | ||
| // Remove unused data | ||
| unset($formData['id']); | ||
| unset($formData['created']); | ||
| unset($formData['lastUpdated']); | ||
|
|
@@ -199,7 +220,9 @@ | |
| $formData['ownerId'] = $this->currentUser->getUID(); | ||
| $formData['hash'] = $this->formsService->generateFormHash(); | ||
| // TRANSLATORS Appendix to the form Title of a duplicated/copied form. | ||
| $formData['title'] .= ' - ' . $this->l10n->t('Copy'); | ||
| if (!$import) { | ||
| $formData['title'] .= ' - ' . $this->l10n->t('Copy'); | ||
| } | ||
| $formData['access'] = [ | ||
| 'permitAllUsers' => false, | ||
| 'showToAllUsers' => false, | ||
|
|
@@ -213,26 +236,35 @@ | |
| $form = Form::fromParams($formData); | ||
| $this->formMapper->insert($form); | ||
|
|
||
| // Get Questions, set new formId, reinsert | ||
| $questions = $this->questionMapper->findByForm($oldForm->getId()); | ||
| $oldConfirmationEmailQuestionId = $oldForm->getConfirmationEmailQuestionId(); | ||
|
|
||
| foreach ($questions as $oldQuestion) { | ||
| $questionData = $oldQuestion->read(); | ||
| if ($import) { | ||
| if (!isset($oldQuestion['id'])) { | ||
|
Check failure on line 241 in lib/Controller/ApiController.php
|
||
| throw new OCSBadRequestException('Invalid question data: missing id'); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
| } | ||
| $questionData = $oldQuestion; | ||
| $oldQuestionId = $oldQuestion['id'] ?? []; | ||
| $options = $oldQuestion['options']; | ||
| } else { | ||
| $questionData = $oldQuestion->read(); | ||
| $oldQuestionId = $oldQuestion->getId(); | ||
| // Get Options, set new QuestionId, reinsert | ||
| $options = $this->optionMapper->findByQuestion($oldQuestionId); | ||
| } | ||
|
|
||
| unset($questionData['id']); | ||
| unset($questionData['options']); | ||
| unset($questionData['accept']); | ||
|
|
||
| $questionData['formId'] = $form->getId(); | ||
| $newQuestion = Question::fromParams($questionData); | ||
| $this->questionMapper->insert($newQuestion); | ||
|
|
||
| if (isset($oldConfirmationEmailQuestionId) && $oldConfirmationEmailQuestionId === $oldQuestion->getId()) { | ||
| if (isset($oldConfirmationEmailQuestionId) && $oldConfirmationEmailQuestionId === $oldQuestionId) { | ||
| $form->setConfirmationEmailQuestionId($newQuestion->getId()); | ||
| } | ||
|
|
||
| // Get Options, set new QuestionId, reinsert | ||
| $options = $this->optionMapper->findByQuestion($oldQuestion->getId()); | ||
| foreach ($options as $oldOption) { | ||
| $optionData = $oldOption->read(); | ||
| $optionData = $import ? $oldOption : $oldOption->read(); | ||
|
|
||
| unset($optionData['id']); | ||
| $optionData['questionId'] = $newQuestion->getId(); | ||
|
|
@@ -694,8 +726,10 @@ | |
| throw new OCSBadRequestException('Invalid extraSettings, will not update.'); | ||
| } | ||
|
|
||
| if ($form->getConfirmationEmailQuestionId() === $question->getId() | ||
| && !$question->isEmailType($keyValuePairs['type'] ?? null, $keyValuePairs['extraSettings'] ?? null)) { | ||
| if ( | ||
| $form->getConfirmationEmailQuestionId() === $question->getId() | ||
| && !$question->isEmailType($keyValuePairs['type'] ?? null, $keyValuePairs['extraSettings'] ?? null) | ||
| ) { | ||
| $form->setConfirmationEmailQuestionId(null); | ||
| } | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add this to the docblock