From e2b643c52b6007830d4e927b07bab9ab0b279a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 2 Sep 2019 23:09:58 +0200 Subject: [PATCH] Allow to select the master picture attachment. --- src/Entity/Attachments/Attachment.php | 6 ++++++ src/Entity/Parts/Part.php | 18 +++++++++++++--- src/Form/Part/PartBaseType.php | 24 ++++++++++++++++++++- templates/Parts/edit/_attachments.html.twig | 1 + templates/Parts/info/_main_infos.html.twig | 4 ++-- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 104337c2..3df48ae5 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -73,12 +73,18 @@ abstract class Attachment extends NamedDBElement /** * Check if this attachement is a picture (analyse the file's extension). + * If the link is external, it is assumed that this is false. * * @return bool * true if the file extension is a picture extension * * otherwise false */ public function isPicture(): bool { + //We can not check if a external link is a picture, so just assume this is false + if ($this->isExternal()) { + return true; + } + $extension = pathinfo($this->getPath(), PATHINFO_EXTENSION); // list all file extensions which are supported to display them by HTML code diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 2ea740b7..5fce8a99 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -121,7 +121,7 @@ class Part extends AttachmentContainingDBElement * @var Attachment * @ORM\ManyToOne(targetEntity="App\Entity\Attachments\Attachment") * @ORM\JoinColumn(name="id_master_picture_attachement", referencedColumnName="id") - * + * @Assert\Expression("value == null or value.isPicture()") * @ColumnSecurity(prefix="attachments", type="object") */ protected $master_picture_attachment; @@ -508,15 +508,27 @@ class Part extends AttachmentContainingDBElement } /** - * Get the master picture "Attachement"-object of this part (if there is one). + * Get the master picture "Attachment"-object of this part (if there is one). + * The master picture should be used as a visual description/representation of this part. * * @return Attachment the master picture Attachement of this part (if there is one) */ - public function getMasterPictureAttachement(): ?Attachment + public function getMasterPictureAttachment(): ?Attachment { return $this->master_picture_attachment; } + /** + * Sets the new master picture for this part. + * @param Attachment|null $new_master_attachment + * @return Part + */ + public function setMasterPictureAttachment(?Attachment $new_master_attachment): Part + { + $this->master_picture_attachment = $new_master_attachment; + return $this; + } + /** * Get all orderdetails of this part. * diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index 331cccaa..042b0dcf 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -31,6 +31,7 @@ namespace App\Form\Part; +use App\Entity\Attachments\Attachment; use App\Entity\Attachments\PartAttachment; use App\Entity\Parts\Category; use App\Entity\Parts\Footprint; @@ -44,6 +45,7 @@ use App\Form\AttachmentType; use App\Form\Type\SIUnitType; use App\Form\Type\StructuralEntityType; use Doctrine\DBAL\Types\FloatType; +use Doctrine\ORM\EntityRepository; use FOS\CKEditorBundle\Form\Type\CKEditorType; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -60,6 +62,7 @@ use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Security; use Symfony\Contracts\Translation\TranslatorInterface; +use function foo\func; class PartBaseType extends AbstractType { @@ -164,7 +167,26 @@ class PartBaseType extends AbstractType 'by_reference' => false ]); - //Attachment section + $builder->add('master_picture_attachment', EntityType::class, [ + 'required' => false, + 'label' => 'part.edit.master_attachment', + 'class' => PartAttachment::class, + 'attr' => ['class' => 'selectpicker'], + 'choice_attr' => function ($choice, $key, $value) { + /** @var Attachment $choice */ + return ['data-subtext' => $choice->getFilename() ?? "URL"]; + }, + 'choice_label' => 'name', + 'query_builder' => function (EntityRepository $er) use ($part) { + return $er->createQueryBuilder('u') + ->where('u.element = ?1') + ->andWhere("u.path <> ''") + ->orderBy('u.name', 'ASC') + ->setParameter(1, $part); + } + ]); + + //Orderdetails section $builder->add('orderdetails', CollectionType::class, [ 'entry_type' => OrderdetailType::class, 'allow_add' => true, 'allow_delete' => true, diff --git a/templates/Parts/edit/_attachments.html.twig b/templates/Parts/edit/_attachments.html.twig index e41c7e22..3255dc34 100644 --- a/templates/Parts/edit/_attachments.html.twig +++ b/templates/Parts/edit/_attachments.html.twig @@ -5,6 +5,7 @@ {% endset %} +{{ form_row(form.master_picture_attachment) }} diff --git a/templates/Parts/info/_main_infos.html.twig b/templates/Parts/info/_main_infos.html.twig index cb46e13c..9bccda15 100644 --- a/templates/Parts/info/_main_infos.html.twig +++ b/templates/Parts/info/_main_infos.html.twig @@ -1,7 +1,7 @@
- {% if part.masterPictureAttachement %} - Part main image + {% if part.masterPictureAttachment and part.masterPictureAttachment.picture %} + Part main image {% else %} Part main image {% endif %}