Allow to specify a preview attachment in the AdminPages.

This commit is contained in:
Jan Böhmer 2019-10-03 13:42:06 +02:00
parent 280b2d4427
commit d9fe77d0e8
5 changed files with 120 additions and 26 deletions

View file

@ -337,6 +337,8 @@ abstract class Attachment extends NamedDBElement
}
$this->path = $url;
//Reset internal filename
$this->original_filename = null;
}
return $this;

View file

@ -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)]);
}

View file

@ -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

View file

@ -0,0 +1,95 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 2019 Jan Böhmer
* https://github.com/jbtronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
namespace App\Form\Type;
use App\Entity\Attachments\Attachment;
use App\Entity\Attachments\AttachmentContainingDBElement;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MasterPictureAttachmentType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->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;
}
}

View file

@ -67,6 +67,10 @@
{% trans %}attachment.create{% endtrans %}
</button>
{% block master_picture_block %}
{{ form_row(form.master_picture_attachment) }}
{% endblock %}
<script>
function delete_attachment_entry(btn) {
window.bootbox.confirm('{% trans %}part_lot.edit.delete.confirm{% endtrans %}', function (result) {