Skip to content

Commit

Permalink
feat: Add craftsman or map to filename if only one
Browse files Browse the repository at this point in the history
  • Loading branch information
famoser committed Sep 18, 2023
1 parent f071f85 commit 068634d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/Helper/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 21 additions & 3 deletions src/Service/Report/Pdf/PdfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 068634d

Please sign in to comment.