diff --git a/src/Controller/DocumentController.php b/src/Controller/DocumentController.php new file mode 100644 index 0000000..f0846b5 --- /dev/null +++ b/src/Controller/DocumentController.php @@ -0,0 +1,115 @@ +getRepository(Document::class); + $records = $repo->findBy([], ['modifiedAt' => 'DESC']); + $count = count($records); + + $records = array_slice($records, 0, 15); + + return $this->render('document/index.html.twig', [ + 'controller_name' => 'DocumentController', + 'records' => $records, + 'count' => $count, + ]); + } + + #[Route('/document/{id<\d+>}', name: 'app_document_view')] + public function view(document $document, EntityManagerInterface $em): Response + { + $repo = $em->getRepository(Bibliography::class); + /* + $bibliographies = $repo->findAllByDocument($document->getId()); + $document->setBibliographies($bibliographies); + */ + + $repo = $em->getRepository(document::class); + $isEditable = $repo->hasCreatorEditor($document->getCreator()); + $document->setEditableStatus($isEditable); + + return $this->render('document/view.html.twig', [ + 'controller_name' => 'documentController', + 'record' => $document, + ]); + } + + #[Route('/document/delete/{id<\d+>}', name: 'app_document_del')] + public function delete(Document $document, EntityManagerInterface $em): Response + { + try { + $this->denyAccessUnlessGranted(RecordVoter::DELETE, $document); + } + catch (AccessDeniedException) { + $this->addFlash('warning', 'You are not authorized to delete this record'); + return $this->redirectToRoute('app_home'); + } + + if ($document) { + $em->remove($document); + $em->flush(); + } + + $this->addFlash('notice', 'Record deleted successfully'); + + return $this->redirectToRoute('app_document'); + } + /** + * @todo Move clone logic to __clone() in Entity or Repository + */ + #[Route('/document/copy/{id<\d+>}', name: 'app_document_copy')] + public function copy(Document $document, EntityManagerInterface $em): Response + { + try { + $this->denyAccessUnlessGranted(RecordVoter::EDIT, $document); + } + 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 $document; + $copy->setEditor($editor); + $copy->setOwner($editor); + $copy->setCreator($user->getUsername()); + $repo = $em->getRepository(Bibliography::class); + $copy->setBibliographies( + $repo->findAllBydocument($document->getId()) + ); + /* + $repo = $em->getRepository(Collector::class); + $copy->setCollectors( + $repo->findAllByBibliography($bibliography->getId()) + ); + */ + $copy->setTitle("{$document->getTitle()} - Copy"); + $copy->setModifiedAt(new \DateTimeImmutable()); + $copy->setStatus(RecordStatus::Draft->value); + + $em->persist($copy); + $em->flush(); + + $this->addFlash('notice', 'Record copied successfully'); + + return $this->redirectToRoute('app_document_view', ['id' => $copy->getId()]); + } +} diff --git a/src/Entity/Document.php b/src/Entity/Document.php new file mode 100644 index 0000000..047b070 --- /dev/null +++ b/src/Entity/Document.php @@ -0,0 +1,339 @@ +id; + } + + public function getStatus(): ?string + { + $status = RecordStatus::tryFrom($this->status); + return $status->toString(); + } + + public function setStatus(int $status): static + { + $this->status = $status; + + return $this; + } + + public function getModifiedAt(): ?DateTimeImmutable + { + return $this->modifiedAt; + } + + public function setModifiedAt(DateTimeImmutable $modifiedAt): static + { + $this->modifiedAt = $modifiedAt; + + return $this; + } + + public function getOwner(): ?string + { + return $this->owner; + } + + public function setOwner(string $owner): static + { + $this->owner = $owner; + + return $this; + } + + public function getEditor(): ?string + { + return $this->editor; + } + + public function setEditor(string $editor): static + { + $this->editor = $editor; + + return $this; + } + + public function getCreator(): ?string + { + return $this->creator; + } + + public function setCreator(string $creator): static + { + $this->creator = $creator; + + return $this; + } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): static + { + $this->title = $title; + + return $this; + } + + public function getAuthor(): ?string + { + return $this->author; + } + + public function setAuthor(string $author): static + { + $this->author = $author; + + return $this; + } + + public function getChronology(): ?string + { + return $this->chronology; + } + + public function setChronology(string $chronology): static + { + $this->chronology = $chronology; + + return $this; + } + + public function getInventory(): ?string + { + return $this->inventory; + } + + public function setInventory(string $inventory): static + { + $this->inventory = $inventory; + + return $this; + } + + public function getStartDate(): ?string + { + return $this->startDate; + } + + public function setStartDate(string $startDate): static + { + $this->startDate = $startDate; + + return $this; + } + + public function getEndDate(): ?string + { + return $this->endDate; + } + + public function setEndDate(string $endDate): static + { + $this->endDate = $endDate; + + return $this; + } + + public function getUnpublished(): ?bool + { + return $this->unpublished; + } + + public function setUnpublished(bool $unpublished): static + { + $this->unpublished = $unpublished; + + return $this; + } + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(string $description): static + { + $this->description = $description; + + return $this; + } + + public function getShortDescription(): ?string + { + return $this->shortDescription; + } + + public function setShortDescription(string $shortDescription): static + { + $this->shortDescription = $shortDescription; + + return $this; + } + + public function getExternalIdentifier(): ?string + { + return $this->externalIdentifier; + } + + public function setExternalIdentifier(string $externalIdentifier): static + { + $this->externalIdentifier = $externalIdentifier; + + return $this; + } + + public function getLink(): ?string + { + return $this->link; + } + + public function setLink(string $link): static + { + $this->link = $link; + + return $this; + } + + public function getSubjectHeadings(): ?string + { + return $this->subjectHeadings; + } + + public function setSubjectHeadings(string $subjectHeadings): static + { + $this->subjectHeadings = $subjectHeadings; + + return $this; + } + + public function getNotes(): ?string + { + return $this->notes; + } + + public function setNotes(string $notes): static + { + $this->notes = $notes; + + return $this; + } + + public function getBibliographies(): ?DoctrineCollection + { + return $this->bibliographies; + } + + public function setBibliographies(DoctrineCollection $bibliographies): static + { + $this->bibliographies = $bibliographies; + + return $this; + } + + public function getEditableStatus(): bool + { + return $this->isEditable; + } + + public function setEditableStatus(bool $status): static + { + $this->isEditable = $status; + + return $this; + } +} + diff --git a/src/Repository/DocumentRepository.php b/src/Repository/DocumentRepository.php new file mode 100644 index 0000000..e8a0bd8 --- /dev/null +++ b/src/Repository/DocumentRepository.php @@ -0,0 +1,55 @@ + + */ +class DocumentRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Document::class); + } + + public function hasCreatorEditor(string $creator): bool + { + $em = $this->getEntityManager(); + $repo = $em->getRepository(User::class); + + $creator = $repo->findOneBy(['username' => $creator]); + + return in_array('ROLE_EDITOR', $creator->getRoles()); + } + + public function findAllByBibliography(int $biblioId): ?ArrayCollection + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\Document', 'd'); + + $query = $this->getEntityManager()->createNativeQuery( + "SELECT + id, + stato, + editor, + tit_doc, + aut_doc + FROM document d + JOIN rel_riferimento_documento + ON Documento_id_doc = id + WHERE Bibliografia_id_bib = :biblioId", + $rsm + ); + $query->setParameter('biblioId', $biblioId); + + return new ArrayCollection($query->getResult()); + } +} + diff --git a/templates/conservation_place/view.html.twig b/templates/conservation_place/view.html.twig index ce8e830..505e1df 100644 --- a/templates/conservation_place/view.html.twig +++ b/templates/conservation_place/view.html.twig @@ -94,7 +94,8 @@