From 5fa894e56aa93a079ad07a728935a00969dcda90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Fri, 15 Nov 2024 16:32:01 +0100 Subject: [PATCH] Add Site entity (draft) --- src/Entity/Site.php | 323 ++++++++++++++++++++++++++++++ src/Repository/SiteRepository.php | 96 +++++++++ 2 files changed, 419 insertions(+) create mode 100644 src/Entity/Site.php create mode 100644 src/Repository/SiteRepository.php diff --git a/src/Entity/Site.php b/src/Entity/Site.php new file mode 100644 index 0000000..854301b --- /dev/null +++ b/src/Entity/Site.php @@ -0,0 +1,323 @@ +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 getName(): ?string + { + return $this->name; + } + + public function setName(string $name): static + { + $this->name = $name; + + return $this; + } + + public function getCountry(): ?string + { + return $this->country; + } + + public function setCountry(string $country): static + { + $this->country = $country; + + return $this; + } + + public function getAncientName(): ?string + { + return $this->ancientName; + } + + public function setAncientName(string $ancientName): static + { + $this->ancientName = $ancientName; + + 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 getUri(): ?string + { + return $this->uri; + } + + public function setUri(string $uri): static + { + $this->uri = $uri; + + return $this; + } + + public function getNotes(): ?string + { + return $this->notes; + } + + public function setNotes(string $notes): static + { + $this->notes = $notes; + + return $this; + } + + public function getCollections(): ?DoctrineCollection + { + return $this->collections; + } + + public function setCollections(DoctrineCollection $collections): static + { + $this->collections = $collections; + + return $this; + } + + public function getEditableStatus(): bool + { + return $this->isEditable; + } + + public function setEditableStatus(bool $status): static + { + $this->isEditable = $status; + + return $this; + } + + public function getLat(): float + { + return $this->lat; + } + + public function setLat(float $lat): static + { + $this->lat = $lat; + + return $this; + } + + public function getLng(): float + { + return $this->lng; + } + + public function setLng(float $lng): static + { + $this->lng = $lng; + + return $this; + } +} diff --git a/src/Repository/SiteRepository.php b/src/Repository/SiteRepository.php new file mode 100644 index 0000000..b39b25a --- /dev/null +++ b/src/Repository/SiteRepository.php @@ -0,0 +1,96 @@ + + */ +class SiteRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Site::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 findAllByCollection(int $collectionId): ?ArrayCollection + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\ConservationPlace', 'c'); + + $query = $this->getEntityManager()->createNativeQuery( + "SELECT + id, + stato, + editor, + luogo_cons, + com_cons + FROM sito s + JOIN rel_sito_collezione + ON Sito_id_sito = id + WHERE Collezione_id_coll = :collId", + $rsm + ); + $query->setParameter('collId', $collectionId); + + return new ArrayCollection($query->getResult()); + } + + public function findAllByObject(int $objectId): ?ArrayCollection + { + $rsm = new ResultSetMappingBuilder($this->getEntityManager()); + $rsm->addRootEntityFromClassMetadata('App\Entity\ConservationPlace', 'c'); + + $query = $this->getEntityManager()->createNativeQuery( + "SELECT + id, + stato, + editor, + luogo_cons, + com_cons + FROM conservation_place c + JOIN rel_conservazione_reperto + ON Conservazione_id_cons = id + WHERE Reperto_id_rep = :objectId", + $rsm + ); + $query->setParameter('objectId', $objectId); + + return new ArrayCollection($query->getResult()); + } + + public function coordinates(int $id): ?array + { + $conn = $this->getEntityManager()->getConnection(); + + $sql = ' + SELECT + ST_X(coord_cons) as lng, + ST_Y(coord_cons) as lat + FROM sito s + WHERE cp.id = :id + '; + + $result = $conn->executeQuery($sql, ['id' => $id]); + + + return $result->fetchAssociative(); + } +}