Skip to content

Commit 8957766

Browse files
authored
Merge pull request #6 from OS2Forms/feature/SUPP0RT-1066-file-naming-changes
SUPP0RT-1066: Updated filenaming strategy
2 parents d4c993a + 5febd28 commit 8957766

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ about writing changes to this log.
88

99
## [Unreleased]
1010

11+
* Updated GO file naming strategy
12+
(<https://github.com/OS2Forms/os2forms_get_organized/pull/6>).
13+
1114
## [1.1.1] 03.05.2023
1215

1316
* Updated GO handler config to allow new nemid elements

src/Helper/ArchiveHelper.php

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\os2forms_get_organized\Exception\CitizenArchivingException;
99
use Drupal\os2forms_get_organized\Exception\GetOrganizedCaseIdException;
1010
use Drupal\webform\Entity\WebformSubmission;
11+
use Drupal\webform_attachment\Element\WebformAttachmentBase;
1112
use Drupal\webform_entity_print_attachment\Element\WebformEntityPrintAttachment;
1213
use ItkDev\GetOrganized\Client;
1314
use 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

Comments
 (0)