strolap-calendar/src/Controller/CalendarController.php
2025-03-14 06:49:45 +01:00

69 lines
2.6 KiB
PHP

<?php
namespace App\Controller;
use App\Service\AbsenceManager;
use App\Service\CalendarExportService;
use App\Service\CalendarService;
use DateTime;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class CalendarController extends AbstractController
{
public function __construct(
private readonly AbsenceManager $absenceManager,
private readonly CalendarService $calendarService,
private readonly CalendarExportService $calendarExportService,
private readonly string $requiredApiKey
) {
}
#[Route('/', name: 'calendar_index')]
public function index(Request $request): Response
{
if (!$request->query->has('key') || $request->query->get('key') !== $this->requiredApiKey) {
return new Response('Kein Zugriff', Response::HTTP_FORBIDDEN);
}
$startDate = (new DateTime())->format('Y-m-d');
$endDate = (new DateTime('+90 days'))->format('Y-m-d');
$absences = $this->absenceManager->getAbsencesForUser('tim.lappe@check24.de', new DateTime('-1 day'));
$days = $this->calendarService->getAllDays($startDate, $endDate);
$days = $this->calendarService->processAbsences($days, $absences);
return $this->render('calendar/index.html.twig', [
'startDate' => new DateTime($startDate),
'endDate' => new DateTime($endDate),
'days' => $days,
]);
}
#[Route('/cal', name: 'calendar_ics')]
public function exportCalendarIcs(Request $request): Response
{
if (!$request->query->has('key') || $request->query->get('key') !== $this->requiredApiKey) {
return new Response('Kein Zugriff', Response::HTTP_FORBIDDEN);
}
$startDate = (new DateTime())->format('Y-m-d');
$endDate = (new DateTime('+180 days'))->format('Y-m-d');
$absences = $this->absenceManager->getAbsencesForUser('tim.lappe@check24.de', new DateTime('-1 day'));
$days = $this->calendarService->getAllDays($startDate, $endDate);
$days = $this->calendarService->processAbsences($days, $absences);
$icsContent = $this->calendarExportService->generateIcsContent($days, $absences);
$response = new Response($icsContent);
$response->headers->set('Content-Type', 'text/calendar; charset=utf-8');
$response->headers->set('Content-Disposition', 'attachment; filename="work_calendar.ics"');
return $response;
}
}