diff --git a/src/Controller/AdminPages/AttachmentTypeController.php b/src/Controller/AdminPages/AttachmentTypeController.php index bd220f99..b65b406d 100644 --- a/src/Controller/AdminPages/AttachmentTypeController.php +++ b/src/Controller/AdminPages/AttachmentTypeController.php @@ -33,6 +33,7 @@ namespace App\Controller\AdminPages; use App\Entity\Attachments\AttachmentType; +use App\Entity\Attachments\AttachmentTypeAttachment; use App\Form\AdminPages\BaseEntityAdminForm; use App\Services\EntityExporter; use App\Services\EntityImporter; @@ -53,6 +54,7 @@ class AttachmentTypeController extends BaseAdminController protected $twig_template = 'AdminPages/AttachmentTypeAdmin.html.twig'; protected $form_class = BaseEntityAdminForm::class; protected $route_base = 'attachment_type'; + protected $attachment_class = AttachmentTypeAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="attachment_type_edit") diff --git a/src/Controller/AdminPages/BaseAdminController.php b/src/Controller/AdminPages/BaseAdminController.php index eb78f2c4..e2c0692a 100644 --- a/src/Controller/AdminPages/BaseAdminController.php +++ b/src/Controller/AdminPages/BaseAdminController.php @@ -36,11 +36,13 @@ use App\Entity\Base\StructuralDBElement; use App\Entity\UserSystem\User; use App\Form\AdminPages\ImportType; use App\Form\AdminPages\MassCreationForm; +use App\Services\AttachmentHelper; use App\Services\EntityExporter; use App\Services\EntityImporter; use App\Services\StructuralElementRecursionHelper; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; @@ -56,19 +58,26 @@ abstract class BaseAdminController extends AbstractController protected $form_class = ''; protected $twig_template = ''; protected $route_base = ''; + protected $attachment_class = ''; protected $passwordEncoder; protected $translator; + protected $attachmentHelper; - public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder) + public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder, AttachmentHelper $attachmentHelper) { if ($this->entity_class === '' || $this->form_class === '' || $this->twig_template === '' || $this->route_base === '') { throw new \InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!'); } + if ($this->attachment_class === '') { + throw new \InvalidArgumentException('You have to override the $attachment_class value in your subclass!'); + } + $this->translator = $translator; $this->passwordEncoder = $passwordEncoder; + $this->attachmentHelper = $attachmentHelper; } protected function _edit(NamedDBElement $entity, Request $request, EntityManagerInterface $em) @@ -76,7 +85,7 @@ abstract class BaseAdminController extends AbstractController $this->denyAccessUnlessGranted('read', $entity); - $form = $this->createForm($this->form_class, $entity); + $form = $this->createForm($this->form_class, $entity, ['attachment_class' => $this->attachment_class]); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -88,20 +97,28 @@ abstract class BaseAdminController extends AbstractController $entity->setNeedPwChange(true); } + //Upload passed files + $attachments = $form['attachments']; + foreach ($attachments as $attachment) { + /** @var $attachment FormInterface */ + $this->attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData()); + } + $em->persist($entity); $em->flush(); $this->addFlash('success', $this->translator->trans('entity.edit_flash')); //Rebuild form, so it is based on the updated data. Important for the parent field! //We can not use dynamic form events here, because the parent entity list is build from database! - $form = $this->createForm($this->form_class, $entity); + $form = $this->createForm($this->form_class, $entity, ['attachment_class' => $this->attachment_class]); } elseif ($form->isSubmitted() && ! $form->isValid()) { $this->addFlash('error', $this->translator->trans('entity.edit_flash.invalid')); } return $this->render($this->twig_template, [ 'entity' => $entity, - 'form' => $form->createView() + 'form' => $form->createView(), + 'attachment_helper' => $this->attachmentHelper ]); } @@ -113,7 +130,7 @@ abstract class BaseAdminController extends AbstractController $this->denyAccessUnlessGranted('read', $new_entity); //Basic edit form - $form = $this->createForm($this->form_class, $new_entity); + $form = $this->createForm($this->form_class, $new_entity, ['attachment_class' => $this->attachment_class]); $form->handleRequest($request); @@ -124,6 +141,14 @@ abstract class BaseAdminController extends AbstractController //By default the user must change the password afterwards $new_entity->setNeedPwChange(true); } + + //Upload passed files + $attachments = $form['attachments']; + foreach ($attachments as $attachment) { + /** @var $attachment FormInterface */ + $this->attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData()); + } + $em->persist($new_entity); $em->flush(); $this->addFlash('success', $this->translator->trans('entity.created_flash')); @@ -178,7 +203,8 @@ abstract class BaseAdminController extends AbstractController 'entity' => $new_entity, 'form' => $form->createView(), 'import_form' => $import_form->createView(), - 'mass_creation_form' => $mass_creation_form->createView() + 'mass_creation_form' => $mass_creation_form->createView(), + 'attachment_helper' => $this->attachmentHelper ]); } diff --git a/src/Controller/AdminPages/CategoryController.php b/src/Controller/AdminPages/CategoryController.php index b3cf9809..6b32a794 100644 --- a/src/Controller/AdminPages/CategoryController.php +++ b/src/Controller/AdminPages/CategoryController.php @@ -32,6 +32,7 @@ namespace App\Controller\AdminPages; +use App\Entity\Attachments\CategoryAttachment; use App\Entity\Parts\Category; use App\Form\AdminPages\CategoryAdminForm; use App\Services\EntityExporter; @@ -53,6 +54,7 @@ class CategoryController extends BaseAdminController protected $twig_template = 'AdminPages/CategoryAdmin.html.twig'; protected $form_class = CategoryAdminForm::class; protected $route_base = 'category'; + protected $attachment_class = CategoryAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="category_edit") diff --git a/src/Controller/AdminPages/CurrencyController.php b/src/Controller/AdminPages/CurrencyController.php index b261d66c..03c8d5c9 100644 --- a/src/Controller/AdminPages/CurrencyController.php +++ b/src/Controller/AdminPages/CurrencyController.php @@ -32,6 +32,7 @@ namespace App\Controller\AdminPages; +use App\Entity\Attachments\CurrencyAttachment; use App\Entity\PriceInformations\Currency; use App\Form\AdminPages\CurrencyAdminForm; use App\Services\EntityExporter; @@ -54,6 +55,7 @@ class CurrencyController extends BaseAdminController protected $twig_template = 'AdminPages/CurrencyAdmin.html.twig'; protected $form_class = CurrencyAdminForm::class; protected $route_base = 'currency'; + protected $attachment_class = CurrencyAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="currency_edit") diff --git a/src/Controller/AdminPages/DeviceController.php b/src/Controller/AdminPages/DeviceController.php index 1b50079d..26f48509 100644 --- a/src/Controller/AdminPages/DeviceController.php +++ b/src/Controller/AdminPages/DeviceController.php @@ -32,6 +32,7 @@ namespace App\Controller\AdminPages; +use App\Entity\Attachments\DeviceAttachment; use App\Entity\Devices\Device; use App\Form\AdminPages\BaseEntityAdminForm; use App\Services\EntityExporter; @@ -53,6 +54,7 @@ class DeviceController extends BaseAdminController protected $twig_template = 'AdminPages/DeviceAdmin.html.twig'; protected $form_class = BaseEntityAdminForm::class; protected $route_base = 'device'; + protected $attachment_class = DeviceAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="device_edit") diff --git a/src/Controller/AdminPages/FootprintController.php b/src/Controller/AdminPages/FootprintController.php index 903e42a0..cd9c88d8 100644 --- a/src/Controller/AdminPages/FootprintController.php +++ b/src/Controller/AdminPages/FootprintController.php @@ -34,6 +34,7 @@ namespace App\Controller\AdminPages; use App\Entity\Attachments\AttachmentType; +use App\Entity\Attachments\FootprintAttachment; use App\Entity\Parts\Footprint; use App\Form\AdminPages\BaseEntityAdminForm; use App\Services\EntityExporter; @@ -56,6 +57,7 @@ class FootprintController extends BaseAdminController protected $twig_template = 'AdminPages/FootprintAdmin.html.twig'; protected $form_class = BaseEntityAdminForm::class; protected $route_base = 'footprint'; + protected $attachment_class = FootprintAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="footprint_edit") diff --git a/src/Controller/AdminPages/ManufacturerController.php b/src/Controller/AdminPages/ManufacturerController.php index 5f23c13e..895e8088 100644 --- a/src/Controller/AdminPages/ManufacturerController.php +++ b/src/Controller/AdminPages/ManufacturerController.php @@ -32,6 +32,7 @@ namespace App\Controller\AdminPages; +use App\Entity\Attachments\ManufacturerAttachment; use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Supplier; use App\Form\AdminPages\CompanyForm; @@ -55,6 +56,7 @@ class ManufacturerController extends BaseAdminController protected $twig_template = 'AdminPages/ManufacturerAdmin.html.twig'; protected $form_class = CompanyForm::class; protected $route_base = 'manufacturer'; + protected $attachment_class = ManufacturerAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="manufacturer_edit") diff --git a/src/Controller/AdminPages/MeasurementUnitController.php b/src/Controller/AdminPages/MeasurementUnitController.php index ee63d71d..790e401d 100644 --- a/src/Controller/AdminPages/MeasurementUnitController.php +++ b/src/Controller/AdminPages/MeasurementUnitController.php @@ -33,6 +33,7 @@ namespace App\Controller\AdminPages; use App\Entity\Attachments\AttachmentType; +use App\Entity\Attachments\MeasurementUnitAttachment; use App\Entity\Parts\MeasurementUnit; use App\Form\AdminPages\MeasurementUnitAdminForm; use App\Services\EntityExporter; @@ -55,6 +56,7 @@ class MeasurementUnitController extends BaseAdminController protected $twig_template = 'AdminPages/MeasurementUnitAdmin.html.twig'; protected $form_class = MeasurementUnitAdminForm::class; protected $route_base = 'measurement_unit'; + protected $attachment_class = MeasurementUnitAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="measurement_unit_edit") diff --git a/src/Controller/AdminPages/StorelocationController.php b/src/Controller/AdminPages/StorelocationController.php index be6fb869..cf4f80f0 100644 --- a/src/Controller/AdminPages/StorelocationController.php +++ b/src/Controller/AdminPages/StorelocationController.php @@ -55,6 +55,7 @@ class StorelocationController extends BaseAdminController protected $twig_template = 'AdminPages/StorelocationAdmin.html.twig'; protected $form_class = StorelocationAdminForm::class; protected $route_base = 'store_location'; + protected $attachment_class = StorelocationAdminForm::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="store_location_edit") diff --git a/src/Controller/AdminPages/SupplierController.php b/src/Controller/AdminPages/SupplierController.php index 86d42eea..d11a915a 100644 --- a/src/Controller/AdminPages/SupplierController.php +++ b/src/Controller/AdminPages/SupplierController.php @@ -32,6 +32,7 @@ namespace App\Controller\AdminPages; +use App\Entity\Attachments\SupplierAttachment; use App\Entity\Parts\Supplier; use App\Form\AdminPages\SupplierForm; use App\Services\EntityExporter; @@ -54,6 +55,7 @@ class SupplierController extends BaseAdminController protected $twig_template = 'AdminPages/SupplierAdmin.html.twig'; protected $form_class = SupplierForm::class; protected $route_base = 'supplier'; + protected $attachment_class = SupplierAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="supplier_edit") diff --git a/src/Controller/GroupController.php b/src/Controller/GroupController.php index 3a0f5f04..623a3fbd 100644 --- a/src/Controller/GroupController.php +++ b/src/Controller/GroupController.php @@ -33,6 +33,7 @@ namespace App\Controller; use App\Controller\AdminPages\BaseAdminController; +use App\Entity\Attachments\GroupAttachment; use App\Entity\UserSystem\Group; use App\Form\AdminPages\GroupAdminForm; use App\Services\EntityExporter; @@ -52,6 +53,7 @@ class GroupController extends BaseAdminController protected $twig_template = 'AdminPages/GroupAdmin.html.twig'; protected $form_class = GroupAdminForm::class; protected $route_base = 'group'; + protected $attachment_class = GroupAttachment::class; /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="group_edit") diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 041faf17..2a9846da 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -30,6 +30,7 @@ namespace App\Controller; use App\Entity\Attachments\AttachmentType; +use App\Entity\Attachments\UserAttachment; use App\Entity\UserSystem\User; use App\Form\Permissions\PermissionsType; use App\Form\UserAdminForm; @@ -62,6 +63,7 @@ class UserController extends AdminPages\BaseAdminController protected $twig_template = 'AdminPages/UserAdmin.html.twig'; protected $form_class = UserAdminForm::class; protected $route_base = 'user'; + protected $attachment_class = UserAttachment::class; /** diff --git a/src/Entity/Attachments/AttachmentType.php b/src/Entity/Attachments/AttachmentType.php index f366899a..c831109a 100644 --- a/src/Entity/Attachments/AttachmentType.php +++ b/src/Entity/Attachments/AttachmentType.php @@ -67,7 +67,7 @@ class AttachmentType extends StructuralDBElement { /** * @var Collection|AttachmentTypeAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\AttachmentTypeAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\AttachmentTypeAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Devices/Device.php b/src/Entity/Devices/Device.php index 9670c059..e234763a 100644 --- a/src/Entity/Devices/Device.php +++ b/src/Entity/Devices/Device.php @@ -78,7 +78,7 @@ class Device extends PartsContainingDBElement /** * @var Collection|DeviceAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\DeviceAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\DeviceAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/Category.php b/src/Entity/Parts/Category.php index 07ddc0ba..9cde8a5b 100644 --- a/src/Entity/Parts/Category.php +++ b/src/Entity/Parts/Category.php @@ -68,7 +68,7 @@ class Category extends PartsContainingDBElement /** * @var Collection|CategoryAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\CategoryAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\CategoryAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/Footprint.php b/src/Entity/Parts/Footprint.php index 4c05cb72..0e9dfaf7 100644 --- a/src/Entity/Parts/Footprint.php +++ b/src/Entity/Parts/Footprint.php @@ -77,7 +77,7 @@ class Footprint extends PartsContainingDBElement { /** * @var Collection|FootprintAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\FootprintAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\FootprintAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/Manufacturer.php b/src/Entity/Parts/Manufacturer.php index ed1619fe..e27dd456 100644 --- a/src/Entity/Parts/Manufacturer.php +++ b/src/Entity/Parts/Manufacturer.php @@ -77,7 +77,7 @@ class Manufacturer extends Company { /** * @var Collection|ManufacturerAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/MeasurementUnit.php b/src/Entity/Parts/MeasurementUnit.php index f4595764..ce88ed26 100644 --- a/src/Entity/Parts/MeasurementUnit.php +++ b/src/Entity/Parts/MeasurementUnit.php @@ -54,7 +54,7 @@ class MeasurementUnit extends PartsContainingDBElement /** * @var Collection|MeasurementUnitAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\MeasurementUnitAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\MeasurementUnitAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/Storelocation.php b/src/Entity/Parts/Storelocation.php index 5f937a83..a7014f05 100644 --- a/src/Entity/Parts/Storelocation.php +++ b/src/Entity/Parts/Storelocation.php @@ -78,7 +78,7 @@ class Storelocation extends PartsContainingDBElement /** * @var Collection|StorelocationAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\StorelocationAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\StorelocationAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/Parts/Supplier.php b/src/Entity/Parts/Supplier.php index ee3b03e0..a54e48f9 100644 --- a/src/Entity/Parts/Supplier.php +++ b/src/Entity/Parts/Supplier.php @@ -80,7 +80,7 @@ class Supplier extends Company { /** * @var Collection|SupplierAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\SupplierAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/PriceInformations/Currency.php b/src/Entity/PriceInformations/Currency.php index ac9f63b9..db2dcbe4 100644 --- a/src/Entity/PriceInformations/Currency.php +++ b/src/Entity/PriceInformations/Currency.php @@ -54,7 +54,7 @@ class Currency extends StructuralDBElement /** * @var Collection|CurrencyAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\CurrencyAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\CurrencyAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/UserSystem/Group.php b/src/Entity/UserSystem/Group.php index e321480a..67c2e256 100644 --- a/src/Entity/UserSystem/Group.php +++ b/src/Entity/UserSystem/Group.php @@ -50,7 +50,7 @@ class Group extends StructuralDBElement implements HasPermissionsInterface /** * @var Collection|GroupAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Entity/UserSystem/User.php b/src/Entity/UserSystem/User.php index b695c579..6557bd99 100644 --- a/src/Entity/UserSystem/User.php +++ b/src/Entity/UserSystem/User.php @@ -90,7 +90,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe /** * @var Collection|UserAttachment[] - * @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element") + * @ORM\OneToMany(targetEntity="App\Entity\Attachments\UserAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) */ protected $attachments; diff --git a/src/Form/AdminPages/BaseEntityAdminForm.php b/src/Form/AdminPages/BaseEntityAdminForm.php index 60cbde5e..6356b1cd 100644 --- a/src/Form/AdminPages/BaseEntityAdminForm.php +++ b/src/Form/AdminPages/BaseEntityAdminForm.php @@ -32,8 +32,12 @@ namespace App\Form\AdminPages; +use App\Entity\Attachments\Attachment; +use App\Entity\Attachments\FootprintAttachment; +use App\Entity\Attachments\PartAttachment; use App\Entity\Base\NamedDBElement; use App\Entity\Base\StructuralDBElement; +use App\Form\AttachmentFormType; use App\Form\Type\StructuralEntityType; use FOS\CKEditorBundle\Form\Type\CKEditorType; use Symfony\Bridge\Doctrine\Form\Type\EntityType; @@ -41,10 +45,12 @@ use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\ResetType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Security; use Symfony\Component\Translation\Reader\TranslationReader; use Symfony\Contracts\Translation\TranslatorInterface; @@ -63,6 +69,11 @@ class BaseEntityAdminForm extends AbstractType $this->trans = $trans; } + public function configureOptions(OptionsResolver $resolver) + { + parent::configureOptions($resolver); // TODO: Change the autogenerated stub + $resolver->setRequired('attachment_class'); + } public function buildForm(FormBuilderInterface $builder, array $options) { @@ -92,6 +103,18 @@ class BaseEntityAdminForm extends AbstractType $this->additionalFormElements($builder, $options, $entity); + //Attachment section + $builder->add('attachments', CollectionType::class, [ + 'entry_type' => AttachmentFormType::class, + 'allow_add' => true, + 'allow_delete' => true, + 'label' => false, + 'entry_options' => [ + 'data_class' => $options['attachment_class'], + ], + 'by_reference' => false + ]); + //Buttons $builder->add('save', SubmitType::class, [ 'label' => $is_new ? $this->trans->trans('entity.create') : $this->trans->trans('entity.edit.save'), diff --git a/src/Form/UserAdminForm.php b/src/Form/UserAdminForm.php index 341c531c..471f0e17 100644 --- a/src/Form/UserAdminForm.php +++ b/src/Form/UserAdminForm.php @@ -44,6 +44,7 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\LanguageType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\RepeatedType; @@ -52,6 +53,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TimezoneType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Security; use Symfony\Component\Validator\Constraints\Length; use Symfony\Contracts\Translation\TranslatorInterface; @@ -68,6 +70,11 @@ class UserAdminForm extends AbstractType $this->trans = $trans; } + public function configureOptions(OptionsResolver $resolver) + { + parent::configureOptions($resolver); // TODO: Change the autogenerated stub + $resolver->setRequired('attachment_class'); + } public function buildForm(FormBuilderInterface $builder, array $options) { @@ -182,6 +189,18 @@ class UserAdminForm extends AbstractType $this->additionalFormElements($builder, $options, $entity); + //Attachment section + $builder->add('attachments', CollectionType::class, [ + 'entry_type' => AttachmentFormType::class, + 'allow_add' => true, + 'allow_delete' => true, + 'label' => false, + 'entry_options' => [ + 'data_class' => $options['attachment_class'], + ], + 'by_reference' => false + ]); + //Buttons $builder->add('save', SubmitType::class, [ 'label' => $is_new ? $this->trans->trans('user.create') : $this->trans->trans('user.edit.save'), diff --git a/src/Services/AttachmentHelper.php b/src/Services/AttachmentHelper.php index ae8ac403..a7033b8f 100644 --- a/src/Services/AttachmentHelper.php +++ b/src/Services/AttachmentHelper.php @@ -33,7 +33,21 @@ namespace App\Services; use App\Entity\Attachments\Attachment; +use App\Entity\Attachments\AttachmentTypeAttachment; +use App\Entity\Attachments\CategoryAttachment; +use App\Entity\Attachments\CurrencyAttachment; +use App\Entity\Attachments\DeviceAttachment; +use App\Entity\Attachments\FootprintAttachment; +use App\Entity\Attachments\GroupAttachment; +use App\Entity\Attachments\ManufacturerAttachment; +use App\Entity\Attachments\MeasurementUnitAttachment; use App\Entity\Attachments\PartAttachment; +use App\Entity\Attachments\StorelocationAttachment; +use App\Entity\Attachments\SupplierAttachment; +use App\Entity\Attachments\UserAttachment; +use App\Entity\Parts\Part; +use App\Entity\UserSystem\Group; +use App\Entity\UserSystem\User; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -46,6 +60,10 @@ class AttachmentHelper */ protected $base_path; + protected $footprints_path; + + protected $footprints_3d_path; + public function __construct(ParameterBagInterface $params, KernelInterface $kernel) { $tmp_base_path = $params->get('media_directory'); @@ -58,6 +76,10 @@ class AttachmentHelper } else { $this->base_path = realpath($kernel->getProjectDir() . DIRECTORY_SEPARATOR . $tmp_base_path); } + + $this->footprints_path = realpath($kernel->getProjectDir() . "/public/img/footprints"); + //TODO + $this->footprints_3d_path = "TODO"; } /** @@ -91,11 +113,15 @@ class AttachmentHelper */ public function placeholderToRealPath(string $placeholder_path) : string { - //The new attachments use %MEDIA% as placeholders, which is the directory set in media_directory - $placeholder_path = str_replace("%MEDIA%", $this->base_path, $placeholder_path); + $placeholders = ["%MEDIA%", "%BASE%/data/media", "%FOOTPRINTS%", "%FOOTPRINTS_3D"]; + $targets = [$this->base_path, $this->base_path, $this->footprints_path, $this->footprints_3d_path]; + + dump($placeholder_path); + //The new attachments use %MEDIA% as placeholders, which is the directory set in media_directory //Older path entries are given via %BASE% which was the project root - $placeholder_path = str_replace("%BASE%/data/media", $this->base_path, $placeholder_path); + $placeholder_path = str_replace($placeholders, $targets, $placeholder_path); + dump($placeholder_path); //Normalize path $placeholder_path = str_replace('\\', '/', $placeholder_path); @@ -207,7 +233,12 @@ class AttachmentHelper */ public function generateFolderForAttachment(Attachment $attachment) : string { - $mapping = [PartAttachment::class => 'part']; + $mapping = [PartAttachment::class => 'part', AttachmentTypeAttachment::class => 'attachment_type', + CategoryAttachment::class => 'category', CurrencyAttachment::class => 'currency', + DeviceAttachment::class => 'device', FootprintAttachment::class => 'footprint', + GroupAttachment::class => 'group', ManufacturerAttachment::class => 'manufacturer', + MeasurementUnitAttachment::class => 'measurement_unit', StorelocationAttachment::class => 'storelocation', + SupplierAttachment::class => 'supplier', UserAttachment::class => 'user']; $path = $this->base_path . DIRECTORY_SEPARATOR . $mapping[get_class($attachment)] . DIRECTORY_SEPARATOR . $attachment->getElement()->getID(); return $path; diff --git a/templates/AdminPages/EntityAdminBase.html.twig b/templates/AdminPages/EntityAdminBase.html.twig index e1736cf9..41ea95a9 100644 --- a/templates/AdminPages/EntityAdminBase.html.twig +++ b/templates/AdminPages/EntityAdminBase.html.twig @@ -57,16 +57,20 @@ {{ form_start(form) }} - {% if block('additional_pills') is not empty %} + - {% endif %}
-
+
{{ form_row(form.name) }} {% if form.parent%} {{ form_row(form.parent) }} @@ -83,6 +87,10 @@
{% block additional_panes %}{% endblock %} + +
+ {% include "AdminPages/_attachments.html.twig" %} +
{{ form_row(form.save) }} diff --git a/templates/AdminPages/_attachments.html.twig b/templates/AdminPages/_attachments.html.twig new file mode 100644 index 00000000..46736b5d --- /dev/null +++ b/templates/AdminPages/_attachments.html.twig @@ -0,0 +1,94 @@ +{% set delete_btn %} + +{% endset %} + +{#{{ form_row(form.master_picture_attachment) }} #} + + + + {% for attachment in form.attachments %} + + + + + {% endfor %} + +
+ {{ form_widget(attachment) }} + + {{ delete_btn }} + + {% set attach = attachment.vars.value %} + + {% if attachment_helper.fileExisting(attach) %} + {% if not attach.external %} +

+
+ + {{ attach.filename }} + +
+ + {{ attachment_helper.humanFileSize(attach) }} + +
+ {% else %} +

+
+ + {% trans %}attachment.external{% endtrans %} + +
+ {% endif %} + {% else %} +

+
+ + {% trans %}attachment.file_not_found{% endtrans %} + +
+ {% endif %} + +
+ + + + \ No newline at end of file