Added possibility to add/edit attachments on Admin pages.

This commit is contained in:
Jan Böhmer 2019-09-24 18:28:35 +02:00
parent 97cb91a3b2
commit eb1d8fd4e4
28 changed files with 247 additions and 25 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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