Copy record for bibliography

This commit is contained in:
Nicolò P. 2024-11-07 18:53:34 +01:00
parent 3c2b804498
commit 239240360e
3 changed files with 45 additions and 4 deletions

View File

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

View File

@ -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;

View File

@ -28,12 +28,13 @@
<i class="fa fa-edit"></i>
</span>
</button>
<button class="button is-link">
<a href="{{ path('app_bibliography_copy', {'id' : record.id}) }}"
class="button is-link">
Copy
<span class="icon ml-2">
<i class="fa fa-copy"></i>
</span>
</button>
</a>
<a href="{{ path('app_bibliography_del', {'id' : record.id}) }}"
class="button is-danger"
data-delete-record-target="path" data-action="click->delete-record#show">