<?php
namespace App\Controller;
use App\Entity\Vacation;
use App\Entity\VacationDay;
use App\Repository\ShortUrlRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends MyController
{
/**
* @Route("/u/{hash}", name="shorten_url_redirect_url", host="click.hypercrew.pl", schemes={"https"})
*/
public function retrace(string $hash, ShortUrlRepository $repo, EntityManagerInterface $em): Response
{
$shortUrl = $repo->findByHash($hash);
if (!$shortUrl) {
return new Response(" ",404);
}
$shortUrl->setViews($shortUrl->getViews() + 1);
try {
$em->flush();
}catch (\Exception $e){}
return new RedirectResponse($shortUrl->getOriginalUrl());
}
/**
* @Route("/{slug}", name="shorten_url_fallback", requirements={"slug"=".*"}, host="click.hypercrew.pl")
*/
public function fallback(string $slug = ''): Response
{
return new Response(" ",404);
}
/**
* @Route("/", name="home")
*/
public function index(EntityManagerInterface $entityManager)
{
$start = \DateTime::createFromFormat("Y-m-d H:i:s", date("Y-m-d")." 00:00:00");
// $start = \DateTime::createFromFormat("Y-m-d H:i:s", "2025-01-11 00:00:00");
$howManyDays = 7;
$w = (int)$start->format("w");
if( $w == 6 ) $w = -1;
$howManyDays += 5 - $w;
$end = (\DateTime::createFromFormat("Y-m-d H:i:s", $start->format("Y-m-d 23:59:59")))->modify("+$howManyDays days");
$items = $entityManager->getRepository(VacationDay::class)->findForRange($start, $end);
$nextVacations = [];
foreach ($items as $item){
$author = $item->getVacation()->getAuthor();
if( empty($nextVacations[$author->getId()])) {
$nextVacations[$author->getId()] = ['author' => $author, 'days' => []];
}
$nextVacations[$author->getId()]['days'][$item->getDayDate()->format("Y-m-d")] = $item->getVacation();
}
$days = [$start->format('Y-m-d')];
for($i = 1; $i <= $howManyDays; $i++){
$days[] = (new \DateTime($days[$i-1]))->modify('+1 day')->format('Y-m-d');
}
return $this->render('default/index.html.twig', [
'nextVacations' => $nextVacations,
'days' => $days,
'start' => $start,
'end' => $end,
]);
}
public function notfound(){
return $this->render('default/notfound.html.twig', []);
}
}