2019-04-20 20:39:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-04-20 20:39:06 +02:00
|
|
|
*
|
2019-11-01 13:40:30 +01:00
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
2019-04-20 20:39:06 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-04-28 13:10:11 +02:00
|
|
|
namespace App\Controller\AdminPages;
|
2019-04-20 20:39:06 +02:00
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Base\NamedDBElement;
|
|
|
|
use App\Entity\Base\StructuralDBElement;
|
2019-09-19 12:35:28 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2019-10-19 18:50:17 +02:00
|
|
|
use App\Exceptions\AttachmentDownloadException;
|
2019-08-12 22:41:58 +02:00
|
|
|
use App\Form\AdminPages\ImportType;
|
|
|
|
use App\Form\AdminPages\MassCreationForm;
|
2019-10-19 23:29:51 +02:00
|
|
|
use App\Services\Attachments\AttachmentManager;
|
2019-10-19 17:13:13 +02:00
|
|
|
use App\Services\Attachments\AttachmentSubmitHandler;
|
2019-04-20 20:39:06 +02:00
|
|
|
use App\Services\EntityExporter;
|
|
|
|
use App\Services\EntityImporter;
|
|
|
|
use App\Services\StructuralElementRecursionHelper;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2019-09-24 18:28:35 +02:00
|
|
|
use Symfony\Component\Form\FormInterface;
|
2019-04-20 20:39:06 +02:00
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2019-09-19 12:35:28 +02:00
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
2019-04-20 20:39:06 +02:00
|
|
|
use Symfony\Component\Validator\ConstraintViolationList;
|
2019-08-25 18:15:58 +02:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-04-20 20:39:06 +02:00
|
|
|
|
|
|
|
abstract class BaseAdminController extends AbstractController
|
|
|
|
{
|
2019-08-20 18:39:57 +02:00
|
|
|
protected $entity_class = '';
|
|
|
|
protected $form_class = '';
|
|
|
|
protected $twig_template = '';
|
|
|
|
protected $route_base = '';
|
2019-09-24 18:28:35 +02:00
|
|
|
protected $attachment_class = '';
|
2019-04-20 20:39:06 +02:00
|
|
|
|
2019-09-19 12:35:28 +02:00
|
|
|
protected $passwordEncoder;
|
2019-08-25 18:15:58 +02:00
|
|
|
protected $translator;
|
2019-09-24 18:28:35 +02:00
|
|
|
protected $attachmentHelper;
|
2019-10-19 17:13:13 +02:00
|
|
|
protected $attachmentSubmitHandler;
|
2019-08-25 18:15:58 +02:00
|
|
|
|
2019-10-19 17:13:13 +02:00
|
|
|
public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder,
|
2019-10-19 23:29:51 +02:00
|
|
|
AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler)
|
2019-04-20 20:39:06 +02:00
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
if ('' === $this->entity_class || '' === $this->form_class || '' === $this->twig_template || '' === $this->route_base) {
|
2019-04-20 20:39:06 +02:00
|
|
|
throw new \InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!');
|
|
|
|
}
|
2019-08-25 18:15:58 +02:00
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if ('' === $this->attachment_class) {
|
2019-09-24 18:28:35 +02:00
|
|
|
throw new \InvalidArgumentException('You have to override the $attachment_class value in your subclass!');
|
|
|
|
}
|
|
|
|
|
2019-08-25 18:15:58 +02:00
|
|
|
$this->translator = $translator;
|
2019-09-19 12:35:28 +02:00
|
|
|
$this->passwordEncoder = $passwordEncoder;
|
2019-09-24 18:28:35 +02:00
|
|
|
$this->attachmentHelper = $attachmentHelper;
|
2019-10-19 17:13:13 +02:00
|
|
|
$this->attachmentSubmitHandler = $attachmentSubmitHandler;
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
protected function _edit(NamedDBElement $entity, Request $request, EntityManagerInterface $em)
|
2019-04-20 20:39:06 +02:00
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('read', $entity);
|
|
|
|
|
2019-09-24 18:28:35 +02:00
|
|
|
$form = $this->createForm($this->form_class, $entity, ['attachment_class' => $this->attachment_class]);
|
2019-04-20 20:39:06 +02:00
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2019-09-19 12:35:28 +02:00
|
|
|
//Check if we editing a user and if we need to change the password of it
|
|
|
|
if ($entity instanceof User && !empty($form['new_password']->getData())) {
|
|
|
|
$password = $this->passwordEncoder->encodePassword($entity, $form['new_password']->getData());
|
|
|
|
$entity->setPassword($password);
|
|
|
|
//By default the user must change the password afterwards
|
|
|
|
$entity->setNeedPwChange(true);
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:28:35 +02:00
|
|
|
//Upload passed files
|
|
|
|
$attachments = $form['attachments'];
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
/** @var $attachment FormInterface */
|
2019-10-19 18:42:06 +02:00
|
|
|
$options = [
|
|
|
|
'secure_attachment' => $attachment['secureFile']->getData(),
|
2019-11-09 00:47:20 +01:00
|
|
|
'download_url' => $attachment['downloadURL']->getData(),
|
2019-10-19 18:42:06 +02:00
|
|
|
];
|
2019-10-19 18:50:17 +02:00
|
|
|
try {
|
|
|
|
$this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options);
|
|
|
|
} catch (AttachmentDownloadException $ex) {
|
|
|
|
$this->addFlash(
|
|
|
|
'error',
|
2019-11-09 00:47:20 +01:00
|
|
|
$this->translator->trans('attachment.download_failed').' '.$ex->getMessage()
|
2019-10-19 18:50:17 +02:00
|
|
|
);
|
|
|
|
}
|
2019-09-24 18:28:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 20:39:06 +02:00
|
|
|
$em->persist($entity);
|
|
|
|
$em->flush();
|
2019-08-25 18:15:58 +02:00
|
|
|
$this->addFlash('success', $this->translator->trans('entity.edit_flash'));
|
2019-08-26 11:41:39 +02:00
|
|
|
|
|
|
|
//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!
|
2019-09-24 18:28:35 +02:00
|
|
|
$form = $this->createForm($this->form_class, $entity, ['attachment_class' => $this->attachment_class]);
|
2019-11-09 00:47:20 +01:00
|
|
|
} elseif ($form->isSubmitted() && !$form->isValid()) {
|
2019-08-25 18:15:58 +02:00
|
|
|
$this->addFlash('error', $this->translator->trans('entity.edit_flash.invalid'));
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render($this->twig_template, [
|
|
|
|
'entity' => $entity,
|
2019-09-24 18:28:35 +02:00
|
|
|
'form' => $form->createView(),
|
2019-11-09 00:47:20 +01:00
|
|
|
'attachment_helper' => $this->attachmentHelper,
|
2019-04-20 20:39:06 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _new(Request $request, EntityManagerInterface $em, EntityImporter $importer)
|
|
|
|
{
|
|
|
|
/** @var StructuralDBElement $new_entity */
|
|
|
|
$new_entity = new $this->entity_class();
|
|
|
|
|
|
|
|
$this->denyAccessUnlessGranted('read', $new_entity);
|
|
|
|
|
|
|
|
//Basic edit form
|
2019-09-24 18:28:35 +02:00
|
|
|
$form = $this->createForm($this->form_class, $new_entity, ['attachment_class' => $this->attachment_class]);
|
2019-04-20 20:39:06 +02:00
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2019-09-19 12:35:28 +02:00
|
|
|
if ($new_entity instanceof User && !empty($form['new_password']->getData())) {
|
|
|
|
$password = $this->passwordEncoder->encodePassword($new_entity, $form['new_password']->getData());
|
|
|
|
$new_entity->setPassword($password);
|
|
|
|
//By default the user must change the password afterwards
|
|
|
|
$new_entity->setNeedPwChange(true);
|
|
|
|
}
|
2019-09-24 18:28:35 +02:00
|
|
|
|
|
|
|
//Upload passed files
|
|
|
|
$attachments = $form['attachments'];
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
/** @var $attachment FormInterface */
|
2019-10-19 18:50:17 +02:00
|
|
|
$options = [
|
|
|
|
'secure_attachment' => $attachment['secureFile']->getData(),
|
2019-11-09 00:47:20 +01:00
|
|
|
'download_url' => $attachment['downloadURL']->getData(),
|
2019-10-19 18:50:17 +02:00
|
|
|
];
|
|
|
|
try {
|
|
|
|
$this->attachmentSubmitHandler->handleFormSubmit($attachment->getData(), $attachment['file']->getData(), $options);
|
|
|
|
} catch (AttachmentDownloadException $ex) {
|
|
|
|
$this->addFlash(
|
|
|
|
'error',
|
2019-11-09 00:47:20 +01:00
|
|
|
$this->translator->trans('attachment.download_failed').' '.$ex->getMessage()
|
2019-10-19 18:50:17 +02:00
|
|
|
);
|
|
|
|
}
|
2019-09-24 18:28:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 20:39:06 +02:00
|
|
|
$em->persist($new_entity);
|
|
|
|
$em->flush();
|
2019-08-25 18:15:58 +02:00
|
|
|
$this->addFlash('success', $this->translator->trans('entity.created_flash'));
|
2019-04-20 20:39:06 +02:00
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
return $this->redirectToRoute($this->route_base.'_edit', ['id' => $new_entity->getID()]);
|
2019-09-16 22:04:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
if ($form->isSubmitted() && !$form->isValid()) {
|
2019-08-25 18:15:58 +02:00
|
|
|
$this->addFlash('error', $this->translator->trans('entity.created_flash.invalid'));
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Import form
|
|
|
|
$import_form = $this->createForm(ImportType::class, ['entity_class' => $this->entity_class]);
|
|
|
|
$import_form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($import_form->isSubmitted() && $import_form->isValid()) {
|
|
|
|
/** @var UploadedFile $file */
|
|
|
|
$file = $import_form['file']->getData();
|
|
|
|
$data = $import_form->getData();
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$options = ['parent' => $data['parent'], 'preserve_children' => $data['preserve_children'],
|
|
|
|
'format' => $data['format'], 'csv_separator' => $data['csv_separator'], ];
|
2019-04-20 20:39:06 +02:00
|
|
|
|
|
|
|
$errors = $importer->fileToDBEntities($file, $this->entity_class, $options);
|
|
|
|
|
|
|
|
foreach ($errors as $name => $error) {
|
2019-11-09 00:47:20 +01:00
|
|
|
/* @var $error ConstraintViolationList */
|
|
|
|
$this->addFlash('error', $name.':'.$error);
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:41:58 +02:00
|
|
|
//Mass creation form
|
|
|
|
$mass_creation_form = $this->createForm(MassCreationForm::class, ['entity_class' => $this->entity_class]);
|
|
|
|
$mass_creation_form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($mass_creation_form->isSubmitted() && $mass_creation_form->isValid()) {
|
|
|
|
$data = $mass_creation_form->getData();
|
|
|
|
|
|
|
|
dump($data);
|
|
|
|
|
|
|
|
//Create entries based on input
|
|
|
|
$errors = $importer->massCreation($data['lines'], $this->entity_class, $data['parent']);
|
|
|
|
|
|
|
|
//Show errors to user:
|
|
|
|
foreach ($errors as $name => $error) {
|
2019-11-09 00:47:20 +01:00
|
|
|
/* @var $error ConstraintViolationList */
|
|
|
|
$this->addFlash('error', $name.':'.$error);
|
2019-08-12 22:41:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 20:39:06 +02:00
|
|
|
return $this->render($this->twig_template, [
|
|
|
|
'entity' => $new_entity,
|
|
|
|
'form' => $form->createView(),
|
2019-08-12 22:41:58 +02:00
|
|
|
'import_form' => $import_form->createView(),
|
2019-09-24 18:28:35 +02:00
|
|
|
'mass_creation_form' => $mass_creation_form->createView(),
|
2019-11-09 00:47:20 +01:00
|
|
|
'attachment_helper' => $this->attachmentHelper,
|
2019-04-20 20:39:06 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
protected function _delete(Request $request, NamedDBElement $entity, StructuralElementRecursionHelper $recursionHelper)
|
2019-04-20 20:39:06 +02:00
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('delete', $entity);
|
|
|
|
|
|
|
|
if ($this->isCsrfTokenValid('delete'.$entity->getId(), $request->request->get('_token'))) {
|
|
|
|
$entityManager = $this->getDoctrine()->getManager();
|
|
|
|
|
|
|
|
//Check if we need to remove recursively
|
2019-04-28 14:18:11 +02:00
|
|
|
if ($entity instanceof StructuralDBElement && $request->get('delete_recursive', false)) {
|
2019-04-20 20:39:06 +02:00
|
|
|
$recursionHelper->delete($entity, false);
|
|
|
|
} else {
|
2019-09-19 12:35:28 +02:00
|
|
|
if ($entity instanceof StructuralDBElement) {
|
|
|
|
$parent = $entity->getParent();
|
|
|
|
|
|
|
|
//Move all sub entities to the current parent
|
|
|
|
foreach ($entity->getSubelements() as $subelement) {
|
|
|
|
$subelement->setParent($parent);
|
|
|
|
$entityManager->persist($subelement);
|
|
|
|
}
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Remove current element
|
|
|
|
$entityManager->remove($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Flush changes
|
|
|
|
$entityManager->flush();
|
|
|
|
|
|
|
|
$this->addFlash('success', 'attachment_type.deleted');
|
2019-10-26 22:27:04 +02:00
|
|
|
} else {
|
|
|
|
$this->addFlash('error', 'csfr_invalid');
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
return $this->redirectToRoute($this->route_base.'_new');
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function _exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request)
|
|
|
|
{
|
|
|
|
$entity = new $this->entity_class();
|
|
|
|
|
|
|
|
$this->denyAccessUnlessGranted('read', $entity);
|
|
|
|
|
|
|
|
$entities = $em->getRepository($this->entity_class)->findAll();
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
return $exporter->exportEntityFromRequest($entities, $request);
|
2019-04-20 20:39:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
protected function _exportEntity(NamedDBElement $entity, EntityExporter $exporter, Request $request)
|
2019-04-20 20:39:06 +02:00
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('read', $entity);
|
|
|
|
|
|
|
|
return $exporter->exportEntityFromRequest($entity, $request);
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|