diff --git a/src/Entity/Attachments/Attachment.php b/src/Entity/Attachments/Attachment.php index 96399fe2..34770572 100644 --- a/src/Entity/Attachments/Attachment.php +++ b/src/Entity/Attachments/Attachment.php @@ -337,6 +337,8 @@ abstract class Attachment extends NamedDBElement } $this->path = $url; + //Reset internal filename + $this->original_filename = null; } return $this; diff --git a/src/Form/AdminPages/BaseEntityAdminForm.php b/src/Form/AdminPages/BaseEntityAdminForm.php index 044c0133..75c5fc9c 100644 --- a/src/Form/AdminPages/BaseEntityAdminForm.php +++ b/src/Form/AdminPages/BaseEntityAdminForm.php @@ -38,7 +38,9 @@ use App\Entity\Attachments\PartAttachment; use App\Entity\Base\NamedDBElement; use App\Entity\Base\StructuralDBElement; use App\Form\AttachmentFormType; +use App\Form\Type\MasterPictureAttachmentType; use App\Form\Type\StructuralEntityType; +use Doctrine\ORM\EntityRepository; use FOS\CKEditorBundle\Form\Type\CKEditorType; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; @@ -87,7 +89,7 @@ class BaseEntityAdminForm extends AbstractType 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity), ]) ->add('parent', StructuralEntityType::class, ['class' => get_class($entity), - 'required' => false, 'label' => $this->trans->trans('parent.label'), + 'required' => false, 'label' => $this->trans->trans('parent.label'), 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'move', $entity), ]) ->add('not_selectable', CheckboxType::class, ['required' => false, @@ -101,7 +103,7 @@ class BaseEntityAdminForm extends AbstractType 'attr' => ['rows' => 4], 'help' => $this->trans->trans('bbcode.hint'), 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]); - $this->additionalFormElements($builder, $options, $entity); + $this->additionalFormElements($builder, $options, $entity); //Attachment section $builder->add('attachments', CollectionType::class, [ @@ -116,11 +118,18 @@ class BaseEntityAdminForm extends AbstractType 'by_reference' => false ]); - //Buttons - $builder->add('save', SubmitType::class, [ - 'label' => $is_new ? $this->trans->trans('entity.create') : $this->trans->trans('entity.edit.save'), - 'attr' => ['class' => $is_new ? 'btn-success' : ''], - 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]) + $builder->add('master_picture_attachment', MasterPictureAttachmentType::class, [ + 'required' => false, + 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity), + 'label' => $this->trans->trans('part.edit.master_attachment'), + 'entity' => $entity + ]); + + //Buttons + $builder->add('save', SubmitType::class, [ + 'label' => $is_new ? $this->trans->trans('entity.create') : $this->trans->trans('entity.edit.save'), + 'attr' => ['class' => $is_new ? 'btn-success' : ''], + 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]) ->add('reset', ResetType::class, ['label' => 'entity.edit.reset', 'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]); } diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index ff3769af..8dcade42 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -41,6 +41,7 @@ use App\Entity\Parts\Part; use App\Entity\Parts\Storelocation; use App\Entity\PriceInformations\Orderdetail; use App\Form\AttachmentFormType; +use App\Form\Type\MasterPictureAttachmentType; use App\Form\Type\SIUnitType; use App\Form\Type\StructuralEntityType; use Doctrine\DBAL\Types\FloatType; @@ -219,28 +220,11 @@ class PartBaseType extends AbstractType 'by_reference' => false ]); - $builder->add('master_picture_attachment', EntityType::class, [ + $builder->add('master_picture_attachment', MasterPictureAttachmentType::class, [ 'required' => false, 'disabled' => !$this->security->isGranted('attachments.edit', $part), 'label' => $this->trans->trans('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) { - if ($part->getID() == null) { - //This query is always false, so we get empty results - return $er->createQueryBuilder('u')->where('0 = 2'); - } - return $er->createQueryBuilder('u') - ->where('u.element = ?1') - ->andWhere("u.path <> ''") - ->orderBy('u.name', 'ASC') - ->setParameter(1, $part); - } + 'entity' => $part ]); //Orderdetails section diff --git a/src/Form/Type/MasterPictureAttachmentType.php b/src/Form/Type/MasterPictureAttachmentType.php new file mode 100644 index 00000000..bbe093ab --- /dev/null +++ b/src/Form/Type/MasterPictureAttachmentType.php @@ -0,0 +1,95 @@ +setRequired('entity'); + $resolver->setAllowedTypes('entity', AttachmentContainingDBElement::class); + + $resolver->setDefaults([ + 'filter' => 'picture', + 'attr' => ['class' => 'selectpicker'], + 'choice_attr' => function (Options $options) { + return function ($choice, $key, $value) use ($options) { + /** @var Attachment $choice */ + $tmp = ['data-subtext' => $choice->getFilename() ?? 'URL']; + + if ($options['filter'] === 'picture' && !$choice->isPicture()) { + $tmp += ['disabled' => 'disabled']; + } + + return $tmp; + }; + }, + 'choice_label' => 'name', + 'class' => function (Options $options) { + $short_class_name = (new \ReflectionClass($options['entity']))->getShortName(); + //Category becomes CategoryAttachment + return 'App\\Entity\\Attachments\\' . $short_class_name . 'Attachment'; + }, + 'query_builder' => function (Options $options) { + return function (EntityRepository $er) use ($options) { + $entity = $options['entity']; + if ($entity->getID() === null) { + //This query is always false, so we get empty results + return $er->createQueryBuilder('u')->where('0 = 2'); + } + return $er->createQueryBuilder('u') + ->where('u.element = ?1') + ->andWhere("u.path <> ''") + ->orderBy('u.name', 'ASC') + ->setParameter(1, $entity); + }; + } + ]); + + $resolver->setAllowedValues('filter', ['', 'picture', '3d_models']); + } + + public function getParent() + { + return EntityType::class; + } +} \ No newline at end of file diff --git a/templates/AdminPages/_attachments.html.twig b/templates/AdminPages/_attachments.html.twig index 6c774ef7..29394c4d 100644 --- a/templates/AdminPages/_attachments.html.twig +++ b/templates/AdminPages/_attachments.html.twig @@ -67,6 +67,10 @@ {% trans %}attachment.create{% endtrans %} +{% block master_picture_block %} + {{ form_row(form.master_picture_attachment) }} +{% endblock %} +