src/Controller/WeatherController.php line 34

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Weather;
  4. use App\Form\WeatherType;
  5. use App\Repository\WeatherRepository;
  6. use App\Services\WeatherService;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route("/weather")
  14.  */
  15. class WeatherController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/index", name="weather_index", methods={"GET"})
  19.      */
  20.     public function index(Request $requestWeatherRepository $weatherRepository): Response
  21.     {
  22.         $today = new \DateTime('today');
  23.         $tomorrow = (clone $today)->modify('+1 day');
  24.         $dateParam $request->query->get('date');
  25.         $selectedDate null;
  26.         if ($dateParam) {
  27.             try {
  28.                 $selectedDate = new \DateTime($dateParam);
  29.             } catch (\Exception $e) {
  30.                 $selectedDate null// invalid date falls back
  31.             }
  32.         }
  33.         $weather $weatherRepository->findFutureWeather($today$selectedDate);
  34.         $dates = [];
  35.         for ($i 0$i 4$i++) {
  36.             $current = (clone $today)->modify("+{$i} days");
  37.             $label $current->format('D, j M');
  38.             if ($i === 0) {
  39.                 $label .= ' (Today)';
  40.             } elseif ($i === 1) {
  41.                 $label .= ' (Tmrw)';
  42.             }
  43.             $dates[] = [
  44.                 'date'  => $current,
  45.                 'label' => $label
  46.             ];
  47.         }
  48.         return $this->render('weather/index.html.twig', [
  49.             'weather'       => $weather,
  50.             'dates'         => $dates,
  51.             'selectedDate'  => $selectedDate $selectedDate->format('Y-m-d') : null,
  52.             'today'         => $today,
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/new", name="weather_new", methods={"GET","POST"})
  57.      */
  58.     public function new(Request $requestEntityManagerInterface $entityManager): Response
  59.     {
  60.         $weather = new Weather();
  61.         $form $this->createForm(WeatherType::class, $weather);
  62.         $form->handleRequest($request);
  63.         if ($form->isSubmitted() && $form->isValid()) {
  64.             $entityManager->persist($weather);
  65.             $entityManager->flush();
  66.             return $this->redirectToRoute('weather_index');
  67.         }
  68.         return $this->render('weather/new.html.twig', [
  69.             'weather' => $weather,
  70.             'form' => $form->createView(),
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/show/{id}", name="weather_show", methods={"GET"})
  75.      */
  76.     public function show(Weather $weather): Response
  77.     {
  78.         return $this->render('weather/show.html.twig', [
  79.             'weather' => $weather,
  80.         ]);
  81.     }
  82.     /**
  83.      * @Route("/edit/{id}", name="weather_edit", methods={"GET","POST"})
  84.      */
  85.     public function edit(Request $requestWeather $weatherEntityManagerInterface $entityManager): Response
  86.     {
  87.         $form $this->createForm(WeatherType::class, $weather);
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             $entityManager->flush();
  91.             return $this->redirectToRoute('weather_index');
  92.         }
  93.         return $this->render('weather/edit.html.twig', [
  94.             'weather' => $weather,
  95.             'form' => $form->createView(),
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/delete/{id}", name="weather_delete", methods={"POST"})
  100.      */
  101.     public function delete(Request $requestWeather $weatherEntityManagerInterface $entityManager): Response
  102.     {
  103.         if ($this->isCsrfTokenValid('delete' $weather->getId(), $request->request->get('_token'))) {
  104.             $entityManager->remove($weather);
  105.             $entityManager->flush();
  106.         }
  107.         return $this->redirectToRoute('weather_index');
  108.     }
  109.     /**
  110.      * @Route("/weather/delete_all", name="weather_delete_all")
  111.      */
  112.     public function deleteAllWeatherData(WeatherRepository $weatherRepositoryEntityManagerInterface $entityManager)
  113.     {
  114.         $weathers $weatherRepository->findAll();
  115.         foreach ($weathers as $weather) {
  116.             $entityManager->remove($weather);
  117.             $entityManager->flush();
  118.         }
  119.         return $this->redirectToRoute('weather_index');
  120.     }
  121.     /**
  122.      * @Route("/weather/fetch_weather_data", name="fetch_weather_data", methods={"GET"})
  123.      */
  124.     public function weatherFetch(WeatherService $weatherServiceRequest $request)
  125.     {
  126.         $referer $request->headers->get('Referer');
  127.         $weatherService->update();
  128.         $weatherService->hourlyUpdate();
  129.         return $this->redirect($referer);
  130.     }
  131.     /**
  132.      * @Route("/weather/fetch_weather_data_hourly", name="fetch_weather_data_hourly", methods={"GET"})
  133.      */
  134.     public function weatherHourly(WeatherService $weatherServiceRequest $request)
  135.     {
  136.         $referer $request->headers->get('Referer');
  137.         $weatherService->hourlyUpdate();
  138.         return $this->redirect($referer);
  139.     }
  140. }