src/Controller/DefaultController.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Vacation;
  4. use App\Entity\VacationDay;
  5. use App\Repository\ShortUrlRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class DefaultController extends MyController
  12. {
  13.     /**
  14.      * @Route("/u/{hash}", name="shorten_url_redirect_url", host="click.hypercrew.pl", schemes={"https"})
  15.      */
  16.     public function retrace(string $hashShortUrlRepository $repoEntityManagerInterface $em): Response
  17.     {
  18.         $shortUrl $repo->findByHash($hash);
  19.         if (!$shortUrl) {
  20.             return new Response(" ",404);
  21.         }
  22.         $shortUrl->setViews($shortUrl->getViews() + 1);
  23.         try {
  24.             $em->flush();
  25.         }catch (\Exception $e){}
  26.         return new RedirectResponse($shortUrl->getOriginalUrl());
  27.     }
  28.     /**
  29.      * @Route("/{slug}", name="shorten_url_fallback", requirements={"slug"=".*"}, host="click.hypercrew.pl")
  30.      */
  31.     public function fallback(string $slug ''): Response
  32.     {
  33.         return new Response(" ",404);
  34.     }
  35.     /**
  36.      * @Route("/", name="home")
  37.      */
  38.     public function index(EntityManagerInterface $entityManager)
  39.     {
  40.         $start \DateTime::createFromFormat("Y-m-d H:i:s"date("Y-m-d")." 00:00:00");
  41. //        $start = \DateTime::createFromFormat("Y-m-d H:i:s", "2025-01-11 00:00:00");
  42.         $howManyDays 7;
  43.         $w = (int)$start->format("w");
  44.         if( $w == $w = -1;
  45.         $howManyDays += $w;
  46.         $end = (\DateTime::createFromFormat("Y-m-d H:i:s"$start->format("Y-m-d 23:59:59")))->modify("+$howManyDays days");
  47.         $items $entityManager->getRepository(VacationDay::class)->findForRange($start$end);
  48.         $nextVacations = [];
  49.         foreach ($items as $item){
  50.             $author $item->getVacation()->getAuthor();
  51.             if( empty($nextVacations[$author->getId()])) {
  52.                 $nextVacations[$author->getId()] = ['author' => $author'days' => []];
  53.             }
  54.             $nextVacations[$author->getId()]['days'][$item->getDayDate()->format("Y-m-d")] = $item->getVacation();
  55.         }
  56.         $days = [$start->format('Y-m-d')];
  57.         for($i 1$i <= $howManyDays$i++){
  58.             $days[] = (new \DateTime($days[$i-1]))->modify('+1 day')->format('Y-m-d');
  59.         }
  60.         return $this->render('default/index.html.twig', [
  61.             'nextVacations' => $nextVacations,
  62.             'days' => $days,
  63.             'start' => $start,
  64.             'end' => $end,
  65.         ]);
  66.     }
  67.     public function notfound(){
  68.         return $this->render('default/notfound.html.twig', []);
  69.     }
  70. }