diff --git a/assets/css/app.css b/assets/css/app.css index 84449721..a783b960 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -351,7 +351,8 @@ btn-xs } .hoverpic:hover { - transform: scale(5); + transform: scale(6); + background: white; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); } diff --git a/src/DataTables/PartsDataTable.php b/src/DataTables/PartsDataTable.php index 7beb61d7..13f805b9 100644 --- a/src/DataTables/PartsDataTable.php +++ b/src/DataTables/PartsDataTable.php @@ -39,6 +39,7 @@ use App\Entity\Parts\PartLot; use App\Entity\Parts\Storelocation; use App\Entity\Parts\Supplier; use App\Services\AmountFormatter; +use App\Services\Attachments\PartPreviewGenerator; use App\Services\EntityURLGenerator; use App\Services\ToolsTreeBuilder; use App\Services\TreeBuilder; @@ -51,6 +52,7 @@ use Omines\DataTablesBundle\Column\MapColumn; use Omines\DataTablesBundle\Column\TextColumn; use Omines\DataTablesBundle\DataTable; use Omines\DataTablesBundle\DataTableTypeInterface; +use SebastianBergmann\CodeCoverage\Report\Text; use Symfony\Contracts\Translation\TranslatorInterface; class PartsDataTable implements DataTableTypeInterface @@ -62,14 +64,16 @@ class PartsDataTable implements DataTableTypeInterface protected $translator; protected $treeBuilder; protected $amountFormatter; + protected $previewGenerator; public function __construct(EntityURLGenerator $urlGenerator, TranslatorInterface $translator, - TreeBuilder $treeBuilder, AmountFormatter $amountFormatter) + TreeBuilder $treeBuilder, AmountFormatter $amountFormatter, PartPreviewGenerator $previewGenerator) { $this->urlGenerator = $urlGenerator; $this->translator = $translator; $this->treeBuilder = $treeBuilder; $this->amountFormatter = $amountFormatter; + $this->previewGenerator = $previewGenerator; } protected function getQuery(QueryBuilder $builder) @@ -149,6 +153,22 @@ class PartsDataTable implements DataTableTypeInterface public function configure(DataTable $dataTable, array $options) { $dataTable + ->add('picture', TextColumn::class, [ + 'label' => '', + 'render' => function ($value, Part $context) { + $preview_attachment = $this->previewGenerator->previewAttachment($context); + if ($preview_attachment === null) { + return ''; + } + + return sprintf( + '%s', + 'Part image', + $this->urlGenerator->viewURL($preview_attachment), + 'img-fluid hoverpic' + ); + } + ]) ->add('name', TextColumn::class, [ 'label' => $this->translator->trans('part.table.name'), 'render' => function ($value, Part $context) { diff --git a/src/Services/Attachments/PartPreviewGenerator.php b/src/Services/Attachments/PartPreviewGenerator.php new file mode 100644 index 00000000..135097c5 --- /dev/null +++ b/src/Services/Attachments/PartPreviewGenerator.php @@ -0,0 +1,88 @@ +attachmentHelper = $attachmentHelper; + } + + /** + * 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 + { + //First of all we check if the master attachment of the part is set (and a picture) + $attachment = $part->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + return $attachment; + } + + //Otherwise check if the part has a footprint with a valid masterattachment + if ($part->getFootprint() !== null) { + $attachment = $part->getFootprint()->getMasterPictureAttachment(); + if ($this->isAttachmentValidPicture($attachment)) { + return $attachment; + } + } + + //If nothing is available return null + return null; + } + + /** + * Checks if a attachment is exising and a valid picture. + * @param Attachment|null $attachment The attachment that should be checked. + * @return bool True if the attachment is valid. + */ + protected function isAttachmentValidPicture(?Attachment $attachment) : bool + { + return $attachment !== null + && $attachment->isPicture() + && $this->attachmentHelper->isFileExisting($attachment); + } +} \ No newline at end of file