2929 * ---------------------------------------------------------------------
3030 */
3131
32- use Glpi \Application \View \TemplateRenderer ;
3332use Glpi \Toolbox \Sanitizer ;
34- use GlpiPlugin \Formcreator \Field \FileField ;
33+ use GlpiPlugin \Formcreator \Field \TextareaField ;
3534
3635if (!defined ('GLPI_ROOT ' )) {
3736 die ("Sorry. You can't access this file directly " );
@@ -2264,28 +2263,21 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
22642263 return $ data ;
22652264 }
22662265
2267- /**
2268- * Emulate file uploads for documents provided to file questions
2269- *
2270- * @param array $data
2271- * @param PluginFormcreatorFormAnswer $formanswer a form answer
2272- * @return array input $data updated with (fake) file uploads
2273- */
2274- protected function prepareUploadedFiles (array $ data , $ formanswer ): array {
2266+ protected function prepareUploadsFromTextarea (array $ data , PluginFormcreatorFormAnswer $ formanswer ): array {
22752267 $ saved_documents = $ formanswer ->getFileProperties ();
22762268
22772269 if ($ saved_documents ) {
22782270 foreach ($ formanswer ->getForm ()->getFields () as $ questionId => $ field ) {
2279- if (!($ field instanceOf FileField )) {
2271+ if (!($ field instanceOf TextareaField )) {
22802272 continue ;
22812273 }
2282- if (!isset ($ saved_documents ["_filename " ][$ questionId ])) {
2274+ if (!isset ($ saved_documents ["_content " ][$ questionId ])) {
22832275 continue ;
22842276 }
2285- $ data ["_filename " ] = array_merge ($ data ["_filename " ], $ saved_documents ["_filename " ][$ questionId ] ?? []);
2286- $ data ["_tag_filename " ] = array_merge ($ data ["_tag_filename " ], $ saved_documents ["_tag_filename " ][$ questionId ] ?? []);
2277+ $ data ["_content " ] = array_merge ($ data ["_content " ], $ saved_documents ["_content " ][$ questionId ] ?? []);
2278+ $ data ["_tag_content " ] = array_merge ($ data ["_tag_content " ], $ saved_documents ["_tag_content " ][$ questionId ] ?? []);
22872279
2288- foreach ($ saved_documents ["_filename " ][$ questionId ] as $ key => $ filename ) {
2280+ foreach ($ saved_documents ["_content " ][$ questionId ] as $ key => $ filename ) {
22892281 $ uploaded_filename = $ formanswer ->getFileName ($ questionId , $ key );
22902282 if ($ uploaded_filename != '' ) {
22912283 copy (GLPI_DOC_DIR . '/ ' . $ uploaded_filename , GLPI_TMP_DIR . '/ ' . $ filename );
@@ -2294,12 +2286,12 @@ protected function prepareUploadedFiles(array $data, $formanswer): array {
22942286 }
22952287 } else {
22962288 foreach ($ formanswer ->getForm ()->getFields () as $ questionId => $ field ) {
2297- if (!($ field instanceOf FileField )) {
2289+ if (!($ field instanceOf TextareaField )) {
22982290 continue ;
22992291 }
2300- $ data ["_filename " ] = array_merge ($ data ["_filename " ], $ formanswer ->input ["_formcreator_field_ " . $ questionId ]);
2301- $ data ["_prefix_filename " ] = array_merge ($ data ["_prefix_filename " ], $ formanswer ->input ["_prefix_formcreator_field_ " . $ questionId ]);
2302- $ data ["_tag_filename " ] = array_merge ($ data ["_tag_filename " ], $ formanswer ->input ["_tag_formcreator_field_ " . $ questionId ]);
2292+ $ data ["_content " ] = array_merge ($ data ["_content " ], $ formanswer ->input ["_formcreator_field_ " . $ questionId ]);
2293+ $ data ["_prefix_content " ] = array_merge ($ data ["_prefix_content " ], $ formanswer ->input ["_prefix_formcreator_field_ " . $ questionId ]);
2294+ $ data ["_tag_content " ] = array_merge ($ data ["_tag_content " ], $ formanswer ->input ["_tag_formcreator_field_ " . $ questionId ]);
23032295 foreach ($ formanswer ->input ["_formcreator_field_ " . $ questionId ] as $ key => $ filename ) {
23042296 $ uploaded_filename = $ formanswer ->getFileName ($ questionId , $ key );
23052297 if ($ uploaded_filename != '' ) {
@@ -2312,6 +2304,65 @@ protected function prepareUploadedFiles(array $data, $formanswer): array {
23122304 return $ data ;
23132305 }
23142306
2307+ /**
2308+ * Emulate file uploads for documents provided to file questions
2309+ *
2310+ * @param array $data
2311+ * @return array input $data updated with (fake) file uploads
2312+ */
2313+ protected function prepareUploadedFiles (array $ data ): array {
2314+ $ data ['_filename ' ] = [];
2315+ $ data ['_prefix_filename ' ] = [];
2316+ $ data ['_tag_filename ' ] = [];
2317+
2318+ // emulate file uploads of inline images
2319+ // TODO: replace PluginFormcreatorCommon::getDocumentsFromTag by Toolbox::getDocumentsFromTag
2320+ // when is merged https://github.com/glpi-project/glpi/pull/9335
2321+ foreach (PluginFormcreatorCommon::getDocumentsFromTag ($ data ['content ' ]) as $ document ) {
2322+ $ prefix = uniqid ('' , true );
2323+ $ filename = $ prefix . 'image_paste. ' . pathinfo ($ document ['filename ' ], PATHINFO_EXTENSION );
2324+ if (!copy (GLPI_DOC_DIR . '/ ' . $ document ['filepath ' ], GLPI_TMP_DIR . '/ ' . $ filename )) {
2325+ continue ;
2326+ }
2327+
2328+ // Formanswers answers contains document tags to allow
2329+ // Replace them with a IMG tag similar to those found after pasting an
2330+ // image in a textarea
2331+ // <img id="..." src="blob:http://..." data-upload_id=".." />
2332+ // the attribute id is requires to let GLPI process the upload properly
2333+ $ img = "<img id=' " . $ document ['tag ' ] . "' src='' /> " ;
2334+ $ data ['content ' ] = preg_replace (
2335+ '/ ' . Document::getImageTag ($ document ['tag ' ]) . '/ ' ,
2336+ Sanitizer::sanitize ($ img ),
2337+ $ data ['content ' ]
2338+ );
2339+
2340+ $ data ['_filename ' ][] = $ filename ;
2341+ $ data ['_prefix_filename ' ][] = $ prefix ;
2342+ $ data ['_tag_filename ' ][] = $ document ['tag ' ];
2343+ }
2344+
2345+ // emulate file upload
2346+ foreach (array_keys ($ this ->attachedDocuments ) as $ documentId ) {
2347+ $ document = new Document ();
2348+ if (!$ document ->getFromDB ($ documentId )) {
2349+ continue ;
2350+ }
2351+
2352+ $ prefix = uniqid ('' , true );
2353+ $ filename = $ prefix . $ document ->fields ['filename ' ];
2354+ if (!copy (GLPI_DOC_DIR . '/ ' . $ document ->fields ['filepath ' ], GLPI_TMP_DIR . '/ ' . $ filename )) {
2355+ continue ;
2356+ }
2357+
2358+ $ data ['_filename ' ][] = $ filename ;
2359+ $ data ['_prefix_filename ' ][] = $ prefix ;
2360+ $ data ['_tag_filename ' ][] = $ document ->fields ['tag ' ];
2361+ }
2362+
2363+ return $ data ;
2364+ }
2365+
23152366 public static function getTargetType (): int {
23162367 return self ::TARGET_TYPE_OBJECT ;
23172368 }
0 commit comments