Add mapping for community resources

This commit is contained in:
Ryan Prather 2024-12-22 22:57:32 +00:00
parent 7ae335a716
commit c5b8148f00
3 changed files with 133 additions and 5 deletions

View File

@ -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()}<br>{$rsc->getAddress()}, {$rsc->getCity()}, {$rsc->getState()->value} {$rsc->getZip()}<br>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);
}
}

View File

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

27
src/Libs/Libs.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Libs;
class Libs
{
public static function getLatLonFromGeoapify($address): ?array
{
$address = urlencode($address);
$url = "https://api.geoapify.com/v1/geocode/search?text={$address}&format=json&apiKey={$_ENV['GEOAPIFY_API_KEY']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
if (isset($result['results'][0]['lat']) && isset($result['results'][0]['lon'])) {
$lat = $result['results'][0]['lat'];
$lon = $result['results'][0]['lon'];
return [$lat, $lon];
}
return null;
}
}