diff --git a/src/Controller/AttachmentTypeController.php b/src/Controller/AttachmentTypeController.php index e9fd62e1..4af5a467 100644 --- a/src/Controller/AttachmentTypeController.php +++ b/src/Controller/AttachmentTypeController.php @@ -33,52 +33,36 @@ namespace App\Controller; use App\Entity\AttachmentType; -use App\Entity\NamedDBElement; -use App\Entity\StructuralDBElement; + use App\Form\BaseEntityAdminForm; -use App\Form\ExportType; -use App\Form\ImportType; 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\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\SerializerInterface; -use Symfony\Component\Validator\ConstraintViolationList; /** * @Route("/attachment_type") * @package App\Controller */ -class AttachmentTypeController extends AbstractController +class AttachmentTypeController extends BaseAdminController { + protected $entity_class = AttachmentType::class; + protected $twig_template = 'AdminPages/AttachmentTypeAdmin.html.twig'; + protected $form_class = BaseEntityAdminForm::class; + protected $route_base = "attachment_type"; + /** * @Route("/{id}/edit", requirements={"id"="\d+"}, name="attachment_type_edit") * @Route("/{id}/", requirements={"id"="\d+"}) */ public function edit(AttachmentType $entity, Request $request, EntityManagerInterface $em) { - - $this->denyAccessUnlessGranted('read', $entity); - - $form = $this->createForm(BaseEntityAdminForm::class, $entity); - - $form->handleRequest($request); - if ($form->isSubmitted() && $form->isValid()) { - $em->persist($entity); - $em->flush(); - } - - return $this->render('AdminPages/AttachmentTypeAdmin.html.twig', [ - 'entity' => $entity, - 'form' => $form->createView() - ]); + return $this->_edit($entity, $request, $em); } /** @@ -89,48 +73,7 @@ class AttachmentTypeController extends AbstractController */ public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer) { - $new_entity = new AttachmentType(); - - $this->denyAccessUnlessGranted('read', $new_entity); - - //Basic edit form - $form = $this->createForm(BaseEntityAdminForm::class, $new_entity); - - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $em->persist($new_entity); - $em->flush(); - //$this->addFlash('success', $translator->trans('part.created_flash')); - - return $this->redirectToRoute('attachment_type_edit', ['id' => $new_entity->getID()]); - } - - //Import form - $import_form = $this->createForm(ImportType::class, ['entity_class' => AttachmentType::class]); - $import_form->handleRequest($request); - - if ($import_form->isSubmitted() && $import_form->isValid()) { - /** @var UploadedFile $file */ - $file = $import_form['file']->getData(); - $data = $import_form->getData(); - - $options = array('parent' => $data['parent'], 'preserve_children' => $data['preserve_children'], - 'format' => $data['format'], 'csv_separator' => $data['csv_separator']); - - $errors = $importer->fileToDBEntities($file, AttachmentType::class, $options); - - foreach ($errors as $name => $error) { - /** @var $error ConstraintViolationList */ - $this->addFlash('error', $name . ":" . $error); - } - } - - return $this->render('AdminPages/AttachmentTypeAdmin.html.twig', [ - 'entity' => $new_entity, - 'form' => $form->createView(), - 'import_form' => $import_form->createView() - ]); + return $this->_new($request, $em, $importer); } /** @@ -138,34 +81,7 @@ class AttachmentTypeController extends AbstractController */ public function delete(Request $request, AttachmentType $entity, StructuralElementRecursionHelper $recursionHelper) { - $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 - if ($request->get('delete_recursive', false)) { - $recursionHelper->delete($entity, false); - } else { - $parent = $entity->getParent(); - - //Move all sub entities to the current parent - foreach ($entity->getSubelements() as $subelement) { - $subelement->setParent($parent); - $entityManager->persist($subelement); - } - - //Remove current element - $entityManager->remove($entity); - } - - //Flush changes - $entityManager->flush(); - - $this->addFlash('success', 'attachment_type.deleted'); - } - - return $this->redirectToRoute('attachment_type_new'); + return $this->_delete($request, $entity, $recursionHelper); } /** @@ -177,11 +93,7 @@ class AttachmentTypeController extends AbstractController */ public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request) { - $this->denyAccessUnlessGranted('read', $entity); - - $entities = $em->getRepository(AttachmentType::class)->findAll(); - - return $exporter->exportEntityFromRequest($entities,$request); + return $this->_exportAll($em, $exporter, $request); } /** @@ -191,9 +103,7 @@ class AttachmentTypeController extends AbstractController */ public function exportEntity(AttachmentType $entity, EntityExporter $exporter, Request $request) { - $this->denyAccessUnlessGranted('read', $entity); - - return $exporter->exportEntityFromRequest($entity, $request); + return $this->_exportEntity($entity, $exporter, $request); } } \ No newline at end of file diff --git a/src/Controller/BaseAdminController.php b/src/Controller/BaseAdminController.php new file mode 100644 index 00000000..a0bd9239 --- /dev/null +++ b/src/Controller/BaseAdminController.php @@ -0,0 +1,177 @@ +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!'); + } + } + + protected function _edit(StructuralDBElement $entity, Request $request, EntityManagerInterface $em) + { + + $this->denyAccessUnlessGranted('read', $entity); + + $form = $this->createForm($this->form_class, $entity); + + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($entity); + $em->flush(); + } + + return $this->render($this->twig_template, [ + 'entity' => $entity, + 'form' => $form->createView() + ]); + } + + 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 + $form = $this->createForm($this->form_class, $new_entity); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $em->persist($new_entity); + $em->flush(); + //$this->addFlash('success', $translator->trans('part.created_flash')); + + return $this->redirectToRoute($this->route_base . '_edit', ['id' => $new_entity->getID()]); + } + + //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(); + + $options = array('parent' => $data['parent'], 'preserve_children' => $data['preserve_children'], + 'format' => $data['format'], 'csv_separator' => $data['csv_separator']); + + $errors = $importer->fileToDBEntities($file, $this->entity_class, $options); + + foreach ($errors as $name => $error) { + /** @var $error ConstraintViolationList */ + $this->addFlash('error', $name . ":" . $error); + } + } + + return $this->render($this->twig_template, [ + 'entity' => $new_entity, + 'form' => $form->createView(), + 'import_form' => $import_form->createView() + ]); + } + + protected function _delete(Request $request, StructuralDBElement $entity, StructuralElementRecursionHelper $recursionHelper) + { + $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 + if ($request->get('delete_recursive', false)) { + $recursionHelper->delete($entity, false); + } else { + $parent = $entity->getParent(); + + //Move all sub entities to the current parent + foreach ($entity->getSubelements() as $subelement) { + $subelement->setParent($parent); + $entityManager->persist($subelement); + } + + //Remove current element + $entityManager->remove($entity); + } + + //Flush changes + $entityManager->flush(); + + $this->addFlash('success', 'attachment_type.deleted'); + } + + return $this->redirectToRoute($this->route_base . '_new'); + } + + 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(); + + return $exporter->exportEntityFromRequest($entities,$request); + } + + protected function _exportEntity(StructuralDBElement $entity, EntityExporter $exporter, Request $request) + { + $this->denyAccessUnlessGranted('read', $entity); + + return $exporter->exportEntityFromRequest($entity, $request); + } +} \ No newline at end of file diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php new file mode 100644 index 00000000..3e6b8b89 --- /dev/null +++ b/src/Controller/CategoryController.php @@ -0,0 +1,109 @@ +_edit($entity, $request, $em); + } + + /** + * @Route("/new", name="category_new") + * @Route("/") + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer) + { + return $this->_new($request, $em, $importer); + } + + /** + * @Route("/{id}", name="category_delete", methods={"DELETE"}) + */ + public function delete(Request $request, Category $entity, StructuralElementRecursionHelper $recursionHelper) + { + return $this->_delete($request, $entity, $recursionHelper); + } + + /** + * @Route("/export", name="category_export_all") + * @param Request $request + * @param SerializerInterface $serializer + * @param EntityManagerInterface $em + * @return Response + */ + public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request) + { + return $this->_exportAll($em, $exporter, $request); + } + + /** + * @Route("/{id}/export", name="category_export") + * @param Request $request + * @param AttachmentType $entity + */ + public function exportEntity(Category $entity, EntityExporter $exporter, Request $request) + { + return $this->_exportEntity($entity, $exporter, $request); + } + +} \ No newline at end of file diff --git a/src/Services/EntityURLGenerator.php b/src/Services/EntityURLGenerator.php index f29b79eb..6561ac17 100644 --- a/src/Services/EntityURLGenerator.php +++ b/src/Services/EntityURLGenerator.php @@ -114,6 +114,10 @@ class EntityURLGenerator return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); } + if($entity instanceof Category) { + return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); + } + //Otherwise throw an error throw new EntityNotSupported('The given entity is not supported yet!'); } @@ -135,6 +139,10 @@ class EntityURLGenerator return $this->urlGenerator->generate('attachment_type_new'); } + if($entity instanceof Category) { + return $this->urlGenerator->generate('category_new'); + } + throw new EntityNotSupported('The given entity is not supported yet!'); } @@ -177,6 +185,10 @@ class EntityURLGenerator return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); } + if($entity instanceof Category) { + return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); + } + throw new EntityNotSupported('The given entity is not supported yet!'); } diff --git a/src/Services/ToolsTreeBuilder.php b/src/Services/ToolsTreeBuilder.php index 6fc4421e..333fac7b 100644 --- a/src/Services/ToolsTreeBuilder.php +++ b/src/Services/ToolsTreeBuilder.php @@ -61,6 +61,10 @@ class ToolsTreeBuilder $nodes = array(); $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.attachment_types'), $this->urlGenerator->generate('attachment_type_new')); + $nodes[] = new TreeViewNode($this->translator->trans('tree.tools.edit.categories'), + $this->urlGenerator->generate('category_new')); + + $nodes[] = new TreeViewNode('Node 2'); $tree[] = new TreeViewNode($this->translator->trans('tree.tools.edit'), null, $nodes); diff --git a/templates/AdminPages/CategoryAdmin.html.twig b/templates/AdminPages/CategoryAdmin.html.twig new file mode 100644 index 00000000..17208bb3 --- /dev/null +++ b/templates/AdminPages/CategoryAdmin.html.twig @@ -0,0 +1,5 @@ +{% extends "AdminPages/EntityAdminBase.html.twig" %} + +{% block card_title %} + {% trans %}category.labelp{% endtrans %} +{% endblock %} \ No newline at end of file