Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function getForms(string $type = 'owned'): DataResponse {
* 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> $form (optional) The formdata to import
* @return DataResponse<Http::STATUS_CREATED, FormsForm, array{}>
* @throws OCSForbiddenException The user is not allowed to create forms
*
Expand All @@ -157,14 +159,14 @@ public function getForms(string $type = 'owned'): DataResponse {
#[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 $form = []): 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) {
if ($fromId === null && $import === false) {
// Create Form
$form = new Form();
$form->setOwnerId($this->currentUser->getUID());
Expand All @@ -183,10 +185,22 @@ public function newForm(?int $fromId = null): DataResponse {

$this->formMapper->insert($form);
} else {
$oldForm = $this->formsService->getFormIfAllowed($fromId, Constants::PERMISSION_EDIT);

// Read old form, (un)set new form specific data, extend title
$formData = $oldForm->read();
$formData = [];
$questions = [];
if ($fromId !== null) {
$oldForm = $this->formsService->getFormIfAllowed($fromId, Constants::PERMISSION_EDIT);

// 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();
} else {
$questions = $form['questions'];
$oldConfirmationEmailQuestionId = $form['confirmationEmailQuestionId'];
unset($form['questions']);
$formData = $form;
}
unset($formData['id']);
unset($formData['created']);
unset($formData['lastUpdated']);
Expand All @@ -199,7 +213,9 @@ public function newForm(?int $fromId = null): DataResponse {
$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 ($fromId !== null) {
$formData['title'] .= ' - ' . $this->l10n->t('Copy');
}
$formData['access'] = [
'permitAllUsers' => false,
'showToAllUsers' => false,
Expand All @@ -213,26 +229,36 @@ public function newForm(?int $fromId = null): DataResponse {
$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 ($fromId !== null) {
$questionData = $oldQuestion->read();
$oldQuestionId = $oldQuestion->getId();
// Get Options, set new QuestionId, reinsert
$options = $this->optionMapper->findByQuestion($oldQuestionId);
} else {
$questionData = $oldQuestion;
$oldQuestionId = $oldQuestion['id'];
$options = $oldQuestion["options"];
}

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();
if ($fromId !== null) {
$optionData = $oldOption->read();
} else {
$optionData = $oldOption;
}

unset($optionData['id']);
$optionData['questionId'] = $newQuestion->getId();
Expand Down
15 changes: 15 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,21 @@
"nullable": true,
"default": null,
"description": "(optional) Id of the form that should be cloned"
},
"import": {
"type": "boolean",
"nullable": true,
"default": false,
"description": "(optional) If it should import the form from post body"
},
"form": {
"type": "object",
"nullable": true,
"default": {},
"description": "(optional) The formdata to import",
"additionalProperties": {
"type": "object"
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"markdown-it": "^14.2.0",
"p-queue": "^9.3.0",
"qrcode": "^1.5.4",
"semver": "^7.8.2",
"vue": "^3.5.22",
"vue-draggable-plus": "^0.6.1",
"vue-router": "^4.6.4"
Expand Down
Loading