src/Controller/WeatherController.php line 34
<?phpnamespace App\Controller;use App\Entity\Weather;use App\Form\WeatherType;use App\Repository\WeatherRepository;use App\Services\WeatherService;use Doctrine\ORM\EntityManagerInterface;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;/*** @Route("/weather")*/class WeatherController extends AbstractController{/*** @Route("/index", name="weather_index", methods={"GET"})*/public function index(Request $request, WeatherRepository $weatherRepository): Response{$today = new \DateTime('today');$tomorrow = (clone $today)->modify('+1 day');$dateParam = $request->query->get('date');$selectedDate = null;if ($dateParam) {try {$selectedDate = new \DateTime($dateParam);} catch (\Exception $e) {$selectedDate = null; // invalid date falls back}}$weather = $weatherRepository->findFutureWeather($today, $selectedDate);$dates = [];for ($i = 0; $i < 4; $i++) {$current = (clone $today)->modify("+{$i} days");$label = $current->format('D, j M');if ($i === 0) {$label .= ' (Today)';} elseif ($i === 1) {$label .= ' (Tmrw)';}$dates[] = ['date' => $current,'label' => $label];}return $this->render('weather/index.html.twig', ['weather' => $weather,'dates' => $dates,'selectedDate' => $selectedDate ? $selectedDate->format('Y-m-d') : null,'today' => $today,]);}/*** @Route("/new", name="weather_new", methods={"GET","POST"})*/public function new(Request $request, EntityManagerInterface $entityManager): Response{$weather = new Weather();$form = $this->createForm(WeatherType::class, $weather);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$entityManager->persist($weather);$entityManager->flush();return $this->redirectToRoute('weather_index');}return $this->render('weather/new.html.twig', ['weather' => $weather,'form' => $form->createView(),]);}/*** @Route("/show/{id}", name="weather_show", methods={"GET"})*/public function show(Weather $weather): Response{return $this->render('weather/show.html.twig', ['weather' => $weather,]);}/*** @Route("/edit/{id}", name="weather_edit", methods={"GET","POST"})*/public function edit(Request $request, Weather $weather, EntityManagerInterface $entityManager): Response{$form = $this->createForm(WeatherType::class, $weather);$form->handleRequest($request);if ($form->isSubmitted() && $form->isValid()) {$entityManager->flush();return $this->redirectToRoute('weather_index');}return $this->render('weather/edit.html.twig', ['weather' => $weather,'form' => $form->createView(),]);}/*** @Route("/delete/{id}", name="weather_delete", methods={"POST"})*/public function delete(Request $request, Weather $weather, EntityManagerInterface $entityManager): Response{if ($this->isCsrfTokenValid('delete' . $weather->getId(), $request->request->get('_token'))) {$entityManager->remove($weather);$entityManager->flush();}return $this->redirectToRoute('weather_index');}/*** @Route("/weather/delete_all", name="weather_delete_all")*/public function deleteAllWeatherData(WeatherRepository $weatherRepository, EntityManagerInterface $entityManager){$weathers = $weatherRepository->findAll();foreach ($weathers as $weather) {$entityManager->remove($weather);$entityManager->flush();}return $this->redirectToRoute('weather_index');}/*** @Route("/weather/fetch_weather_data", name="fetch_weather_data", methods={"GET"})*/public function weatherFetch(WeatherService $weatherService, Request $request){$referer = $request->headers->get('Referer');$weatherService->update();$weatherService->hourlyUpdate();return $this->redirect($referer);}/*** @Route("/weather/fetch_weather_data_hourly", name="fetch_weather_data_hourly", methods={"GET"})*/public function weatherHourly(WeatherService $weatherService, Request $request){$referer = $request->headers->get('Referer');$weatherService->hourlyUpdate();return $this->redirect($referer);}}