@@ -148,6 +148,8 @@ public function getForms(string $type = 'owned'): DataResponse {
148148 * Return a copy of the form if the parameter $fromId is set
149149 *
150150 * @param ?int $fromId (optional) Id of the form that should be cloned
151+ * @param ?bool $import (optional) If it should import the form from post body
152+ * @param ?array<string, mixed> $formData (optional) The formdata to import
151153 * @return DataResponse<Http::STATUS_CREATED, FormsForm, array{}>
152154 * @throws OCSForbiddenException The user is not allowed to create forms
153155 *
@@ -157,14 +159,19 @@ public function getForms(string $type = 'owned'): DataResponse {
157159 #[NoAdminRequired()]
158160 #[BruteForceProtection(action: 'form ' )]
159161 #[ApiRoute(verb: 'POST ' , url: '/api/v3/forms ' )]
160- public function newForm (?int $ fromId = null ): DataResponse {
162+ public function newForm (?int $ fromId = null , ? bool $ import = false , ? array $ formData = [] ): DataResponse {
161163 // Check if user is allowed
162164 if (!$ this ->configService ->canCreateForms ()) {
163165 $ this ->logger ->debug ('This user is not allowed to create Forms. ' );
164166 throw new OCSForbiddenException ('This user is not allowed to create Forms. ' );
165167 }
166168
167- if ($ fromId === null ) {
169+ // Validate mutually exclusive parameters
170+ if ($ fromId !== null && $ import === true ) {
171+ throw new OCSBadRequestException ('Cannot use both fromId and import parameters ' );
172+ }
173+
174+ if ($ fromId === null && $ import === false ) {
168175 // Create Form
169176 $ form = new Form ();
170177 $ form ->setOwnerId ($ this ->currentUser ->getUID ());
@@ -183,10 +190,24 @@ public function newForm(?int $fromId = null): DataResponse {
183190
184191 $ this ->formMapper ->insert ($ form );
185192 } else {
186- $ oldForm = $ this ->formsService ->getFormIfAllowed ($ fromId , Constants::PERMISSION_EDIT );
193+ // Fill variables from json or database
194+ if ($ import ) {
195+ if (!isset ($ formData ['questions ' ]) || !\is_array ($ formData ['questions ' ])) {
196+ throw new OCSBadRequestException ('Invalid form data: missing questions ' );
197+ }
198+ $ questions = $ formData ['questions ' ];
199+ $ oldConfirmationEmailQuestionId = $ formData ['confirmationEmailQuestionId ' ] ?? null ;
200+ unset($ formData ['questions ' ]);
201+ } else {
202+ $ oldForm = $ this ->formsService ->getFormIfAllowed ($ fromId , Constants::PERMISSION_EDIT );
187203
188- // Read old form, (un)set new form specific data, extend title
189- $ formData = $ oldForm ->read ();
204+ // Read old form, (un)set new form specific data, extend title
205+ $ formData = $ oldForm ->read ();
206+ // Get Questions, set new formId, reinsert
207+ $ questions = $ this ->questionMapper ->findByForm ($ oldForm ->getId ());
208+ $ oldConfirmationEmailQuestionId = $ oldForm ->getConfirmationEmailQuestionId ();
209+ }
210+ // Remove unused data
190211 unset($ formData ['id ' ]);
191212 unset($ formData ['created ' ]);
192213 unset($ formData ['lastUpdated ' ]);
@@ -199,7 +220,9 @@ public function newForm(?int $fromId = null): DataResponse {
199220 $ formData ['ownerId ' ] = $ this ->currentUser ->getUID ();
200221 $ formData ['hash ' ] = $ this ->formsService ->generateFormHash ();
201222 // TRANSLATORS Appendix to the form Title of a duplicated/copied form.
202- $ formData ['title ' ] .= ' - ' . $ this ->l10n ->t ('Copy ' );
223+ if (!$ import ) {
224+ $ formData ['title ' ] .= ' - ' . $ this ->l10n ->t ('Copy ' );
225+ }
203226 $ formData ['access ' ] = [
204227 'permitAllUsers ' => false ,
205228 'showToAllUsers ' => false ,
@@ -213,26 +236,35 @@ public function newForm(?int $fromId = null): DataResponse {
213236 $ form = Form::fromParams ($ formData );
214237 $ this ->formMapper ->insert ($ form );
215238
216- // Get Questions, set new formId, reinsert
217- $ questions = $ this ->questionMapper ->findByForm ($ oldForm ->getId ());
218- $ oldConfirmationEmailQuestionId = $ oldForm ->getConfirmationEmailQuestionId ();
219-
220239 foreach ($ questions as $ oldQuestion ) {
221- $ questionData = $ oldQuestion ->read ();
240+ if ($ import ) {
241+ if (!isset ($ oldQuestion ['id ' ])) {
242+ throw new OCSBadRequestException ('Invalid question data: missing id ' );
243+ }
244+ $ questionData = $ oldQuestion ;
245+ $ oldQuestionId = $ oldQuestion ['id ' ] ?? [];
246+ $ options = $ oldQuestion ['options ' ];
247+ } else {
248+ $ questionData = $ oldQuestion ->read ();
249+ $ oldQuestionId = $ oldQuestion ->getId ();
250+ // Get Options, set new QuestionId, reinsert
251+ $ options = $ this ->optionMapper ->findByQuestion ($ oldQuestionId );
252+ }
222253
223254 unset($ questionData ['id ' ]);
255+ unset($ questionData ['options ' ]);
256+ unset($ questionData ['accept ' ]);
257+
224258 $ questionData ['formId ' ] = $ form ->getId ();
225259 $ newQuestion = Question::fromParams ($ questionData );
226260 $ this ->questionMapper ->insert ($ newQuestion );
227261
228- if (isset ($ oldConfirmationEmailQuestionId ) && $ oldConfirmationEmailQuestionId === $ oldQuestion -> getId () ) {
262+ if (isset ($ oldConfirmationEmailQuestionId ) && $ oldConfirmationEmailQuestionId === $ oldQuestionId ) {
229263 $ form ->setConfirmationEmailQuestionId ($ newQuestion ->getId ());
230264 }
231265
232- // Get Options, set new QuestionId, reinsert
233- $ options = $ this ->optionMapper ->findByQuestion ($ oldQuestion ->getId ());
234266 foreach ($ options as $ oldOption ) {
235- $ optionData = $ oldOption ->read ();
267+ $ optionData = $ import ? $ oldOption : $ oldOption ->read ();
236268
237269 unset($ optionData ['id ' ]);
238270 $ optionData ['questionId ' ] = $ newQuestion ->getId ();
@@ -694,8 +726,10 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
694726 throw new OCSBadRequestException ('Invalid extraSettings, will not update. ' );
695727 }
696728
697- if ($ form ->getConfirmationEmailQuestionId () === $ question ->getId ()
698- && !$ question ->isEmailType ($ keyValuePairs ['type ' ] ?? null , $ keyValuePairs ['extraSettings ' ] ?? null )) {
729+ if (
730+ $ form ->getConfirmationEmailQuestionId () === $ question ->getId ()
731+ && !$ question ->isEmailType ($ keyValuePairs ['type ' ] ?? null , $ keyValuePairs ['extraSettings ' ] ?? null )
732+ ) {
699733 $ form ->setConfirmationEmailQuestionId (null );
700734 }
701735
0 commit comments