22
33namespace Drupal \os2forms_fordelingskomponent \Helper ;
44
5+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
56use Drupal \Core \Logger \LoggerChannelInterface ;
7+ use Drupal \file \FileStorageInterface ;
68use Drupal \key \KeyRepositoryInterface ;
79use Drupal \os2forms_fordelingskomponent \Exception \Exception ;
810use Drupal \os2forms_fordelingskomponent \Exception \InvalidAttachmentElementException ;
911use Drupal \os2forms_fordelingskomponent \Exception \RuntimeException ;
1012use Drupal \os2forms_fordelingskomponent \Model \Attachment ;
13+ use Drupal \os2forms_fordelingskomponent \Model \DistributionFormular ;
14+ use Drupal \os2forms_fordelingskomponent \Model \DistributionObjectFiles ;
1115use Drupal \os2forms_fordelingskomponent \Model \Fordelingskomponent \AnvenderForsendelse ;
1216use Drupal \os2forms_fordelingskomponent \Model \TransactionContext ;
1317use Drupal \os2forms_fordelingskomponent \Model \XmlRenderResult ;
6771final class FordelingskomponentHelper implements LoggerInterface, EventSubscriberInterface {
6872 use LoggerTrait;
6973
74+ /**
75+ * The file storage.
76+ */
77+ private FileStorageInterface $ fileStorage ;
78+
7079 /**
7180 * Constructor.
7281 */
@@ -78,13 +87,15 @@ public function __construct(
7887 private readonly KeyRepositoryInterface $ keyRepository ,
7988 private readonly KeyHelper $ keyHelper ,
8089 private readonly AnvenderForsendelseRepository $ anvenderForsendelseRepository ,
90+ EntityTypeManagerInterface $ entityTypeManager ,
8191 #[Autowire(service: 'logger.channel.os2forms_fordelingskomponent ' )]
8292 private readonly LoggerChannelInterface $ logger ,
8393 #[Autowire(service: 'logger.channel.os2forms_fordelingskomponent_submission ' )]
8494 private readonly LoggerChannelInterface $ submissionLogger ,
8595 #[Autowire(service: 'os2web_audit.logger ' )]
8696 private readonly AuditLogger $ auditLogger ,
8797 ) {
98+ $ this ->fileStorage = $ entityTypeManager ->getStorage ('file ' );
8899 }
89100
90101 /**
@@ -251,8 +262,9 @@ private function buildDistributionFormularType(
251262 WebformSubmissionInterface $ submission ,
252263 HandlerSettings $ handlerSettings ,
253264 Attachment $ attachment ,
254- ): DistributionFormularType {
255- $ xml = $ this ->renderXml ($ handlerSettings , $ submission )->rendered ;
265+ ): DistributionFormular {
266+ $ files = $ this ->buildFiles ($ handlerSettings , $ submission );
267+ $ xml = $ this ->renderXml ($ handlerSettings , $ submission , $ files )->rendered ;
256268 $ xsdUrl = $ handlerSettings ->distributionObject ->xsdUrl ;
257269
258270 $ this ->xmlHelper ->validateXml ($ xml );
@@ -282,12 +294,13 @@ private function buildDistributionFormularType(
282294 ),
283295 );
284296
285- return new DistributionFormularType (
297+ return ( new DistributionFormular (
286298 iD: $ id ,
287299 kLEEmneForslag: $ handlerSettings ->distributionContext ->kleEmne ,
288300 meddelelse: $ meddelelse ,
289301 handlingFacetForslag: $ handlerSettings ->distributionContext ->handlingFacet ,
290- );
302+ ))
303+ ->setFiles ($ files );
291304 }
292305
293306 /**
@@ -296,14 +309,15 @@ private function buildDistributionFormularType(
296309 public function renderXml (
297310 HandlerSettings $ handlerSettings ,
298311 WebformSubmissionInterface $ submission ,
312+ ?DistributionObjectFiles $ files ,
299313 bool $ validateXml = TRUE ,
300314 ): XmlRenderResult {
301315 $ template = $ handlerSettings ->distributionObject ->xmlTemplate ;
302316 if (empty (trim ((string ) $ template ))) {
303317 throw new RuntimeException ('Missing XML template ' );
304318 }
305319
306- $ context = $ this ->xmlHelper ->getRenderContext ($ handlerSettings , $ submission );
320+ $ context = $ this ->xmlHelper ->getRenderContext ($ handlerSettings , $ submission, $ files );
307321
308322 return new XmlRenderResult (
309323 rendered: $ this ->xmlHelper ->render ($ template , $ context , validateXml: $ validateXml ),
@@ -312,6 +326,44 @@ public function renderXml(
312326 );
313327 }
314328
329+ private const FILE_ELEMENT_TYPES = [
330+ 'managed_file ' ,
331+ 'webform_document_file ' ,
332+ 'webform_image_file ' ,
333+ ];
334+
335+ /**
336+ * Build files for a districution object.
337+ */
338+ public function buildFiles (HandlerSettings $ handlerSettings , WebformSubmissionInterface $ submission ): DistributionObjectFiles {
339+ $ files = new DistributionObjectFiles ();
340+ $ elements = $ submission ->getWebform ()->getElementsDecodedAndFlattened ();
341+ $ fileElements = array_filter ($ elements ,
342+ static fn (array $ element ) => in_array ($ element ['#type ' ] ?? NULL , self ::FILE_ELEMENT_TYPES ));
343+ foreach ($ fileElements as $ type => $ _ ) {
344+ $ values = $ submission ->getData ()[$ type ] ?? NULL ;
345+ if ($ values ) {
346+ foreach ($ values as $ index => $ id ) {
347+ /** @var \Drupal\file\Entity\FileInterface $file */
348+ $ file = $ this ->fileStorage ->load ($ id );
349+ $ sftpFilename = implode ('_ ' , [
350+ 'os2forms_fordelingskomponent ' ,
351+ $ handlerSettings ->handlerId ,
352+ $ submission ->uuid (),
353+ $ file ->getFilename (),
354+ ]);
355+
356+ $ files ->addFile ($ type ,
357+ sftpFilename: $ sftpFilename ,
358+ file: $ file
359+ );
360+ }
361+ }
362+ }
363+
364+ return $ files ;
365+ }
366+
315367 /**
316368 * Send dokument.
317369 *
@@ -336,6 +388,18 @@ public function sendDokument(
336388 $ sftp = $ sf2900 ->sftp ();
337389 $ dokumentFilNavn = $ sftp ->putContents ($ attachment ->contents , $ attachment ->filename );
338390 }
391+ // Upload files if any.
392+ elseif ($ dokument instanceof DistributionFormular) {
393+ $ files = $ dokument ->getFiles ();
394+ $ sftp = $ sf2900 ->sftp ();
395+ foreach ($ files as $ items ) {
396+ foreach ($ items as $ item ) {
397+ /** @var \Drupal\file\Entity\File $file */
398+ $ file = $ item ['file ' ];
399+ $ sftp ->putFile ($ file ->getFileUri (), $ file ->getFilename (), $ item ['sftp_filename ' ]);
400+ }
401+ }
402+ }
339403
340404 $ transactionId = Serializer::createUuid ();
341405
0 commit comments