88use Drupal \os2forms_get_organized \Exception \CitizenArchivingException ;
99use Drupal \os2forms_get_organized \Exception \GetOrganizedCaseIdException ;
1010use Drupal \webform \Entity \WebformSubmission ;
11+ use Drupal \webform_attachment \Element \WebformAttachmentBase ;
1112use Drupal \webform_entity_print_attachment \Element \WebformEntityPrintAttachment ;
1213use ItkDev \GetOrganized \Client ;
1314use ItkDev \GetOrganized \Service \Cases ;
@@ -199,7 +200,7 @@ private function archiveToCitizen(string $submissionId, array $handlerConfigurat
199200 $ caseQuery
200201 );
201202
202- // Subcases may also contain contain the 'ows_CCMContactData_CPR' property,
203+ // Subcases may also contain the 'ows_CCMContactData_CPR' property,
203204 // i.e. we need to check result cases are not subcases.
204205 // $caseResult will always contain the 'CasesInfo' key,
205206 // and its value will always be an array.
@@ -322,17 +323,25 @@ private function createSubCase(string $caseId, string $caseName): string {
322323 */
323324 private function uploadDocumentToCase (string $ caseId , string $ webformAttachmentElementId , WebformSubmission $ submission , bool $ shouldArchiveFiles , bool $ shouldBeFinalized ): void {
324325 // Handle main document (the attachment).
325- $ element = $ submission ->getWebform ()->getElement ($ webformAttachmentElementId );
326- $ fileContent = WebformEntityPrintAttachment::getFileContent ($ element , $ submission );
326+ $ webformAttachmentElement = $ submission ->getWebform ()->getElement ($ webformAttachmentElementId );
327+ $ fileContent = WebformEntityPrintAttachment::getFileContent ($ webformAttachmentElement , $ submission );
328+ $ webformLabel = $ submission ->getWebform ()->label ();
329+ $ pdfExtension = '.pdf ' ;
327330
328- // Ids that should possibly be finalized (jornaliseret) later.
329- $ documentIdsForFinalizing = [];
331+ if ($ webformAttachmentElement ['#filename ' ]) {
332+ // Computes webform attachment's file name.
333+ $ baseName = WebformAttachmentBase::getFileName ($ webformAttachmentElement , $ submission );
330334
331- // Create temp file with attachment-element contents.
332- $ webformLabel = $ submission ->getWebform ()->label ();
333- $ getOrganizedFileName = $ webformLabel . '- ' . $ submission ->serial () . '.pdf ' ;
335+ $ getOrganizedFilename = $ this ->computeGetOrganizedFilename ($ baseName , $ submission );
336+ }
337+ else {
338+ $ getOrganizedFilename = $ webformLabel . '- ' . $ submission ->serial () . $ pdfExtension ;
339+ }
340+
341+ // Ids that should possibly be finalized (journaliseret) later.
342+ $ documentIdsForFinalizing = [];
334343
335- $ parentDocumentId = $ this ->archiveDocumentToGetOrganizedCase ($ caseId , $ getOrganizedFileName , $ fileContent );
344+ $ parentDocumentId = $ this ->archiveDocumentToGetOrganizedCase ($ caseId , $ getOrganizedFilename , $ fileContent );
336345
337346 $ documentIdsForFinalizing [] = $ parentDocumentId ;
338347
@@ -347,11 +356,12 @@ private function uploadDocumentToCase(string $caseId, string $webformAttachmentE
347356 foreach ($ fileIds as $ fileId ) {
348357 /** @var \Drupal\file\Entity\File $file */
349358 $ file = $ fileStorage ->load ($ fileId );
359+ $ filename = $ file ->getFilename ();
360+ $ getOrganizedFilename = $ this ->computeGetOrganizedFilename ($ filename , $ submission );
350361
351362 $ fileContent = file_get_contents ($ file ->getFileUri ());
352- $ getOrganizedFileName = $ webformLabel . '- ' . $ submission ->serial () . '- ' . $ file ->getFilename ();
353363
354- $ childDocumentId = $ this ->archiveDocumentToGetOrganizedCase ($ caseId , $ getOrganizedFileName , $ fileContent );
364+ $ childDocumentId = $ this ->archiveDocumentToGetOrganizedCase ($ caseId , $ getOrganizedFilename , $ fileContent );
355365
356366 $ childDocumentIds [] = $ childDocumentId ;
357367 }
@@ -441,4 +451,26 @@ private function getFileElementKeysFromSubmission(WebformSubmission $submission)
441451 return array_merge (...$ fileIds );
442452 }
443453
454+ /**
455+ * Convert filename into GetOrganized filename.
456+ *
457+ * Adds webform label and submission number before its file extension.
458+ *
459+ * Example:
460+ *
461+ * Input: SomeFilename.pdf
462+ * Output: SomeFilename-[FORMULAR_LABEL]-[SUBMISSION_NUMBER].pdf
463+ */
464+ private function computeGetOrganizedFilename (string $ filename , WebformSubmission $ submission ): string {
465+ $ fileExtension = pathinfo ($ filename , PATHINFO_EXTENSION );
466+ $ webformLabel = $ submission ->getWebform ()->label ();
467+ $ submissionNumber = $ submission ->serial ();
468+
469+ // Find position of last occurrence of extension.
470+ $ position = strrpos ($ filename , '. ' . $ fileExtension );
471+
472+ // Inject the webform label and submission number at found position.
473+ return substr_replace ($ filename , '- ' . $ webformLabel . '- ' . $ submissionNumber , $ position , 0 );
474+ }
475+
444476}
0 commit comments