diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 0cf7f0ee..960d83fd 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -34,6 +34,7 @@ use App\Entity\Parts\Category; use App\Entity\Parts\Part; use App\Form\Part\PartBaseType; use App\Services\AttachmentHelper; +use App\Services\Attachments\PartPreviewGenerator; use App\Services\PricedetailHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -55,17 +56,19 @@ class PartController extends AbstractController * @param AttachmentHelper $attachmentHelper * @return \Symfony\Component\HttpFoundation\Response */ - public function show(Part $part, AttachmentHelper $attachmentHelper, PricedetailHelper $pricedetailHelper) + public function show(Part $part, AttachmentHelper $attachmentHelper, PricedetailHelper $pricedetailHelper, PartPreviewGenerator $previewGenerator) { $this->denyAccessUnlessGranted('read', $part); - return $this->render('Parts/info/show_part_info.html.twig', + return $this->render( + 'Parts/info/show_part_info.html.twig', [ 'part' => $part, 'attachment_helper' => $attachmentHelper, - 'pricedetail_helper' => $pricedetailHelper + 'pricedetail_helper' => $pricedetailHelper, + 'pictures' => $previewGenerator->getPreviewAttachments($part) ] - ); + ); } /** @@ -78,7 +81,7 @@ class PartController extends AbstractController * @return \Symfony\Component\HttpFoundation\Response */ public function edit(Part $part, Request $request, EntityManagerInterface $em, TranslatorInterface $translator, - AttachmentHelper $attachmentHelper) + AttachmentHelper $attachmentHelper) { $this->denyAccessUnlessGranted('edit', $part); diff --git a/src/DataTables/PartsDataTable.php b/src/DataTables/PartsDataTable.php index 13f805b9..e3d87bbd 100644 --- a/src/DataTables/PartsDataTable.php +++ b/src/DataTables/PartsDataTable.php @@ -156,7 +156,7 @@ class PartsDataTable implements DataTableTypeInterface ->add('picture', TextColumn::class, [ 'label' => '', 'render' => function ($value, Part $context) { - $preview_attachment = $this->previewGenerator->previewAttachment($context); + $preview_attachment = $this->previewGenerator->getTablePreviewAttachment($context); if ($preview_attachment === null) { return ''; } diff --git a/src/Services/Attachments/PartPreviewGenerator.php b/src/Services/Attachments/PartPreviewGenerator.php index 135097c5..2b79d92e 100644 --- a/src/Services/Attachments/PartPreviewGenerator.php +++ b/src/Services/Attachments/PartPreviewGenerator.php @@ -48,13 +48,70 @@ class PartPreviewGenerator $this->attachmentHelper = $attachmentHelper; } + /** + * Returns a list of attachments that can be used for previewing the part ordered by priority. + * The priority is: Part MasterAttachment -> Footprint MasterAttachment -> Category MasterAttachment + * -> Storelocation Attachment -> MeasurementUnit Attachment -> ManufacturerAttachment + * @param Part $part The part for which the attachments should be determined. + * @return Attachment[] + */ + public function getPreviewAttachments(Part $part) : array + { + $list = []; + + //Master attachment has top priority + $attachment = $part->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + + if ($part->getFootprint() !== null) { + $attachment = $part->getFootprint()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + } + + if ($part->getCategory() !== null) { + $attachment = $part->getCategory()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + } + + foreach ($part->getPartLots() as $lot) { + if ($lot->getStorageLocation() !== null) { + $attachment = $lot->getStorageLocation()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + } + } + + if ($part->getPartUnit() !== null) { + $attachment = $part->getPartUnit()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + } + + if ($part->getManufacturer() !== null) { + $attachment = $part->getManufacturer()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + $list[] = $attachment; + } + } + + return $list; + } + /** * Determines what attachment should be used for previewing a part (especially in part table). * The returned attachment is guaranteed to be existing and be a picture. * @param Part $part The part for which the attachment should be determined * @return Attachment|null */ - public function previewAttachment(Part $part) : ?Attachment + public function getTablePreviewAttachment(Part $part) : ?Attachment { //First of all we check if the master attachment of the part is set (and a picture) $attachment = $part->getMasterPictureAttachment(); diff --git a/templates/Parts/info/_main_infos.html.twig b/templates/Parts/info/_main_infos.html.twig index 7045f292..d47417dc 100644 --- a/templates/Parts/info/_main_infos.html.twig +++ b/templates/Parts/info/_main_infos.html.twig @@ -1,14 +1,10 @@ {% import "helper.twig" as helper %}
-
- {% if part.masterPictureAttachment and part.masterPictureAttachment.picture %} - Part main image - {% else %} - Part main image - {% endif %} +
+ {% include "Parts/info/_picture.html.twig" %}
-
+
{% if part.manufacturer %} {% if part.manufacturer.id is not null %} diff --git a/templates/Parts/info/_picture.html.twig b/templates/Parts/info/_picture.html.twig new file mode 100644 index 00000000..5290cd1b --- /dev/null +++ b/templates/Parts/info/_picture.html.twig @@ -0,0 +1,26 @@ +{% if not pictures is empty %} + {# Part main image #} + + + +{% else %} + Part main image +{% endif %} \ No newline at end of file