From 239240360e6659a1cea26fe7e9f1b86f2f23545d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P=2E?= Date: Thu, 7 Nov 2024 18:53:34 +0100 Subject: [PATCH] Copy record for bibliography --- src/Controller/BibliographyController.php | 41 +++++++++++++++++++++-- src/Entity/Bibliography.php | 3 ++ templates/bibliography/index.html.twig | 5 +-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Controller/BibliographyController.php b/src/Controller/BibliographyController.php index e95e7d0..0169ef9 100644 --- a/src/Controller/BibliographyController.php +++ b/src/Controller/BibliographyController.php @@ -18,7 +18,6 @@ class BibliographyController extends AbstractController #[Route('/bibliography/{id<\d+>}', name: 'app_bibliography')] public function index(Bibliography $bibliography, EntityManagerInterface $em): Response { - $repo = $em->getRepository(Collection::class); $collections = $repo->findAllByBibliography($bibliography->getId()); $repo = $em->getRepository(Collector::class); @@ -37,7 +36,7 @@ class BibliographyController extends AbstractController public function landing(EntityManagerInterface $em): Response { $repo = $em->getRepository(Bibliography::class); - $records = $repo->findBy([], ['modifiedAt' => 'DESC']); + $records = $repo->findBy([], ['id' => 'DESC']); $count = count($records); $records = array_slice($records, 0, 15); @@ -91,4 +90,42 @@ class BibliographyController extends AbstractController return $this->redirectToRoute('app_bibliography_landing'); } + + /** + * @todo Permissions! Return JSON with 403 when AJAX + */ + #[Route('/bibliography/copy/{id<\d+>}', name: 'app_bibliography_copy')] + public function copy(Bibliography $bibliography, EntityManagerInterface $em): Response + { + try { + $this->denyAccessUnlessGranted(RecordVoter::EDIT, $bibliography); + } + catch (AccessDeniedException) { + $this->addFlash('warning', 'You are not authorized to copy this record'); + return $this->redirectToRoute('app_home'); + } + + $user = $this->getUser(); + $editor = "{$user->getFirstname()} {$user->getLastName()}"; + + // TODO Move clone logic to __clone() in Entity or Repository + $copy = clone $bibliography; + $copy->setEditor($editor); + $copy->setOwner($editor); + $copy->setCreator($user->getUsername()); + $repo = $em->getRepository(Collection::class); + $copy->setCollections( + $repo->findAllByBibliography($bibliography->getId()) + ); + $repo = $em->getRepository(Collector::class); + $copy->setCollectors( + $repo->findAllByBibliography($bibliography->getId()) + ); + $copy->setCitation("{$bibliography->getCitation()} - Copy"); + + $em->persist($copy); + $em->flush(); + + return $this->redirectToRoute('app_bibliography', ['id' => $copy->getId()]); + } } diff --git a/src/Entity/Bibliography.php b/src/Entity/Bibliography.php index 9341f3f..cd089c1 100644 --- a/src/Entity/Bibliography.php +++ b/src/Entity/Bibliography.php @@ -4,7 +4,10 @@ namespace App\Entity; use App\RecordInterface; use App\Repository\BibliographyRepository; +use App\Repository\CollectionRepository; +use App\Repository\CollectorRepository; use DateTimeImmutable; +use Doctrine\ORM\EntityManager; use Doctrine\ORM\Mapping as ORM; use Doctrine\DBAL\Types\Types; use App\RecordStatus; diff --git a/templates/bibliography/index.html.twig b/templates/bibliography/index.html.twig index 403df21..51e00cd 100644 --- a/templates/bibliography/index.html.twig +++ b/templates/bibliography/index.html.twig @@ -28,12 +28,13 @@ - +