@@ -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> $form (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,14 @@ 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 $ form = [] ): 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+ if ($ fromId === null && $ import === false ) {
168170 // Create Form
169171 $ form = new Form ();
170172 $ form ->setOwnerId ($ this ->currentUser ->getUID ());
@@ -183,10 +185,22 @@ public function newForm(?int $fromId = null): DataResponse {
183185
184186 $ this ->formMapper ->insert ($ form );
185187 } else {
186- $ oldForm = $ this ->formsService ->getFormIfAllowed ($ fromId , Constants::PERMISSION_EDIT );
187-
188- // Read old form, (un)set new form specific data, extend title
189- $ formData = $ oldForm ->read ();
188+ $ formData = [];
189+ $ questions = [];
190+ if ($ fromId !== null ) {
191+ $ oldForm = $ this ->formsService ->getFormIfAllowed ($ fromId , Constants::PERMISSION_EDIT );
192+
193+ // Read old form, (un)set new form specific data, extend title
194+ $ formData = $ oldForm ->read ();
195+ // Get Questions, set new formId, reinsert
196+ $ questions = $ this ->questionMapper ->findByForm ($ oldForm ->getId ());
197+ $ oldConfirmationEmailQuestionId = $ oldForm ->getConfirmationEmailQuestionId ();
198+ } else {
199+ $ questions = $ form ['questions ' ];
200+ $ oldConfirmationEmailQuestionId = $ form ['confirmationEmailQuestionId ' ];
201+ unset($ form ['questions ' ]);
202+ $ formData = $ form ;
203+ }
190204 unset($ formData ['id ' ]);
191205 unset($ formData ['created ' ]);
192206 unset($ formData ['lastUpdated ' ]);
@@ -199,7 +213,9 @@ public function newForm(?int $fromId = null): DataResponse {
199213 $ formData ['ownerId ' ] = $ this ->currentUser ->getUID ();
200214 $ formData ['hash ' ] = $ this ->formsService ->generateFormHash ();
201215 // TRANSLATORS Appendix to the form Title of a duplicated/copied form.
202- $ formData ['title ' ] .= ' - ' . $ this ->l10n ->t ('Copy ' );
216+ if ($ fromId !== null ) {
217+ $ formData ['title ' ] .= ' - ' . $ this ->l10n ->t ('Copy ' );
218+ }
203219 $ formData ['access ' ] = [
204220 'permitAllUsers ' => false ,
205221 'showToAllUsers ' => false ,
@@ -213,26 +229,36 @@ public function newForm(?int $fromId = null): DataResponse {
213229 $ form = Form::fromParams ($ formData );
214230 $ this ->formMapper ->insert ($ form );
215231
216- // Get Questions, set new formId, reinsert
217- $ questions = $ this ->questionMapper ->findByForm ($ oldForm ->getId ());
218- $ oldConfirmationEmailQuestionId = $ oldForm ->getConfirmationEmailQuestionId ();
219-
220232 foreach ($ questions as $ oldQuestion ) {
221- $ questionData = $ oldQuestion ->read ();
233+ if ($ fromId !== null ) {
234+ $ questionData = $ oldQuestion ->read ();
235+ $ oldQuestionId = $ oldQuestion ->getId ();
236+ // Get Options, set new QuestionId, reinsert
237+ $ options = $ this ->optionMapper ->findByQuestion ($ oldQuestionId );
238+ } else {
239+ $ questionData = $ oldQuestion ;
240+ $ oldQuestionId = $ oldQuestion ['id ' ];
241+ $ options = $ oldQuestion ["options " ];
242+ }
222243
223244 unset($ questionData ['id ' ]);
245+ unset($ questionData ['options ' ]);
246+ unset($ questionData ['accept ' ]);
247+
224248 $ questionData ['formId ' ] = $ form ->getId ();
225249 $ newQuestion = Question::fromParams ($ questionData );
226250 $ this ->questionMapper ->insert ($ newQuestion );
227251
228- if (isset ($ oldConfirmationEmailQuestionId ) && $ oldConfirmationEmailQuestionId === $ oldQuestion -> getId () ) {
252+ if (isset ($ oldConfirmationEmailQuestionId ) && $ oldConfirmationEmailQuestionId === $ oldQuestionId ) {
229253 $ form ->setConfirmationEmailQuestionId ($ newQuestion ->getId ());
230254 }
231255
232- // Get Options, set new QuestionId, reinsert
233- $ options = $ this ->optionMapper ->findByQuestion ($ oldQuestion ->getId ());
234256 foreach ($ options as $ oldOption ) {
235- $ optionData = $ oldOption ->read ();
257+ if ($ fromId !== null ) {
258+ $ optionData = $ oldOption ->read ();
259+ } else {
260+ $ optionData = $ oldOption ;
261+ }
236262
237263 unset($ optionData ['id ' ]);
238264 $ optionData ['questionId ' ] = $ newQuestion ->getId ();
0 commit comments