diff --git a/src/Helper/FileHelper.php b/src/Helper/FileHelper.php index 3a52afde..2fae39f9 100644 --- a/src/Helper/FileHelper.php +++ b/src/Helper/FileHelper.php @@ -20,10 +20,12 @@ public static function ensureFolderExists(string $folderName) } } - public static function sanitizeFileName(string $fileName) + public static function sanitizeFileName(string $fileName, int $maxLength = 40): string { $noUmlautFileName = str_replace(['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'], ['ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue'], $fileName); - return preg_replace('/[^A-Za-z0-9]+/', '_', $noUmlautFileName); + $sanitized = preg_replace('/[^A-Za-z0-9]+/', '_', $noUmlautFileName); + + return substr($sanitized, 0, $maxLength); } } diff --git a/src/Service/Report/Pdf/PdfService.php b/src/Service/Report/Pdf/PdfService.php index eceecb6b..b084ca8c 100644 --- a/src/Service/Report/Pdf/PdfService.php +++ b/src/Service/Report/Pdf/PdfService.php @@ -90,9 +90,7 @@ public function generatePdfReport(array $issues, Filter $filter, ReportElements $folder = $this->pathService->getTransientFolderForReports(); FileHelper::ensureFolderExists($folder); - $sanitizedConstructionSiteName = FileHelper::sanitizeFileName($constructionSite->getName()); - $humanReadablePrefix = (new \DateTime())->format(DateTimeFormatter::FILESYSTEM_DATE_TIME_FORMAT).'_'.$sanitizedConstructionSiteName; - + $humanReadablePrefix = $this->getHumanReadableFilenamePrefix($constructionSite, $filter); $optimalFilename = $humanReadablePrefix.'.pdf'; $optimalPath = $folder.'/'.$optimalFilename; $filename = file_exists($optimalPath) ? $humanReadablePrefix.'_'.uniqid().'.pdf' : $optimalFilename; @@ -103,6 +101,26 @@ public function generatePdfReport(array $issues, Filter $filter, ReportElements return $filename; } + private function getHumanReadableFilenamePrefix(ConstructionSite $constructionSite, Filter $filter): string + { + $humanReadablePrefix = (new \DateTime())->format(DateTimeFormatter::FILESYSTEM_DATE_TIME_FORMAT); + $humanReadablePrefix .= '_'.FileHelper::sanitizeFileName($constructionSite->getName()); + + if ($filter->getCraftsmanIds() && 1 === count($filter->getCraftsmanIds())) { + $craftsmanRepository = $this->doctrine->getRepository(Craftsman::class); + $craftsman = $craftsmanRepository->findOneBy(['id' => $filter->getCraftsmanIds()[0]]); + $humanReadablePrefix .= '_'.FileHelper::sanitizeFileName($craftsman->getCompany()); + } + + if ($filter->getMapIds() && 1 === count($filter->getMapIds())) { + $mapRepository = $this->doctrine->getRepository(Map::class); + $map = $mapRepository->findOneBy(['id' => $filter->getMapIds()[0]]); + $humanReadablePrefix .= '_'.FileHelper::sanitizeFileName($map->getName()); + } + + return $humanReadablePrefix; + } + private function addIssueContent(Filter $filter, ReportElements $reportElements, array $issues, Report $report): void { // add tables