diff --git a/src/Controller/MapApiController.php b/src/Controller/MapApiController.php index 1138552..67fe022 100644 --- a/src/Controller/MapApiController.php +++ b/src/Controller/MapApiController.php @@ -5,68 +5,100 @@ namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; + +use Doctrine\Persistence\ManagerRegistry; +use Doctrine\Common\Collections\ArrayCollection; + use App\Repository\SiteRepository; -use App\Repository\RoomRepository; +use App\Repository\FloorRepository; +use App\Repository\PlaceRepository; +use App\Repository\PlaceNameRepository; +use App\Repository\RoomUserNameRepository; +use App\Repository\EditionRepository; + +use App\Entity\PlaceName; +use App\Entity\RoomUserName; +use App\Entity\Edition; + + class MapApiController extends AbstractController { + //function get_Name($rep) {return $rep->getName();} + #[Route('/map/api/test')] - public function test(SiteRepository $rep): JsonResponse + public function test(PlaceNameRepository $pNRep, RoomUserNameRepository $uNRep, PlaceRepository $pRep, SiteRepository $sRep): JsonResponse { - + $place0 = $pRep->findPlaceById(1)[0]; return $this->json([ 'message' => 'Welcome to your new controller!', 'path' => 'src/Controller/MapApiController.php', - 'sites' => $rep->allSites()[0]->getZeroLatitude() + 'allUnames' => $uNRep->allNames(), + 'allPnames' => $pNRep->allNames(), + 'Place0Id' => $place0->getId(), + 'Place0UserNames0' => $place0->getRoomUsers()[0]->getUserName(), + 'Place0Names0' => $place0->getNames()[0]->getName(), + 'sites' => $sRep->allSites() ]); } - #[Route(' ')] - public function get_floor(FloorRepository $rep, int $id, string $representation): JsonResponse + #[Route('/map/api/get_floor/{id}/{representation}')] + public function get_floor(FloorRepository $rep, int $id, string $representation='Cylinder'): JsonResponse { - $floor = $rep->findFloorById($id); - $places = $floor->getPlace(); + //id is the floor's id, representation being Cylinder or PolySurface or other if implemented + //it defines which representation should we use to project it on 2 dimension + $floors = $rep->findFloorById($id); + if (sizeof($floors)!=1) { return $this->json(["Error : None or Too Many Floors have this id"]); } + $floor = $floors[0]; + $places = $floor->getPlaces(); $jsonPlaces = new ArrayCollection(); - foreach ($places as $place){ - $jsonPlaces[] = $this->json([ - 'id' => $place->getId(), - 'name' => $place->getName(), - 'type' => $place->getType(), - 'surface' => $place->getTwoDRepresentation($representation, $floor->getAltitude()) - ]); + if ($representation == 'Cylinder' || $representation == 'PolySurface'){ + foreach ($places as $place){ + $jsonPlaces[] = array( + 'id' => $place->getId(), + 'name' => $place->getJoinedNames(), + 'type' => $place->getType(), + 'surface' => $place->getTwoDRepresentation($representation, $floor->getAltitude()) + ); + } } - return $jsonPlaces; + else { + return $this->json(["Error on represantation attribute"]); + } + return $this->json($jsonPlaces->toArray()); } - #[Route('/map/api/find_place_by_name')] + #[Route('/map/api/find_place_by_name/{name}')] public function find_place_by_name(PlaceRepository $rep, string $name): JsonResponse { $places = $rep->findPlaceByName($name); - if (sizeof($places)!=1) { - return false; - } + if (sizeof($places)!=1) { return $this->json([0]); } else { $place = $places[0]; + $floorsId = array(); + foreach ($place->getFloors() as $floor) { + $floorsId[] = $floor->getId(); + } return $this->json([ 'idRoom' => $place->getId(), - 'idFloor' => $place->getFloor()->getId() + 'idFloors' => $floorsId ]); } } - #[Route('/map/api/show_place_info')] + #[Route('/map/api/show_place_info/{id}')] public function index(PlaceRepository $rep, int $id): JsonResponse { $places = $rep->findPlaceById($id); if (sizeof($places)!=1) { - return null; + return $this->json([0]); } else { return $this->json([ 'idRoom' => $places[0]->getId(), - 'names' => $places[0]->getName(), - 'users' => $places[0]->getUser() + 'names' => $places[0]->getJoinedNames(), + 'users' => $places[0]->getJoinedRoomUsersNames() ]); } } @@ -76,80 +108,95 @@ class MapApiController extends AbstractController { $edits = $rep->getAllEditions(); if (sizeof($edits)<1) { - return null; + return $this->json([0]); } else { - $listEdits = new ArrayCollection(); + $listEdits = array(); foreach ($edits as $edit){ - $listEdits[] = $this->json([ + $listEdits[] = array( 'id' => $edit->getId(), 'mode' => $edit->getMode(), - 'name' => $edit->getNamePlace(), - 'idPlace' => $edit->getIdPlace(), - 'value' => $edit->getValue() - ]); + 'placeNames' => $edit->getEditedPlace()->getJoinedNames(), + 'placeId' => $edit->getEditedPlace()->getId(), + 'editorValue' => $edit->getValue() + ); } - } - return $listEdits; + return $this->json($listEdits); } - #[Route('/map/api/create_edition')] - public function createEdition(EditionRepository $Erep, PlaceRepository $Prep, string $mode, int $idPlace, string $value): bool + #[Route('/map/api/create_edition/{mode}/{placeId}/{value}')] + public function createEdition(EditionRepository $Erep, PlaceRepository $Prep, string $mode, int $placeId, string $value): JsonResponse { + //mode is the type of modification, can be AddPlaceName ; DelPlaceName ; AddRoomUserName or DelRoomUserName $edit = new Edition(); - $edit->createEdition($Prep, $mode, $idPlace, $value); - return $Erep->add($edit); + if ($edit->initEdition($Prep, $mode, $placeId, $value) == null) {return $this->json(["Error during initEdition : None or Too Many Places found with id"]);} + $Erep->add($edit, true); + return $this->json(["added"]); } - #[Route('/map/api/remove_edition')] - public function removeEdition(EditionRepository $rep, int $id): bool + #[Route('/map/api/remove_edition/{id}')] + public function removeEdition(EditionRepository $rep, int $id): JsonResponse { $edit = $rep->findEditionById($id); - if ($edit == null) {return false;} - $rep->remove($edit); - return true; + if (sizeof($edit) == 0) {return $this->json([0]);} + $rep->remove($edit[0], true); + return $this->json(["removed"]); + } + + #[Route('/map/api/add_place_name/{placeId}/{value}')] + public function addPlaceName(PlaceRepository $pRep, PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse + { + $places = $pRep->findPlaceById($placeId); + if (sizeof($places) != 1) { return $this->json([0]); } + else{ + $names = new ArrayCollection(); + foreach($places[0]->getNames() as $plName) { $names[] = $plName->getName(); } + if ($names->contains($value)) { return $this->json([2]); } + else { + $pN = new PlaceName(); + $pN->setPlace($places[0]); + $pN->setName($value); + $pNRep->add($pN, true); + return $this->json(["added"]); + } + } + } + #[Route('/map/api/del_place_name/{placeId}/{value}')] + public function delPlaceName(PlaceNameRepository $pNRep, int $placeId, string $value): JsonResponse + { + $pN = $pNRep->findPlaceName($placeId, $value); + if (sizeof($pN)==0) { return $this->json([0]); } + $pNRep->remove($pN[0], true); + return $this->json(["removed"]); } - #[Route('/map/api/del_place_name')] - public function delPlaceName(PlaceRepository $rep, int $id, string $value): bool + + #[Route('/map/api/add_room_user_name/{placeId}/{value}')] + public function addRoomUserName(PlaceRepository $pRep, RoomUserNameRepository $rURep, ManagerRegistry $mr, int $placeId, string $value): JsonResponse { - $places = $rep->findPlaceById($id); - if (sizeof($places)!=1) { return null; } - else { - $places[0]->removeName($value); - return true; + $places = $pRep->findPlaceById($placeId); + if (sizeof($places) != 1) { return $this->json([0]); } + else{ + $names = new ArrayCollection(); + foreach($places[0]->getRoomUsers() as $rUser) { $names[] = $rUser->getUserName(); } + if ($names->contains($value)) { return $this->json([2]); } + else { + $rU = new RoomUserName(); + $rU->setPlace($places[0]); + $rU->setUserName($value); + $rURep->add($rU, true); + return $this->json(["added"]); + } } } - #[Route('/map/api/add_place_name')] - public function addPlaceName(PlaceRepository $rep, int $id, string $value): bool + #[Route('/map/api/del_room_user_name/{placeId}/{value}')] + public function delRoomUserName(RoomUserNameRepository $uRep, int $placeId, string $value): JsonResponse { - $places = $rep->findPlaceById($id); - if (sizeof($places)!=1) { return null; } - else { - $places[0]->addName($value); - return true; - } - } - #[Route('/map/api/del_room_user_name')] - public function delRoomUserName(PlaceRepository $rep, int $id, string $value): bool - { - $rooms = $rep->findPlaceById($id); - if (sizeof($rooms)!=1) { return null; } - else { - $rooms[0]->removeRoomUser($value); - return true; - } - } - #[Route('/map/api/add_room_user_name')] - public function addRoomUserName(PlaceRepository $rep, int $id, string $value): bool - { - $rooms = $rep->findPlaceById($id); - if (sizeof($rooms)!=1) { return null; } - else { - $rooms[0]->addRoomUser($value); - return true; - } + $rU = $uRep->findRoomUserName($placeId, $value); + if (sizeof($rU)==0) { return $this->json([0]); } + $uRep->remove($rU[0], true); + return $this->json(["removed"]); } } diff --git a/src/Entity/Building.php b/src/Entity/Building.php index 77ccd2a..942ef29 100644 --- a/src/Entity/Building.php +++ b/src/Entity/Building.php @@ -15,6 +15,9 @@ class Building #[ORM\Column(type: "integer")] private $id; + #[ORM\Column(type: "string")] + private $name; + #[ORM\ManyToOne(targetEntity: Site::class, inversedBy: "buildings")] #[ORM\JoinColumn(nullable: false)] private $site; diff --git a/src/Entity/Edition.php b/src/Entity/Edition.php index 1c5e30a..4e5e440 100644 --- a/src/Entity/Edition.php +++ b/src/Entity/Edition.php @@ -20,7 +20,7 @@ class Edition #[ORM\Column(type: "string")] private $mode; - #[ORM\OneToMany(targetEntity: Place::class, mappedBy: "editions")] + #[ORM\ManyToOne(targetEntity: Place::class, inversedBy: "editions")] private $editedPlace; #[ORM\Column(type: "string")] @@ -43,11 +43,11 @@ class Edition return $this; } - public function getIdPlace(): ?int - { return $this->idPlace; } - public function setIdPlace(int $id): self + public function getEditedPlace(): Place + { return $this->editedPlace; } + public function setEditedPlace(Place $p): self { - $this->idPlace = $id; + $this->editedPlace = $p; return $this; } @@ -68,18 +68,19 @@ class Edition } - public function createEdition(PlaceRepository $rep, string $mode, int $id, string $val): self + public function initEdition(PlaceRepository $rep, string $mode, int $id, string $val): self { - $place = $rep->findPlaceById($id); - if ($place==null) { return null; } - $names = $place->getName(); - $name = ''; - foreach ($names as $n) { - $name = $name + $n + ", " ; - } + $places = $rep->findPlaceById($id); + if (sizeof($places)!=1) { return null; } + $place=$places[0]; + // $names = $place->getJoinedNames(); + // $name = ''; + // foreach ($names as $n) { + // $name = $name . $n . ", " ; + // } $this->mode = $mode; - $this->idPlace = $id; - $this->namePlace = $name; + $this->editedPlace = $place; + // $this->namePlace = $name; $this->val = $val; return $this; } diff --git a/src/Entity/Place.php b/src/Entity/Place.php index a9b59e1..43ae3f1 100644 --- a/src/Entity/Place.php +++ b/src/Entity/Place.php @@ -45,7 +45,7 @@ class Place #[ORM\OneToMany(targetEntity: PolySurface::class, mappedBy: "place")] private $PolySurfaceRepresentation; - #[ORM\ManyToOne(targetEntity: Edition::class, inversedBy: "editedPlace")] + #[ORM\OneToMany(targetEntity: Edition::class, mappedBy: "editedPlace")] private $editions; public function __construct() @@ -86,7 +86,16 @@ class Place return $this->names; } - public function addName(self $name): self + public function getJoinedNames(): array + { + $names = new ArrayCollection(); + foreach ($this->names as $plName) { + $names[] = $plName->getName(); + } + return $names->toArray(); + } + + public function addName(PlaceName $name): self { if (!$this->names->contains($name)) { $this->names[] = $name; @@ -94,11 +103,28 @@ class Place return $this; } - public function removeName(self $name): self + public function removeName(PlaceName $name): self { $this->names->removeElement($name); return $this; } + + /** + * @return Collection + */ + public function getEditions(): Collection + { return $this->editions; + } + public function addEdition(Edition $edition): self + { if (!$this->editions->contains($edition)) { + $this->editions[] = $edition; + } + return $this; + } + public function removeEdition(Edition $edition): self + { $this->editions->removeElement($edition); + return $this; + } /** * @return Collection @@ -108,8 +134,16 @@ class Place { return $this->users; } + public function getJoinedRoomUsersNames(): array + { + $names = new ArrayCollection(); + foreach ($this->users as $roomUserName) { + $names[] = $roomUserName->getUserName(); + } + return $names->toArray(); + } - public function addRoomUser(self $user): self + public function addRoomUser(RoomUserName $user): self { if (!$this->users->contains($user)) { $this->users[] = $user; @@ -117,7 +151,7 @@ class Place return $this; } - public function removeRoomUser(self $user): self + public function removeRoomUser(RoomUserName $user): self { $this->users->removeElement($user); return $this; @@ -317,26 +351,26 @@ class Place public function getTwoDRepresentation(string $representation, int $alt) : array { if ($representation=='Cylinder') { - $rep = $this->getCylinderRepresentation(); + $rep = $this->getCylinderRepresentation()[0]; //ARBITRARY if ($rep != null){ $repPoints = $rep->getCylinderbase()->getPolygonpoint(); //Points in repository format $Points = new ArrayCollection(); foreach ($repPoints as $p) { - $xy=array([$p->getX(), $p->getY()]); + $xy=array('x'=>$p->getX(), 'y'=>$p->getY()); if (!$Points->contains($xy)){ $Points[] = $xy; } } - return $Points; + return $Points->toArray(); } } elseif ($representation=='PolySurface') { - $rep = $this->getPolySurfaceRepresentation(); + $rep = $this->getPolySurfaceRepresentation()[0]; //ARBITRARY if ($rep != null){ $repPoints = $rep->getPolysurfaceComponent()->getPolygonpoint(); //Points in repository format $Points = new ArrayCollection(); foreach ($repPoints as $p) { - $xy=array([$p->getX(), $p->getY()]); + $xy=array('x'=>$p->getX(), 'y'=>$p->getY()); if (!$Points->contains($xy)){ $Points[] = $xy; } diff --git a/src/Entity/Site.php b/src/Entity/Site.php index f016b9d..38b5627 100644 --- a/src/Entity/Site.php +++ b/src/Entity/Site.php @@ -15,6 +15,9 @@ class Site #[ORM\Column(type: "integer")] private $id; +// #[ORM\Column(type: "string")] +// private $name; + #[ORM\Column(type: "float")] private $zeroLatitude; diff --git a/src/Repository/FloorRepository.php b/src/Repository/FloorRepository.php index 5b00c40..b23f5c5 100644 --- a/src/Repository/FloorRepository.php +++ b/src/Repository/FloorRepository.php @@ -39,9 +39,19 @@ class FloorRepository extends ServiceEntityRepository } } -// /** -// * @return Floor[] Returns an array of Floor objects -// */ + /** + * @return Floor[] Returns an array of Floor objects + */ + public function findFloorById(int $id): array + { + return $this->createQueryBuilder('f') + ->andWhere('f.id = :val') + ->setParameter('val', $id) + ->orderBy('f.id', 'ASC') + ->getQuery() + ->getResult() + ; + } // public function findByExampleField($value): array // { // return $this->createQueryBuilder('f') diff --git a/src/Repository/PlaceNameRepository.php b/src/Repository/PlaceNameRepository.php index a9393c2..1a5ac73 100644 --- a/src/Repository/PlaceNameRepository.php +++ b/src/Repository/PlaceNameRepository.php @@ -36,14 +36,27 @@ class PlaceNameRepository extends ServiceEntityRepository /** * @return PlaceName[] Returns an array of PlaceName objects */ - public function findPlaceNameById(int $id): array - { - return $this->createQueryBuilder('p') - ->andWhere('p.id = :val') - ->setParameter('val', $id) - ->orderBy('p.id', 'ASC') - ->getQuery() - ->getResult() - ; - } + public function findPlaceNameById(int $id): array + { + return $this->createQueryBuilder('p') + ->andWhere('p.id = :val') + ->setParameter('val', $id) + ->orderBy('p.id', 'ASC') + ->getQuery() + ->getResult() + ; + } + public function allNames(): array + { return $this->createQueryBuilder('pn')->select('pn.name')->orderBy('pn.id','ASC')->getQuery()->getResult(); } + + public function findPlaceName(int $id, string $value): array + { + return $this->createQueryBuilder('pn') + ->where('pn.place = :id') + ->andWhere('pn.name = :val') + ->setParameter('id', $id) + ->setParameter('val', $value) + ->orderBy('pn.id','ASC') + ->getQuery()->getResult(); + } } diff --git a/src/Repository/PlaceRepository.php b/src/Repository/PlaceRepository.php index ed2e3a3..c553e7c 100644 --- a/src/Repository/PlaceRepository.php +++ b/src/Repository/PlaceRepository.php @@ -3,6 +3,7 @@ namespace App\Repository; use App\Entity\Place; +use App\Entity\PlaceName; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -45,7 +46,8 @@ class PlaceRepository extends ServiceEntityRepository public function findPlaceByName(string $name): array { return $this->createQueryBuilder('p') - ->andWhere('LOWER(p.name) = :val') + ->join('App\Entity\PlaceName', 'n', 'WITH', 'p.id = n.place') + ->andWhere('LOWER(n.name) = :val') ->setParameter('val', strtolower($name)) ->orderBy('p.id', 'ASC') ->getQuery() @@ -66,6 +68,10 @@ class PlaceRepository extends ServiceEntityRepository ; } + public function allPlaces(): array + { + return $this->createQueryBuilder('s')->orderBy('s.id','ASC')->getQuery()->getResult(); + } // public function findOneBySomeField($value): ?Place diff --git a/src/Repository/RoomUserNameRepository.php b/src/Repository/RoomUserNameRepository.php index 8ddeec7..d712ab4 100644 --- a/src/Repository/RoomUserNameRepository.php +++ b/src/Repository/RoomUserNameRepository.php @@ -46,4 +46,18 @@ class RoomUserNameRepository extends ServiceEntityRepository ->getResult() ; } + + public function allNames(): array + { return $this->createQueryBuilder('ru')->select('ru.userName')->orderBy('ru.id','ASC')->getQuery()->getResult(); } + + public function findRoomUserName(int $placeId, string $value): array + { + return $this->createQueryBuilder('u') + ->where('u.place = :id') + ->andWhere('u.userName = :val') + ->setParameter('id', $placeId) + ->setParameter('val', $value) + ->orderBy('u.id','ASC') + ->getQuery()->getResult(); + } }