From 40714c1914f82a69cc2beea132e2f19d7d23b382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 12 Mar 2019 19:59:18 +0100 Subject: [PATCH] Allow to show the footprint image in part info. Also added an placehoder image for parts, who dont have an image. --- public/img/part_placeholder.svg | 22 ++++++++ src/Controller/PartController.php | 6 ++- src/Services/AttachmentFilenameService.php | 59 ++++++++++++++++++++++ templates/show_part_info.html.twig | 2 +- 4 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 public/img/part_placeholder.svg create mode 100644 src/Services/AttachmentFilenameService.php diff --git a/public/img/part_placeholder.svg b/public/img/part_placeholder.svg new file mode 100644 index 00000000..9904975c --- /dev/null +++ b/public/img/part_placeholder.svg @@ -0,0 +1,22 @@ + + + + + + + diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 217ca681..c1f77148 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -34,6 +34,7 @@ namespace App\Controller; use App\Entity\Part; +use App\Services\AttachmentFilenameService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; @@ -44,14 +45,15 @@ class PartController extends AbstractController * @Route("/part/{id}/info", name="part_info") * @Route("/part/{id}") */ - function show(Part $part) + function show(Part $part, AttachmentFilenameService $attachmentFilenameService) { $filename = $part->getMasterPictureFilename(true); dump($filename); return $this->render('show_part_info.html.twig', [ - "part" => $part + "part" => $part, + "main_image" => $attachmentFilenameService->attachmentPathToAbsolutePath($filename) ] ); } diff --git a/src/Services/AttachmentFilenameService.php b/src/Services/AttachmentFilenameService.php new file mode 100644 index 00000000..5bb0199a --- /dev/null +++ b/src/Services/AttachmentFilenameService.php @@ -0,0 +1,59 @@ +package = $package; + } + + public function attachmentPathToAbsolutePath(?string $filename) : ?string + { + //Return placeholder if a part does not have an attachment + if ($filename == null) { + return $this->package->getUrl('/img/part_placeholder.svg'); + } + if (stripos($filename, "%BASE%/img/") !== false) { + return $this->package->getUrl(str_replace('%BASE%', '', $filename)); + } + + //If no other method works, return placeholder + return $this->package->getUrl('/img/part_placeholder.svg'); + } +} \ No newline at end of file diff --git a/templates/show_part_info.html.twig b/templates/show_part_info.html.twig index a367fd10..b01d2a67 100644 --- a/templates/show_part_info.html.twig +++ b/templates/show_part_info.html.twig @@ -19,7 +19,7 @@
- +
{{ part.manufacturer.name ?? ""}}