Copy collection; Stimulus controller for vocabs (incomplete)

This commit is contained in:
2024-11-08 17:32:45 +01:00
parent 239240360e
commit dce0e1b693
10 changed files with 197 additions and 139 deletions

View File

@@ -12,6 +12,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use DateTimeImmutable;
class BibliographyController extends AbstractController
{
@@ -92,7 +93,7 @@ class BibliographyController extends AbstractController
}
/**
* @todo Permissions! Return JSON with 403 when AJAX
* @todo Move clone logic to __clone() in Entity or Repository
*/
#[Route('/bibliography/copy/{id<\d+>}', name: 'app_bibliography_copy')]
public function copy(Bibliography $bibliography, EntityManagerInterface $em): Response
@@ -108,24 +109,26 @@ class BibliographyController extends AbstractController
$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->findAllByBibliography($bibliography->getId())
);
$repo = $em->getRepository(Collector::class);
$copy->setCollectors(
$repo->findAllByBibliography($bibliography->getId())
$repo->findAllByBibliography($bibliography->getId())
);
$copy->setCitation("{$bibliography->getCitation()} - Copy");
$copy->setModifiedAt(new DateTimeImmutable());
$em->persist($copy);
$em->flush();
$this->addFlash('notice', 'Record copied successfully');
return $this->redirectToRoute('app_bibliography', ['id' => $copy->getId()]);
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\Collection;
use App\Entity\Bibliography;
use App\Entity\Collector;
use App\Security\Voter\RecordVoter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -61,4 +62,45 @@ class CollectionController extends AbstractController
return $this->redirectToRoute('app_collection_landing');
}
/**
* @todo Move clone logic to __clone() in Entity or Repository
*/
#[Route('/collection/copy/{id<\d+>}', name: 'app_collection_copy')]
public function copy(Collection $collection, EntityManagerInterface $em): Response
{
try {
$this->denyAccessUnlessGranted(RecordVoter::EDIT, $collection);
}
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()}";
$copy = clone $collection;
$copy->setEditor($editor);
$copy->setOwner($editor);
$copy->setCreator($user->getUsername());
$repo = $em->getRepository(Bibliography::class);
$copy->setBibliographies(
$repo->findAllByCollection($collection->getId())
);
/*
$repo = $em->getRepository(Collector::class);
$copy->setCollectors(
$repo->findAllByBibliography($bibliography->getId())
);
*/
$copy->setTitle("{$collection->getTitle()} - Copy");
$copy->setModifiedAt(new \DateTimeImmutable());
$em->persist($copy);
$em->flush();
$this->addFlash('notice', 'Record copied successfully');
return $this->redirectToRoute('app_collection', ['id' => $copy->getId()]);
}
}