From c5b8148f007aa4f90a083aab8062ae8250932fa4 Mon Sep 17 00:00:00 2001 From: Ryan Prather Date: Sun, 22 Dec 2024 22:57:32 +0000 Subject: [PATCH] Add mapping for community resources --- .../CommunityResourceController.php | 79 ++++++++++++++++++- src/Entity/CommunityResource.php | 32 +++++++- src/Libs/Libs.php | 27 +++++++ 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 src/Libs/Libs.php diff --git a/src/Controller/CommunityResourceController.php b/src/Controller/CommunityResourceController.php index 65cb444..47b29e8 100644 --- a/src/Controller/CommunityResourceController.php +++ b/src/Controller/CommunityResourceController.php @@ -7,6 +7,7 @@ use App\Entity\Messages; use App\Entity\User; use App\Form\ResourceFormType; use App\Libs\Breadcrumb; +use App\Libs\Libs; use App\Libs\NavList; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -14,7 +15,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Security\Http\Attribute\CurrentUser; -use Symfony\Component\Validator\Constraints\Regex; +use Symfony\UX\Map\InfoWindow; +use Symfony\UX\Map\Map; +use Symfony\UX\Map\Marker; +use Symfony\UX\Map\Point; class CommunityResourceController extends AbstractController { @@ -59,10 +63,42 @@ class CommunityResourceController extends AbstractController } #[Route('/resource/map', name: 'app_community_resource_map')] - public function map(): Response + public function map(#[CurrentUser()] User $user): Response { - return $this->render('internal/community_resource/map.html.twig', [ - ]); + $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($user); + $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($user); + + $rcs = $this->entityManager->getRepository(CommunityResource::class)->findAll(); + + $map = new Map('default'); + $map->center(new Point(39.768502, -86.157918)) + ->zoom(9) + ; + + foreach ($rcs as $rsc) { + $map->addMarker(new Marker( + position: new Point($rsc->getLat(), $rsc->getLon()), + title: $rsc->getName(), + infoWindow: new InfoWindow( + content: "{$rsc->getName()}
{$rsc->getAddress()}, {$rsc->getCity()}, {$rsc->getState()->value} {$rsc->getZip()}
Services: " . $rsc->getServicesAvailable() + ) + )); + } + + return $this->render( + 'internal/community_resource/map.html.twig', + array_merge( + $this->navLinks, + [ + 'map' => $map, + 'breadcrumbs' => [ + new Breadcrumb('#', 'Community Resources') + ], + 'notifications' => $this->msgs, + 'notificationCount' => $this->notificationCount, + ] + ) + ); } #[Route('/resource/add', name: 'app_community_resource_add')] @@ -75,7 +111,21 @@ class CommunityResourceController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + /** @var CommunityResource $rsc */ $rsc = $form->getData(); + + $res = Libs::getLatLonFromGeoapify("{$rsc->getAddress()}, {$rsc->getCity()}, {$rsc->getState()->value} {$rsc->getZip()} US"); + + if (!$res) { + $this->addFlash('warning', 'Could not retrieve latitude and longitude. Please try again.'); + return $this->redirectToRoute('app_community_resource_add'); + } + + list($lat, $lon) = $res; + + $rsc->setLat($lat); + $rsc->setLon($lon); + $this->entityManager->persist($rsc); $this->entityManager->flush(); @@ -114,6 +164,18 @@ class CommunityResourceController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + $res = Libs::getLatLonFromGeoapify("{$rsc->getAddress()}, {$rsc->getCity()}, {$rsc->getState()->value} {$rsc->getZip()} US"); + + if (!$res) { + $this->addFlash('warning', 'Could not retrieve latitude and longitude. Please try again.'); + return $this->redirectToRoute('app_community_resource_edit', ['id' => $id]); + } + + list($lat, $lon) = $res; + + $rsc->setLat($lat); + $rsc->setLon($lon); + $this->entityManager->flush(); $this->addFlash('success', 'Resource updated successfully'); @@ -160,4 +222,13 @@ class CommunityResourceController extends AbstractController 'Content-Transfer-Encoding' => 'binary' ]); } + + #[Route('/api/filter-resource-by-county', name: 'app_api_filter_resource_by_county')] + public function filterResourceByCounty(Request $request): Response + { + $data = json_decode($request->getContent(), true); + $county = $data['county']; + $resources = $this->entityManager->getRepository(CommunityResource::class)->findBy(['county' => $county]); + return $this->json($resources); + } } diff --git a/src/Entity/CommunityResource.php b/src/Entity/CommunityResource.php index e73cbb0..d854309 100644 --- a/src/Entity/CommunityResource.php +++ b/src/Entity/CommunityResource.php @@ -100,9 +100,15 @@ class CommunityResource #[ORM\Column(length: 255, nullable: true)] private ?string $servicesAvailable = null; - #[ORM\Column(type: Types::SIMPLE_ARRAY, enumType: ResourceType::class)] + #[ORM\Column(type: Types::JSON, enumType: ResourceType::class)] private array $type = []; + #[ORM\Column(nullable: true)] + private ?float $lat = null; + + #[ORM\Column(nullable: true)] + private ?float $lon = null; + public function __construct( private DateTime $today = new DateTime('now', new DateTimeZone('America/New_York')) ) { @@ -641,4 +647,28 @@ class CommunityResource return $this; } + + public function getLat(): ?float + { + return $this->lat; + } + + public function setLat(?float $lat): static + { + $this->lat = $lat; + + return $this; + } + + public function getLon(): ?float + { + return $this->lon; + } + + public function setLon(?float $lon): static + { + $this->lon = $lon; + + return $this; + } } diff --git a/src/Libs/Libs.php b/src/Libs/Libs.php new file mode 100644 index 0000000..a19f6d3 --- /dev/null +++ b/src/Libs/Libs.php @@ -0,0 +1,27 @@ +