<?php
namespace App\EventSubscriber;
use App\Controller\EventController;
use App\Entity\Event as EntityEvent;
use App\Form\EventType;
use App\Repository\EventRepository;
use CalendarBundle\CalendarEvents;
use CalendarBundle\Entity\Event;
use CalendarBundle\Event\CalendarEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
class CalendarSubscriber implements EventSubscriberInterface
{
public function __construct(
private EventRepository $eventRepository,
private UrlGeneratorInterface $router,
private FormFactoryInterface $formFactory,
private Security $security,
) {
//$this->eventRepository = $eventRepository;
//$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
CalendarEvents::SET_DATA => 'onCalendarSetData',
];
}
public function onCalendarSetData(CalendarEvent $calendar)
{
$start = $calendar->getStart();
$end = $calendar->getEnd();
$filters = $calendar->getFilters();
$user=$this->security->getUser();
$roles=$user->getRoles();
if (in_array('ROLE_ADMIN',$roles)) {
$events = $this->eventRepository
->createQueryBuilder('event')
->where('event.startDateTime BETWEEN :start and :end OR event.endDateTime BETWEEN :start and :end')
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'))
->getQuery()
->getResult();
} else {
$events = $this->eventRepository
->createQueryBuilder('event')
->join('event.users', 'user')
->where('user = :user')
->andWhere('event.startDateTime BETWEEN :start and :end OR event.endDateTime BETWEEN :start and :end')
->setParameter('user', $user)
->setParameter('start', $start->format('Y-m-d H:i:s'))
->setParameter('end', $end->format('Y-m-d H:i:s'))
->getQuery()
->getResult();
;
}
foreach ($events as $event) {
// this creates the events with your data (here event data) to fill calendar
$meetingEvent = new Event(
$event->getTitle(),
//$event->getDescription(),
$event->getStartDateTime(),
$event->getEndDateTime(),
);
$meetingEvent->setOptions([
'id'=>$event->getId(),
'description'=> $event->getDescription(),
'backgroundColor' => $event->getBackgroundColor(),
'borderColor' => $event->getBorderColor(),
'textColor'=>$event->getTextColor(),
'allDay'=>$event->isAllDay(),
]);
$editURL=$this->router->generate('edit_event', [
'ide' => $event->getId(),
]);
$deleteURL=$this->router->generate('delete_event', [
'ide' => $event->getId(),
]);
$newURL=$this->router->generate('new_event', [
]);
/*
$moveURL=$this->router->generate('move_event', [
'ide' => $event->getId(),
]);
*/
/*
$meetingEvent->addOption(
'url',
$editURL
);
*/
$meetingEvent->addOption(
'extendedProps',
[
'editURL' => $editURL,
'deleteURL' => $deleteURL,
'newURL'=>$newURL,
//'moveURL'=> $moveURL,
]
);
/*
$meetingEvent->addOption(
'url',
$this->router->generate('new_event', [
//'ide' => $event->getId(),
])
);
*/
// finally, add the event to the CalendarEvent to fill the calendar
$calendar->addEvent($meetingEvent);
}
}
/*
public function onKernelController(ControllerEvent $event)
{
$entityEvent = new EntityEvent();
$form = $this->formFactory->create(EventType::class, $entityEvent);
$user = $this->security->getUser();
$form->handleRequest($event->getRequest());
if ($form->isSubmitted() && $form->isValid()) {
$entityEvent->setCreatedAt(new \DateTime('now'));
$entityEvent->addUser($user);
$this->eventRepository->save($entityEvent, true);
// Get an instance of the controller
$controllerInstance = $this->container->get(EventController::class);
$dashboardUrl = $controllerInstance->getDashboardUrl();
return new RedirectResponse($dashboardUrl);
}
$response = new Response($this->twig->render('events/index.html.twig', [
'form' => $form->createView(),
]));
$event->setController(function () use ($response) {
return $response;
});
}
*/
}